Skip to content

A Swiss Army knife for developers.

Fix bug where we can't restore the window when maximized on Windows 10/11 (#1226)

Etienne Baudoux03958870c99d
diff --git a/src/app/dev/platforms/desktop/DevToys.Windows/Controls/MicaWindowWithOverlay.xaml.cs b/src/app/dev/platforms/desktop/DevToys.Windows/Controls/MicaWindowWithOverlay.xaml.cs
index d736c0d01..d9a44b153 100644
--- a/src/app/dev/platforms/desktop/DevToys.Windows/Controls/MicaWindowWithOverlay.xaml.cs
+++ b/src/app/dev/platforms/desktop/DevToys.Windows/Controls/MicaWindowWithOverlay.xaml.cs
@@ -34,6 +34,8 @@ public abstract partial class MicaWindowWithOverlay : Window
private Button? _maximizeButton;
private Button? _minimizeButton;
private StackPanel? _windowStateButtonsStackPanel;
+ private bool _isMouseButtonDownOnDraggableTitleBarArea;
+ private Point _mouseDownPositionOnDraggableTitleBarArea;
protected MicaWindowWithOverlay()
{
@@ -129,7 +131,9 @@ public abstract partial class MicaWindowWithOverlay : Window
_restoreButton.Click += RestoreButton_Click;
_maximizeButton.Click += MaximizeButton_Click;
draggableTitleBarArea.MouseLeftButtonDown += DraggableTitleBarArea_MouseLeftButtonDown;
+ draggableTitleBarArea.MouseLeftButtonUp += DraggableTitleBarArea_MouseLeftButtonUp;
draggableTitleBarArea.MouseRightButtonUp += DraggableTitleBarArea_MouseRightButtonUp;
+ draggableTitleBarArea.MouseMove += DraggableTitleBarArea_MouseMove;
this.PreviewKeyDown += MicaWindowWithOverlay_PreviewKeyDown;
overlayControl.WndProc = WndProc;
}
@@ -220,12 +224,65 @@ public abstract partial class MicaWindowWithOverlay : Window
{
SystemCommands.MaximizeWindow(this);
}
+
+ return;
}
}
else
{
- // Move the window.
- DragMove();
+ if (WindowState == WindowState.Maximized)
+ {
+ _isMouseButtonDownOnDraggableTitleBarArea = true;
+ _mouseDownPositionOnDraggableTitleBarArea = e.GetPosition(this);
+ }
+ else
+ {
+ try
+ {
+ DragMove();
+ }
+ catch
+ {
+ }
+ }
+ }
+ }
+
+ private void DraggableTitleBarArea_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
+ {
+ _isMouseButtonDownOnDraggableTitleBarArea = false;
+ }
+
+ private void DraggableTitleBarArea_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
+ {
+ if (_isMouseButtonDownOnDraggableTitleBarArea
+ && e.LeftButton == MouseButtonState.Pressed
+ && WindowState == WindowState.Maximized)
+ {
+ Point position = e.GetPosition(this);
+ Point screenPosition = PointToScreen(position);
+
+ Vector vector = _mouseDownPositionOnDraggableTitleBarArea - position;
+
+ if (vector.Length > 1)
+ {
+ double relativeDistance = position.X / ActualWidth;
+
+ WindowState = WindowState.Normal;
+
+ double actualDistance = ActualWidth * relativeDistance;
+
+ try
+ {
+ Top = screenPosition.Y - position.Y;
+ Left = screenPosition.X - actualDistance;
+
+ DragMove();
+ }
+ catch
+ {
+ }
+ }
}
}