File size: 12,984 Bytes
670b81d
8cd514d
670b81d
149bab1
51e54a7
 
670b81d
51e54a7
 
 
8cd514d
faf5550
 
 
6cdc5ca
 
 
51e54a7
 
 
 
 
 
32726bd
faf5550
51e54a7
 
 
 
faf5550
51e54a7
 
faf5550
51e54a7
 
8cd514d
263e434
 
8cd514d
 
 
 
 
2931426
8cd514d
 
 
 
7e97feb
9d4e528
 
 
 
 
 
 
 
 
 
7e97feb
 
 
 
 
 
 
 
faf5550
 
 
 
 
 
 
 
 
 
 
 
 
9d4e528
 
 
 
 
 
 
 
 
 
 
 
 
faf5550
 
 
 
 
 
 
 
263e434
 
 
 
 
32726bd
263e434
 
 
 
 
 
 
 
7e97feb
 
 
 
 
 
 
 
9d4e528
 
 
 
 
 
 
 
 
 
7e97feb
263e434
7e97feb
 
263e434
 
 
faf5550
 
9d4e528
 
 
 
 
faf5550
 
 
 
 
32726bd
faf5550
32726bd
 
 
 
 
 
 
 
 
 
faf5550
8cd514d
 
2931426
8cd514d
 
 
 
 
 
798be0d
149bab1
 
 
 
 
 
 
 
 
3510ac4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b355108
2931426
 
 
 
 
 
3510ac4
 
 
149bab1
51e54a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d576ef
51e54a7
 
 
 
 
 
 
 
 
 
 
 
 
dccd280
 
 
 
 
51e54a7
 
 
 
 
 
1d576ef
 
 
 
51e54a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
using Newtonsoft.Json.Linq;
using ScreenGrab;
using System.Drawing;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using Bitmap = System.Drawing.Bitmap;
using MessageBox = System.Windows.MessageBox;
using Point = System.Windows.Point;

using CefSharp;
using CefSharp.Wpf;


namespace WpfEditor
{
    public partial class TreeMenuPage : Window
    {
        private bool _isMenuVisible = false;
        private bool _forceClose = false;
        private NotifyIcon _notifyIcon;

        public TreeMenuPage()
        {
            InitializeComponent();

            // 初始化时隐藏菜单
            TreeMenuBorder.Visibility = Visibility.Collapsed;

            // 添加点击其他区域隐藏菜单的事件
            this.MouseDown += TreeMenuPage_MouseDown;



            GlobalHotKey.RegisterHotKey("Alt + S", () => {

                if (ScreenGrabber.IsCapturing) return;
                    // https://github.com/wuqinchao/ScreenCapturer
                        // 替代品
                ScreenGrabber.OnCaptured = async (bitmap, type) => await ScreenshotCallbackAsync(bitmap, type, null);
                ScreenGrabber.Capture(true);

            });

            GlobalHotKey.RegisterHotKey("Alt + D", () => {
                //var win = Echodict.EchodictWindow.Instance;
                //if (win.WindowState == WindowState.Minimized)
                //{
                //    win.WindowState = win.LastWindowState;
                //}
                //win.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                //win.Show();
                //win.Activate();
                //win.Topmost = true;
                showDict();
            });

            GlobalHotKey.RegisterHotKey("Alt + E", () => {
                Show();
                WindowState = WindowState.Normal;
                Activate();
            });

            Loaded += TreeMenuPage_Loaded;
            Closing += TreeMenuPage_Closing;
            StateChanged += TreeMenuPage_StateChanged;
        }

        private void TreeMenuPage_StateChanged(object? sender, EventArgs e)
        {
            if (WindowState == WindowState.Minimized)
            {
                Hide();
            }
        }

        private void showDict()
        {
            var win = Echodict.EchodictWindow.Instance;
            if (win.WindowState == WindowState.Minimized)
            {
                win.WindowState = win.LastWindowState;
            }
            win.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            win.Show();
            win.Activate();
            win.Topmost = true;
        }

