|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Runtime.InteropServices; |
| 4 | +using Flow.Launcher.Infrastructure.Hotkey; |
| 5 | +using Flow.Launcher.Infrastructure.Logger; |
| 6 | +using Interop.UIAutomationClient; |
| 7 | +using NHotkey; |
| 8 | +using SHDocVw; |
| 9 | +using Shell32; |
| 10 | +using Windows.Win32; |
| 11 | +using Windows.Win32.Foundation; |
| 12 | +using Windows.Win32.UI.Accessibility; |
| 13 | +using WindowsInput; |
| 14 | +using WindowsInput.Native; |
| 15 | + |
| 16 | +namespace Flow.Launcher.Infrastructure.QuickSwitch |
| 17 | +{ |
| 18 | + public static class QuickSwitch |
| 19 | + { |
| 20 | + private static CUIAutomation8 _automation = new CUIAutomation8Class(); |
| 21 | + |
| 22 | + private static InternetExplorer lastExplorerView = null; |
| 23 | + |
| 24 | + private static readonly InputSimulator _inputSimulator = new(); |
| 25 | + |
| 26 | + private static UnhookWinEventSafeHandle _hookWinEventSafeHandle = null; |
| 27 | + |
| 28 | + public static void Initialize(Action<HotkeyModel, EventHandler<HotkeyEventArgs>> setHotkeyAction) |
| 29 | + { |
| 30 | + try |
| 31 | + { |
| 32 | + // Inspired from: https://github.com/citelao/dotnet_win32/blob/c830132d84eeed3a77e3a6e7f9ed6109258c7947/window_events/Program.cs |
| 33 | + // Here we use an UnhookWinEventSafeHandle as return value so the result is IDisposable and |
| 34 | + // can be cleaned up automatically for us. |
| 35 | + _hookWinEventSafeHandle = PInvoke.SetWinEventHook( |
| 36 | + PInvoke.EVENT_SYSTEM_FOREGROUND, |
| 37 | + PInvoke.EVENT_SYSTEM_FOREGROUND, |
| 38 | + null, |
| 39 | + WindowSwitch, |
| 40 | + 0, |
| 41 | + 0, |
| 42 | + PInvoke.WINEVENT_OUTOFCONTEXT); |
| 43 | + |
| 44 | + if (_hookWinEventSafeHandle.IsInvalid) |
| 45 | + { |
| 46 | + Log.Error("Failed to set window event hook"); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + setHotkeyAction(new HotkeyModel("Alt+G"), (_, _) => |
| 51 | + { |
| 52 | + NavigateDialogPath(_automation.ElementFromHandle(Win32Helper.GetForegroundWindow())); |
| 53 | + }); |
| 54 | + } |
| 55 | + catch (System.Exception e) |
| 56 | + { |
| 57 | + Log.Exception(nameof(QuickSwitch), "Failed to initialize QuickSwitch", e); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + private static void NavigateDialogPath(IUIAutomationElement window) |
| 62 | + { |
| 63 | + if (window is not { CurrentClassName: "#32770" } dialog) return; |
| 64 | + |
| 65 | + object document; |
| 66 | + try |
| 67 | + { |
| 68 | + document = lastExplorerView?.Document; |
| 69 | + } |
| 70 | + catch (COMException) |
| 71 | + { |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + if (document is not IShellFolderViewDual2 folder) return; |
| 76 | + |
| 77 | + var path = folder.Folder.Items().Item().Path; |
| 78 | + if (!Path.IsPathRooted(path)) return; |
| 79 | + |
| 80 | + _inputSimulator.Keyboard.ModifiedKeyStroke(VirtualKeyCode.MENU, VirtualKeyCode.VK_D); |
| 81 | + |
| 82 | + var address = dialog.FindFirst(TreeScope.TreeScope_Subtree, _automation.CreateAndCondition( |
| 83 | + _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_ControlTypePropertyId, UIA_ControlTypeIds.UIA_EditControlTypeId), |
| 84 | + _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_AccessKeyPropertyId, "d"))); |
| 85 | + |
| 86 | + if (address == null) |
| 87 | + { |
| 88 | + Log.Error("Cannot Get specific Control"); |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + var edit = (IUIAutomationValuePattern)address.GetCurrentPattern(UIA_PatternIds.UIA_ValuePatternId); |
| 93 | + edit.SetValue(path); |
| 94 | + |
| 95 | + PInvoke.SendMessage( |
| 96 | + new(address.CurrentNativeWindowHandle), |
| 97 | + PInvoke.WM_KEYDOWN, |
| 98 | + (nuint)VirtualKeyCode.RETURN, |
| 99 | + IntPtr.Zero); |
| 100 | + } |
| 101 | + |
| 102 | + private static void WindowSwitch( |
| 103 | + HWINEVENTHOOK hWinEventHook, |
| 104 | + uint eventType, |
| 105 | + HWND hwnd, |
| 106 | + int idObject, |
| 107 | + int idChild, |
| 108 | + uint dwEventThread, |
| 109 | + uint dwmsEventTime |
| 110 | + ) |
| 111 | + { |
| 112 | + IUIAutomationElement window = null; |
| 113 | + try |
| 114 | + { |
| 115 | + window = _automation.ElementFromHandle(hwnd); |
| 116 | + } |
| 117 | + catch |
| 118 | + { |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + if (window is { CurrentClassName: "#32770" }) |
| 123 | + { |
| 124 | + NavigateDialogPath(window); |
| 125 | + return; |
| 126 | + } |
| 127 | + |
| 128 | + ShellWindowsClass shellWindows = null; |
| 129 | + try |
| 130 | + { |
| 131 | + shellWindows = new ShellWindowsClass(); |
| 132 | + |
| 133 | + foreach (var shellWindow in shellWindows) |
| 134 | + { |
| 135 | + if (shellWindow is not InternetExplorer explorer) |
| 136 | + { |
| 137 | + continue; |
| 138 | + } |
| 139 | + |
| 140 | + // Fix for CA2020: Wrap the conversion in a 'checked' statement |
| 141 | + if (explorer.HWND != checked((int)hwnd)) |
| 142 | + { |
| 143 | + continue; |
| 144 | + } |
| 145 | + |
| 146 | + // Release previous reference if exists |
| 147 | + if (lastExplorerView != null) |
| 148 | + { |
| 149 | + Marshal.ReleaseComObject(lastExplorerView); |
| 150 | + lastExplorerView = null; |
| 151 | + } |
| 152 | + |
| 153 | + lastExplorerView = explorer; |
| 154 | + } |
| 155 | + } |
| 156 | + catch (System.Exception e) |
| 157 | + { |
| 158 | + Log.Exception(nameof(QuickSwitch), "Failed to get shell windows", e); |
| 159 | + } |
| 160 | + finally |
| 161 | + { |
| 162 | + if (window != null) |
| 163 | + { |
| 164 | + Marshal.ReleaseComObject(window); |
| 165 | + window = null; |
| 166 | + } |
| 167 | + if (shellWindows != null) |
| 168 | + { |
| 169 | + Marshal.ReleaseComObject(shellWindows); |
| 170 | + shellWindows = null; |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + public static void Dispose() |
| 176 | + { |
| 177 | + // Dispose handle |
| 178 | + if (_hookWinEventSafeHandle != null) |
| 179 | + { |
| 180 | + _hookWinEventSafeHandle.Dispose(); |
| 181 | + _hookWinEventSafeHandle = null; |
| 182 | + } |
| 183 | + |
| 184 | + // Release ComObjects |
| 185 | + if (lastExplorerView != null) |
| 186 | + { |
| 187 | + Marshal.ReleaseComObject(lastExplorerView); |
| 188 | + lastExplorerView = null; |
| 189 | + } |
| 190 | + if (_automation != null) |
| 191 | + { |
| 192 | + Marshal.ReleaseComObject(_automation); |
| 193 | + _automation = null; |
| 194 | + } |
| 195 | + } |
| 196 | + } |
| 197 | +} |
0 commit comments