imradv3 / src /WpfEditor /DictMenuHandler.cs
fasdfsa's picture
添加翻译右键菜单
8a96ad7
using CefSharp;
using Newtonsoft.Json.Linq;
using System;
using System.Windows;
using WpfEditor;
namespace Echodict
{
public class DictMenuHandler : IContextMenuHandler
{
private const CefMenuCommand QueryCommandId = (CefMenuCommand)26501;
private const CefMenuCommand JishoCommandId = (CefMenuCommand)26502;
private const CefMenuCommand NaverjaCommandId = (CefMenuCommand)26503;
private const CefMenuCommand TranslateCommandId = (CefMenuCommand)26504;
public void OnBeforeContextMenu(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model)
{
// Clear default menu items
model.Clear();
// Add "Copy" if text is selected
if (!string.IsNullOrEmpty(parameters.SelectionText))
{
model.AddItem(CefMenuCommand.Copy, "Copy");
model.AddItem(QueryCommandId, "查询"); // Query
model.AddItem(JishoCommandId, "Jisho"); // Jisho
model.AddItem(NaverjaCommandId, "Naverja"); // Naverja
model.AddItem(TranslateCommandId, "Translate"); // Translate
}
}
public bool OnContextMenuCommand(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IContextMenuParams parameters, CefMenuCommand commandId, CefEventFlags eventFlags)
{
if (commandId == QueryCommandId)
{
string text = parameters.SelectionText;
if (!string.IsNullOrEmpty(text))
{
Application.Current.Dispatcher.Invoke(() =>
{
EchodictWindow.Instance.ShowWord(text);
});
}
return true;
} else if (commandId == JishoCommandId) {
string text = parameters.SelectionText;
if (!string.IsNullOrEmpty(text))
{
Application.Current.Dispatcher.Invoke(() =>
{
var content = System.Web.HttpUtility.UrlEncode(text);
String url = $"https://jisho.org/search/{content}";
var win = Echodict.EchodictWindow.Instance;
//win.WindowStartupLocation = WindowStartupLocation.CenterScreen;
win.OpenUrl(url);
});
}
return true;
} else if (commandId == NaverjaCommandId) {
string text = parameters.SelectionText;
if (!string.IsNullOrEmpty(text))
{
Application.Current.Dispatcher.Invoke(() =>
{
var content = System.Web.HttpUtility.UrlEncode(text);
String url = $"https://ja.dict.naver.com/#/search?query={content}&range=all";
var win = Echodict.EchodictWindow.Instance;
//win.WindowStartupLocation = WindowStartupLocation.CenterScreen;
win.OpenUrl(url);
});
}
return true;
} else if (commandId == TranslateCommandId) {
string text = parameters.SelectionText;
if (!string.IsNullOrEmpty(text))
{
Application.Current.Dispatcher.Invoke(async () =>
{
string res = await DeepSeek.translate(text);
if (res != "")
{
JObject jsn = Newtonsoft.Json.JsonConvert.DeserializeObject<JObject>(res);
string zh = jsn["choices"][0]["message"]["content"].Value<string>();
var win = new TranslationResultWindow(zh);
win.WindowStartupLocation = WindowStartupLocation.CenterScreen;
win.Topmost = true;
win.Show();
}
});
}
return true;
}
return false;
}
public void OnContextMenuDismissed(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame)
{
}
public bool RunContextMenu(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model, IRunContextMenuCallback callback)
{
return false; // Return false to let the default menu (modified by OnBeforeContextMenu) show
}
}
}