anshdadhich commited on
Commit
33732ba
·
verified ·
1 Parent(s): 519d15e

Upload FastSeekWpf/App.xaml.cs

Browse files
Files changed (1) hide show
  1. FastSeekWpf/App.xaml.cs +91 -0
FastSeekWpf/App.xaml.cs ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ using System;
2
+ using System.Diagnostics;
3
+ using System.IO;
4
+ using System.Runtime.InteropServices;
5
+ using System.Threading;
6
+ using System.Windows;
7
+ using System.Windows.Interop;
8
+ using FastSeekWpf.NativeInterop;
9
+
10
+ namespace FastSeekWpf;
11
+
12
+ public partial class App : Application
13
+ {
14
+ private const string MutexName = "FastSeekWpf_SingleInstance_Mutex";
15
+ private const uint WM_TOGGLE_WINDOW = Win32Api.WM_USER + 3;
16
+ private Mutex? _mutex;
17
+ private bool _shutdown;
18
+ private MainWindow? _mainWindow;
19
+ private Thread? _hotkeyThread;
20
+
21
+ private void OnStartup(object sender, StartupEventArgs e)
22
+ {
23
+ // Single-instance guard
24
+ _mutex = new Mutex(true, MutexName, out bool createdNew);
25
+ if (!createdNew)
26
+ {
27
+ IntPtr hwnd = Win32Api.FindWindowW("FastSeekWpf_MainWindow", null);
28
+ if (hwnd != IntPtr.Zero)
29
+ Win32Api.PostMessageW(hwnd, WM_TOGGLE_WINDOW, IntPtr.Zero, IntPtr.Zero);
30
+ Shutdown();
31
+ return;
32
+ }
33
+
34
+ _mainWindow = new MainWindow();
35
+ _mainWindow.Show();
36
+ _mainWindow.Hide(); // Start hidden, wait for hotkey
37
+
38
+ // Start global hotkey thread (Alt+Space)
39
+ _hotkeyThread = new Thread(HotkeyLoop)
40
+ {
41
+ IsBackground = true,
42
+ Name = "HotkeyThread"
43
+ };
44
+ var helper = new WindowInteropHelper(_mainWindow);
45
+ _hotkeyThread.Start(helper.Handle);
46
+ }
47
+
48
+ private void OnExit(object sender, ExitEventArgs e)
49
+ {
50
+ _shutdown = true;
51
+ _mutex?.ReleaseMutex();
52
+ _mutex?.Dispose();
53
+ }
54
+
55
+ private void HotkeyLoop(object? param)
56
+ {
57
+ IntPtr targetHwnd = (IntPtr)(param ?? IntPtr.Zero);
58
+ if (targetHwnd == IntPtr.Zero) return;
59
+
60
+ // Register Alt+Space (MOD_ALT=0x0001, VK_SPACE=0x20)
61
+ if (!Win32Api.RegisterHotKey(IntPtr.Zero, 1, 0x0001, 0x20))
62
+ return;
63
+
64
+ try
65
+ {
66
+ while (!_shutdown)
67
+ {
68
+ if (Win32Api.PeekMessageW(out Win32Api.MSG msg, IntPtr.Zero, 0, 0, Win32Api.PM_REMOVE))
69
+ {
70
+ if (msg.message == Win32Api.WM_HOTKEY)
71
+ {
72
+ Win32Api.PostMessageW(targetHwnd, WM_TOGGLE_WINDOW, IntPtr.Zero, IntPtr.Zero);
73
+ }
74
+ else
75
+ {
76
+ Win32Api.TranslateMessage(ref msg);
77
+ Win32Api.DispatchMessageW(ref msg);
78
+ }
79
+ }
80
+ else
81
+ {
82
+ Thread.Sleep(10);
83
+ }
84
+ }
85
+ }
86
+ finally
87
+ {
88
+ Win32Api.UnregisterHotKey(IntPtr.Zero, 1);
89
+ }
90
+ }
91
+ }