Skip to content

Commit

Permalink
Code style
Browse files Browse the repository at this point in the history
  • Loading branch information
TheJoeFin committed May 2, 2024
1 parent 549c559 commit e35d49f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 66 deletions.
13 changes: 6 additions & 7 deletions Tests/ScreenLayoutTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Dapplo.Windows.User32;
using System.Windows;
using Text_Grab;
using Windows.Networking.NetworkOperators;

namespace Tests;
public class ScreenLayoutTests
Expand Down Expand Up @@ -94,7 +93,7 @@ public void SmallRectanglesContained()
double smallLeft2 = display2.CenterPoint().X - (sideLength / 2);
double smallTop2 = display2.CenterPoint().Y - (sideLength / 2);
Rect smallRect2 = new(smallLeft2, smallTop2, sideLength, sideLength);

Assert.True(display2.Contains(smallRect2));
Assert.False(display1.Contains(smallRect2));
Assert.False(display3.Contains(smallRect2));
Expand Down Expand Up @@ -123,7 +122,7 @@ public void SmallRectanglesContained456()
double smallLeft5 = display5.CenterPoint().X - (sideLength / 2);
double smallTop5 = display5.CenterPoint().Y - (sideLength / 2);
Rect smallRect5 = new(smallLeft5, smallTop5, sideLength, sideLength);

