-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathDockingDragManager.cs
More file actions
312 lines (278 loc) · 9.7 KB
/
Copy pathDockingDragManager.cs
File metadata and controls
312 lines (278 loc) · 9.7 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
#region BSD License
/*
*
* Original BSD 3-Clause License (https://github.com/ComponentFactory/Krypton/blob/master/LICENSE)
* © Component Factory Pty Ltd, 2006 - 2016, (Version 4.5.0.0) All rights reserved.
*
* New BSD 3-Clause License (https://github.com/Krypton-Suite/Standard-Toolkit/blob/master/LICENSE)
* Modifications by Peter Wagner (aka Wagnerp), Simon Coghlan (aka Smurf-IV), Giduac, Ahmed Abdelhameed, tobitege, KamaniAR, Lesandro Gotardo (aka lesandrog), Jorge A. Avilés (aka mcpbcs) et al. 2017 - 2026. All rights reserved.
*
*/
#endregion
using Timer = System.Windows.Forms.Timer;
namespace Krypton.Docking;
/// <summary>
/// Docking-specific drag manager: tracks a <see cref="KryptonFloatingWindow"/> during page moves,
/// filters Win32 messages for float-window caption drag, and clears temporary store pages on dispose.
/// </summary>
public class DockingDragManager : DragManager,
IFloatingMessages,
IMessageFilter
{
#region Instance Fields
private readonly KryptonDockingManager _manager;
private Point _offset;
private Point _screenPt;
private readonly Timer _moveTimer;
private bool _addedFilter;
private bool _monitorMouse;
#endregion
#region Identity
/// <summary>
/// Initialize a new instance of the DockingDragManager class.
/// </summary>
/// <param name="manager">Reference to manager creating this instance.</param>
/// <param name="c">Control that is starting the drag operation.</param>
public DockingDragManager(KryptonDockingManager manager, Control? c)
{
_manager = manager;
_offset = Point.Empty;
// Timer is kept for backward compatibility but no longer used for position updates
// Position updates now happen immediately in DragMove for responsive dragging
// Use timer to ensure we do not update the display too quickly which then causes tearing
// TODO: Remove in a future version
_moveTimer = new Timer
{
Interval = 10
};
_moveTimer.Tick += OnFloatingWindowMove;
}
/// <summary>
/// Release unmanaged and optionally managed resources.
/// </summary>
/// <param name="disposing">Called from Dispose method.</param>
protected override void Dispose(bool disposing)
{
RemoveFilter();
// Remove any temporary pages created during the dragging process that are used to prevent cells being removed
_manager.PropogateAction(DockingPropogateAction.ClearStoredPages, new[] { "TemporaryPage" });
// Remember to unhook event and dispose timer to prevent resource leak
_moveTimer.Tick -= OnFloatingWindowMove;
_moveTimer.Stop();
_moveTimer.Dispose();
base.Dispose(disposing);
}
#endregion
#region Public
/// <summary>
/// Gets and sets the window that is moved in sync with the mouse movement.
/// </summary>
public KryptonFloatingWindow? FloatingWindow { get; set; }
/// <summary>
/// Gets and sets the offset of the floating window from the screen cursor.
/// </summary>
public Point FloatingWindowOffset
{
get => _offset;
set => _offset = value;
}
/// <summary>
/// Occurs when dragging starts.
/// </summary>
/// <param name="screenPt">Mouse screen point at start of drag.</param>
/// <param name="dragEndData">Data to be dropped at destination.</param>
/// <returns>True if dragging was started; otherwise false.</returns>
public override bool DragStart(Point screenPt, PageDragEndData? dragEndData)
{
if (FloatingWindow != null)
{
FloatingWindow.Capture = true;
}
AddFilter();
return base.DragStart(screenPt, dragEndData);
}
/// <summary>
/// Occurs on dragging movement.
/// </summary>
/// <param name="screenPt">Latest screen point during dragging.</param>
public override void DragMove(Point screenPt)
{
if (FloatingWindow != null)
{
_screenPt = screenPt;
// Update position immediately for responsive dragging
UpdateFloatingWindowPosition();
}
base.DragMove(screenPt);
}
private void UpdateFloatingWindowPosition()
{
// Position the floating window relative to the screen position
if (FloatingWindow != null)
{
if (_offset.X > (FloatingWindow.Width - 20))
{
_offset.X = FloatingWindow.Width - 20;
}
if (_offset.Y > (FloatingWindow.Height - 20))
{
_offset.Y = FloatingWindow.Height - 20;
}
FloatingWindow.SetBounds(_screenPt.X - FloatingWindowOffset.X,
_screenPt.Y - FloatingWindowOffset.Y,
FloatingWindow.Width,
FloatingWindow.Height,
BoundsSpecified.Location);
}
}
private void OnFloatingWindowMove(object? sender, EventArgs e)
{
_moveTimer.Stop();
// This method is no longer used but kept for backward compatibility
// Position updates now happen immediately in DragMove
}
/// <summary>
/// Occurs when dragging ends because of dropping.
/// </summary>
/// <param name="screenPt">Ending screen point when dropping.</param>
/// <returns>Drop was performed and the source can perform any removal of pages as required.</returns>
public override bool DragEnd(Point screenPt)
{
RemoveFilter();
var ret = base.DragEnd(screenPt);
_manager.RaiseDoDragDropEnd(EventArgs.Empty);
return ret;
}
/// <summary>
/// Occurs when dragging quits.
/// </summary>
public override void DragQuit()
{
RemoveFilter();
base.DragQuit();
_manager.RaiseDoDragDropQuit(EventArgs.Empty);
}
/// <summary>
/// Processes the WM_KEYDOWN from the floating window.
/// </summary>
/// <returns>True to eat message; otherwise false.</returns>
public bool OnKEYDOWN(ref Message m)
{
// Pressing escape ends dragging
if ((int)m.WParam.ToInt64() == (int)Keys.Escape)
{
DragQuit();
Dispose();
return true;
}
return false;
}
/// <summary>
/// Processes the WM_MOUSEMOVE from the floating window.
/// </summary>
public void OnMOUSEMOVE() =>
// Update feedback to reflect the current mouse position
DragMove(Control.MousePosition);
/// <summary>
/// Processes the WM_LBUTTONUP from the floating window.
/// </summary>
public void OnLBUTTONUP()
{
DragEnd(Control.MousePosition);
Dispose();
}
/// <summary>
/// Filters out a message before it is dispatched.
/// </summary>
/// <param name="m">The message to be dispatched.</param>
/// <returns>true to filter the message and stop it from being dispatched.</returns>
public bool PreFilterMessage(ref Message m)
{
switch (m.Msg)
{
case PI.WM_.MOUSELEAVE:
// Only interested in the mouse leave if it relates to the floating window and so ignore any
// message that comes from the mouse leaving the source of a drag such as a docked window or
// a workspace/navigator tab.
if (m.HWnd == FloatingWindow?.Handle)
{
// If mouse has left then we need to finish with docking. Most likely reason is the focus
// has been shifted to another application with ALT+TAB.
DragEnd(Control.MousePosition);
Dispose();
}
break;
case PI.WM_.KEYDOWN:
{
// Extract the keys being pressed
var keys = (Keys)(int)m.WParam.ToInt64();
// Pressing escape ends dragging
if (keys == Keys.Escape)
{
DragQuit();
Dispose();
}
return true;
}
case PI.WM_.SYSKEYDOWN:
// Pressing ALT+TAB ends dragging because user is moving to another app
if (PI.IsKeyDown(Keys.Tab)
&& ((Control.ModifierKeys & Keys.Alt) == Keys.Alt)
)
{
DragQuit();
Dispose();
}
break;
case PI.WM_.MOUSEMOVE:
if (_monitorMouse)
{
// Update feedback to reflect the current mouse position
DragMove(Control.MousePosition);
}
break;
case PI.WM_.LBUTTONUP:
if (_monitorMouse)
{
DragEnd(Control.MousePosition);
Dispose();
}
break;
}
return false;
}
#endregion
#region Implementation
private void AddFilter()
{
if (FloatingWindow != null)
{
_monitorMouse = false;
FloatingWindow.FloatingMessages = this;
}
else
{
_monitorMouse = true;
}
// We always monitor for keyboard events and sometimes mouse events
if (!_addedFilter)
{
Application.AddMessageFilter(this);
_addedFilter = true;
}
}
private void RemoveFilter()
{
if (FloatingWindow != null)
{
FloatingWindow.FloatingMessages = null;
}
// Must remove filter to prevent memory leaks
if (_addedFilter)
{
Application.RemoveMessageFilter(this);
_addedFilter = false;
}
}
#endregion
}