Skip to content

Commit

Permalink
Merge pull request #483 from ZGGSONG/dev
Browse files Browse the repository at this point in the history
Fix(FullscreenGrab): Fix the issue where TopPanel cannot be displayed immediately after switching between Freeze and Unfreeze and Sub-screen shift move selection doesn't cover full screen
  • Loading branch information
TheJoeFin authored Aug 9, 2024
2 parents a290607 + 7fdc86c commit b85c3e9
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 17 deletions.
37 changes: 35 additions & 2 deletions Text-Grab/Utilities/WindowUtilities.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using Dapplo.Windows.User32;
using Fasetto.Word;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
Expand Down Expand Up @@ -31,7 +33,7 @@ public static void SetWindowPosition(Window passedWindow)
storedPositionString = AppUtilities.TextGrabSettings.EditTextWindowSizeAndPosition;

if (passedWindow is GrabFrame)
storedPositionString = AppUtilities.TextGrabSettings.GrabFrameWindowSizeAndPosition;
storedPositionString = AppUtilities.TextGrabSettings.GrabFrameWindowSizeAndPosition;

List<string> storedPosition = new(storedPositionString.Split(','));

Expand Down Expand Up @@ -286,4 +288,35 @@ public static void ShouldShutDown()
if (shouldShutDown)
Application.Current.Shutdown();
}
}

public static bool GetMousePosition(out Point mousePosition)
{
if (GetCursorPos(out POINT point))
{
mousePosition = new Point(point.X, point.Y);
return true;
}
mousePosition = default;
return false;
}

public static bool IsMouseInWindow(this Window window)
{
GetMousePosition(out Point mousePosition);

DpiScale dpi = System.Windows.Media.VisualTreeHelper.GetDpi(window);
Point absPosPoint = window.GetAbsolutePosition();
Rect windowRect = new(absPosPoint.X, absPosPoint.Y,
window.ActualWidth * dpi.DpiScaleX,
window.ActualHeight * dpi.DpiScaleY);
return windowRect.Contains(mousePosition);
}

#region DLLImport

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetCursorPos(out POINT lpPoint);

#endregion
}
29 changes: 14 additions & 15 deletions Text-Grab/Views/FullscreenGrab.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ private async void FreezeUnfreeze(bool Activate)
RegionClickCanvas.ContextMenu.IsOpen = false;
await Task.Delay(150);
SetImageToBackground();

if (IsMouseOver)
if (this.IsMouseInWindow())
TopButtonsStackPanel.Visibility = Visibility.Visible;
}
else
Expand Down Expand Up @@ -423,10 +423,10 @@ private void PanSelection(System.Windows.Point movingPoint)

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 = 0;
double currentScreenTop = 0;
double currentScreenRight = CurrentScreen.Bounds.Width / dpiScale.Value.DpiScaleX;
double currentScreenBottom = CurrentScreen.Bounds.Height / dpiScale.Value.DpiScaleY;

leftValue = Math.Clamp(leftValue, currentScreenLeft, (currentScreenRight - selectBorder.Width));
topValue = Math.Clamp(topValue, currentScreenTop, (currentScreenBottom - selectBorder.Height));
Expand Down Expand Up @@ -495,8 +495,8 @@ private void RegionClickCanvas_MouseDown(object sender, MouseButtonEventArgs e)
RegionClickCanvas.CaptureMouse();
CursorClipper.ClipCursor(this);
clickedPoint = e.GetPosition(this);
selectBorder.Height = 1;
selectBorder.Width = 1;
selectBorder.Height = 2;
selectBorder.Width = 2;

dpiScale = VisualTreeHelper.GetDpi(this);

Expand All @@ -509,13 +509,12 @@ private void RegionClickCanvas_MouseDown(object sender, MouseButtonEventArgs e)
Canvas.SetLeft(selectBorder, clickedPoint.X);
Canvas.SetTop(selectBorder, clickedPoint.Y);

DisplayInfo[] screens = DisplayInfo.AllDisplayInfos;
System.Windows.Point formsPoint = new((int)clickedPoint.X, (int)clickedPoint.Y);
foreach (DisplayInfo scr in screens)
WindowUtilities.GetMousePosition(out System.Windows.Point mousePoint);
foreach (DisplayInfo? screen in DisplayInfo.AllDisplayInfos)
{
Rect bound = scr.ScaledBounds();
if (bound.Contains(formsPoint))
CurrentScreen = scr;
Rect bound = screen.ScaledBounds();
if (bound.Contains(mousePoint))
CurrentScreen = screen;
}
}

Expand Down Expand Up @@ -597,7 +596,7 @@ private async void RegionClickCanvas_MouseUp(object sender, MouseButtonEventArgs
if (LanguagesComboBox.SelectedItem is TessLang tessLang)
tessTag = tessLang.LanguageTag;

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

bool isSingleLine = SingleLineMenuItem is not null && SingleLineMenuItem.IsChecked;
bool isTable = TableMenuItem is not null && TableMenuItem.IsChecked;
Expand Down

0 comments on commit b85c3e9

Please sign in to comment.