[ITT] fix crashes on shift selection and clipboard access (#20166)

* [ITT] fix crashes on shift selection and clipboard access

* exception logging
This commit is contained in:
Andrey Nekrasov
2022-08-30 19:21:43 +03:00
committed by GitHub
parent eb235eef37
commit 1383e9666c
2 changed files with 16 additions and 14 deletions

View File

@@ -58,7 +58,7 @@ public partial class App : Application, IDisposable
} }
catch (Exception ex) catch (Exception ex)
{ {
Debug.WriteLine(ex.Message); Logger.LogError($"PowerOCR got an exception on start: {ex}");
} }
} }
else else

View File

@@ -42,6 +42,8 @@ public partial class OCROverlay : Window
private double xShiftDelta; private double xShiftDelta;
private double yShiftDelta; private double yShiftDelta;
private const double ActiveOpacity = 0.4;
public OCROverlay() public OCROverlay()
{ {
InitializeComponent(); InitializeComponent();
@@ -55,7 +57,7 @@ public partial class OCROverlay : Window
KeyUp += MainWindow_KeyUp; KeyUp += MainWindow_KeyUp;
BackgroundImage.Source = ImageMethods.GetWindowBoundsImage(this); BackgroundImage.Source = ImageMethods.GetWindowBoundsImage(this);
BackgroundBrush.Opacity = 0.4; BackgroundBrush.Opacity = ActiveOpacity;
} }
private void Window_Unloaded(object sender, RoutedEventArgs e) private void Window_Unloaded(object sender, RoutedEventArgs e)
@@ -84,9 +86,6 @@ public partial class OCROverlay : Window
switch (e.Key) switch (e.Key)
{ {
case Key.LeftShift: case Key.LeftShift:
isShiftDown = false;
clickedPoint = new Point(clickedPoint.X + xShiftDelta, clickedPoint.Y + yShiftDelta);
break;
case Key.RightShift: case Key.RightShift:
isShiftDown = false; isShiftDown = false;
clickedPoint = new Point(clickedPoint.X + xShiftDelta, clickedPoint.Y + yShiftDelta); clickedPoint = new Point(clickedPoint.X + xShiftDelta, clickedPoint.Y + yShiftDelta);
@@ -110,7 +109,7 @@ public partial class OCROverlay : Window
private void RegionClickCanvas_MouseDown(object sender, MouseButtonEventArgs e) private void RegionClickCanvas_MouseDown(object sender, MouseButtonEventArgs e)
{ {
if (e.RightButton == MouseButtonState.Pressed) if (e.LeftButton != MouseButtonState.Pressed)
{ {
return; return;
} }
@@ -147,6 +146,7 @@ public partial class OCROverlay : Window
if (scr.Bounds.Contains(formsPoint)) if (scr.Bounds.Contains(formsPoint))
{ {
CurrentScreen = scr; CurrentScreen = scr;
break;
} }
} }
} }
@@ -190,7 +190,7 @@ public partial class OCROverlay : Window
clippingGeometry.Rect = new Rect( clippingGeometry.Rect = new Rect(
new Point(leftValue, topValue), new Point(leftValue, topValue),
new Size(selectBorder.Width - 2, selectBorder.Height - 2)); new Size(selectBorder.Width, selectBorder.Height));
Canvas.SetLeft(selectBorder, leftValue - 1); Canvas.SetLeft(selectBorder, leftValue - 1);
Canvas.SetTop(selectBorder, topValue - 1); Canvas.SetTop(selectBorder, topValue - 1);
return; return;
@@ -235,11 +235,6 @@ public partial class OCROverlay : Window
movingPoint.X = Math.Round(movingPoint.X); movingPoint.X = Math.Round(movingPoint.X);
movingPoint.Y = Math.Round(movingPoint.Y); movingPoint.Y = Math.Round(movingPoint.Y);
if (mPt == movingPoint)
{
Debug.WriteLine("Probably on Screen 1");
}
double xDimScaled = Canvas.GetLeft(selectBorder) * m.M11; double xDimScaled = Canvas.GetLeft(selectBorder) * m.M11;
double yDimScaled = Canvas.GetTop(selectBorder) * m.M22; double yDimScaled = Canvas.GetTop(selectBorder) * m.M22;
@@ -262,7 +257,6 @@ public partial class OCROverlay : Window
if (regionScaled.Width < 3 || regionScaled.Height < 3) if (regionScaled.Width < 3 || regionScaled.Height < 3)
{ {
BackgroundBrush.Opacity = 0;
grabbedText = await ImageMethods.GetClickedWord(this, new Point(xDimScaled, yDimScaled)); grabbedText = await ImageMethods.GetClickedWord(this, new Point(xDimScaled, yDimScaled));
} }
else else
@@ -272,7 +266,15 @@ public partial class OCROverlay : Window
if (string.IsNullOrWhiteSpace(grabbedText) == false) if (string.IsNullOrWhiteSpace(grabbedText) == false)
{ {
Clipboard.SetText(grabbedText); try
{
Clipboard.SetText(grabbedText);
}
catch (Exception ex)
{
Logger.LogError($"Clipboard.SetText exception: {ex}");
}
WindowUtilities.CloseAllOCROverlays(); WindowUtilities.CloseAllOCROverlays();
} }
} }