-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainController.cs
More file actions
238 lines (210 loc) · 8.53 KB
/
MainController.cs
File metadata and controls
238 lines (210 loc) · 8.53 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
using System;
using System.Drawing;
using System.Windows;
using TheGriddler.Models;
using TheGriddler.Views;
namespace TheGriddler.Core;
public class MainController : IDisposable
{
private GlobalHook _hook;
private Settings _settings;
private GridOverlay? _overlay;
private IntPtr _targetHWnd;
private bool _isDragging;
private bool _isLButtonDown;
private bool _suppressRightUp;
public MainController()
{
_settings = Settings.Instance;
_hook = new GlobalHook();
// Initialize state to avoid missing the first drag if app started while button was down
_isLButtonDown = (NativeMethods.GetAsyncKeyState(NativeMethods.VK_LBUTTON) & 0x8000) != 0;
_hook.LeftButtonDown += OnLeftButtonDown;
_hook.RightButtonDown += OnRightButtonDown;
_hook.MouseMoved += OnMouseMoved;
}
private void OnMouseMoved(System.Drawing.Point pos)
{
if (_isDragging && _overlay != null)
{
// Pass raw screen coordinates (physical pixels)
// GridOverlay handles DPI and offset conversion internally
_overlay.UpdateMouse(new System.Windows.Point(pos.X, pos.Y));
}
}
private void OnLeftButtonDown(bool down)
{
if (_isLButtonDown != down)
{
_isLButtonDown = down;
}
if (!down && _isDragging)
{
SnapAndClose();
}
}
private bool HandleRightClick()
{
var sw = System.Diagnostics.Stopwatch.StartNew();
Logger.Log($"HandleRightClick: START");
// Re-check left button state using GetAsyncKeyState to be absolutely sure we aren't out of sync
bool physicalLButtonDown = (NativeMethods.GetAsyncKeyState(NativeMethods.VK_LBUTTON) & 0x8000) != 0;
if (_isLButtonDown != physicalLButtonDown)
{
Logger.Log($"HandleRightClick: LButton state corrected from {_isLButtonDown} to {physicalLButtonDown}");
_isLButtonDown = physicalLButtonDown;
}
if (_isLButtonDown && !_isDragging)
{
var cursorPosition = System.Windows.Forms.Cursor.Position;
IntPtr target = WindowManager.GetTargetWindow(new System.Drawing.Point(cursorPosition.X, cursorPosition.Y));
Logger.Log($"HandleRightClick: GetTargetWindow returned {target:X} at {sw.ElapsedMilliseconds}ms");
if (target != IntPtr.Zero)
{
// Verify if the window is truly in a move/size loop
uint processId;
uint threadId = NativeMethods.GetWindowThreadProcessId(target, out processId);
NativeMethods.GUITHREADINFO guiInfo = new NativeMethods.GUITHREADINFO();
guiInfo.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(guiInfo);
if (NativeMethods.GetGUIThreadInfo(threadId, ref guiInfo))
{
// Check if flags indicate move/size OR if hwndMoveSize is set (either/or check is robust)
bool isMoving = (guiInfo.flags & NativeMethods.GUI_INMOVESIZE) != 0 || guiInfo.hwndMoveSize != IntPtr.Zero;
Logger.Log($"HandleRightClick: GUI_INMOVESIZE check - flags=0x{guiInfo.flags:X}, hwndMoveSize={guiInfo.hwndMoveSize:X}, hwndCapture={guiInfo.hwndCapture:X}, isMoving={isMoving} at {sw.ElapsedMilliseconds}ms");
if (!isMoving)
{
// Not dragging/sizing -> fail gracefully (pass through right click)
Logger.Log($"HandleRightClick: Window not in move/size mode, passing through");
return false;
}
}
else
{
Logger.Log($"HandleRightClick: GetGUIThreadInfo FAILED for threadId={threadId}");
}
// Break drag loop IMMEDIATELY on the hook thread
Logger.Log($"HandleRightClick: Calling BreakDragLoop at {sw.ElapsedMilliseconds}ms");
WindowManager.BreakDragLoop(target);
Logger.Log($"HandleRightClick: BreakDragLoop completed at {sw.ElapsedMilliseconds}ms");
// Verify the drag was actually broken
NativeMethods.GUITHREADINFO postBreakInfo = new NativeMethods.GUITHREADINFO();
postBreakInfo.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(postBreakInfo);
if (NativeMethods.GetGUIThreadInfo(threadId, ref postBreakInfo))
{
bool stillMoving = (postBreakInfo.flags & NativeMethods.GUI_INMOVESIZE) != 0 || postBreakInfo.hwndMoveSize != IntPtr.Zero;
Logger.Log($"HandleRightClick: POST-BREAK check - flags=0x{postBreakInfo.flags:X}, hwndMoveSize={postBreakInfo.hwndMoveSize:X}, stillMoving={stillMoving}");
if (stillMoving)
{
Logger.Log($"HandleRightClick: WARNING - BreakDragLoop did NOT cancel the native drag!");
}
}
Logger.Log($"HandleRightClick: Calling ActivateGrid at {sw.ElapsedMilliseconds}ms");
ActivateGrid(target, cursorPosition);
Logger.Log($"HandleRightClick: ActivateGrid call returned at {sw.ElapsedMilliseconds}ms (async work continues)");
// We are now technically "dragging" (activating).
// Any following right-clicks should be blocked too.
return true;
}
}
else if (_isDragging)
{
// If we are already dragging/activating, we ALWAYS handle (block) the right-click
if (_overlay != null)
{
if (!_overlay.IsSelecting)
{
var cursorPosition = System.Windows.Forms.Cursor.Position;
_overlay.StartSelection(new System.Windows.Point(cursorPosition.X, cursorPosition.Y));
}
else
{
SnapAndClose();
}
}
return true;
}
return false;
}
private bool OnRightButtonDown(bool down)
{
if (down)
{
if (HandleRightClick())
{
_suppressRightUp = true;
return true;
}
}
else
{
// Always check _suppressRightUp first
if (_suppressRightUp)
{
_suppressRightUp = false;
return true;
}
// If we are dragging, we should also block the RightUp even if suppress was missed somehow
if (_isDragging) return true;
}
return false;
}
private async void ActivateGrid(IntPtr target, System.Drawing.Point startPoint)
{
try
{
_targetHWnd = target;
_isDragging = true;
// Offload the heavy work (WPF window creation) to ensure we don't block the UI thread too much
await System.Windows.Application.Current.Dispatcher.InvokeAsync(() =>
{
// Ensure restored BEFORE showing overlay
WindowManager.EnsureRestored(_targetHWnd);
_overlay = new GridOverlay(_settings, _targetHWnd, startPoint);
_overlay.Show();
// Immediately start selection from the captured cursor position
_overlay.StartSelection(new System.Windows.Point(startPoint.X, startPoint.Y));
});
}
catch (Exception ex)
{
Logger.Log($"Error activating grid: {ex.Message}");
_isDragging = false;
}
}
private void SnapAndClose()
{
if (_overlay != null)
{
try
{
if (_overlay.IsSelecting)
{
_overlay.Snap(final: true);
}
}
catch (Exception ex)
{
Logger.Log($"Error during snap: {ex.Message}");
}
finally
{
_overlay.Close();
_overlay = null;
}
}
_isDragging = false;
}
private void CloseOverlay()
{
if (_overlay != null)
{
try { _overlay.Close(); } catch { }
_overlay = null;
}
}
public void Dispose()
{
_hook.Dispose();
CloseOverlay();
}
}