Skip to content

Commit 6f89909

Browse files
committed
Move dll import codes to win32 helper
1 parent 1073821 commit 6f89909

File tree

2 files changed

+104
-100
lines changed

2 files changed

+104
-100
lines changed

Flow.Launcher.Core/Resource/Theme.cs

Lines changed: 2 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
using System.IO;
44
using System.Linq;
55
using System.Xml;
6-
using System.Runtime.InteropServices;
76
using System.Windows;
87
using System.Windows.Controls;
9-
using System.Windows.Interop;
108
using System.Windows.Markup;
119
using System.Windows.Media;
1210
using System.Windows.Media.Effects;
@@ -98,12 +96,12 @@ public bool ChangeTheme(string theme)
9896
_oldTheme = Path.GetFileNameWithoutExtension(_oldResource.Source.AbsolutePath);
9997
}
10098

101-
BlurEnabled = IsBlurTheme();
99+
BlurEnabled = Win32Helper.IsBlurTheme();
102100

103101
if (Settings.UseDropShadowEffect && !BlurEnabled)
104102
AddDropShadowEffectToCurrentTheme();
105103

106-
SetBlurForWindow();
104+
Win32Helper.SetBlurForWindow(Application.Current.MainWindow, BlurEnabled);
107105
}
108106
catch (DirectoryNotFoundException)
109107
{
@@ -357,98 +355,6 @@ public void RemoveDropShadowEffectFromCurrentTheme()
357355
UpdateResourceDictionary(dict);
358356
}
359357

360-
#region Blur Handling
361-
/*
362-
Found on https://github.com/riverar/sample-win10-aeroglass
363-
*/
364-
private enum AccentState
365-
{
366-
ACCENT_DISABLED = 0,
367-
ACCENT_ENABLE_GRADIENT = 1,
368-
ACCENT_ENABLE_TRANSPARENTGRADIENT = 2,
369-
ACCENT_ENABLE_BLURBEHIND = 3,
370-
ACCENT_INVALID_STATE = 4
371-
}
372-
373-
[StructLayout(LayoutKind.Sequential)]
374-
private struct AccentPolicy
375-
{
376-
public AccentState AccentState;
377-
public int AccentFlags;
378-
public int GradientColor;
379-
public int AnimationId;
380-
}
381-
382-
[StructLayout(LayoutKind.Sequential)]
383-
private struct WindowCompositionAttributeData
384-
{
385-
public WindowCompositionAttribute Attribute;
386-
public IntPtr Data;
387-
public int SizeOfData;
388-
}
389-
390-
private enum WindowCompositionAttribute
391-
{
392-
WCA_ACCENT_POLICY = 19
393-
}
394-
[DllImport("user32.dll")]
395-
private static extern int SetWindowCompositionAttribute(IntPtr hwnd, ref WindowCompositionAttributeData data);
396-
397-
/// <summary>
398-
/// Sets the blur for a window via SetWindowCompositionAttribute
399-
/// </summary>
400-
public void SetBlurForWindow()
401-
{
402-
if (BlurEnabled)
403-
{
404-
SetWindowAccent(Application.Current.MainWindow, AccentState.ACCENT_ENABLE_BLURBEHIND);
405-
}
406-
else
407-
{
408-
SetWindowAccent(Application.Current.MainWindow, AccentState.ACCENT_DISABLED);
409-
}
410-
}
411-
412-
private bool IsBlurTheme()
413-
{
414-
if (Environment.OSVersion.Version >= new Version(6, 2))
415-
{
416-
var resource = Application.Current.TryFindResource("ThemeBlurEnabled");
417-
418-
if (resource is bool)
419-
return (bool)resource;
420-
421-
return false;
422-
}
423-
424-
return false;
425-
}
426-
427-
private void SetWindowAccent(Window w, AccentState state)
428-
{
429-
var windowHelper = new WindowInteropHelper(w);
430-
431-
windowHelper.EnsureHandle();
432-
433-
var accent = new AccentPolicy { AccentState = state };
434-
var accentStructSize = Marshal.SizeOf(accent);
435-
436-
var accentPtr = Marshal.AllocHGlobal(accentStructSize);
437-
Marshal.StructureToPtr(accent, accentPtr, false);
438-
439-
var data = new WindowCompositionAttributeData
440-
{
441-
Attribute = WindowCompositionAttribute.WCA_ACCENT_POLICY,
442-
SizeOfData = accentStructSize,
443-
Data = accentPtr
444-
};
445-
446-
SetWindowCompositionAttribute(windowHelper.Handle, ref data);
447-
448-
Marshal.FreeHGlobal(accentPtr);
449-
}
450-
#endregion
451-
452358
public record ThemeData(string FileNameWithoutExtension, string Name, bool? IsDark = null, bool? HasBlur = null);
453359
}
454360
}

