8
8
using System . Windows . Controls . Primitives ;
9
9
using System . Windows . Input ;
10
10
using System . Windows . Media ;
11
+ using Text_Grab . Extensions ;
11
12
using Text_Grab . Interfaces ;
12
13
using Text_Grab . Models ;
13
14
using Text_Grab . Properties ;
@@ -25,16 +26,16 @@ public partial class FullscreenGrab : Window
25
26
{
26
27
#region Fields
27
28
28
- private System . Windows . Point clickedPoint = new System . Windows . Point ( ) ;
29
+ private System . Windows . Point clickedPoint = new ( ) ;
29
30
private TextBox ? destinationTextBox ;
30
31
private DpiScale ? dpiScale ;
31
32
private bool isComboBoxReady = false ;
32
33
private bool isSelecting = false ;
33
34
private bool isShiftDown = false ;
34
- private Border selectBorder = new Border ( ) ;
35
+ private Border selectBorder = new ( ) ;
35
36
private double selectLeft ;
36
37
private double selectTop ;
37
- private System . Windows . Point shiftPoint = new System . Windows . Point ( ) ;
38
+ private System . Windows . Point shiftPoint = new ( ) ;
38
39
private double xShiftDelta ;
39
40
private double yShiftDelta ;
40
41
private HistoryInfo ? historyInfo ;
@@ -70,8 +71,8 @@ public TextBox? DestinationTextBox
70
71
}
71
72
72
73
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 ; }
75
76
76
77
#endregion Properties
77
78
@@ -264,7 +265,6 @@ private void GetDpiAdjustedRegionOfSelectBorder(out DpiScale dpi, out double pos
264
265
{
265
266
System . Windows . Point absPosPoint = this . GetAbsolutePosition ( ) ;
266
267
dpi = VisualTreeHelper . GetDpi ( this ) ;
267
- int firstScreenBPP = System . Windows . Forms . Screen . AllScreens [ 0 ] . BitsPerPixel ;
268
268
269
269
posLeft = Canvas . GetLeft ( selectBorder ) + ( absPosPoint . X / dpi . PixelsPerDip ) ;
270
270
posTop = Canvas . GetTop ( selectBorder ) + ( absPosPoint . Y / dpi . PixelsPerDip ) ;
@@ -362,7 +362,7 @@ private static async Task LoadOcrLanguages(ComboBox languagesComboBox, bool usin
362
362
haveSetLastLang = true ;
363
363
364
364
if ( tesseractIncompatibleElements is not null )
365
- foreach ( var element in tesseractIncompatibleElements )
365
+ foreach ( FrameworkElement element in tesseractIncompatibleElements )
366
366
element . Visibility = Visibility . Collapsed ;
367
367
}
368
368
@@ -421,12 +421,12 @@ private void PanSelection(System.Windows.Point movingPoint)
421
421
double leftValue = selectLeft + xShiftDelta ;
422
422
double topValue = selectTop + yShiftDelta ;
423
423
424
- if ( currentScreen is not null && dpiScale is not null )
424
+ if ( CurrentScreen is not null && dpiScale is not null )
425
425
{
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 ;
430
430
431
431
leftValue = Math . Clamp ( leftValue , currentScreenLeft , ( currentScreenRight - selectBorder . Width ) ) ;
432
432
topValue = Math . Clamp ( topValue , currentScreenTop , ( currentScreenBottom - selectBorder . Height ) ) ;
@@ -445,9 +445,7 @@ private void PlaceGrabFrameInSelectionRect()
445
445
// Then place it where the user just drew the region
446
446
// Add space around the window to account for Titlebar
447
447
// 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 ) ;
451
449
452
450
GrabFrame grabFrame = new ( )
453
451
{
@@ -515,9 +513,9 @@ private void RegionClickCanvas_MouseDown(object sender, MouseButtonEventArgs e)
515
513
System . Windows . Point formsPoint = new ( ( int ) clickedPoint . X , ( int ) clickedPoint . Y ) ;
516
514
foreach ( DisplayInfo scr in screens )
517
515
{
518
- Rect bound = scr . Bounds ;
516
+ Rect bound = scr . ScaledBounds ( ) ;
519
517
if ( bound . Contains ( formsPoint ) )
520
- currentScreen = scr ;
518
+ CurrentScreen = scr ;
521
519
}
522
520
}
523
521
@@ -536,13 +534,13 @@ private void RegionClickCanvas_MouseMove(object sender, MouseEventArgs e)
536
534
537
535
isShiftDown = false ;
538
536
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 ) ;
541
539
542
540
selectBorder . Height = Math . Max ( clickedPoint . Y , movingPoint . Y ) - top ;
543
541
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 ;
546
544
547
545
clippingGeometry . Rect = new Rect (
548
546
new System . Windows . Point ( left , top ) ,
@@ -557,7 +555,7 @@ private async void RegionClickCanvas_MouseUp(object sender, MouseButtonEventArgs
557
555
return ;
558
556
559
557
isSelecting = false ;
560
- currentScreen = null ;
558
+ CurrentScreen = null ;
561
559
CursorClipper . UnClipCursor ( ) ;
562
560
RegionClickCanvas . ReleaseMouseCapture ( ) ;
563
561
clippingGeometry . Rect = new Rect (
@@ -572,25 +570,16 @@ private async void RegionClickCanvas_MouseUp(object sender, MouseButtonEventArgs
572
570
movingPoint . X = Math . Round ( movingPoint . X ) ;
573
571
movingPoint . Y = Math . Round ( movingPoint . Y ) ;
574
572
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
-
584
573
double xDimScaled = Canvas . GetLeft ( selectBorder ) * m . M11 ;
585
574
double yDimScaled = Canvas . GetTop ( selectBorder ) * m . M22 ;
586
575
587
- Rectangle regionScaled = new Rectangle (
576
+ Rectangle regionScaled = new (
588
577
( int ) xDimScaled ,
589
578
( int ) yDimScaled ,
590
579
( int ) ( selectBorder . Width * m . M11 ) ,
591
580
( int ) ( selectBorder . Height * m . M22 ) ) ;
592
581
593
- textFromOCR = string . Empty ;
582
+ TextFromOCR = string . Empty ;
594
583
595
584
if ( NewGrabFrameMenuItem . IsChecked is true )
596
585
{
@@ -600,9 +589,7 @@ private async void RegionClickCanvas_MouseUp(object sender, MouseButtonEventArgs
600
589
601
590
try { RegionClickCanvas . Children . Remove ( selectBorder ) ; } catch { }
602
591
603
- Language ? selectedOcrLang = LanguagesComboBox . SelectedItem as Language ;
604
-
605
- if ( selectedOcrLang is null )
592
+ if ( LanguagesComboBox . SelectedItem is not Language selectedOcrLang )
606
593
selectedOcrLang = LanguageUtilities . GetOCRLanguage ( ) ;
607
594
608
595
string tessTag = string . Empty ;
@@ -612,22 +599,22 @@ private async void RegionClickCanvas_MouseUp(object sender, MouseButtonEventArgs
612
599
613
600
bool isSmallClick = ( regionScaled . Width < 3 || regionScaled . Height < 3 ) ;
614
601
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 ;
617
604
618
605
if ( isSmallClick )
619
606
{
620
607
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 ) ;
622
609
}
623
610
else if ( isTable )
624
- textFromOCR = await OcrUtilities . GetRegionsTextAsTableAsync ( this , regionScaled , selectedOcrLang ) ;
611
+ TextFromOCR = await OcrUtilities . GetRegionsTextAsTableAsync ( this , regionScaled , selectedOcrLang ) ;
625
612
else
626
- textFromOCR = await OcrUtilities . GetRegionsTextAsync ( this , regionScaled , selectedOcrLang , tessTag ) ;
613
+ TextFromOCR = await OcrUtilities . GetRegionsTextAsync ( this , regionScaled , selectedOcrLang , tessTag ) ;
627
614
628
615
if ( DefaultSettings . UseHistory && ! isSmallClick )
629
616
{
630
- GetDpiAdjustedRegionOfSelectBorder ( out DpiScale dpi , out double posLeft , out double posTop ) ;
617
+ GetDpiAdjustedRegionOfSelectBorder ( out _ , out double posLeft , out double posTop ) ;
631
618
632
619
Rect historyRect = new ( )
633
620
{
@@ -645,13 +632,13 @@ private async void RegionClickCanvas_MouseUp(object sender, MouseButtonEventArgs
645
632
CaptureDateTime = DateTimeOffset . Now ,
646
633
PositionRect = historyRect ,
647
634
IsTable = TableToggleButton . IsChecked ! . Value ,
648
- TextContent = textFromOCR ,
635
+ TextContent = TextFromOCR ,
649
636
ImageContent = Singleton < HistoryService > . Instance . CachedBitmap ,
650
637
SourceMode = TextGrabMode . Fullscreen ,
651
638
} ;
652
639
}
653
640
654
- if ( ! string . IsNullOrWhiteSpace ( textFromOCR ) )
641
+ if ( ! string . IsNullOrWhiteSpace ( TextFromOCR ) )
655
642
{
656
643
if ( SendToEditTextToggleButton . IsChecked is true && destinationTextBox is null )
657
644
{
@@ -660,7 +647,7 @@ private async void RegionClickCanvas_MouseUp(object sender, MouseButtonEventArgs
660
647
}
661
648
662
649
OutputUtilities . HandleTextFromOcr (
663
- textFromOCR ,
650
+ TextFromOCR ,
664
651
isSingleLine ,
665
652
isTable ,
666
653
destinationTextBox ) ;
@@ -711,8 +698,8 @@ private async void Window_Loaded(object sender, RoutedEventArgs e)
711
698
{
712
699
WindowState = WindowState . Maximized ;
713
700
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 ;
716
703
717
704
SetImageToBackground ( ) ;
718
705
@@ -729,10 +716,10 @@ private async void Window_Loaded(object sender, RoutedEventArgs e)
729
716
Topmost = false ;
730
717
#endif
731
718
732
- List < FrameworkElement > tesseractIncompatibleFrameworkElements = new ( )
733
- {
719
+ List < FrameworkElement > tesseractIncompatibleFrameworkElements =
720
+ [
734
721
TableMenuItem , TableToggleButton
735
- } ;
722
+ ] ;
736
723
await LoadOcrLanguages ( LanguagesComboBox , usingTesseract , tesseractIncompatibleFrameworkElements ) ;
737
724
isComboBoxReady = true ;
738
725
@@ -744,12 +731,12 @@ private void Window_Unloaded(object sender, RoutedEventArgs e)
744
731
{
745
732
BackgroundImage . Source = null ;
746
733
BackgroundImage . UpdateLayout ( ) ;
747
- currentScreen = null ;
734
+ CurrentScreen = null ;
748
735
dpiScale = null ;
749
- textFromOCR = null ;
736
+ TextFromOCR = null ;
750
737
751
- this . Loaded -= Window_Loaded ;
752
- this . Unloaded -= Window_Unloaded ;
738
+ Loaded -= Window_Loaded ;
739
+ Unloaded -= Window_Unloaded ;
753
740
754
741
RegionClickCanvas . MouseDown -= RegionClickCanvas_MouseDown ;
755
742
RegionClickCanvas . MouseMove -= RegionClickCanvas_MouseMove ;
@@ -771,14 +758,14 @@ private void Window_Unloaded(object sender, RoutedEventArgs e)
771
758
SettingsButton . Click -= SettingsMenuItem_Click ;
772
759
CancelButton . Click -= CancelMenuItem_Click ;
773
760
774
- this . KeyDown -= FullscreenGrab_KeyDown ;
775
- this . KeyUp -= FullscreenGrab_KeyUp ;
761
+ KeyDown -= FullscreenGrab_KeyDown ;
762
+ KeyUp -= FullscreenGrab_KeyUp ;
776
763
}
777
764
778
765
private void StandardModeToggleButton_Click ( object sender , RoutedEventArgs e )
779
766
{
780
767
bool isActive = CheckIfCheckingOrUnchecking ( sender ) ;
781
- WindowUtilities . FullscreenKeyDown ( Key . N , isActive ) ;
768
+ WindowUtilities . FullscreenKeyDown ( Key . N , isActive ) ;
782
769
SelectSingleToggleButton ( sender ) ;
783
770
784
771
if ( isActive )
0 commit comments