Skip to content

Commit

Permalink
Merge pull request #4446 from MahApps/jk/4445-close-on-icon-doubleclick
Browse files Browse the repository at this point in the history
Add new property CloseOnIconDoubleClick to MetroWindow
  • Loading branch information
punker76 committed Dec 10, 2023
2 parents 28bf413 + 5ea6fd1 commit c28bcce
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 77 deletions.
1 change: 1 addition & 0 deletions src/MahApps.Metro/Behaviors/WindowsSettingBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Windows.Win32.Graphics.Gdi;
using Windows.Win32.UI.WindowsAndMessaging;
using MahApps.Metro.Controls;
using MahApps.Metro.Native;
using Microsoft.Xaml.Behaviors;

namespace MahApps.Metro.Behaviors
Expand Down
1 change: 1 addition & 0 deletions src/MahApps.Metro/Controls/Dialogs/DialogManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Windows;
using System.Windows.Controls;
using ControlzEx.Theming;
using MahApps.Metro.Native;
using MahApps.Metro.ValueBoxes;

namespace MahApps.Metro.Controls.Dialogs
Expand Down
37 changes: 25 additions & 12 deletions src/MahApps.Metro/Controls/MetroWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,22 @@ public MultiFrameImageMode IconScalingMode
set => this.SetValue(IconScalingModeProperty, value);
}

/// <summary>Identifies the <see cref="CloseOnIconDoubleClick"/> dependency property.</summary>
public static readonly DependencyProperty CloseOnIconDoubleClickProperty
= DependencyProperty.Register(nameof(CloseOnIconDoubleClick),
typeof(bool),
typeof(MetroWindow),
new PropertyMetadata(BooleanBoxes.TrueBox));

/// <summary>
/// Gets or sets the value to close the window if the user double click on the window icon.
/// </summary>
public bool CloseOnIconDoubleClick
{
get => (bool)this.GetValue(CloseOnIconDoubleClickProperty);
set => this.SetValue(CloseOnIconDoubleClickProperty, BooleanBoxes.Box(value));
}

/// <summary>Identifies the <see cref="ShowTitleBar"/> dependency property.</summary>
public static readonly DependencyProperty ShowTitleBarProperty
= DependencyProperty.Register(nameof(ShowTitleBar),
Expand Down Expand Up @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/MahApps.Metro/Controls/WindowButtonCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Threading;
using MahApps.Metro.Native;

namespace MahApps.Metro.Controls
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// See the LICENSE file in the project root for more information.

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
Expand All @@ -15,7 +14,7 @@
using Windows.Win32.Graphics.Gdi;
using Windows.Win32.UI.WindowsAndMessaging;

namespace MahApps.Metro.Controls
namespace MahApps.Metro.Native
{
internal static class WinApiHelper
{
Expand Down Expand Up @@ -132,66 +131,5 @@ private static int CalcIntValue(double? value, double fallback)

return (int)fallback;
}

/// <summary> Gets the text associated with the given window handle. </summary>
/// <param name="window"> The window to act on. </param>
/// <returns> The window text. </returns>
internal static string? GetWindowText(this Window? window)
{
if (window is null == false
&& PresentationSource.FromVisual(window) is HwndSource source
&& source.IsDisposed == false
&& source.RootVisual is null == false
&& source.Handle != IntPtr.Zero)
{
int bufferSize = PInvoke.GetWindowTextLength(new HWND(source.Handle)) + 1;
unsafe
{
fixed (char* windowNameChars = new char[bufferSize])
{
PInvoke.GetWindowText(new HWND(source.Handle), windowNameChars, bufferSize);
return new string(windowNameChars);
}
}
}

return default;
}

/// <summary> Gets the text associated with the given window handle. </summary>
/// <param name="window"> The window to act on. </param>
/// <returns> The window text. </returns>
internal static Rect GetWindowBoundingRectangle(this Window? window)
{
var bounds = new Rect(0, 0, 0, 0);

if (window is null == false
&& PresentationSource.FromVisual(window) is HwndSource source
&& source.IsDisposed == false
&& source.RootVisual is null == false
&& source.Handle != IntPtr.Zero)
{
var rc = new RECT();

try
{
unsafe
{
PInvoke.GetWindowRect((HWND)source.Handle, &rc);
}
}
// Allow empty catch statements.
#pragma warning disable 56502
catch (Win32Exception)
{
}
// Disallow empty catch statements.
#pragma warning restore 56502

bounds = new Rect(rc.left, rc.top, rc.GetWidth(), rc.GetHeight());
}

return bounds;
}
}
}
2 changes: 0 additions & 2 deletions src/MahApps.Metro/NativeMethods.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ GetKeyNameText
GetMonitorInfo
GetWindowPlacement
GetWindowRect
GetWindowText
GetWindowTextLength
LoadLibrary
LoadString
MapVirtualKey
Expand Down

0 comments on commit c28bcce

Please sign in to comment.