CmdPal: fix alt+f4 handling (#49708)

As I threw in
https://github.com/microsoft/PowerToys/issues/49572#issuecomment-5195807441:

Our alt+f4 handling is wack. We shouldn't close the dock when you press
alt+f4 on it, just like you can't close the taskbar with alt+f4.

But also some folks want alt+f4 to quit cmdpal, and some folks don't. So
there's a setting for what happens when you alt+f4 cmdpal.

Closes #38333
Closes #40277
Closes #49572
This commit is contained in:
Mike Griese
2026-08-05 16:06:50 -05:00
committed by GitHub
parent 2c1a14b9b8
commit 30d070be85
7 changed files with 41 additions and 3 deletions

View File

@@ -46,6 +46,8 @@ public record SettingsModel
public bool AllowExternalReload { get; init; }
public bool AllowAltF4 { get; init; }
public bool CompactMode { get; set; }
// When compact mode is on and the palette is centered on launch, this is the relative

View File

@@ -69,6 +69,15 @@ public partial class SettingsViewModel : INotifyPropertyChanged,
}
}
public bool AllowAltF4
{
get => _settingsService.Settings.AllowAltF4;
set
{
_settingsService.UpdateSettings(s => s with { AllowAltF4 = value });
}
}
public bool ShowAppDetails
{
get => _settingsService.Settings.ShowAppDetails;

View File

@@ -1351,13 +1351,12 @@ public sealed partial class DockWindow : WindowEx,
HandleDeactivationForAutoHide();
}
// Intercept WM_SYSCOMMAND to prevent minimize and maximize
// Prevent close, minimize, and maximize system commands.
else if (msg == PInvoke.WM_SYSCOMMAND)
{
var command = (int)(wParam.Value & 0xFFF0);
if (command == PInvoke.SC_MINIMIZE || command == PInvoke.SC_MAXIMIZE)
if (command == PInvoke.SC_CLOSE || command == PInvoke.SC_MINIMIZE || command == PInvoke.SC_MAXIMIZE)
{
// Block minimize and maximize commands
return new LRESULT(0);
}
}

View File

@@ -1700,6 +1700,23 @@ public sealed partial class MainWindow : WindowEx,
case PInvoke.WM_NCACTIVATE when _hwndFrameVisible != true:
return PInvoke.CallWindowProc(_originalWndProc, hwnd, uMsg, wParam, new LPARAM(-1));
case PInvoke.WM_SYSCOMMAND:
{
var command = (int)(wParam.Value & 0xFFF0);
if (command == PInvoke.SC_CLOSE)
{
var settings = App.Current.Services.GetRequiredService<ISettingsService>().Settings;
if (settings.AllowAltF4)
{
WeakReferenceMessenger.Default.Send<QuitMessage>();
}
return (LRESULT)IntPtr.Zero;
}
break;
}
case PInvoke.WM_HOTKEY:
{
var hotkeyIndex = (int)wParam.Value;

View File

@@ -115,6 +115,7 @@ WM_GETMINMAXINFO
MINMAXINFO
SetWinEventHook
WINDOW_STYLE
SC_CLOSE
SC_MINIMIZE
SC_MAXIMIZE
SET_WINDOW_POS_FLAGS

View File

@@ -172,6 +172,10 @@
<ToggleSwitch AutomationProperties.AutomationId="CmdPal_GeneralPage_ShowSystemTrayIcon" IsOn="{x:Bind viewModel.ShowSystemTrayIcon, Mode=TwoWay}" />
</controls:SettingsCard>
<controls:SettingsCard x:Uid="Settings_GeneralPage_AllowAltF4_SettingsCard" HeaderIcon="{ui:FontIcon Glyph=&#xE7E8;}">
<ToggleSwitch AutomationProperties.AutomationId="CmdPal_GeneralPage_AllowAltF4" IsOn="{x:Bind viewModel.AllowAltF4, Mode=TwoWay}" />
</controls:SettingsCard>
<!-- 'For Developers' section -->
<TextBlock x:Uid="ForDevelopersSettingsHeader" Style="{StaticResource SettingsSectionHeaderTextBlockStyle}" />

View File

@@ -495,6 +495,12 @@ Right-click to remove the key combination, thereby deactivating the shortcut.</v
<data name="Settings_GeneralPage_ShowSystemTrayIcon_SettingsCard.Description" xml:space="preserve">
<value>Choose if Command Palette is visible in the system tray</value>
</data>
<data name="Settings_GeneralPage_AllowAltF4_SettingsCard.Header" xml:space="preserve">
<value>Quit Command Palette with Alt+F4</value>
</data>
<data name="Settings_GeneralPage_AllowAltF4_SettingsCard.Description" xml:space="preserve">
<value>If this setting is on, pressing Alt+F4 will quit Command Palette</value>
</data>
<data name="Settings_GeneralPage_DisableAnimations_SettingsCard.Header" xml:space="preserve">
<value>Disable animations</value>
</data>