Skip to content

Commit

Permalink
Move dll import codes to win32 helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack251970 committed Dec 24, 2024
1 parent 1073821 commit 6f89909
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 100 deletions.
98 changes: 2 additions & 96 deletions Flow.Launcher.Core/Resource/Theme.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
using System.IO;
using System.Linq;
using System.Xml;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Effects;
Expand Down Expand Up @@ -98,12 +96,12 @@ public bool ChangeTheme(string theme)
_oldTheme = Path.GetFileNameWithoutExtension(_oldResource.Source.AbsolutePath);
}

BlurEnabled = IsBlurTheme();
BlurEnabled = Win32Helper.IsBlurTheme();

if (Settings.UseDropShadowEffect && !BlurEnabled)
AddDropShadowEffectToCurrentTheme();

SetBlurForWindow();
Win32Helper.SetBlurForWindow(Application.Current.MainWindow, BlurEnabled);
}
catch (DirectoryNotFoundException)
{
Expand Down Expand Up @@ -357,98 +355,6 @@ public void RemoveDropShadowEffectFromCurrentTheme()
UpdateResourceDictionary(dict);
}

#region Blur Handling
/*
Found on https://github.com/riverar/sample-win10-aeroglass
*/
private enum AccentState
{
ACCENT_DISABLED = 0,
ACCENT_ENABLE_GRADIENT = 1,
ACCENT_ENABLE_TRANSPARENTGRADIENT = 2,
ACCENT_ENABLE_BLURBEHIND = 3,
ACCENT_INVALID_STATE = 4
}

[StructLayout(LayoutKind.Sequential)]
private struct AccentPolicy
{
public AccentState AccentState;
public int AccentFlags;
public int GradientColor;
public int AnimationId;
}

[StructLayout(LayoutKind.Sequential)]
private struct WindowCompositionAttributeData
{
public WindowCompositionAttribute Attribute;
public IntPtr Data;
public int SizeOfData;
}

private enum WindowCompositionAttribute
{
WCA_ACCENT_POLICY = 19
}
[DllImport("user32.dll")]
private static extern int SetWindowCompositionAttribute(IntPtr hwnd, ref WindowCompositionAttributeData data);

/// <summary>
/// Sets the blur for a window via SetWindowCompositionAttribute
/// </summary>
public void SetBlurForWindow()
{
if (BlurEnabled)
{
SetWindowAccent(Application.Current.MainWindow, AccentState.ACCENT_ENABLE_BLURBEHIND);
}
else
{
SetWindowAccent(Application.Current.MainWindow, AccentState.ACCENT_DISABLED);
}
}

private bool IsBlurTheme()
{
if (Environment.OSVersion.Version >= new Version(6, 2))
{
var resource = Application.Current.TryFindResource("ThemeBlurEnabled");

if (resource is bool)
return (bool)resource;

return false;
}

return false;
}

private void SetWindowAccent(Window w, AccentState state)
{
var windowHelper = new WindowInteropHelper(w);

windowHelper.EnsureHandle();

var accent = new AccentPolicy { AccentState = state };
var accentStructSize = Marshal.SizeOf(accent);

var accentPtr = Marshal.AllocHGlobal(accentStructSize);
Marshal.StructureToPtr(accent, accentPtr, false);

var data = new WindowCompositionAttributeData
{
Attribute = WindowCompositionAttribute.WCA_ACCENT_POLICY,
SizeOfData = accentStructSize,
Data = accentPtr
};

SetWindowCompositionAttribute(windowHelper.Handle, ref data);

Marshal.FreeHGlobal(accentPtr);
}
#endregion

public record ThemeData(string FileNameWithoutExtension, string Name, bool? IsDark = null, bool? HasBlur = null);
}
}
106 changes: 102 additions & 4 deletions Flow.Launcher.Infrastructure/Win32Helper.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Interop;
using System.Windows;
using Windows.Win32;

namespace Flow.Launcher.Infrastructure
{
/// <summary>
/// Provides static helper for Win32.
/// Codes are edited from: https://github.com/files-community/Files.
/// </summary>
public static class Win32Helper
{
#region STA Thread

/*
Found on https://github.com/files-community/Files
*/

public static Task StartSTATaskAsync(Action action)
{
var taskCompletionSource = new TaskCompletionSource();
Expand Down Expand Up @@ -137,5 +142,98 @@ public static Task StartSTATaskAsync(Func<Task> func)

return taskCompletionSource.Task;
}

#endregion

#region Blur Handling

/*
Found on https://github.com/riverar/sample-win10-aeroglass
*/

private enum AccentState
{
ACCENT_DISABLED = 0,
ACCENT_ENABLE_GRADIENT = 1,
ACCENT_ENABLE_TRANSPARENTGRADIENT = 2,
ACCENT_ENABLE_BLURBEHIND = 3,
ACCENT_INVALID_STATE = 4
}

[StructLayout(LayoutKind.Sequential)]
private struct AccentPolicy
{
public AccentState AccentState;
public int AccentFlags;
public int GradientColor;
public int AnimationId;
}

[StructLayout(LayoutKind.Sequential)]
private struct WindowCompositionAttributeData
{
public WindowCompositionAttribute Attribute;
public IntPtr Data;
public int SizeOfData;
}

private enum WindowCompositionAttribute
{
WCA_ACCENT_POLICY = 19
}

[DllImport("user32.dll")]
private static extern int SetWindowCompositionAttribute(IntPtr hwnd, ref WindowCompositionAttributeData data);

/// <summary>
/// Checks if the blur theme is enabled
/// </summary>
public static bool IsBlurTheme()
{
if (Environment.OSVersion.Version >= new Version(6, 2))
{
var resource = Application.Current.TryFindResource("ThemeBlurEnabled");

if (resource is bool b)
return b;

return false;
}

return false;
}

/// <summary>
/// Sets the blur for a window via SetWindowCompositionAttribute
/// </summary>
public static void SetBlurForWindow(Window w, bool blur)
{
SetWindowAccent(w, blur ? AccentState.ACCENT_ENABLE_BLURBEHIND : AccentState.ACCENT_DISABLED);
}

private static void SetWindowAccent(Window w, AccentState state)
{
var windowHelper = new WindowInteropHelper(w);

windowHelper.EnsureHandle();

var accent = new AccentPolicy { AccentState = state };
var accentStructSize = Marshal.SizeOf(accent);

var accentPtr = Marshal.AllocHGlobal(accentStructSize);
Marshal.StructureToPtr(accent, accentPtr, false);

var data = new WindowCompositionAttributeData
{
Attribute = WindowCompositionAttribute.WCA_ACCENT_POLICY,
SizeOfData = accentStructSize,
Data = accentPtr
};

SetWindowCompositionAttribute(windowHelper.Handle, ref data);

Marshal.FreeHGlobal(accentPtr);
}
#endregion
}
}

0 comments on commit 6f89909

Please sign in to comment.