Assert.True(display5.Contains(smallRect5));
Assert.False(display4.Contains(smallRect5));
Assert.False(display6.Contains(smallRect5));
Expand All @@ -141,9 +140,9 @@ public void SmallRectanglesContained456()
[Fact]
public void CompareDapploToWinForms()
{
var dapploDisplays = Dapplo.Windows.User32.DisplayInfo.AllDisplayInfos;
DisplayInfo[] dapploDisplays = Dapplo.Windows.User32.DisplayInfo.AllDisplayInfos;

var winFormsDisplays = System.Windows.Forms.Screen.AllScreens;
System.Windows.Forms.Screen[] winFormsDisplays = System.Windows.Forms.Screen.AllScreens;

Assert.Equal(dapploDisplays.Length, winFormsDisplays.Length);

Expand All @@ -152,8 +151,8 @@ public void CompareDapploToWinForms()
Rect dapploRect = dapploDisplays[i].Bounds;
Rect winFormsRect = winFormsDisplays[i].Bounds.AsRect();

var dapploCenterPoint = dapploRect.CenterPoint();
var winFormsCenterPoint = winFormsRect.CenterPoint();
Point dapploCenterPoint = dapploRect.CenterPoint();
Point winFormsCenterPoint = winFormsRect.CenterPoint();

Assert.Equal(dapploCenterPoint, winFormsCenterPoint);
}
Expand Down
4 changes: 2 additions & 2 deletions Text-Grab/Utilities/WindowUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ internal static async void CloseAllFullscreenGrabs()
{
if (window is FullscreenGrab fsg)
{
if (!string.IsNullOrWhiteSpace(fsg.textFromOCR))
stringFromOCR = fsg.textFromOCR;
if (!string.IsNullOrWhiteSpace(fsg.TextFromOCR))
stringFromOCR = fsg.TextFromOCR;

if (fsg.DestinationTextBox is not null)
{
Expand Down
101 changes: 44 additions & 57 deletions Text-Grab/Views/FullscreenGrab.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
using Text_Grab.Extensions;
using Text_Grab.Interfaces;
using Text_Grab.Models;
using Text_Grab.Properties;
Expand All @@ -25,16 +26,16 @@ public partial class FullscreenGrab : Window
{
#region Fields

private System.Windows.Point clickedPoint = new System.Windows.Point();
private System.Windows.Point clickedPoint = new();
private TextBox? destinationTextBox;
private DpiScale? dpiScale;
private bool isComboBoxReady = false;
private bool isSelecting = false;
private bool isShiftDown = false;
private Border selectBorder = new Border();
private Border selectBorder = new();
private double selectLeft;
private double selectTop;
private System.Windows.Point shiftPoint = new System.Windows.Point();
private System.Windows.Point shiftPoint = new();
private double xShiftDelta;
private double yShiftDelta;
private HistoryInfo? historyInfo;
Expand Down Expand Up @@ -70,8 +71,8 @@ public TextBox? DestinationTextBox
}

public bool IsFreeze { get; set; } = false;
public string? textFromOCR { get; set; }
private DisplayInfo? currentScreen { get; set; }
public string? TextFromOCR { get; set; }
private DisplayInfo? CurrentScreen { get; set; }

#endregion Properties

Expand Down Expand Up @@ -264,7 +265,6 @@ private void GetDpiAdjustedRegionOfSelectBorder(out DpiScale dpi, out double pos
{
System.Windows.Point absPosPoint = this.GetAbsolutePosition();
dpi = VisualTreeHelper.GetDpi(this);
int firstScreenBPP = System.Windows.Forms.Screen.AllScreens[0].BitsPerPixel;

posLeft = Canvas.GetLeft(selectBorder) + (absPosPoint.X / dpi.PixelsPerDip);
posTop = Canvas.GetTop(selectBorder) + (absPosPoint.Y / dpi.PixelsPerDip);
Expand Down Expand Up @@ -362,7 +362,7 @@ private static async Task LoadOcrLanguages(ComboBox languagesComboBox, bool usin
haveSetLastLang = true;

if (tesseractIncompatibleElements is not null)
foreach (var element in tesseractIncompatibleElements)
foreach (FrameworkElement element in tesseractIncompatibleElements)
element.Visibility = Visibility.Collapsed;
}

Expand Down Expand Up @@ -421,12 +421,12 @@ private void PanSelection(System.Windows.Point movingPoint)
double leftValue = selectLeft + xShiftDelta;
double topValue = selectTop + yShiftDelta;

if (currentScreen is not null && dpiScale is not null)
if (CurrentScreen is not null && dpiScale is not null)
{
double currentScreenLeft = currentScreen.Bounds.Left; // Should always be 0
double currentScreenRight = currentScreen.Bounds.Right / dpiScale.Value.DpiScaleX;
double currentScreenTop = currentScreen.Bounds.Top; // Should always be 0
double currentScreenBottom = currentScreen.Bounds.Bottom / dpiScale.Value.DpiScaleY;
double currentScreenLeft = CurrentScreen.Bounds.Left; // Should always be 0
double currentScreenRight = CurrentScreen.Bounds.Right / dpiScale.Value.DpiScaleX;
double currentScreenTop = CurrentScreen.Bounds.Top; // Should always be 0
double currentScreenBottom = CurrentScreen.Bounds.Bottom / dpiScale.Value.DpiScaleY;

leftValue = Math.Clamp(leftValue, currentScreenLeft, (currentScreenRight - selectBorder.Width));
topValue = Math.Clamp(topValue, currentScreenTop, (currentScreenBottom - selectBorder.Height));
Expand All @@ -445,9 +445,7 @@ private void PlaceGrabFrameInSelectionRect()
// Then place it where the user just drew the region
// Add space around the window to account for Titlebar
// bottom bar and width of GrabFrame
DpiScale dpi;
double posLeft, posTop;
GetDpiAdjustedRegionOfSelectBorder(out dpi, out posLeft, out posTop);
GetDpiAdjustedRegionOfSelectBorder(out DpiScale dpi, out double posLeft, out double posTop);

GrabFrame grabFrame = new()
{
Expand Down Expand Up @@ -515,9 +513,9 @@ private void RegionClickCanvas_MouseDown(object sender, MouseButtonEventArgs e)
System.Windows.Point formsPoint = new((int)clickedPoint.X, (int)clickedPoint.Y);
foreach (DisplayInfo scr in screens)
{
Rect bound = scr.Bounds;
Rect bound = scr.ScaledBounds();
if (bound.Contains(formsPoint))
currentScreen = scr;
CurrentScreen = scr;
}
}

Expand All @@ -536,13 +534,13 @@ private void RegionClickCanvas_MouseMove(object sender, MouseEventArgs e)

isShiftDown = false;

var left = Math.Min(clickedPoint.X, movingPoint.X);
var top = Math.Min(clickedPoint.Y, movingPoint.Y);
double left = Math.Min(clickedPoint.X, movingPoint.X);
double top = Math.Min(clickedPoint.Y, movingPoint.Y);

selectBorder.Height = Math.Max(clickedPoint.Y, movingPoint.Y) - top;
selectBorder.Width = Math.Max(clickedPoint.X, movingPoint.X) - left;
selectBorder.Height = selectBorder.Height + 2;
selectBorder.Width = selectBorder.Width + 2;
selectBorder.Height += 2;
selectBorder.Width += 2;

clippingGeometry.Rect = new Rect(
new System.Windows.Point(left, top),
Expand All @@ -557,7 +555,7 @@ private async void RegionClickCanvas_MouseUp(object sender, MouseButtonEventArgs
return;

isSelecting = false;
currentScreen = null;
CurrentScreen = null;
CursorClipper.UnClipCursor();
RegionClickCanvas.ReleaseMouseCapture();
clippingGeometry.Rect = new Rect(
Expand All @@ -572,25 +570,16 @@ private async void RegionClickCanvas_MouseUp(object sender, MouseButtonEventArgs
movingPoint.X = Math.Round(movingPoint.X);
movingPoint.Y = Math.Round(movingPoint.Y);

double correctedLeft = Left;
double correctedTop = Top;

if (correctedLeft < 0)
correctedLeft = 0;

if (correctedTop < 0)
correctedTop = 0;

double xDimScaled = Canvas.GetLeft(selectBorder) * m.M11;
double yDimScaled = Canvas.GetTop(selectBorder) * m.M22;

Rectangle regionScaled = new Rectangle(
Rectangle regionScaled = new(
(int)xDimScaled,
(int)yDimScaled,
(int)(selectBorder.Width * m.M11),
(int)(selectBorder.Height * m.M22));

textFromOCR = string.Empty;
TextFromOCR = string.Empty;

if (NewGrabFrameMenuItem.IsChecked is true)
{
Expand All @@ -600,9 +589,7 @@ private async void RegionClickCanvas_MouseUp(object sender, MouseButtonEventArgs

try { RegionClickCanvas.Children.Remove(selectBorder); } catch { }

Language? selectedOcrLang = LanguagesComboBox.SelectedItem as Language;

if (selectedOcrLang is null)
if (LanguagesComboBox.SelectedItem is not Language selectedOcrLang)
selectedOcrLang = LanguageUtilities.GetOCRLanguage();

string tessTag = string.Empty;
Expand All @@ -612,22 +599,22 @@ private async void RegionClickCanvas_MouseUp(object sender, MouseButtonEventArgs

bool isSmallClick = (regionScaled.Width < 3 || regionScaled.Height < 3);

bool isSingleLine = SingleLineMenuItem is null ? false : SingleLineMenuItem.IsChecked;
bool isTable = TableMenuItem is null ? false : TableMenuItem.IsChecked;
bool isSingleLine = SingleLineMenuItem is not null && SingleLineMenuItem.IsChecked;
bool isTable = TableMenuItem is not null && TableMenuItem.IsChecked;

if (isSmallClick)
{
BackgroundBrush.Opacity = 0;
textFromOCR = await OcrUtilities.GetClickedWordAsync(this, new System.Windows.Point(xDimScaled, yDimScaled), selectedOcrLang);
TextFromOCR = await OcrUtilities.GetClickedWordAsync(this, new System.Windows.Point(xDimScaled, yDimScaled), selectedOcrLang);
}
else if (isTable)
textFromOCR = await OcrUtilities.GetRegionsTextAsTableAsync(this, regionScaled, selectedOcrLang);
TextFromOCR = await OcrUtilities.GetRegionsTextAsTableAsync(this, regionScaled, selectedOcrLang);
else
textFromOCR = await OcrUtilities.GetRegionsTextAsync(this, regionScaled, selectedOcrLang, tessTag);
TextFromOCR = await OcrUtilities.GetRegionsTextAsync(this, regionScaled, selectedOcrLang, tessTag);

if (DefaultSettings.UseHistory && !isSmallClick)
{
GetDpiAdjustedRegionOfSelectBorder(out DpiScale dpi, out double posLeft, out double posTop);
GetDpiAdjustedRegionOfSelectBorder(out _, out double posLeft, out double posTop);

Rect historyRect = new()
{
Expand All @@ -645,13 +632,13 @@ private async void RegionClickCanvas_MouseUp(object sender, MouseButtonEventArgs
CaptureDateTime = DateTimeOffset.Now,
PositionRect = historyRect,
IsTable = TableToggleButton.IsChecked!.Value,
TextContent = textFromOCR,
TextContent = TextFromOCR,
ImageContent = Singleton<HistoryService>.Instance.CachedBitmap,
SourceMode = TextGrabMode.Fullscreen,
};
}

if (!string.IsNullOrWhiteSpace(textFromOCR))
if (!string.IsNullOrWhiteSpace(TextFromOCR))
{
if (SendToEditTextToggleButton.IsChecked is true && destinationTextBox is null)
{
Expand All @@ -660,7 +647,7 @@ private async void RegionClickCanvas_MouseUp(object sender, MouseButtonEventArgs
}

OutputUtilities.HandleTextFromOcr(
textFromOCR,
TextFromOCR,
isSingleLine,
isTable,
destinationTextBox);
Expand Down Expand Up @@ -711,8 +698,8 @@ private async void Window_Loaded(object sender, RoutedEventArgs e)
{
WindowState = WindowState.Maximized;
FullWindow.Rect = new System.Windows.Rect(0, 0, Width, Height);
this.KeyDown += FullscreenGrab_KeyDown;
this.KeyUp += FullscreenGrab_KeyUp;
KeyDown += FullscreenGrab_KeyDown;
KeyUp += FullscreenGrab_KeyUp;

SetImageToBackground();

Expand All @@ -729,10 +716,10 @@ private async void Window_Loaded(object sender, RoutedEventArgs e)
Topmost = false;
#endif

List<FrameworkElement> tesseractIncompatibleFrameworkElements = new()
{
List<FrameworkElement> tesseractIncompatibleFrameworkElements =
[
TableMenuItem, TableToggleButton
};
];
await LoadOcrLanguages(LanguagesComboBox, usingTesseract, tesseractIncompatibleFrameworkElements);
isComboBoxReady = true;

Expand All @@ -744,12 +731,12 @@ private void Window_Unloaded(object sender, RoutedEventArgs e)
{
BackgroundImage.Source = null;
BackgroundImage.UpdateLayout();
currentScreen = null;
CurrentScreen = null;
dpiScale = null;
textFromOCR = null;
TextFromOCR = null;

this.Loaded -= Window_Loaded;
this.Unloaded -= Window_Unloaded;
Loaded -= Window_Loaded;
Unloaded -= Window_Unloaded;

RegionClickCanvas.MouseDown -= RegionClickCanvas_MouseDown;
RegionClickCanvas.MouseMove -= RegionClickCanvas_MouseMove;
Expand All @@ -771,14 +758,14 @@ private void Window_Unloaded(object sender, RoutedEventArgs e)
SettingsButton.Click -= SettingsMenuItem_Click;
CancelButton.Click -= CancelMenuItem_Click;

this.KeyDown -= FullscreenGrab_KeyDown;
this.KeyUp -= FullscreenGrab_KeyUp;
KeyDown -= FullscreenGrab_KeyDown;
KeyUp -= FullscreenGrab_KeyUp;
}

private void StandardModeToggleButton_Click(object sender, RoutedEventArgs e)
{
bool isActive = CheckIfCheckingOrUnchecking(sender);
WindowUtilities.FullscreenKeyDown(Key.N, isActive);
WindowUtilities.FullscreenKeyDown(Key.N, isActive);
SelectSingleToggleButton(sender);

if (isActive)
Expand Down

0 comments on commit e35d49f

Please sign in to comment.