From 5ea6fd1ff9b4c80f3c8e5f35fad7607620d04249 Mon Sep 17 00:00:00 2001 From: punker76 Date: Sun, 10 Dec 2023 21:39:54 +0100 Subject: [PATCH] feat: #4445 add new property CloseOnIconDoubleClick Add new property CloseOnIconDoubleClick to allow disable closing window if the user double clicks on window icon. --- src/MahApps.Metro/Controls/MetroWindow.cs | 37 +++++++++++++++-------- 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/src/MahApps.Metro/Controls/MetroWindow.cs b/src/MahApps.Metro/Controls/MetroWindow.cs index 590d153d2..a683aa804 100644 --- a/src/MahApps.Metro/Controls/MetroWindow.cs +++ b/src/MahApps.Metro/Controls/MetroWindow.cs @@ -159,6 +159,22 @@ public MultiFrameImageMode IconScalingMode set => this.SetValue(IconScalingModeProperty, value); } + /// Identifies the dependency property. + public static readonly DependencyProperty CloseOnIconDoubleClickProperty + = DependencyProperty.Register(nameof(CloseOnIconDoubleClick), + typeof(bool), + typeof(MetroWindow), + new PropertyMetadata(BooleanBoxes.TrueBox)); + + /// + /// Gets or sets the value to close the window if the user double click on the window icon. + /// + public bool CloseOnIconDoubleClick + { + get => (bool)this.GetValue(CloseOnIconDoubleClickProperty); + set => this.SetValue(CloseOnIconDoubleClickProperty, BooleanBoxes.Box(value)); + } + /// Identifies the dependency property. public static readonly DependencyProperty ShowTitleBarProperty = DependencyProperty.Register(nameof(ShowTitleBar), @@ -1479,7 +1495,7 @@ private void ClearWindowEvents() if (this.icon != null) { - this.icon.MouseDown -= this.IconMouseDown; + this.icon.MouseLeftButtonDown -= this.OnIconMouseLeftButtonDown; } this.SizeChanged -= this.MetroWindow_SizeChanged; @@ -1493,7 +1509,7 @@ private void SetWindowEvents() // set mouse down/up for icon if (this.icon != null && this.icon.Visibility == Visibility.Visible) { - this.icon.MouseDown += this.IconMouseDown; + this.icon.MouseDown += this.OnIconMouseLeftButtonDown; } if (this.windowTitleThumb != null) @@ -1527,20 +1543,17 @@ private void SetWindowEvents() } } - private void IconMouseDown(object sender, MouseButtonEventArgs e) + private void OnIconMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { - if (e.ChangedButton == MouseButton.Left) + if (e.ClickCount == 2 && this.CloseOnIconDoubleClick) + { + this.Close(); + } + else if (this.ShowSystemMenu) { - if (e.ClickCount == 2) - { - this.Close(); - } - else if (this.ShowSystemMenu) - { #pragma warning disable 618 - ControlzEx.SystemCommands.ShowSystemMenuPhysicalCoordinates(this, this.PointToScreen(new Point(this.BorderThickness.Left, this.TitleBarHeight + this.BorderThickness.Top))); + ControlzEx.SystemCommands.ShowSystemMenuPhysicalCoordinates(this, this.PointToScreen(new Point(this.BorderThickness.Left, this.TitleBarHeight + this.BorderThickness.Top))); #pragma warning restore 618 - } } }