Skip to content

Commit

Permalink
Update ModernSystemMenu.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
ghost1372 committed Feb 25, 2025
1 parent 17f010c commit bafabb8
Showing 1 changed file with 32 additions and 19 deletions.
51 changes: 32 additions & 19 deletions dev/DevWinUI/Common/ModernSystemMenu/ModernSystemMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public partial class ModernSystemMenu : INotifyPropertyChanged
private MenuFlyout titleBarMenuFlyout;
private readonly ContentCoordinateConverter contentCoordinateConverter;
private readonly OverlappedPresenter overlappedPresenter;
private bool isModernSystemMenuEnabled = true;

private bool _isWindowMaximized;

public bool IsWindowMaximized
{
get { return _isWindowMaximized; }
Expand Down Expand Up @@ -45,6 +45,7 @@ public ModernSystemMenu(Window window, MenuFlyout menuFlyout) : this(window)
public ModernSystemMenu(Window window)
{
this.window = window;
ReEnableModernSystemMenu();

RestoreCommand = DelegateCommand.Create(Restore, CanExecuteRestore);
MoveCommand = DelegateCommand.Create(Move, CanExecuteMove);
Expand All @@ -71,6 +72,15 @@ public ModernSystemMenu(Window window)
RegisterWindowMonitor();
}

public void DisableModernSystemMenu()
{
isModernSystemMenuEnabled = false;
}
public void ReEnableModernSystemMenu()
{
isModernSystemMenuEnabled = true;
}

private void CreateMenuFlyout()
{
titleBarMenuFlyout = new MenuFlyout()
Expand Down Expand Up @@ -199,7 +209,7 @@ private void RegisterWindowMonitor()

private void OnWindowMessageReceived(object sender, WindowMessageEventArgs e)
{
if (e.MessageType == (uint)NativeValues.WindowMessage.WM_SYSCOMMAND)
if (e.MessageType == (uint)NativeValues.WindowMessage.WM_SYSCOMMAND && isModernSystemMenuEnabled)
{
var sysCommand = e.Message.WParam.ToUInt32() & 0xFFF0;

Expand Down Expand Up @@ -234,30 +244,33 @@ private void OnWindowMessageReceived(object sender, WindowMessageEventArgs e)

private void OnWindowMessageReceivedNonClient(object sender, WindowMessageEventArgs e)
{
if (e.MessageType == (uint)NativeValues.WindowMessage.WM_NCLBUTTONDOWN)
if (isModernSystemMenuEnabled)
{
if (titleBarMenuFlyout.IsOpen)
if (e.MessageType == (uint)NativeValues.WindowMessage.WM_NCLBUTTONDOWN)
{
HideMenuFlyout();
if (titleBarMenuFlyout.IsOpen)
{
HideMenuFlyout();
}
}
}
else if (e.MessageType == (uint)NativeValues.WindowMessage.WM_NCRBUTTONUP)
{
if (e.Message.WParam.ToUInt32() is 2 && window.Content is not null && window.Content.XamlRoot is not null)
else if (e.MessageType == (uint)NativeValues.WindowMessage.WM_NCRBUTTONUP)
{
PointInt32 screenPoint = new(e.Message.LParam.ToInt32() & 0xFFFF, e.Message.LParam.ToInt32() >> 16);
Point localPoint = contentCoordinateConverter.ConvertScreenToLocal(screenPoint);

FlyoutShowOptions options = new()
if (e.Message.WParam.ToUInt32() is 2 && window.Content is not null && window.Content.XamlRoot is not null)
{
ShowMode = FlyoutShowMode.Standard,
Position = OSVersionHelper.IsWindows11_22000_OrGreater ? new Point(localPoint.X / window.Content.XamlRoot.RasterizationScale, localPoint.Y / window.Content.XamlRoot.RasterizationScale) : new Point(localPoint.X, localPoint.Y)
};
PointInt32 screenPoint = new(e.Message.LParam.ToInt32() & 0xFFFF, e.Message.LParam.ToInt32() >> 16);
Point localPoint = contentCoordinateConverter.ConvertScreenToLocal(screenPoint);

ShowMenuFlyout(options);
FlyoutShowOptions options = new()
{
ShowMode = FlyoutShowMode.Standard,
Position = OSVersionHelper.IsWindows11_22000_OrGreater ? new Point(localPoint.X / window.Content.XamlRoot.RasterizationScale, localPoint.Y / window.Content.XamlRoot.RasterizationScale) : new Point(localPoint.X, localPoint.Y)
};

ShowMenuFlyout(options);
}
e.Result = 0;
e.Handled = true;
}
e.Result = 0;
e.Handled = true;
}
}

Expand Down

0 comments on commit bafabb8

Please sign in to comment.