Skip to content

Commit e4ade45

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

File tree

2 files changed

+50
-57
lines changed

2 files changed

+50
-57
lines changed

Flow.Launcher.Core/Resource/Theme.cs

Lines changed: 11 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
using Flow.Launcher.Infrastructure;
1313
using Flow.Launcher.Infrastructure.Logger;
1414
using Flow.Launcher.Infrastructure.UserSettings;
15-
using static Flow.Launcher.Core.Resource.Theme.ParameterTypes;
1615
using System.Runtime.InteropServices;
1716
using System.Windows.Interop;
1817
using Microsoft.Win32;
@@ -86,37 +85,6 @@ public static void SetWindowCornerPreference(System.Windows.Window window, DWM_W
8685
IntPtr hWnd = new WindowInteropHelper(window).Handle;
8786
DwmSetWindowAttribute(hWnd, DWMWA_WINDOW_CORNER_PREFERENCE, ref preference, sizeof(int));
8887
}
89-
public class ParameterTypes
90-
{
91-
92-
[Flags]
93-
public enum DWMWINDOWATTRIBUTE
94-
{
95-
DWMWA_USE_IMMERSIVE_DARK_MODE = 20,
96-
DWMWA_SYSTEMBACKDROP_TYPE = 38,
97-
DWMWA_TRANSITIONS_FORCEDISABLED = 3,
98-
DWMWA_BORDER_COLOR
99-
}
100-
101-
[StructLayout(LayoutKind.Sequential)]
102-
public struct MARGINS
103-
{
104-
public int cxLeftWidth; // width of left border that retains its size
105-
public int cxRightWidth; // width of right border that retains its size
106-
public int cyTopHeight; // height of top border that retains its size
107-
public int cyBottomHeight; // height of bottom border that retains its size
108-
};
109-
}
110-
111-
public static class Methods
112-
{
113-
114-
[DllImport("dwmapi.dll")]
115-
static extern int DwmSetWindowAttribute(IntPtr hwnd, ParameterTypes.DWMWINDOWATTRIBUTE dwAttribute, ref int pvAttribute, int cbAttribute);
116-
117-
public static int SetWindowAttribute(IntPtr hwnd, ParameterTypes.DWMWINDOWATTRIBUTE attribute, int parameter)
118-
=> DwmSetWindowAttribute(hwnd, attribute, ref parameter, Marshal.SizeOf<int>());
119-
}
12088

12189
private Window GetMainWindow()
12290
{
@@ -221,7 +189,7 @@ public void SetBlurForWindow()
221189
if (windowBorderStyle == null)
222190
return;
223191

224-
System.Windows.Window mainWindow = GetMainWindow();
192+
Window mainWindow = GetMainWindow();
225193
if (mainWindow == null)
226194
return;
227195

@@ -232,42 +200,28 @@ public void SetBlurForWindow()
232200
_settings.BackdropType = BackdropTypes.None;
233201
}
234202

235-
// Check the configured BackdropType
236-
int backdropValue = _settings.BackdropType switch
237-
{
238-
BackdropTypes.Acrylic => 3, // Acrylic
239-
BackdropTypes.Mica => 2, // Mica
240-
BackdropTypes.MicaAlt => 4, // MicaAlt
241-
_ => 0 // None
242-
};
243-
244203
if (BlurEnabled && hasBlur && Win32Helper.IsBackdropSupported())
245204
{
246-
// If the BackdropType is Mica or MicaAlt, set the windowborderstyle's background to transparent
205+
// If the BackdropType is Mica or MicaAlt, set the windowborderstyle's background to transparent
247206
if (_settings.BackdropType == BackdropTypes.Mica || _settings.BackdropType == BackdropTypes.MicaAlt)
248207
{
249208
windowBorderStyle.Setters.Remove(windowBorderStyle.Setters.OfType<Setter>().FirstOrDefault(x => x.Property.Name == "Background"));
250209
windowBorderStyle.Setters.Add(new Setter(Border.BackgroundProperty, new SolidColorBrush(Color.FromArgb(1, 0, 0, 0))));
251-
Methods.SetWindowAttribute(new WindowInteropHelper(mainWindow).Handle, DWMWINDOWATTRIBUTE.DWMWA_SYSTEMBACKDROP_TYPE, backdropValue);
252-
ColorizeWindow(GetSystemBG());
253210
}
254211
else if (_settings.BackdropType == BackdropTypes.Acrylic)
255212
{
256213
windowBorderStyle.Setters.Remove(windowBorderStyle.Setters.OfType<Setter>().FirstOrDefault(x => x.Property.Name == "Background"));
257214
windowBorderStyle.Setters.Add(new Setter(Border.BackgroundProperty, new SolidColorBrush(Colors.Transparent)));
258-
Methods.SetWindowAttribute(new WindowInteropHelper(mainWindow).Handle, DWMWINDOWATTRIBUTE.DWMWA_SYSTEMBACKDROP_TYPE, backdropValue);
259-
ColorizeWindow(GetSystemBG());
260-
}
261-
else
262-
{
263-
Methods.SetWindowAttribute(new WindowInteropHelper(mainWindow).Handle, DWMWINDOWATTRIBUTE.DWMWA_SYSTEMBACKDROP_TYPE, backdropValue);
264-
ColorizeWindow(GetSystemBG());
265215
}
216+
217+
// Apply the blur effect
218+
Win32Helper.DWMSetBackdropForWindow(mainWindow, _settings.BackdropType);
219+
ColorizeWindow(GetSystemBG());
266220
}
267221
else
268222
{
269-
// Apply default style when Blur is disabled
270-
Methods.SetWindowAttribute(new WindowInteropHelper(mainWindow).Handle, DWMWINDOWATTRIBUTE.DWMWA_SYSTEMBACKDROP_TYPE, 0);
223+
// Apply default style when Blur is disabled
224+
Win32Helper.DWMSetBackdropForWindow(mainWindow, BackdropTypes.None);
271225
ColorizeWindow(GetSystemBG());
272226
}
273227

