From a87d27c185dc3fdfc61f1f26ffcbac5103ee5fcb Mon Sep 17 00:00:00 2001 From: Henry Lin Date: Fri, 25 Feb 2022 16:27:07 +0800 Subject: [PATCH] Fix ManualOcrOverlay being activated multiple times --- QuickDictionary/UserInterface/MainWindow.xaml.cs | 14 +++++++++----- .../UserInterface/Ocr/ManualOcrOverlay.xaml.cs | 2 +- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/QuickDictionary/UserInterface/MainWindow.xaml.cs b/QuickDictionary/UserInterface/MainWindow.xaml.cs index 24eb5c2..d8c665f 100644 --- a/QuickDictionary/UserInterface/MainWindow.xaml.cs +++ b/QuickDictionary/UserInterface/MainWindow.xaml.cs @@ -249,6 +249,7 @@ private async void AutoOcrTimer_Tick(object sender, EventArgs e) } private Bitmap screenshot; + private ManualOcrOverlay manualOcrOverlay; private async void startOcr() { @@ -259,8 +260,11 @@ private async void startOcr() return; } - var overlay = new ManualOcrOverlay(); - overlay.WordSelected += Overlay_WordSelected; + if (manualOcrOverlay is { IsLoaded: true }) + return; + + manualOcrOverlay = new ManualOcrOverlay(); + manualOcrOverlay.WordSelected += Overlay_WordSelected; screenshot = ScreenCapture.GetScreenshot(); await Task.Run(() => { @@ -276,12 +280,12 @@ await Task.Run(() => bitmapImage.CacheOption = BitmapCacheOption.OnLoad; bitmapImage.StreamSource = ms; bitmapImage.EndInit(); - overlay.SetBg(bitmapImage); + manualOcrOverlay.SetBackground(bitmapImage); ms.Dispose(); }); }); Dispatcher.Invoke(() => progressLoading.Visibility = Visibility.Visible); - overlay.ShowDialog(); + manualOcrOverlay.ShowDialog(); } private void unminimize() @@ -301,7 +305,7 @@ private async void KeyHook_KeyPressed(object sender, KeyPressedEventArgs e) txtWord.Focus(); txtWord.SelectAll(); } - else + else if (e.Key == Keys.G) { if (AutoOcr) { diff --git a/QuickDictionary/UserInterface/Ocr/ManualOcrOverlay.xaml.cs b/QuickDictionary/UserInterface/Ocr/ManualOcrOverlay.xaml.cs index 54d175c..7c276eb 100644 --- a/QuickDictionary/UserInterface/Ocr/ManualOcrOverlay.xaml.cs +++ b/QuickDictionary/UserInterface/Ocr/ManualOcrOverlay.xaml.cs @@ -28,7 +28,7 @@ public ManualOcrOverlay() InitializeComponent(); } - public void SetBg(BitmapImage img) + public void SetBackground(BitmapImage img) { Dispatcher.Invoke(() => {