Skip to content

Commit e35d49f

Browse files
committed
Code style
1 parent 549c559 commit e35d49f

File tree

3 files changed

+52
-66
lines changed

3 files changed

+52
-66
lines changed

Tests/ScreenLayoutTests.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using Dapplo.Windows.User32;
22
using System.Windows;
33
using Text_Grab;
4-
using Windows.Networking.NetworkOperators;
54

65
namespace Tests;
76
public class ScreenLayoutTests
@@ -94,7 +93,7 @@ public void SmallRectanglesContained()
9493
double smallLeft2 = display2.CenterPoint().X - (sideLength / 2);
9594
double smallTop2 = display2.CenterPoint().Y - (sideLength / 2);
9695
Rect smallRect2 = new(smallLeft2, smallTop2, sideLength, sideLength);
97-
96+
9897
Assert.True(display2.Contains(smallRect2));
9998
Assert.False(display1.Contains(smallRect2));
10099
Assert.False(display3.Contains(smallRect2));
@@ -123,7 +122,7 @@ public void SmallRectanglesContained456()
123122
double smallLeft5 = display5.CenterPoint().X - (sideLength / 2);
124123
double smallTop5 = display5.CenterPoint().Y - (sideLength / 2);
125124
Rect smallRect5 = new(smallLeft5, smallTop5, sideLength, sideLength);
126-
125+
127126
Assert.True(display5.Contains(smallRect5));
128127
Assert.False(display4.Contains(smallRect5));
129128
Assert.False(display6.Contains(smallRect5));
@@ -141,9 +140,9 @@ public void SmallRectanglesContained456()
141140
[Fact]
142141
public void CompareDapploToWinForms()
143142
{
144-
var dapploDisplays = Dapplo.Windows.User32.DisplayInfo.AllDisplayInfos;
143+
DisplayInfo[] dapploDisplays = Dapplo.Windows.User32.DisplayInfo.AllDisplayInfos;
145144

146-
var winFormsDisplays = System.Windows.Forms.Screen.AllScreens;
145+
System.Windows.Forms.Screen[] winFormsDisplays = System.Windows.Forms.Screen.AllScreens;
147146

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

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

155-
var dapploCenterPoint = dapploRect.CenterPoint();
156-
var winFormsCenterPoint = winFormsRect.CenterPoint();
154+
Point dapploCenterPoint = dapploRect.CenterPoint();
155+
Point winFormsCenterPoint = winFormsRect.CenterPoint();
157156

158157
Assert.Equal(dapploCenterPoint, winFormsCenterPoint);
159158
}

