Skip to content

Commit

Permalink
Merge pull request #42 from Aelarion/main
Browse files Browse the repository at this point in the history
Implement window dragging when maximized (fix #36)
  • Loading branch information
pomianowski authored Jan 25, 2022
2 parents 33a1516 + 6df0f16 commit a0795c3
Showing 1 changed file with 28 additions and 12 deletions.
40 changes: 28 additions & 12 deletions WPFUI/Controls/TitleBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -459,12 +459,40 @@ private void TitleBar_Loaded(object sender, RoutedEventArgs e)
{
rootGrid.MouseDown += RootGrid_MouseDown;
rootGrid.MouseLeftButtonDown += RootGrid_MouseLeftButtonDown;
rootGrid.MouseMove += RootGrid_MouseMove;
}

if (ParentWindow != null)
ParentWindow.StateChanged += ParentWindow_StateChanged;
}

private void RootGrid_MouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton != MouseButtonState.Pressed || ParentWindow == null) return;

if (IsMaximized)
{
var screenPoint = PointToScreen(e.MouseDevice.GetPosition(this));

// TODO: refine the Left value to be more accurate
// - This calculation is good enough using the center
// of the titlebar, however this isn't quite accurate for
// how the OS operates.
// - It should be set as a % (e.g. screen X / maximized width),
// then offset from the left to line up more naturally.
ParentWindow.Left = screenPoint.X - (ParentWindow.RestoreBounds.Width * 0.5);
ParentWindow.Top = 0d;

// style has to be quickly swapped to avoid restore animation delay
var style = ParentWindow.WindowStyle;
ParentWindow.WindowStyle = WindowStyle.None;
ParentWindow.WindowState = WindowState.Normal;
ParentWindow.WindowStyle = style;

ParentWindow.DragMove();
}
}

private void ParentWindow_StateChanged(object sender, EventArgs e)
{
if (ParentWindow == null) return;
Expand All @@ -478,18 +506,6 @@ private void RootGrid_MouseDown(object sender, MouseButtonEventArgs e)
if (e.ChangedButton != MouseButton.Left)
return;

// TODO: Restore on maximize and move when single click and hold

//if (e.ClickCount == 1 && e.ButtonState == MouseButtonState.Pressed && ParentWindow.WindowState == WindowState.Maximized)
//{
// MaximizeWindow();
// ParentWindow.DragMove();
//}
//else
//{
// ParentWindow.DragMove();
//}

ParentWindow.DragMove();
}

Expand Down

0 comments on commit a0795c3

Please sign in to comment.