        private void TreeMenuPage_Loaded(object sender, RoutedEventArgs e)
        {
            WindowState = WindowState.Minimized;
            Hide();

            _notifyIcon = new NotifyIcon();
            _notifyIcon.Icon = new Icon("ico.ico");
            _notifyIcon.Visible = true;
            _notifyIcon.Text = "EchoDict";

            var contextMenu = new ContextMenuStrip();
            var exitMenuItem = new ToolStripMenuItem("退出");
            exitMenuItem.Click += (s, e) => {
                _forceClose = true;
                System.Windows.Application.Current.Shutdown();
            };
            var ocrMenuItem = new ToolStripMenuItem("Alt+S 截图OCR");
            ocrMenuItem.Click += (s, e) => {
                if (ScreenGrabber.IsCapturing) return;
                ScreenGrabber.OnCaptured = async (bitmap, type) => await ScreenshotCallbackAsync(bitmap, type, null);
                ScreenGrabber.Capture(true);
            };
            var EchoEditorMenuItem = new ToolStripMenuItem("Alt+E EchoEditor");
            EchoEditorMenuItem.Click += (s, e) => {
                Show();
                WindowState = WindowState.Normal;
                Activate();
            };
            var EchoDictMenuItem = new ToolStripMenuItem("Alt+D EchoDict");
            EchoDictMenuItem.Click += (s, e) => {
                //var win = Echodict.EchodictWindow.Instance;
                //if (win.WindowState == WindowState.Minimized)
                //{
                //    win.WindowState = win.LastWindowState;
                //}
                //win.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                //win.Show();
                //win.Activate();
                //win.Topmost = true;
                showDict();
            };
            contextMenu.Items.Add(ocrMenuItem);
            contextMenu.Items.Add(EchoDictMenuItem);
            contextMenu.Items.Add(EchoEditorMenuItem);
            contextMenu.Items.Add(exitMenuItem);
            _notifyIcon.ContextMenuStrip = contextMenu;

            _notifyIcon.DoubleClick += (s, args) =>
            {
                //Show();
                //WindowState = WindowState.Normal;
                //Activate();

                showDict();
            };
        }

        private void TreeMenuPage_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (_forceClose)
            {
                if (_notifyIcon != null)
                {
                    _notifyIcon.Visible = false;
                    _notifyIcon.Dispose();
                }
            } else
            {
                e.Cancel = true;
                WindowState = WindowState.Minimized;
                Hide();
            }
        }

        private async Task ScreenshotCallbackAsync(Bitmap? bitmap, string type, CancellationToken? token = null)
        {
            if (bitmap == null)
            {
                //ShowBalloonTip("图像不存在");
                return;
            }

            // 将 bitmap 转成 bytes
            byte[] bytes;
            using (var ms = new MemoryStream())
            {
                bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                bytes = ms.ToArray();
            }

            // run first -> huggingface/comicocr/main.py
            //string result = await ComicOcr.RecognizeImage("", bytes);
            
            // 获取临时文件夹路径
            string tempDir = Path.GetTempPath();
            // 生成唯一的临时文件名
            string tempFile = Path.Combine(tempDir, Guid.NewGuid().ToString("N") + ".jpg");
            File.WriteAllBytes(tempFile, bytes);
            //try
            //{
            //    // 将 bytes 写入临时文件
            //    File.WriteAllBytes(tempFile, bytes);
            //}
            //finally
            //{
            //    // 删除临时文件
            //    if (File.Exists(tempFile))
            //    {
            //        File.Delete(tempFile);
            //    }
            //}

            //var win = new OCRResultWindow("just a test.");
            //win.Owner = this;
            //win.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            //win.Show();

            var ReviseEditWindow = new MainWindow(EditorMode.ReviseEdit);
            if (type == "文字识别")
            {
                ReviseEditWindow._ocrMode = OcrMode.WeChatOcr;
            } else if (type == "漫画识别")
            {
                ReviseEditWindow._ocrMode = OcrMode.ComicOcr;
            }
            ReviseEditWindow.WindowState = WindowState.Maximized;
            ReviseEditWindow.Show();
            ReviseEditWindow.handle_comicocr_open_img(tempFile);

        }

        private void MenuButton_Click(object sender, RoutedEventArgs e)
        {
            ToggleMenu();
        }

        private void ToggleMenu()
        {
            if (_isMenuVisible)
            {
                HideMenu();
            }
            else
            {
                ShowMenu();
            }
        }

