Skip to content

Commit

Permalink
Merge pull request #2326 from MahApps/2324-DragMove-fix
Browse files Browse the repository at this point in the history
DragMove fix for #2324
  • Loading branch information
punker76 committed Jan 21, 2016
2 parents c4ef252 + cf7510b commit 7cc7485
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 20 deletions.
2 changes: 2 additions & 0 deletions MahApps.Metro/Behaviours/BorderlessWindowBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Security;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Interactivity;
using System.Windows.Interop;
using MahApps.Metro.Controls;
Expand Down Expand Up @@ -340,6 +341,7 @@ private void AssociatedObject_Loaded(object sender, RoutedEventArgs e)

window.SetIsHitTestVisibleInChromeProperty<UIElement>("PART_Icon");
window.SetIsHitTestVisibleInChromeProperty<UIElement>("PART_TitleBar");
window.SetIsHitTestVisibleInChromeProperty<Thumb>("PART_WindowTitleThumb");
window.SetIsHitTestVisibleInChromeProperty<ContentPresenter>("PART_LeftWindowCommands");
window.SetIsHitTestVisibleInChromeProperty<ContentPresenter>("PART_RightWindowCommands");
window.SetIsHitTestVisibleInChromeProperty<ContentControl>("PART_WindowButtonCommands");
Expand Down
12 changes: 12 additions & 0 deletions MahApps.Metro/Controls/Flyout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -494,10 +494,12 @@ public override void OnApplyTemplate()

if (this.windowTitleThumb != null)
{
this.windowTitleThumb.PreviewMouseLeftButtonUp -= WindowTitleThumbOnPreviewMouseLeftButtonUp;
this.windowTitleThumb.DragDelta -= this.WindowTitleThumbMoveOnDragDelta;
this.windowTitleThumb.MouseDoubleClick -= this.WindowTitleThumbChangeWindowStateOnMouseDoubleClick;
this.windowTitleThumb.MouseRightButtonUp -= this.WindowTitleThumbSystemMenuOnMouseRightButtonUp;

this.windowTitleThumb.PreviewMouseLeftButtonUp += WindowTitleThumbOnPreviewMouseLeftButtonUp;
this.windowTitleThumb.DragDelta += this.WindowTitleThumbMoveOnDragDelta;
this.windowTitleThumb.MouseDoubleClick += this.WindowTitleThumbChangeWindowStateOnMouseDoubleClick;
this.windowTitleThumb.MouseRightButtonUp += this.WindowTitleThumbSystemMenuOnMouseRightButtonUp;
Expand All @@ -520,13 +522,23 @@ protected internal void CleanUp(FlyoutsControl flyoutsControl)
{
if (this.windowTitleThumb != null)
{
this.windowTitleThumb.PreviewMouseLeftButtonUp -= WindowTitleThumbOnPreviewMouseLeftButtonUp;
this.windowTitleThumb.DragDelta -= this.WindowTitleThumbMoveOnDragDelta;
this.windowTitleThumb.MouseDoubleClick -= this.WindowTitleThumbChangeWindowStateOnMouseDoubleClick;
this.windowTitleThumb.MouseRightButtonUp -= this.WindowTitleThumbSystemMenuOnMouseRightButtonUp;
}
this.parentWindow = null;
}

private void WindowTitleThumbOnPreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
var window = this.ParentWindow;
if (window != null && this.Position != Position.Bottom)
{
MetroWindow.DoWindowTitleThumbOnPreviewMouseLeftButtonUp(window, e);
}
}

