Skip to content

Commit

Permalink
Merge pull request Flow-Launcher#3122 from Jack251970/dev3
Browse files Browse the repository at this point in the history
Replace DllImport and flags with CSWin32 & Remove useless DllImport and flags & Fix right Win key click issue
  • Loading branch information
taooceros authored Dec 28, 2024
2 parents e7e825e + 9350e82 commit fd3576d
Show file tree
Hide file tree
Showing 31 changed files with 659 additions and 843 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);
}
}
10 changes: 3 additions & 7 deletions Flow.Launcher.Infrastructure/FileExplorerHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using Windows.Win32;

namespace Flow.Launcher.Infrastructure
{
Expand Down Expand Up @@ -54,10 +54,6 @@ private static dynamic GetActiveExplorer()
return explorerWindows.Zip(zOrders).MinBy(x => x.Second).First;
}

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);

private delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);

/// <summary>
Expand All @@ -70,9 +66,9 @@ private static IEnumerable<int> GetZOrder(List<dynamic> hWnds)

var index = 0;
var numRemaining = hWnds.Count;
EnumWindows((wnd, _) =>
PInvoke.EnumWindows((wnd, _) =>
{
var searchIndex = hWnds.FindIndex(x => x.HWND == wnd.ToInt32());
var searchIndex = hWnds.FindIndex(x => x.HWND == wnd.Value);
if (searchIndex != -1)
{
z[searchIndex] = index;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>

<ItemGroup>
<AdditionalFiles Include="NativeMethods.txt" />
</ItemGroup>

<ItemGroup>
<Compile Include="..\SolutionAssemblyInfo.cs" Link="Properties\SolutionAssemblyInfo.cs" />
<None Include="FodyWeavers.xml" />
Expand All @@ -56,6 +60,10 @@
</PackageReference>
<PackageReference Include="MemoryPack" Version="1.21.3" />
<PackageReference Include="Microsoft.VisualStudio.Threading" Version="17.12.19" />
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.3.106">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="NLog" Version="4.7.10" />
<PackageReference Include="PropertyChanged.Fody" Version="3.4.0" />
<PackageReference Include="System.Drawing.Common" Version="7.0.0" />
Expand Down
60 changes: 33 additions & 27 deletions Flow.Launcher.Infrastructure/Hotkey/GlobalHotkey.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
using System;
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Flow.Launcher.Plugin;
using Windows.Win32;
using Windows.Win32.Foundation;
using Windows.Win32.UI.Input.KeyboardAndMouse;
using Windows.Win32.UI.WindowsAndMessaging;

namespace Flow.Launcher.Infrastructure.Hotkey
{
Expand All @@ -10,44 +15,45 @@ namespace Flow.Launcher.Infrastructure.Hotkey
/// </summary>
public unsafe class GlobalHotkey : IDisposable
{
private static readonly IntPtr hookId;



private static readonly HOOKPROC _procKeyboard = HookKeyboardCallback;
private static readonly UnhookWindowsHookExSafeHandle hookId;

public delegate bool KeyboardCallback(KeyEvent keyEvent, int vkCode, SpecialKeyState state);
internal static Func<KeyEvent, int, SpecialKeyState, bool> hookedKeyboardCallback;

//Modifier key constants
private const int VK_SHIFT = 0x10;
private const int VK_CONTROL = 0x11;
private const int VK_ALT = 0x12;
private const int VK_WIN = 91;

static GlobalHotkey()
{
// Set the hook
hookId = InterceptKeys.SetHook(& LowLevelKeyboardProc);
hookId = SetHook(_procKeyboard, WINDOWS_HOOK_ID.WH_KEYBOARD_LL);
}

private static UnhookWindowsHookExSafeHandle SetHook(HOOKPROC proc, WINDOWS_HOOK_ID hookId)
{
using var curProcess = Process.GetCurrentProcess();
using var curModule = curProcess.MainModule;
return PInvoke.SetWindowsHookEx(hookId, proc, PInvoke.GetModuleHandle(curModule.ModuleName), 0);
}

public static SpecialKeyState CheckModifiers()
{
SpecialKeyState state = new SpecialKeyState();
if ((InterceptKeys.GetKeyState(VK_SHIFT) & 0x8000) != 0)
if ((PInvoke.GetKeyState((int)VIRTUAL_KEY.VK_SHIFT) & 0x8000) != 0)
{
//SHIFT is pressed
state.ShiftPressed = true;
}
if ((InterceptKeys.GetKeyState(VK_CONTROL) & 0x8000) != 0)
if ((PInvoke.GetKeyState((int)VIRTUAL_KEY.VK_CONTROL) & 0x8000) != 0)
{
//CONTROL is pressed
state.CtrlPressed = true;
}
if ((InterceptKeys.GetKeyState(VK_ALT) & 0x8000) != 0)
if ((PInvoke.GetKeyState((int)VIRTUAL_KEY.VK_MENU) & 0x8000) != 0)
{
//ALT is pressed
state.AltPressed = true;
}
if ((InterceptKeys.GetKeyState(VK_WIN) & 0x8000) != 0)
if ((PInvoke.GetKeyState((int)VIRTUAL_KEY.VK_LWIN) & 0x8000) != 0 ||
(PInvoke.GetKeyState((int)VIRTUAL_KEY.VK_RWIN) & 0x8000) != 0)
{
//WIN is pressed
state.WinPressed = true;
Expand All @@ -56,38 +62,38 @@ public static SpecialKeyState CheckModifiers()
return state;
}

[UnmanagedCallersOnly]
private static IntPtr LowLevelKeyboardProc(int nCode, UIntPtr wParam, IntPtr lParam)
private static LRESULT HookKeyboardCallback(int nCode, WPARAM wParam, LPARAM lParam)
{
bool continues = true;

if (nCode >= 0)
{
if (wParam.ToUInt32() == (int)KeyEvent.WM_KEYDOWN ||
wParam.ToUInt32() == (int)KeyEvent.WM_KEYUP ||
wParam.ToUInt32() == (int)KeyEvent.WM_SYSKEYDOWN ||
wParam.ToUInt32() == (int)KeyEvent.WM_SYSKEYUP)
if (wParam.Value == (int)KeyEvent.WM_KEYDOWN ||
wParam.Value == (int)KeyEvent.WM_KEYUP ||
wParam.Value == (int)KeyEvent.WM_SYSKEYDOWN ||
wParam.Value == (int)KeyEvent.WM_SYSKEYUP)
{
if (hookedKeyboardCallback != null)
continues = hookedKeyboardCallback((KeyEvent)wParam.ToUInt32(), Marshal.ReadInt32(lParam), CheckModifiers());
continues = hookedKeyboardCallback((KeyEvent)wParam.Value, Marshal.ReadInt32(lParam), CheckModifiers());
}
}

if (continues)
{
return InterceptKeys.CallNextHookEx(hookId, nCode, wParam, lParam);
return PInvoke.CallNextHookEx(hookId, nCode, wParam, lParam);
}
return (IntPtr)(-1);

return new LRESULT(1);
}

public void Dispose()
{
InterceptKeys.UnhookWindowsHookEx(hookId);
hookId.Dispose();
}

~GlobalHotkey()
{
Dispose();
}
}
}
}
38 changes: 0 additions & 38 deletions Flow.Launcher.Infrastructure/Hotkey/InterceptKeys.cs

This file was deleted.

12 changes: 7 additions & 5 deletions Flow.Launcher.Infrastructure/Hotkey/KeyEvent.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
using Windows.Win32;

namespace Flow.Launcher.Infrastructure.Hotkey
{
public enum KeyEvent
{
/// <summary>
/// Key down
/// </summary>
WM_KEYDOWN = 256,
WM_KEYDOWN = (int)PInvoke.WM_KEYDOWN,

/// <summary>
/// Key up
/// </summary>
WM_KEYUP = 257,
WM_KEYUP = (int)PInvoke.WM_KEYUP,

/// <summary>
/// System key up
/// </summary>
WM_SYSKEYUP = 261,
WM_SYSKEYUP = (int)PInvoke.WM_SYSKEYUP,

/// <summary>
/// System key down
/// </summary>
WM_SYSKEYDOWN = 260
WM_SYSKEYDOWN = (int)PInvoke.WM_SYSKEYDOWN
}
}
}
Loading

0 comments on commit fd3576d

Please sign in to comment.