Skip to content

Commit db37ab7

Browse files
committed
Use PInvoke.DwmSetWindowAttribute instead of DllImport
1 parent e4ade45 commit db37ab7

File tree

3 files changed

+35
-50
lines changed

3 files changed

+35
-50
lines changed

Flow.Launcher.Core/Resource/Theme.cs

Lines changed: 6 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
using Flow.Launcher.Infrastructure;
1313
using Flow.Launcher.Infrastructure.Logger;
1414
using Flow.Launcher.Infrastructure.UserSettings;
15-
using System.Runtime.InteropServices;
16-
using System.Windows.Interop;
1715
using Microsoft.Win32;
1816
using Flow.Launcher.Plugin;
1917
using TextBox = System.Windows.Controls.TextBox;
@@ -70,43 +68,11 @@ public Theme(IPublicAPI publicAPI, Settings settings)
7068
}
7169

7270
#region Blur Handling
73-
private const int DWMWA_WINDOW_CORNER_PREFERENCE = 33;
74-
public enum DWM_WINDOW_CORNER_PREFERENCE
75-
{
76-
Default = 0,
77-
DoNotRound = 1,
78-
Round = 2,
79-
RoundSmall = 3
80-
}
81-
[DllImport("dwmapi.dll")]
82-
private static extern int DwmSetWindowAttribute(IntPtr hwnd, int dwAttribute, ref DWM_WINDOW_CORNER_PREFERENCE pvAttribute, int cbAttribute);
83-
public static void SetWindowCornerPreference(System.Windows.Window window, DWM_WINDOW_CORNER_PREFERENCE preference)
84-
{
85-
IntPtr hWnd = new WindowInteropHelper(window).Handle;
86-
DwmSetWindowAttribute(hWnd, DWMWA_WINDOW_CORNER_PREFERENCE, ref preference, sizeof(int));
87-
}
88-
89-
private Window GetMainWindow()
90-
{
91-
return Application.Current.Dispatcher.Invoke(() => Application.Current.MainWindow);
92-
}
9371

9472
public void RefreshFrame()
9573
{
9674
Application.Current.Dispatcher.Invoke(() =>
9775
{
98-
Window mainWindow = Application.Current.MainWindow;
99-
if (mainWindow == null)
100-
return;
101-
102-
IntPtr mainWindowPtr = new WindowInteropHelper(mainWindow).Handle;
103-
if (mainWindowPtr == IntPtr.Zero)
104-
return;
105-
106-
HwndSource mainWindowSrc = HwndSource.FromHwnd(mainWindowPtr);
107-
if (mainWindowSrc == null)
108-
return;
109-
11076
// Remove OS minimizing/maximizing animation
11177
// Methods.SetWindowAttribute(new WindowInteropHelper(mainWindow).Handle, DWMWINDOWATTRIBUTE.DWMWA_TRANSITIONS_FORCEDISABLED, 3);
11278

@@ -124,7 +90,7 @@ public void RefreshFrame()
12490
}, DispatcherPriority.Normal);
12591
}
12692

127-
public void AutoDropShadow()
93+
private void AutoDropShadow()
12894
{
12995
SetWindowCornerPreference("Default");
13096
RemoveDropShadowEffectFromCurrentTheme();
@@ -153,24 +119,15 @@ public void AutoDropShadow()
153119
}
154120
}
155121

156-
public void SetWindowCornerPreference(string cornerType)
122+
private void SetWindowCornerPreference(string cornerType)
157123
{
158124
Application.Current.Dispatcher.Invoke(() =>
159125
{
160-
System.Windows.Window mainWindow = GetMainWindow();
126+
Window mainWindow = Application.Current.MainWindow;
161127
if (mainWindow == null)
162128
return;
163129

164-
DWM_WINDOW_CORNER_PREFERENCE preference = cornerType switch
165-
{
166-
"DoNotRound" => DWM_WINDOW_CORNER_PREFERENCE.DoNotRound,
167-
"Round" => DWM_WINDOW_CORNER_PREFERENCE.Round,
168-
"RoundSmall" => DWM_WINDOW_CORNER_PREFERENCE.RoundSmall,
169-
"Default" => DWM_WINDOW_CORNER_PREFERENCE.Default,
170-
_ => DWM_WINDOW_CORNER_PREFERENCE.Default,
171-
};
172-
173-
SetWindowCornerPreference(mainWindow, preference);
130+
Win32Helper.DWMSetCornerPreferenceForWindow(mainWindow, cornerType);
174131
}, DispatcherPriority.Normal);
175132
}
176133

@@ -189,7 +146,7 @@ public void SetBlurForWindow()
189146
if (windowBorderStyle == null)
190147
return;
191148

192-
Window mainWindow = GetMainWindow();
149+
Window mainWindow = Application.Current.MainWindow;
193150
if (mainWindow == null)
194151
return;
195152

@@ -309,7 +266,7 @@ private void ApplyPreviewBackground(Color? bgColor = null)
309266
}, DispatcherPriority.Render);
310267
}
311268

312-
public void ColorizeWindow(string Mode)
269+
private void ColorizeWindow(string mode)
313270
{
314271
Application.Current.Dispatcher.Invoke(() =>
315272
{

Flow.Launcher.Infrastructure/NativeMethods.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ WM_SYSKEYUP
1919
EnumWindows
2020

2121
DwmSetWindowAttribute
22-
DWM_SYSTEMBACKDROP_TYPE
22+
DWM_SYSTEMBACKDROP_TYPE
23+
DWM_WINDOW_CORNER_PREFERENCE

Flow.Launcher.Infrastructure/Win32Helper.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,33 @@ public static unsafe bool DWMSetDarkModeForWindow(Window window, bool useDarkMod
4646
(uint)Marshal.SizeOf<int>()).Succeeded;
4747
}
4848

49+
/// <summary>
50+
///
51+
/// </summary>
52+
/// <param name="window"></param>
53+
/// <param name="cornerType">DoNotRound, Round, RoundSmall, Default</param>
54+
/// <returns></returns>
55+
public static unsafe bool DWMSetCornerPreferenceForWindow(Window window, string cornerType)
56+
{
57+
var windowHelper = new WindowInteropHelper(window);
58+
windowHelper.EnsureHandle();
59+
60+
var preference = cornerType switch
61+
{
62+
"DoNotRound" => DWM_WINDOW_CORNER_PREFERENCE.DWMWCP_DONOTROUND,
63+
"Round" => DWM_WINDOW_CORNER_PREFERENCE.DWMWCP_ROUND,
64+
"RoundSmall" => DWM_WINDOW_CORNER_PREFERENCE.DWMWCP_ROUNDSMALL,
65+
"Default" => DWM_WINDOW_CORNER_PREFERENCE.DWMWCP_DEFAULT,
66+
_ => throw new InvalidOperationException("Invalid corner type")
67+
};
68+
69+
return PInvoke.DwmSetWindowAttribute(
70+
new(windowHelper.Handle),
71+
DWMWINDOWATTRIBUTE.DWMWA_WINDOW_CORNER_PREFERENCE,
72+
&preference,
73+
(uint)Marshal.SizeOf<int>()).Succeeded;
74+
}
75+
4976
#endregion
5077

5178
#region Backdrop

0 commit comments

Comments
 (0)