private void WindowTitleThumbMoveOnDragDelta(object sender, DragDeltaEventArgs dragDeltaEventArgs)
{
var window = this.ParentWindow;
Expand Down
64 changes: 53 additions & 11 deletions MahApps.Metro/Controls/MetroWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
using MahApps.Metro.Native;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Reflection;
using System.Windows.Controls.Primitives;
using System.Windows.Threading;

namespace MahApps.Metro.Controls
{
Expand Down Expand Up @@ -899,11 +901,23 @@ public override void OnApplyTemplate()
this.SetVisibiltyForAllTitleElements(this.TitlebarHeight > 0);
}

protected IntPtr CriticalHandle
{
get
{
var value = typeof(Window)
.GetProperty("CriticalHandle", BindingFlags.NonPublic | BindingFlags.Instance)
.GetValue(this, new object[0]);
return (IntPtr)value;
}
}

private void ClearWindowEvents()
{
// clear all event handlers first:
if (this.windowTitleThumb != null)
{
this.windowTitleThumb.PreviewMouseLeftButtonUp -= WindowTitleThumbOnPreviewMouseLeftButtonUp;
this.windowTitleThumb.DragDelta -= this.WindowTitleThumbMoveOnDragDelta;
this.windowTitleThumb.MouseDoubleClick -= this.WindowTitleThumbChangeWindowStateOnMouseDoubleClick;
this.windowTitleThumb.MouseRightButtonUp -= this.WindowTitleThumbSystemMenuOnMouseRightButtonUp;
Expand All @@ -928,6 +942,7 @@ private void SetWindowEvents()

if (this.windowTitleThumb != null)
{
this.windowTitleThumb.PreviewMouseLeftButtonUp += WindowTitleThumbOnPreviewMouseLeftButtonUp;
this.windowTitleThumb.DragDelta += this.WindowTitleThumbMoveOnDragDelta;
this.windowTitleThumb.MouseDoubleClick += this.WindowTitleThumbChangeWindowStateOnMouseDoubleClick;
this.windowTitleThumb.MouseRightButtonUp += this.WindowTitleThumbSystemMenuOnMouseRightButtonUp;
Expand Down Expand Up @@ -955,6 +970,11 @@ private void IconMouseDown(object sender, MouseButtonEventArgs e)
}
}

private void WindowTitleThumbOnPreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
DoWindowTitleThumbOnPreviewMouseLeftButtonUp(this, e);
}

private void WindowTitleThumbMoveOnDragDelta(object sender, DragDeltaEventArgs dragDeltaEventArgs)
{
DoWindowTitleThumbMoveOnDragDelta(this, dragDeltaEventArgs);
Expand All @@ -970,18 +990,24 @@ private void WindowTitleThumbSystemMenuOnMouseRightButtonUp(object sender, Mouse
DoWindowTitleThumbSystemMenuOnMouseRightButtonUp(this, e);
}

internal static void DoWindowTitleThumbOnPreviewMouseLeftButtonUp(MetroWindow window, MouseButtonEventArgs mouseButtonEventArgs)
{
Mouse.Capture(null);
}

internal static void DoWindowTitleThumbMoveOnDragDelta(MetroWindow window, DragDeltaEventArgs dragDeltaEventArgs)
{
// drag only if IsWindowDraggable is set to true
if (!window.IsWindowDraggable ||
(!(Math.Abs(dragDeltaEventArgs.HorizontalChange) > 2) &&
!(Math.Abs(dragDeltaEventArgs.VerticalChange) > 2))) return;
(!(Math.Abs(dragDeltaEventArgs.HorizontalChange) > 2) && !(Math.Abs(dragDeltaEventArgs.VerticalChange) > 2)))
{
return;
}

var windowHandle = new WindowInteropHelper(window).Handle;
var cursorPos = Standard.NativeMethods.GetCursorPos();
// tage from DragMove internal code
window.VerifyAccess();

// for the touch usage
UnsafeNativeMethods.ReleaseCapture();
var cursorPos = Standard.NativeMethods.GetCursorPos();

// if the window is maximized dragging is only allowed on title bar (also if not visible)
var windowIsMaximized = window.WindowState == WindowState.Maximized;
Expand All @@ -991,14 +1017,30 @@ internal static void DoWindowTitleThumbMoveOnDragDelta(MetroWindow window, DragD
return;
}

if (windowIsMaximized)
{
// for the touch usage
UnsafeNativeMethods.ReleaseCapture();

if (windowIsMaximized) {
window.Top = 2;
window.Left = Math.Max(cursorPos.x - window.RestoreBounds.Width / 2, 0);
EventHandler windowOnStateChanged = null;
windowOnStateChanged = (sender, args) =>
{
window.StateChanged -= windowOnStateChanged;
if (window.WindowState == WindowState.Normal)
{
Mouse.Capture(window.windowTitleThumb, CaptureMode.Element);
}
};
window.StateChanged += windowOnStateChanged;
}
var lParam = (int)(uint)cursorPos.x | (cursorPos.y << 16);
Standard.NativeMethods.SendMessage(windowHandle, Standard.WM.LBUTTONUP, (IntPtr)Standard.HT.CAPTION, (IntPtr)lParam);
Standard.NativeMethods.SendMessage(windowHandle, Standard.WM.SYSCOMMAND, (IntPtr)Standard.SC.MOUSEMOVE, IntPtr.Zero);

var criticalHandle = window.CriticalHandle;
// DragMove works too
// window.DragMove();
// instead this 2 lines
Standard.NativeMethods.SendMessage(criticalHandle, Standard.WM.SYSCOMMAND, (IntPtr)Standard.SC.MOUSEMOVE, IntPtr.Zero);
Standard.NativeMethods.SendMessage(criticalHandle, Standard.WM.LBUTTONUP, IntPtr.Zero, IntPtr.Zero);
}

internal static void DoWindowTitleThumbChangeWindowStateOnMouseDoubleClick(MetroWindow window, MouseButtonEventArgs mouseButtonEventArgs)
Expand Down
2 changes: 1 addition & 1 deletion MahApps.Metro/MahApps.Metro.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2011/10/nuspec.xsd">
<metadata>
<id>MahApps.Metro</id>
<version>1.2.3.0</version>
<version>1.2.4.0</version>
<title>MahApps.Metro</title>
<authors>Jan Karger, Dennis Daume, Brendan Forster, Paul Jenkins, Jake Ginnivan, Alex Mitchell</authors>
<owners>punker76,shiftkey,aeoth,jakeginnivan</owners>
Expand Down
8 changes: 4 additions & 4 deletions MahApps.Metro/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
[assembly: XmlnsDefinition("http://metro.mahapps.com/winfx/xaml/shared", "MahApps.Metro.Converters")]
[assembly: XmlnsDefinition("http://metro.mahapps.com/winfx/xaml/controls", "MahApps.Metro.Controls")]

[assembly: AssemblyVersion("1.2.3.0")]
[assembly: AssemblyFileVersion("1.2.3.0")]
[assembly: AssemblyInformationalVersion("1.2.3.0")]
[assembly: AssemblyVersion("1.2.4.0")]
[assembly: AssemblyFileVersion("1.2.4.0")]
[assembly: AssemblyInformationalVersion("1.2.4.0")]
[assembly: AssemblyTitleAttribute("MahApps.Metro")]
[assembly: AssemblyDescriptionAttribute("Toolkit for creating Metro styled WPF apps")]
[assembly: AssemblyProductAttribute("MahApps.Metro 1.2.3")]
[assembly: AssemblyProductAttribute("MahApps.Metro 1.2.4")]
[assembly: AssemblyCompany("MahApps")]
11 changes: 7 additions & 4 deletions MahApps.Metro/Themes/Flyout.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,10 @@
DockPanel.Dock="Bottom" />
</DockPanel>
</AdornerDecorator>
<Thumb Style="{StaticResource WindowTitleThumbStyle}"
Height="{Binding Path=TitlebarHeight, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Controls:MetroWindow}}}"
VerticalAlignment="Top"
x:Name="PART_WindowTitleThumb" />
<Controls:MetroThumb Style="{StaticResource WindowTitleThumbStyle}"
Height="{Binding Path=TitlebarHeight, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Controls:MetroWindow}}}"
VerticalAlignment="Top"
x:Name="PART_WindowTitleThumb" />
</Grid>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding Position, RelativeSource={RelativeSource Self}}"
Expand All @@ -256,6 +256,9 @@
<Setter TargetName="PART_Content"
Property="DockPanel.Dock"
Value="Right" />
<Setter TargetName="PART_WindowTitleThumb"
Property="Visibility"
Value="Collapsed" />
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
Expand Down
6 changes: 6 additions & 0 deletions MahApps.Metro/Themes/MetroWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@
<Trigger Property="UseNoneWindowStyle" Value="True">
<Setter TargetName="PART_WindowTitleThumb" Property="Grid.RowSpan" Value="2" />
</Trigger>
<Trigger Property="ShowTitleBar" Value="False">
<Setter TargetName="PART_WindowTitleThumb" Property="Grid.RowSpan" Value="2" />
</Trigger>
<!-- handle active/inactive state -->
<Trigger Property="IsActive"
Value="False">
Expand Down Expand Up @@ -505,6 +508,9 @@
<Trigger Property="UseNoneWindowStyle" Value="True">
<Setter TargetName="PART_WindowTitleThumb" Property="Grid.RowSpan" Value="2" />
</Trigger>
<Trigger Property="ShowTitleBar" Value="False">
<Setter TargetName="PART_WindowTitleThumb" Property="Grid.RowSpan" Value="2" />
</Trigger>
<!-- handle active/inactive state -->
<Trigger Property="IsActive"
Value="False">
Expand Down
9 changes: 9 additions & 0 deletions docs/release-notes/1.2.4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Notes

MahApps.Metro v1.2.4 bug fix release.

# Bugfixes

- #2288 First 30 units of flyout aren't clickable
- #2324 DataGrid activated by Titlebar (rows are hovered while dragging a window over a DataGrid)
- #2287 IsWindowDraggable broke starting in 1.2

0 comments on commit 7cc7485

Please sign in to comment.