Repository navigation
Expand file tree
/
Copy pathVisualCandidateOverlayForm.cs
More file actions
382 lines (326 loc) · 15.9 KB
/
Copy pathVisualCandidateOverlayForm.cs
File metadata and controls
382 lines (326 loc) · 15.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Runtime.InteropServices;
using NaturalCommands.Helpers;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Windows.Forms;
using NaturalCommands.Models;
namespace NaturalCommands
{
public class VisualCandidateOverlayForm : Form
{
private const int WM_NCHITTEST = 0x84;
private const int HTTRANSPARENT = -1;
private const int WS_EX_NOACTIVATE = 0x08000000;
private const int WS_EX_TRANSPARENT = 0x00000020;
private const int WS_EX_TOOLWINDOW = 0x00000080;
private static VisualCandidateOverlayForm? _instance;
private static readonly object _lock = new();
private static System.Threading.SynchronizationContext? _uiContext;
private List<VisualTargetCandidate> _candidates = new();
private readonly Font _numberFont = new("Segoe UI", 12, FontStyle.Bold);
private readonly Font _hintFont = new("Segoe UI", 10, FontStyle.Regular);
private readonly System.Windows.Forms.Timer _timeoutTimer;
private VisualCandidateOverlayForm()
{
FormBorderStyle = FormBorderStyle.None;
ShowInTaskbar = false;
TopMost = true;
BackColor = Color.Lime;
TransparencyKey = Color.Lime;
StartPosition = FormStartPosition.Manual;
KeyPreview = true;
DoubleBuffered = true;
var bounds = Screen.AllScreens.Aggregate(Rectangle.Empty, (current, screen) => Rectangle.Union(current, screen.Bounds));
Bounds = bounds;
_timeoutTimer = new System.Windows.Forms.Timer();
_timeoutTimer.Interval = 9000;
_timeoutTimer.Tick += (s, e) => HideOverlay();
KeyDown += (s, e) =>
{
if (e.KeyCode == Keys.Escape)
{
HideOverlay();
}
};
}
// Low-level keyboard hook to capture numeric key presses while overlay is visible
private const int WH_KEYBOARD_LL = 13;
private const int WM_KEYDOWN = 0x0100;
private const int WM_SYSKEYDOWN = 0x0104;
private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
private static LowLevelKeyboardProc? _keyboardProc;
private static IntPtr _hookId = IntPtr.Zero;
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll")]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
[DllImport("user32.dll")]
private static extern bool SetCursorPos(int X, int Y);
[DllImport("user32.dll")]
private static extern void mouse_event(uint dwFlags, int dx, int dy, uint dwData, int dwExtraInfo);
private const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
private const uint MOUSEEVENTF_LEFTUP = 0x0004;
public static void InitializeUIContext()
{
_uiContext = System.Threading.SynchronizationContext.Current;
}
public static bool IsVisible => _instance != null && !_instance.IsDisposed && _instance.Visible;
public static void ShowCandidates(List<VisualTargetCandidate> candidates, int timeoutMs)
{
try
{
NaturalCommands.Helpers.Logger.LogInfo($"ShowCandidates called with {candidates?.Count ?? 0} candidates, timeout={timeoutMs}ms");
if (_uiContext != null && System.Threading.SynchronizationContext.Current != _uiContext)
{
NaturalCommands.Helpers.Logger.LogInfo("ShowCandidates: posting to UI context");
_uiContext.Post(_ => ShowCandidates(candidates ?? new System.Collections.Generic.List<VisualTargetCandidate>(), timeoutMs), null);
return;
}
lock (_lock)
{
if (_instance == null || _instance.IsDisposed)
{
_instance = new VisualCandidateOverlayForm();
NaturalCommands.Helpers.Logger.LogInfo("ShowCandidates: created new form instance");
}
_instance._candidates = candidates ?? new List<VisualTargetCandidate>();
_instance._timeoutTimer.Stop();
_instance._timeoutTimer.Interval = Math.Max(1000, timeoutMs);
_instance._timeoutTimer.Start();
if (!_instance.Visible)
{
_instance.Show();
NaturalCommands.Helpers.Logger.LogInfo("ShowCandidates: form shown");
}
EnsureKeyboardHookInstalled();
_instance.BringToFront();
_instance.Invalidate();
NaturalCommands.Helpers.Logger.LogInfo("ShowCandidates: form brought to front and invalidated");
}
}
catch (Exception ex)
{
NaturalCommands.Helpers.Logger.LogError($"ShowCandidates exception: {ex.Message}\n{ex.StackTrace}");
}
}
public static void HideOverlay()
{
if (_uiContext != null && System.Threading.SynchronizationContext.Current != _uiContext)
{
_uiContext.Post(_ => HideOverlay(), null);
return;
}
lock (_lock)
{
if (_instance == null || _instance.IsDisposed)
{
return;
}
_instance._timeoutTimer.Stop();
_instance.Hide();
EnsureKeyboardHookRemoved();
}
}
private static void EnsureKeyboardHookInstalled()
{
try
{
if (_hookId != IntPtr.Zero) return;
_keyboardProc = KeyboardHookCallback;
using var curProcess = System.Diagnostics.Process.GetCurrentProcess();
using var curModule = curProcess.MainModule;
var moduleHandle = GetModuleHandle(curModule?.ModuleName ?? string.Empty);
_hookId = SetWindowsHookEx(WH_KEYBOARD_LL, _keyboardProc!, moduleHandle, 0);
Logger.LogDebug($"VisualCandidateOverlayForm: keyboard hook installed (hookId={_hookId})");
}
catch (Exception ex)
{
Logger.LogError($"VisualCandidateOverlayForm: failed to install keyboard hook: {ex.Message}");
}
}
private static void EnsureKeyboardHookRemoved()
{
try
{
if (_hookId == IntPtr.Zero) return;
UnhookWindowsHookEx(_hookId);
_hookId = IntPtr.Zero;
_keyboardProc = null;
Logger.LogDebug("VisualCandidateOverlayForm: keyboard hook removed");
}
catch (Exception ex)
{
Logger.LogError($"VisualCandidateOverlayForm: failed to remove keyboard hook: {ex.Message}");
}
}
private static IntPtr KeyboardHookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
try
{
if (nCode >= 0 && (wParam == (IntPtr)WM_KEYDOWN || wParam == (IntPtr)WM_SYSKEYDOWN))
{
int vkCode = Marshal.ReadInt32(lParam);
int num = -1;
// Top-row digits '0'..'9'
if (vkCode >= 0x30 && vkCode <= 0x39)
num = vkCode - 0x30;
// Numpad digits
else if (vkCode >= 0x60 && vkCode <= 0x69)
num = vkCode - 0x60;
// ESC -> cancel overlay
if (vkCode == 0x1B)
{
_instance?.BeginInvoke(new Action(() => HideOverlay()));
return (IntPtr)1;
}
if (num >= 1)
{
// Dispatch to instance to handle safe UI interaction
_instance?.BeginInvoke(new Action(() => _instance?.HandleNumberKey(num)));
// Swallow the key so underlying app doesn't receive it
return (IntPtr)1;
}
}
}
catch { }
return CallNextHookEx(_hookId, nCode, wParam, lParam);
}
private void HandleNumberKey(int number)
{
try
{
if (!Helpers.VisualCandidateSessionStore.TryGetCandidate(number, out var candidate) || candidate == null)
{
// no candidate for this number
Logger.LogDebug($"No visual candidate for number {number}");
return;
}
// Close/hide overlay first so the click reaches underlying window
HideOverlay();
System.Threading.Thread.Sleep(30);
var session = Helpers.VisualCandidateSessionStore.GetSession();
var point = Helpers.VisualTargetClickPointResolver.Resolve(candidate, session?.Query ?? string.Empty);
if (point == System.Drawing.Point.Empty)
point = candidate.Center;
var foregroundBounds = WindowUtils.GetForegroundWindowBounds();
if (foregroundBounds != System.Drawing.Rectangle.Empty && !foregroundBounds.Contains(point))
{
Logger.LogWarning($"Visual overlay: blocked choose {number} because point ({point.X},{point.Y}) is outside foreground window {foregroundBounds.Left},{foregroundBounds.Top},{foregroundBounds.Width}x{foregroundBounds.Height}.");
return;
}
Logger.LogInfo($"Visual overlay: keyed choose {number} -> click point ({point.X},{point.Y}) for '{candidate.Label}'");
System.Drawing.Point prev = System.Windows.Forms.Cursor.Position;
SetCursorPos(point.X, point.Y);
System.Threading.Thread.Sleep(35);
mouse_event(MOUSEEVENTF_LEFTDOWN, point.X, point.Y, 0, 0);
System.Threading.Thread.Sleep(20);
mouse_event(MOUSEEVENTF_LEFTUP, point.X, point.Y, 0, 0);
System.Threading.Thread.Sleep(70);
try { SetCursorPos(prev.X, prev.Y); } catch { }
Helpers.VisualCandidateSessionStore.Clear();
}
catch (Exception ex)
{
Logger.LogError($"HandleNumberKey error: {ex.Message}");
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
var graphics = e.Graphics;
graphics.SmoothingMode = SmoothingMode.AntiAlias;
int virtualX = SystemInformation.VirtualScreen.Left;
int virtualY = SystemInformation.VirtualScreen.Top;
int renderedIndex = 0;
for (int i = 0; i < _candidates.Count; i++)
{
var candidate = _candidates[i];
if (candidate.Bounds.Width <= 0 || candidate.Bounds.Height <= 0)
{
continue;
}
// Position marker near the candidate bounding rect top-right (more visually stable
// for grid-like tiles). Compute candidate rect relative to the overlay virtual origin.
var candidateRectInOverlay = new Rectangle(
candidate.Bounds.Left - virtualX,
candidate.Bounds.Top - virtualY,
candidate.Bounds.Width,
candidate.Bounds.Height);
if (candidateRectInOverlay.Right < 0
|| candidateRectInOverlay.Bottom < 0
|| candidateRectInOverlay.Left > Width
|| candidateRectInOverlay.Top > Height)
{
continue;
}
renderedIndex++;
int markerLeft = Math.Max(2, candidateRectInOverlay.Right - 28);
int markerTop = Math.Max(2, candidateRectInOverlay.Top + 6);
markerLeft = Math.Min(Math.Max(2, Width - 38), markerLeft);
markerTop = Math.Min(Math.Max(2, Height - 38), markerTop);
var markerRect = new Rectangle(markerLeft, markerTop, 36, 36);
// Draw candidate bounding rect for clarity
Color rectColor = string.Equals(candidate.Source, "uia", StringComparison.OrdinalIgnoreCase)
? Color.FromArgb(200, 0, 200, 0)
: Color.FromArgb(200, 34, 139, 230);
using var rectPen = new Pen(rectColor, 2) { DashStyle = DashStyle.Solid };
var outlineRect = candidateRectInOverlay;
// inflate slightly so the rectangle is visible
outlineRect.Inflate(2, 2);
graphics.DrawRectangle(rectPen, outlineRect);
// Draw label snippet near the marker
var label = candidate.Label ?? string.Empty;
var shortLabel = label.Length > 28 ? label.Substring(0, 25) + "..." : label;
var labelSize = graphics.MeasureString(shortLabel, _hintFont);
var labelBg = new RectangleF(markerRect.Right + 6, markerRect.Y - 2, labelSize.Width + 8, labelSize.Height + 4);
using var labelFill = new SolidBrush(Color.FromArgb(210, 0, 0, 0));
graphics.FillRectangle(labelFill, labelBg);
graphics.DrawString(shortLabel, _hintFont, Brushes.White, labelBg.X + 4, labelBg.Y + 2);
using var fill = new SolidBrush(Color.FromArgb(220, 34, 139, 230));
using var border = new Pen(Color.FromArgb(240, 255, 255, 255), 2);
graphics.FillEllipse(fill, markerRect);
graphics.DrawEllipse(border, markerRect);
string text = renderedIndex.ToString();
var textSize = graphics.MeasureString(text, _numberFont);
graphics.DrawString(text, _numberFont, Brushes.White,
markerRect.X + (markerRect.Width - textSize.Width) / 2,
markerRect.Y + (markerRect.Height - textSize.Height) / 2 - 1);
}
string instruction = "Say 'choose <number>' or press ESC to cancel";
var instructionSize = graphics.MeasureString(instruction, _hintFont);
var rect = new RectangleF((Width - instructionSize.Width) / 2 - 8, Height - 56, instructionSize.Width + 16, instructionSize.Height + 10);
using var bg = new SolidBrush(Color.FromArgb(190, 0, 0, 0));
graphics.FillRectangle(bg, rect);
graphics.DrawString(instruction, _hintFont, Brushes.White, rect.X + 8, rect.Y + 5);
}
protected override bool ShowWithoutActivation => true;
protected override CreateParams CreateParams
{
get
{
var cp = base.CreateParams;
cp.ExStyle |= WS_EX_NOACTIVATE;
cp.ExStyle |= WS_EX_TRANSPARENT;
cp.ExStyle |= WS_EX_TOOLWINDOW;
return cp;
}
}
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_NCHITTEST)
{
m.Result = (IntPtr)HTTRANSPARENT;
return;
}
base.WndProc(ref m);
}
}
}