Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PTRun]Bring back acrylic and proper fix to title bar accent showing #33458

Merged
merged 5 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ internal class LivePreview
/// <param name="hwnd">handle to the window to exclude</param>
public static void SetWindowExclusionFromLivePreview(IntPtr hwnd)
{
int renderPolicy = (int)DwmNCRenderingPolicies.Enabled;
uint renderPolicy = (uint)DwmNCRenderingPolicies.Enabled;

_ = NativeMethods.DwmSetWindowAttribute(
hwnd,
12,
ref renderPolicy,
sizeof(int));
sizeof(uint));
}

/// <summary>
Expand Down
9 changes: 2 additions & 7 deletions src/modules/launcher/PowerLauncher/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@

<Grid x:Name="RootGrid" MouseDown="OnMouseDown">
<!-- We set the background here because the Acrylic can be too translucent / background too bright on Light theme -->
<!--<Grid.Background>
<Grid.Background>
<SolidColorBrush Opacity="0.8" Color="{DynamicResource ApplicationBackgroundColor}" />
</Grid.Background>-->
</Grid.Background>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
Expand All @@ -45,11 +45,6 @@
Grid.Row="0"
Padding="12,4,12,3">
<local:LauncherControl x:Name="SearchBox" />
<Border.Background>
<!-- Setting the background of the search bar to fix https://github.com/microsoft/PowerToys/issues/30206 -->
<!-- The title bar accent would bleed if the option to "Show accent color on title bars and windows borders" is enabled on Windows -->
<SolidColorBrush Opacity="1" Color="{DynamicResource ApplicationBackgroundColor}" />
</Border.Background>
</Border>

