mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-05 10:46:33 +02:00
CmdPal: Make Command Palette's main and toast windows tool windows (#41835)
## Summary of the Pull Request Replaces the `SetVisibilityInSwitchers` method with `WS_EX_TOOLWINDOW` style, removing reliance on the fragile `IsShownInSwitchers` property. Explicitly overrides window corner preference so tool windows keep the same rounded corners as regular app windows (tool windows otherwise default to a smaller corner radius, which would change the look). Benefits: - Avoids use of the unreliable `IsShownInSwitchers` property, which has multiple known failure cases. - Allows window managers to correctly treat Command Palette as a popup window rather than a normal app window, preventing unwanted tiling, grid placement, or similar behaviors. This better matches the intended design of CmdPal as an overlay UI. <!-- Please review the items on the PR checklist before submitting--> ## PR Checklist - [x] Closes: #41772 - [x] Closes: #41593 - [ ] **Communication:** I've discussed this with core contributors already. If the work hasn't been agreed, this work might be rejected - [ ] **Tests:** Added/updated and all pass - [ ] **Localization:** All end-user-facing strings can be localized - [ ] **Dev docs:** Added/updated - [ ] **New binaries:** Added on the required places - [ ] [JSON for signing](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ESRPSigning_core.json) for new binaries - [ ] [WXS for installer](https://github.com/microsoft/PowerToys/blob/main/installer/PowerToysSetup/Product.wxs) for new binaries and localization folder - [ ] [YML for CI pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ci/templates/build-powertoys-steps.yml) for new test projects - [ ] [YML for signed pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/release.yml) - [ ] **Documentation updated:** If checked, please file a pull request on [our docs repo](https://github.com/MicrosoftDocs/windows-uwp/tree/docs/hub/powertoys) and link it here: #xxx <!-- Provide a more detailed description of the PR, other things fixed, or any additional comments/features here --> ## Detailed Description of the Pull Request / Additional comments <!-- Describe how you validated the behavior. Add automated tests wherever possible, but list manual validation steps taken as well --> ## Validation Steps Performed
This commit is contained in:
@@ -5,10 +5,14 @@
|
||||
using Microsoft.UI;
|
||||
using Microsoft.UI.Windowing;
|
||||
using Microsoft.UI.Xaml;
|
||||
using Windows.Win32;
|
||||
using Windows.Win32.Foundation;
|
||||
using Windows.Win32.Graphics.Dwm;
|
||||
using Windows.Win32.UI.WindowsAndMessaging;
|
||||
|
||||
namespace Microsoft.CmdPal.UI.Helpers;
|
||||
|
||||
public static class WindowExtensions
|
||||
internal static class WindowExtensions
|
||||
{
|
||||
public static void SetIcon(this Window window)
|
||||
{
|
||||
@@ -18,18 +22,59 @@ public static class WindowExtensions
|
||||
appWindow.SetIcon(@"Assets\icon.ico");
|
||||
}
|
||||
|
||||
public static void SetVisibilityInSwitchers(this Window window, bool showInSwitchers)
|
||||
private static HWND GetWindowHwnd(this Window window)
|
||||
{
|
||||
try
|
||||
return window is null
|
||||
? throw new ArgumentNullException(nameof(window))
|
||||
: new HWND(WinRT.Interop.WindowNative.GetWindowHandle(window));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Toggles the specified extended window style on or off for the supplied <see cref="Window"/>.
|
||||
/// </summary>
|
||||
/// <param name="window">The <see cref="Window"/> whose extended window styles will be modified. Cannot be null.</param>
|
||||
/// <param name="style">The <see cref="WINDOW_EX_STYLE"/> flag(s) to set or clear.</param>
|
||||
/// <param name="isStyleSet">When true, the specified <paramref name="style"/> bit(s) will be set (added). When false, the bit(s) will be cleared (removed).</param>
|
||||
/// <returns>True if the call to SetWindowLong succeeded and the style was applied; otherwise false.</returns>
|
||||
/// <exception cref="ArgumentNullException">Thrown if <paramref name="window"/> is null.</exception>
|
||||
internal static bool ToggleExtendedWindowStyle(this Window window, WINDOW_EX_STYLE style, bool isStyleSet)
|
||||
{
|
||||
var hWnd = GetWindowHwnd(window);
|
||||
var currentStyle = PInvoke.GetWindowLong(hWnd, WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE);
|
||||
|
||||
if (isStyleSet)
|
||||
{
|
||||
// IsShownInSwitchers needs to change the value to apply the effect, but its state might be out-of-sync with
|
||||
// the actual state of the switchers, so we need to toggle it.
|
||||
window.AppWindow.IsShownInSwitchers = !showInSwitchers;
|
||||
window.AppWindow.IsShownInSwitchers = showInSwitchers;
|
||||
currentStyle |= (int)style;
|
||||
}
|
||||
catch (NotImplementedException)
|
||||
else
|
||||
{
|
||||
// Setting IsShownInSwitchers failed. This can happen if the Explorer is not running.
|
||||
currentStyle &= ~(int)style;
|
||||
}
|
||||
|
||||
return PInvoke.SetWindowLong(hWnd, WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE, currentStyle) != 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the window corner preference
|
||||
/// </summary>
|
||||
/// <param name="window">The window</param>
|
||||
/// <param name="cornerPreference">The desired corner preference</param>
|
||||
/// <returns>True if the operation succeeded</returns>
|
||||
public static bool SetCornerPreference(this Window window, DWM_WINDOW_CORNER_PREFERENCE cornerPreference)
|
||||
{
|
||||
return window.GetWindowHwnd().SetDwmWindowAttribute(DWMWINDOWATTRIBUTE.DWMWA_WINDOW_CORNER_PREFERENCE, cornerPreference);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unified wrapper for DwmSetWindowAttribute calls with enum values
|
||||
/// </summary>
|
||||
private static bool SetDwmWindowAttribute<T>(this HWND hwnd, DWMWINDOWATTRIBUTE attribute, T value)
|
||||
where T : unmanaged, Enum
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
var result = PInvoke.DwmSetWindowAttribute(hwnd, attribute, &value, (uint)sizeof(T));
|
||||
return result.Succeeded;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user