@@ -401,9 +355,9 @@ public void ColorizeWindow(string Mode)
401355
useDarkMode = isSystemDark; // Auto (based on system setting)
402356
}
403357

404-
// Apply DWM Dark Mode
405-
Methods.SetWindowAttribute(new WindowInteropHelper(mainWindow).Handle, DWMWINDOWATTRIBUTE.DWMWA_USE_IMMERSIVE_DARK_MODE, useDarkMode ? 1 : 0);
406-
358+
// Apply DWM Dark Mode
359+
Win32Helper.DWMSetDarkModeForWindow(mainWindow, useDarkMode);
360+
407361
Color LightBG;
408362
Color DarkBG;
409363

Flow.Launcher.Infrastructure/Win32Helper.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,50 @@
44
using System.Windows;
55
using Windows.Win32;
66
using Windows.Win32.Graphics.Dwm;
7+
using Flow.Launcher.Infrastructure.UserSettings;
78

89
namespace Flow.Launcher.Infrastructure
910
{
1011
public static class Win32Helper
1112
{
13+
#region Blur Handling
14+
15+
public static unsafe bool DWMSetBackdropForWindow(Window window, BackdropTypes backdrop)
16+
{
17+
var windowHelper = new WindowInteropHelper(window);
18+
windowHelper.EnsureHandle();
19+
20+
var backdropType = backdrop switch
21+
{
22+
BackdropTypes.Acrylic => DWM_SYSTEMBACKDROP_TYPE.DWMSBT_TRANSIENTWINDOW,
23+
BackdropTypes.Mica => DWM_SYSTEMBACKDROP_TYPE.DWMSBT_MAINWINDOW,
24+
BackdropTypes.MicaAlt => DWM_SYSTEMBACKDROP_TYPE.DWMSBT_TABBEDWINDOW,
25+
_ => DWM_SYSTEMBACKDROP_TYPE.DWMSBT_AUTO
26+
};
27+
28+
return PInvoke.DwmSetWindowAttribute(
29+
new(windowHelper.Handle),
30+
DWMWINDOWATTRIBUTE.DWMWA_SYSTEMBACKDROP_TYPE,
31+
&backdropType,
32+
(uint)Marshal.SizeOf<int>()).Succeeded;
33+
}
34+
35+
public static unsafe bool DWMSetDarkModeForWindow(Window window, bool useDarkMode)
36+
{
37+
var windowHelper = new WindowInteropHelper(window);
38+
windowHelper.EnsureHandle();
39+
40+
var darkMode = useDarkMode ? 1 : 0;
41+
42+
return PInvoke.DwmSetWindowAttribute(
43+
new(windowHelper.Handle),
44+
DWMWINDOWATTRIBUTE.DWMWA_USE_IMMERSIVE_DARK_MODE,
45+
&darkMode,
46+
(uint)Marshal.SizeOf<int>()).Succeeded;
47+
}
48+
49+
#endregion
50+
1251
#region Backdrop
1352

1453
public static bool IsBackdropSupported()

0 commit comments

Comments
 (0)