        private void ShowMenu()
        {
            TreeMenuBorder.Visibility = Visibility.Visible;
            _isMenuVisible = true;
            
            // 添加滑入动画效果
            var slideAnimation = new DoubleAnimation
            {
                From = -250,
                To = 0,
                Duration = TimeSpan.FromMilliseconds(300),
                EasingFunction = new QuadraticEase { EasingMode = EasingMode.EaseOut }
            };
            
            var transform = new TranslateTransform();
            TreeMenuBorder.RenderTransform = transform;
            transform.BeginAnimation(TranslateTransform.XProperty, slideAnimation);
        }

        private void HideMenu()
        {
            var slideAnimation = new DoubleAnimation
            {
                From = 0,
                To = -250,
                Duration = TimeSpan.FromMilliseconds(200),
                EasingFunction = new QuadraticEase { EasingMode = EasingMode.EaseIn }
            };
            
            slideAnimation.Completed += (s, e) => 
            {
                TreeMenuBorder.Visibility = Visibility.Collapsed;
                _isMenuVisible = false;
            };
            
            var transform = TreeMenuBorder.RenderTransform as TranslateTransform ?? new TranslateTransform();
            TreeMenuBorder.RenderTransform = transform;
            transform.BeginAnimation(TranslateTransform.XProperty, slideAnimation);
        }

        private void TreeItem_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (sender is TreeViewItem item)
            {
                string action = item.Tag?.ToString() ?? "";
                HandleMenuAction(action, item.Header?.ToString() ?? "");
            }
        }

        private void HandleMenuAction(string action, string itemName)
        {
            // 更新内容显示
            ContentText.Text = $"您点击了: {itemName}";

            // 根据不同的操作执行相应的功能
            switch (action)
            {
                case "ImageOCR":
                    var ImageOCRWindow = new MainWindow(EditorMode.ImageOCR);
                    ImageOCRWindow.WindowState = WindowState.Maximized;
                    ImageOCRWindow.Show();
                    break;
                case "ReviseEdit":
                    var ReviseEditWindow = new MainWindow(EditorMode.ReviseEdit);
                    ReviseEditWindow.WindowState = WindowState.Maximized;
                    ReviseEditWindow.Show();
                    break;
                case "MenuSplit":
                    var MenuSplitWindow = new MainWindow(EditorMode.MenuSplit);
                    MenuSplitWindow.WindowState = WindowState.Maximized;
                    MenuSplitWindow.Show();
                    break;
                case "SaveFile":
                    MessageBox.Show("执行保存文件操作", "操作提示", MessageBoxButton.OK, MessageBoxImage.Information);
                    break;
                case "Copy":
                    MessageBox.Show("执行复制操作", "操作提示", MessageBoxButton.OK, MessageBoxImage.Information);
                    break;
                case "ExportPDFImage":
                    var exportPdfImageWindow = new ExportPDFImage();
                    exportPdfImageWindow.Show();
                    break;
                case "Paste":
                    MessageBox.Show("执行粘贴操作", "操作提示", MessageBoxButton.OK, MessageBoxImage.Information);
                    break;
                case "About":
                    MessageBox.Show("WPF编辑器 v1.0\n一个功能丰富的文本编辑器", "关于", MessageBoxButton.OK, MessageBoxImage.Information);
                    break;
                default:
                    // 对于其他操作,只显示提示信息
                    break;
            }
            
            // 点击后隐藏菜单
            HideMenu();
        }

        private void TreeMenuPage_MouseDown(object sender, MouseButtonEventArgs e)
        {
            // 如果点击的不是菜单区域,则隐藏菜单
            if (_isMenuVisible && !IsMouseOverElement(TreeMenuBorder, e.GetPosition(this)))
            {
                HideMenu();
            }
        }

        private bool IsMouseOverElement(FrameworkElement element, Point mousePosition)
        {
            var elementPosition = element.TransformToAncestor(this).Transform(new Point(0, 0));
            var elementBounds = new Rect(elementPosition.X, elementPosition.Y, element.ActualWidth, element.ActualHeight);
            return elementBounds.Contains(mousePosition);
        }
    }
}