Flow.Launcher.Infrastructure/Win32Helper.cs

Lines changed: 102 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
using System;
2+
using System.Runtime.InteropServices;
23
using System.Threading;
34
using System.Threading.Tasks;
5+
using System.Windows.Interop;
6+
using System.Windows;
47
using Windows.Win32;
58

69
namespace Flow.Launcher.Infrastructure
710
{
8-
/// <summary>
9-
/// Provides static helper for Win32.
10-
/// Codes are edited from: https://github.com/files-community/Files.
11-
/// </summary>
1211
public static class Win32Helper
1312
{
13+
#region STA Thread
14+
15+
/*
16+
Found on https://github.com/files-community/Files
17+
*/
18+
1419
public static Task StartSTATaskAsync(Action action)
1520
{
1621
var taskCompletionSource = new TaskCompletionSource();
@@ -137,5 +142,98 @@ public static Task StartSTATaskAsync(Func<Task> func)
137142

138143
return taskCompletionSource.Task;
139144
}
145+
146+
#endregion
147+
148+
#region Blur Handling
149+
150+
/*
151+
Found on https://github.com/riverar/sample-win10-aeroglass
152+
*/
153+
154+
private enum AccentState
155+
{
156+
ACCENT_DISABLED = 0,
157+
ACCENT_ENABLE_GRADIENT = 1,
158+
ACCENT_ENABLE_TRANSPARENTGRADIENT = 2,
159+
ACCENT_ENABLE_BLURBEHIND = 3,
160+
ACCENT_INVALID_STATE = 4
161+
}
162+
163+
[StructLayout(LayoutKind.Sequential)]
164+
private struct AccentPolicy
165+
{
166+
public AccentState AccentState;
167+
public int AccentFlags;
168+
public int GradientColor;
169+
public int AnimationId;
170+
}
171+
172+
[StructLayout(LayoutKind.Sequential)]
173+
private struct WindowCompositionAttributeData
174+
{
175+
public WindowCompositionAttribute Attribute;
176+
public IntPtr Data;
177+
public int SizeOfData;
178+
}
179+
180+
private enum WindowCompositionAttribute
181+
{
182+
WCA_ACCENT_POLICY = 19
183+
}
184+
185+
[DllImport("user32.dll")]
186+
private static extern int SetWindowCompositionAttribute(IntPtr hwnd, ref WindowCompositionAttributeData data);
187+
188+
/// <summary>
189+
/// Checks if the blur theme is enabled
190+
/// </summary>
191+
public static bool IsBlurTheme()
192+
{
193+
if (Environment.OSVersion.Version >= new Version(6, 2))
194+
{
195+
var resource = Application.Current.TryFindResource("ThemeBlurEnabled");
196+
197+
if (resource is bool b)
198+
return b;
199+
200+
return false;
201+
}
202+
203+
return false;
204+
}
205+
206+
/// <summary>
207+
/// Sets the blur for a window via SetWindowCompositionAttribute
208+
/// </summary>
209+
public static void SetBlurForWindow(Window w, bool blur)
210+
{
211+
SetWindowAccent(w, blur ? AccentState.ACCENT_ENABLE_BLURBEHIND : AccentState.ACCENT_DISABLED);
212+
}
213+
214+
private static void SetWindowAccent(Window w, AccentState state)
215+
{
216+
var windowHelper = new WindowInteropHelper(w);
217+
218+
windowHelper.EnsureHandle();
219+
220+
var accent = new AccentPolicy { AccentState = state };
221+
var accentStructSize = Marshal.SizeOf(accent);
222+
223+
var accentPtr = Marshal.AllocHGlobal(accentStructSize);
224+
Marshal.StructureToPtr(accent, accentPtr, false);
225+
226+
var data = new WindowCompositionAttributeData
227+
{
228+
Attribute = WindowCompositionAttribute.WCA_ACCENT_POLICY,
229+
SizeOfData = accentStructSize,
230+
Data = accentPtr
231+
};
232+
233+
SetWindowCompositionAttribute(windowHelper.Handle, ref data);
234+
235+
Marshal.FreeHGlobal(accentPtr);
236+
}
237+
#endregion
140238
}
141239
}

0 commit comments

Comments
 (0)