File size: 4,692 Bytes
d76c916
8a96ad7
d76c916
 
8a96ad7
d76c916
 
 
 
 
 
b355108
 
8a96ad7
d76c916
 
 
 
 
 
 
 
 
 
8a96ad7
 
 
 
d76c916
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b355108
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a96ad7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d76c916
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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
        }
    }
}