Skip to content

Commit 98ec2ce

Browse files
committed
Add Acrylic and Tabbed backdrop
1 parent 75bd49a commit 98ec2ce

3 files changed

Lines changed: 149 additions & 24 deletions

File tree

QuickLook.Common/Helpers/WindowHelper.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public static void EnableBlur(Window window)
168168
Marshal.FreeHGlobal(accentPtr);
169169
}
170170

171-
public static void DisableBlur(Window window)
171+
public static void DisableDwmBlur(Window window)
172172
{
173173
var accent = new AccentPolicy();
174174
var accentStructSize = Marshal.SizeOf(accent);
@@ -211,6 +211,11 @@ private static void EnableDwmBlur(Window window, bool isDarkTheme, uint dwAttrib
211211

212212
var hwnd = new WindowInteropHelper(window).Handle;
213213

214+
if (!window.AllowsTransparency && HwndSource.FromHwnd(hwnd) is HwndSource hwndSource)
215+
{
216+
hwndSource.CompositionTarget.BackgroundColor = Colors.Transparent;
217+
}
218+
214219
var isDarkThemeInt = isDarkTheme ? 1 : 0;
215220
Dwmapi.DwmSetWindowAttribute(hwnd, (uint)Dwmapi.WindowAttribute.UseImmersiveDarkMode, ref isDarkThemeInt, Marshal.SizeOf(typeof(bool)));
216221

@@ -228,7 +233,17 @@ public static void EnableMicaBlur(Window window, bool isDarkTheme)
228233

229234
public static void EnableBackdropMicaBlur(Window window, bool isDarkTheme)
230235
{
231-
EnableDwmBlur(window, isDarkTheme, (uint)Dwmapi.WindowAttribute.SystembackdropType, (int)Dwmapi.SystembackdropType.MainWindow);
236+
EnableDwmBlur(window, isDarkTheme, (uint)Dwmapi.WindowAttribute.SystembackdropType, (int)Dwmapi.SystembackdropType.Mica);
237+
}
238+
239+
public static void EnableBackdropAcrylicBlur(Window window, bool isDarkTheme)
240+
{
241+
EnableDwmBlur(window, isDarkTheme, (uint)Dwmapi.WindowAttribute.SystembackdropType, (int)Dwmapi.SystembackdropType.Acrylic);
242+
}
243+
244+
public static void EnableBackdropTabbedBlur(Window window, bool isDarkTheme)
245+
{
246+
EnableDwmBlur(window, isDarkTheme, (uint)Dwmapi.WindowAttribute.SystembackdropType, (int)Dwmapi.SystembackdropType.Tabbed);
232247
}
233248

234249
[StructLayout(LayoutKind.Sequential)]

QuickLook.Common/NativeMethods/Dwmapi.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,12 @@ public enum SystembackdropType
4141
{
4242
Auto = 0,
4343
None = 1,
44-
MainWindow = 2,
45-
TransientWindow = 3,
46-
TabbedWindow = 4,
44+
Mica = 2,
45+
Acrylic = 3, // Automatically selects the best Acrylic effect available on the system (Acrylic11 > Acrylic10)
46+
Tabbed = 4,
47+
48+
Acrylic10, // Windows 10 style, supported on Windows 10 and 11
49+
Acrylic11, // Windows 11 style, supported on Windows 11 22523+ (Insider) and 22621+ (Stable)
4750
}
4851

4952
[DllImport("DwmApi.dll")]

QuickLook/ViewerWindow.xaml.cs

Lines changed: 126 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
using System.Windows.Media.Animation;
2828
using System.Windows.Shell;
2929
using Wpf.Ui.Violeta.Controls;
30+
using static QuickLook.Common.NativeMethods.Dwmapi;
3031
using Brush = System.Windows.Media.Brush;
3132
using FontFamily = System.Windows.Media.FontFamily;
3233
using Size = System.Windows.Size;
@@ -157,6 +158,12 @@ internal ViewerWindow()
157158
base.Close();
158159
}
159160

161+
protected override void OnSourceInitialized(EventArgs e)
162+
{
163+
base.OnSourceInitialized(e);
164+
ApplyWindowBackgroundEffects();
165+
}
166+
160167
public override void OnApplyTemplate()
161168
{
162169
base.OnApplyTemplate();
@@ -171,31 +178,14 @@ private void ApplyWindowBackgroundEffects()
171178
var useTransparency = SettingHelper.Get("UseTransparency", true)
172179
&& SystemParameters.IsGlassEnabled
173180
&& !App.IsGPUInBlacklist;
181+
var backdrop = GetBackdropOption();
174182

175183
var windowChrome = WindowChrome.GetWindowChrome(this);
176184
windowChrome?.GlassFrameThickness = useTransparency ? new Thickness(1) : new Thickness(0);
177185

178186
if (useTransparency)
179187
{
180-
if (App.IsWin11)
181-
{
182-
if (Environment.OSVersion.Version >= new Version(10, 0, 22523))
183-
{
184-
WindowHelper.EnableBackdropMicaBlur(this, CurrentTheme == Themes.Dark);
185-
}
186-
else
187-
{
188-
WindowHelper.EnableMicaBlur(this, CurrentTheme == Themes.Dark);
189-
}
190-
}
191-
else if (App.IsWin10)
192-
{
193-
WindowHelper.EnableBlur(this);
194-
}
195-
else
196-
{
197-
Background = (Brush)FindResource("MainWindowBackgroundNoTransparent");
198-
}
188+
ApplyBackdrop(backdrop);
199189
}
200190
else
201191
{
@@ -216,6 +206,123 @@ private void ApplyWindowBackgroundEffects()
216206
}
217207
}
218208

209+
private void ApplyBackdrop(SystembackdropType backdrop)
210+
{
211+
switch (backdrop)
212+
{
213+
case SystembackdropType.None:
214+
Background = (Brush)FindResource("MainWindowBackgroundNoTransparent");
215+
break;
216+
217+
case SystembackdropType.Mica:
218+
if (App.IsWin11)
219+
{
220+
if (Environment.OSVersion.Version >= new Version(10, 0, 22523))
221+
{
222+
WindowHelper.DisableDwmBlur(this);
223+
WindowHelper.EnableBackdropMicaBlur(this, CurrentTheme == Themes.Dark);
224+
}
225+
else
226+
{
227+
WindowHelper.DisableDwmBlur(this);
228+
WindowHelper.EnableMicaBlur(this, CurrentTheme == Themes.Dark);
229+
}
230+
}
231+
else if (App.IsWin10)
232+
{
233+
WindowHelper.DisableDwmBlur(this);
234+
WindowHelper.EnableBlur(this);
235+
}
236+
else
237+
{
238+
Background = (Brush)FindResource("MainWindowBackgroundNoTransparent");
239+
}
240+
241+
break;
242+
243+
case SystembackdropType.Acrylic:
244+
if (App.IsWin11 && Environment.OSVersion.Version >= new Version(10, 0, 22523))
245+
{
246+
WindowHelper.DisableDwmBlur(this);
247+
WindowHelper.EnableBackdropAcrylicBlur(this, CurrentTheme == Themes.Dark);
248+
}
249+
else if (App.IsWin10)
250+
{
251+
WindowHelper.DisableDwmBlur(this);
252+
WindowHelper.EnableBlur(this);
253+
}
254+
else
255+
{
256+
Background = (Brush)FindResource("MainWindowBackgroundNoTransparent");
257+
}
258+
259+
break;
260+
261+
case SystembackdropType.Tabbed:
262+
if (App.IsWin11 && Environment.OSVersion.Version >= new Version(10, 0, 22523))
263+
{
264+
WindowHelper.DisableDwmBlur(this);
265+
WindowHelper.EnableBackdropTabbedBlur(this, CurrentTheme == Themes.Dark);
266+
}
267+
else if (App.IsWin10)
268+
{
269+
WindowHelper.DisableDwmBlur(this);
270+
WindowHelper.EnableBlur(this);
271+
}
272+
else
273+
{
274+
Background = (Brush)FindResource("MainWindowBackgroundNoTransparent");
275+
}
276+
277+
break;
278+
279+
case SystembackdropType.Auto:
280+
default:
281+
if (App.IsWin11)
282+
{
283+
if (Environment.OSVersion.Version >= new Version(10, 0, 22523))
284+
{
285+
WindowHelper.DisableDwmBlur(this);
286+
WindowHelper.EnableBackdropMicaBlur(this, CurrentTheme == Themes.Dark);
287+
}
288+
else
289+
{
290+
WindowHelper.DisableDwmBlur(this);
291+
WindowHelper.EnableMicaBlur(this, CurrentTheme == Themes.Dark);
292+
}
293+
}
294+
else if (App.IsWin10)
295+
{
296+
WindowHelper.DisableDwmBlur(this);
297+
WindowHelper.EnableBlur(this);
298+
}
299+
else
300+
{
301+
Background = (Brush)FindResource("MainWindowBackgroundNoTransparent");
302+
}
303+
304+
break;
305+
}
306+
}
307+
308+
private static SystembackdropType GetBackdropOption()
309+
{
310+
var option = SettingHelper.Get("WindowBackdrop", nameof(SystembackdropType.Auto), "QuickLook")?.Trim();
311+
312+
if (string.IsNullOrEmpty(option))
313+
return SystembackdropType.Auto;
314+
315+
if (string.Equals(option, nameof(SystembackdropType.Acrylic), StringComparison.OrdinalIgnoreCase))
316+
return SystembackdropType.Acrylic;
317+
318+
if (string.Equals(option, nameof(SystembackdropType.Tabbed), StringComparison.OrdinalIgnoreCase))
319+
return SystembackdropType.Tabbed;
320+
321+
return Enum.TryParse(option, true, out SystembackdropType parsed)
322+
? parsed
323+
: SystembackdropType.Auto;
324+
}
325+
219326
private void SaveWindowSizeOnSizeChanged(object sender, SizeChangedEventArgs e)
220327
{
221328
// first shown?

0 commit comments

Comments
 (0)