Text-Grab/Utilities/WindowUtilities.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ internal static async void CloseAllFullscreenGrabs()
154154
{
155155
if (window is FullscreenGrab fsg)
156156
{
157-
if (!string.IsNullOrWhiteSpace(fsg.textFromOCR))
158-
stringFromOCR = fsg.textFromOCR;
157+
if (!string.IsNullOrWhiteSpace(fsg.TextFromOCR))
158+
stringFromOCR = fsg.TextFromOCR;
159159

160160
if (fsg.DestinationTextBox is not null)
161161
{

Text-Grab/Views/FullscreenGrab.xaml.cs

Lines changed: 44 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Windows.Controls.Primitives;
99
using System.Windows.Input;
1010
using System.Windows.Media;
11+
using Text_Grab.Extensions;
1112
using Text_Grab.Interfaces;
1213
using Text_Grab.Models;
1314
using Text_Grab.Properties;
@@ -25,16 +26,16 @@ public partial class FullscreenGrab : Window
2526
{
2627
#region Fields
2728

28-
private System.Windows.Point clickedPoint = new System.Windows.Point();
29+
private System.Windows.Point clickedPoint = new();
2930
private TextBox? destinationTextBox;
3031
private DpiScale? dpiScale;
3132
private bool isComboBoxReady = false;
3233
private bool isSelecting = false;
3334
private bool isShiftDown = false;
34-
private Border selectBorder = new Border();
35+
private Border selectBorder = new();
3536
private double selectLeft;
3637
private double selectTop;
37-
private System.Windows.Point shiftPoint = new System.Windows.Point();
38+
private System.Windows.Point shiftPoint = new();
3839
private double xShiftDelta;
3940
private double yShiftDelta;
4041
private HistoryInfo? historyInfo;
@@ -70,8 +71,8 @@ public TextBox? DestinationTextBox
7071
}
7172

7273
public bool IsFreeze { get; set; } = false;
73-
public string? textFromOCR { get; set; }
74-
private DisplayInfo? currentScreen { get; set; }
74+
public string? TextFromOCR { get; set; }
75+
private DisplayInfo? CurrentScreen { get; set; }
7576

7677
#endregion Properties
7778

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

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

364364
if (tesseractIncompatibleElements is not null)
365-
foreach (var element in tesseractIncompatibleElements)
365+
foreach (FrameworkElement element in tesseractIncompatibleElements)
366366
element.Visibility = Visibility.Collapsed;
367367
}
368368

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

424-
if (currentScreen is not null && dpiScale is not null)
424+
if (CurrentScreen is not null && dpiScale is not null)
425425
{
426-
double currentScreenLeft = currentScreen.Bounds.Left; // Should always be 0
427-
double currentScreenRight = currentScreen.Bounds.Right / dpiScale.Value.DpiScaleX;
428-
double currentScreenTop = currentScreen.Bounds.Top; // Should always be 0
429-
double currentScreenBottom = currentScreen.Bounds.Bottom / dpiScale.Value.DpiScaleY;
426+
double currentScreenLeft = CurrentScreen.Bounds.Left; // Should always be 0
427+
double currentScreenRight = CurrentScreen.Bounds.Right / dpiScale.Value.DpiScaleX;
428+
double currentScreenTop = CurrentScreen.Bounds.Top; // Should always be 0
429+
double currentScreenBottom = CurrentScreen.Bounds.Bottom / dpiScale.Value.DpiScaleY;
430430

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

452450
GrabFrame grabFrame = new()
453451
{
@@ -515,9 +513,9 @@ private void RegionClickCanvas_MouseDown(object sender, MouseButtonEventArgs e)
515513
System.Windows.Point formsPoint = new((int)clickedPoint.X, (int)clickedPoint.Y);
516514
foreach (DisplayInfo scr in screens)
517515
{
518-
Rect bound = scr.Bounds;
516+
Rect bound = scr.ScaledBounds();
519517
if (bound.Contains(formsPoint))
520-
currentScreen = scr;
518+
CurrentScreen = scr;
521519
}
522520
}
523521

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

537535
isShiftDown = false;
538536

539-
var left = Math.Min(clickedPoint.X, movingPoint.X);
540-
var top = Math.Min(clickedPoint.Y, movingPoint.Y);
537+
double left = Math.Min(clickedPoint.X, movingPoint.X);
538+
double top = Math.Min(clickedPoint.Y, movingPoint.Y);
541539

542540
selectBorder.Height = Math.Max(clickedPoint.Y, movingPoint.Y) - top;
543541
selectBorder.Width = Math.Max(clickedPoint.X, movingPoint.X) - left;
544-
selectBorder.Height = selectBorder.Height + 2;
545-
selectBorder.Width = selectBorder.Width + 2;
542+
selectBorder.Height += 2;
543+
selectBorder.Width += 2;
546544

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

559557
isSelecting = false;
560-
currentScreen = null;
558+
CurrentScreen = null;
561559
CursorClipper.UnClipCursor();
562560
RegionClickCanvas.ReleaseMouseCapture();
563561
clippingGeometry.Rect = new Rect(
@@ -572,25 +570,16 @@ private async void RegionClickCanvas_MouseUp(object sender, MouseButtonEventArgs
572570
movingPoint.X = Math.Round(movingPoint.X);
573571
movingPoint.Y = Math.Round(movingPoint.Y);
574572

575-
double correctedLeft = Left;
576-
double correctedTop = Top;
577-
578-
if (correctedLeft < 0)
579-
correctedLeft = 0;
580-
581-
if (correctedTop < 0)
582-
correctedTop = 0;
583-
584573
double xDimScaled = Canvas.GetLeft(selectBorder) * m.M11;
585574
double yDimScaled = Canvas.GetTop(selectBorder) * m.M22;
586575

587-
Rectangle regionScaled = new Rectangle(
576+
Rectangle regionScaled = new(
588577
(int)xDimScaled,
589578
(int)yDimScaled,
590579
(int)(selectBorder.Width * m.M11),
591580
(int)(selectBorder.Height * m.M22));
592581

593-
textFromOCR = string.Empty;
582+
TextFromOCR = string.Empty;
594583

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

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

603-
Language? selectedOcrLang = LanguagesComboBox.SelectedItem as Language;
604-
605-
if (selectedOcrLang is null)
592+
if (LanguagesComboBox.SelectedItem is not Language selectedOcrLang)
606593
selectedOcrLang = LanguageUtilities.GetOCRLanguage();
607594

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

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

615-
bool isSingleLine = SingleLineMenuItem is null ? false : SingleLineMenuItem.IsChecked;
616-
bool isTable = TableMenuItem is null ? false : TableMenuItem.IsChecked;
602+
bool isSingleLine = SingleLineMenuItem is not null && SingleLineMenuItem.IsChecked;
603+
bool isTable = TableMenuItem is not null && TableMenuItem.IsChecked;
617604

618605
if (isSmallClick)
619606
{
620607
BackgroundBrush.Opacity = 0;
621-
textFromOCR = await OcrUtilities.GetClickedWordAsync(this, new System.Windows.Point(xDimScaled, yDimScaled), selectedOcrLang);
608+
TextFromOCR = await OcrUtilities.GetClickedWordAsync(this, new System.Windows.Point(xDimScaled, yDimScaled), selectedOcrLang);
622609
}
623610
else if (isTable)
624-
textFromOCR = await OcrUtilities.GetRegionsTextAsTableAsync(this, regionScaled, selectedOcrLang);
611+
TextFromOCR = await OcrUtilities.GetRegionsTextAsTableAsync(this, regionScaled, selectedOcrLang);
625612
else
626-
textFromOCR = await OcrUtilities.GetRegionsTextAsync(this, regionScaled, selectedOcrLang, tessTag);
613+
TextFromOCR = await OcrUtilities.GetRegionsTextAsync(this, regionScaled, selectedOcrLang, tessTag);
627614

628615
if (DefaultSettings.UseHistory && !isSmallClick)
629616
{
630-
GetDpiAdjustedRegionOfSelectBorder(out DpiScale dpi, out double posLeft, out double posTop);
617+
GetDpiAdjustedRegionOfSelectBorder(out _, out double posLeft, out double posTop);
631618

632619
Rect historyRect = new()
633620
{
@@ -645,13 +632,13 @@ private async void RegionClickCanvas_MouseUp(object sender, MouseButtonEventArgs
645632
CaptureDateTime = DateTimeOffset.Now,
646633
PositionRect = historyRect,
647634
IsTable = TableToggleButton.IsChecked!.Value,
648-
TextContent = textFromOCR,
635+
TextContent = TextFromOCR,
649636
ImageContent = Singleton<HistoryService>.Instance.CachedBitmap,
650637
SourceMode = TextGrabMode.Fullscreen,
651638
};
652639
}
653640

654-
if (!string.IsNullOrWhiteSpace(textFromOCR))
641+
if (!string.IsNullOrWhiteSpace(TextFromOCR))
655642
{
656643
if (SendToEditTextToggleButton.IsChecked is true && destinationTextBox is null)
657644
{
@@ -660,7 +647,7 @@ private async void RegionClickCanvas_MouseUp(object sender, MouseButtonEventArgs
660647
}
661648

662649
OutputUtilities.HandleTextFromOcr(
663-
textFromOCR,
650+
TextFromOCR,
664651
isSingleLine,
665652
isTable,
666653
destinationTextBox);
@@ -711,8 +698,8 @@ private async void Window_Loaded(object sender, RoutedEventArgs e)
711698
{
712699
WindowState = WindowState.Maximized;
713700
FullWindow.Rect = new System.Windows.Rect(0, 0, Width, Height);
714-
this.KeyDown += FullscreenGrab_KeyDown;
715-
this.KeyUp += FullscreenGrab_KeyUp;
701+
KeyDown += FullscreenGrab_KeyDown;
702+
KeyUp += FullscreenGrab_KeyUp;
716703

717704
SetImageToBackground();
718705

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

732-
List<FrameworkElement> tesseractIncompatibleFrameworkElements = new()
733-
{
719+
List<FrameworkElement> tesseractIncompatibleFrameworkElements =
720+
[
734721
TableMenuItem, TableToggleButton
735-
};
722+
];
736723
await LoadOcrLanguages(LanguagesComboBox, usingTesseract, tesseractIncompatibleFrameworkElements);
737724
isComboBoxReady = true;
738725

@@ -744,12 +731,12 @@ private void Window_Unloaded(object sender, RoutedEventArgs e)
744731
{
745732
BackgroundImage.Source = null;
746733
BackgroundImage.UpdateLayout();
747-
currentScreen = null;
734+
CurrentScreen = null;
748735
dpiScale = null;
749-
textFromOCR = null;
736+
TextFromOCR = null;
750737

751-
this.Loaded -= Window_Loaded;
752-
this.Unloaded -= Window_Unloaded;
738+
Loaded -= Window_Loaded;
739+
Unloaded -= Window_Unloaded;
753740

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

774-
this.KeyDown -= FullscreenGrab_KeyDown;
775-
this.KeyUp -= FullscreenGrab_KeyUp;
761+
KeyDown -= FullscreenGrab_KeyDown;
762+
KeyUp -= FullscreenGrab_KeyUp;
776763
}
777764

778765
private void StandardModeToggleButton_Click(object sender, RoutedEventArgs e)
779766
{
780767
bool isActive = CheckIfCheckingOrUnchecking(sender);
781-
WindowUtilities.FullscreenKeyDown(Key.N, isActive);
768+
WindowUtilities.FullscreenKeyDown(Key.N, isActive);
782769
SelectSingleToggleButton(sender);
783770

784771
if (isActive)

0 commit comments

Comments
 (0)