<!-- Have to use a Grid instead of a StackPanel for scrolling to work? -->
Expand Down
2 changes: 1 addition & 1 deletion src/modules/launcher/PowerLauncher/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public MainWindow(PowerToysRunSettings settings, MainViewModel mainVM, Cancellat

if (OSVersionHelper.IsWindows11())
{
WindowBackdropType = Wpf.Ui.Controls.WindowBackdropType.Mica;
WindowBackdropType = Wpf.Ui.Controls.WindowBackdropType.Acrylic;
}
else
{
Expand Down
32 changes: 32 additions & 0 deletions src/modules/launcher/PowerLauncher/ViewModel/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Threading;
using Common.UI;
using interop;
Expand Down Expand Up @@ -1006,6 +1008,36 @@ public void ToggleWox()
if (MainWindowVisibility != Visibility.Visible)
{
MainWindowVisibility = Visibility.Visible;

// HACK: The following code in this if is a fix for https://github.com/microsoft/PowerToys/issues/30206 and https://github.com/microsoft/PowerToys/issues/33135
// WPF UI theme watcher removes the composition target background color, among other weird stuff.
// https://github.com/lepoco/wpfui/blob/303f0aefcd59a142bc681415dc4360a34a15f33d/src/Wpf.Ui/Controls/Window/WindowBackdrop.cs#L280
// So we set it back with https://github.com/lepoco/wpfui/blob/303f0aefcd59a142bc681415dc4360a34a15f33d/src/Wpf.Ui/Controls/Window/WindowBackdrop.cs#L191
var window = Application.Current.MainWindow;
Wpf.Ui.Controls.WindowBackdrop.RemoveBackground(window);

// Taken from WPFUI's fix for the title bar issue. We should be able to remove this fix when WPF UI 4 is integrated.
// https://github.com/lepoco/wpfui/pull/1122/files#diff-196b404f4db09632665ef546da6c8e57302b2f3e3d082eb4b5c295ae3482d94a
IntPtr windowHandle = new WindowInteropHelper(window).Handle;
if (windowHandle == IntPtr.Zero)
{
return;
}

HwndSource windowSource = HwndSource.FromHwnd(windowHandle);

// Remove background from client area
if (windowSource != null && windowSource.Handle != IntPtr.Zero && windowSource?.CompositionTarget != null)
{
// NOTE: https://learn.microsoft.com/en-us/windows/win32/api/dwmapi/ne-dwmapi-dwmwindowattribute
// Specifying DWMWA_COLOR_DEFAULT (value 0xFFFFFFFF) for the color will reset the window back to using the system's default behavior for the caption color.
uint titlebarPvAttribute = 0xFFFFFFFE;
_ = Wox.Plugin.Common.Win32.NativeMethods.DwmSetWindowAttribute(
windowSource.Handle,
(int)Wox.Plugin.Common.Win32.DwmWindowAttributes.CaptionColor,
ref titlebarPvAttribute,
Marshal.SizeOf(typeof(uint)));
}
}
else
{
Expand Down
46 changes: 23 additions & 23 deletions src/modules/launcher/Wox.Plugin/Common/Win32/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static class NativeMethods
public static extern int DwmpActivateLivePreview([MarshalAs(UnmanagedType.Bool)] bool fActivate, IntPtr hWndExclude, IntPtr hWndInsertBefore, LivePreviewTrigger lpt, IntPtr prcFinalRect);

[DllImport("dwmapi.dll", PreserveSig = false)]
public static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize);
public static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref uint attrValue, int attrSize);

[DllImport("dwmapi.dll", PreserveSig = false)]
public static extern int DwmGetWindowAttribute(IntPtr hwnd, int dwAttribute, out int pvAttribute, int cbAttribute);
Expand Down Expand Up @@ -493,29 +493,29 @@ public enum DwmNCRenderingPolicies
public enum DwmWindowAttributes
{
NCRenderingEnabled = 1,
NCRenderingPolicy,
TransitionsForceDisabled,
AllowNCPaint,
CaptionButtonBounds,
NonClientRtlLayout,
ForceIconicRepresentation,
Flip3DPolicy,
ExtendedFrameBounds,
HasIconicBitmap,
DisallowPeek,
ExcludedFromPeek,
Cloak,
Cloaked,
FreezeRepresentation,
PassiveUpdateMode,
UseHostbackdropbrush,
UseImmersiveDarkMode,
WindowCornerPreference,
BorderColor,
CaptionColor,
TextColor,
NCRenderingPolicy = 2,
TransitionsForceDisabled = 3,
AllowNCPaint = 4,
CaptionButtonBounds = 5,
NonClientRtlLayout = 6,
ForceIconicRepresentation = 7,
Flip3DPolicy = 8,
ExtendedFrameBounds = 9,
HasIconicBitmap = 10,
DisallowPeek = 11,
ExcludedFromPeek = 12,
Cloak = 13,
Cloaked = 14,
Comment on lines +507 to +508
Copy link
Collaborator

@htcfreek htcfreek Jun 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we use a different enum in ww plugin? otherwise please verify that no dummy windows for uwp apps shown in results.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ww plugin just puts 12 directly 😆

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the cloak ones, the number is the same :) It starts with 1 and goes up is the default behavior, so the number will be kept.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was trying to use CaptionColor and that's when I saw this was wrong.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ww plugin just puts 12 directly

No. See Components\Window.cs:L306

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know :) Cloak and Cloaked get the same value due to the default +1 logic in enums. This just makes it explicit since not every value follows that logic

Copy link
Collaborator

@htcfreek htcfreek Jun 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My mistake. Thought the counter changes +1 for the first enum item. But the first two have a very similar name. 🙃

FreezeRepresentation = 15,
PassiveUpdateMode = 16,
UseHostbackdropbrush = 17,
UseImmersiveDarkMode = 20,
WindowCornerPreference = 33,
BorderColor = 34,
CaptionColor = 35,
TextColor = 36,
VisibleFrameBorderThickness,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

number is missing.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right! Thank you! Fixed!

Last,
Last = 37,
}

/// <summary>
Expand Down
Loading