forked from sourcegit-scm/sourcegit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeDataGridSelectionModel.cs
More file actions
462 lines (405 loc) · 16.5 KB
/
Copy pathTreeDataGridSelectionModel.cs
File metadata and controls
462 lines (405 loc) · 16.5 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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
using System;
using System.Collections;
using System.Collections.Generic;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Models.TreeDataGrid;
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Selection;
using Avalonia.Input;
namespace SourceGit.Models
{
public class TreeDataGridSelectionModel<TModel> : TreeSelectionModelBase<TModel>,
ITreeDataGridRowSelectionModel<TModel>,
ITreeDataGridSelectionInteraction
where TModel : class
{
private static readonly Point s_InvalidPoint = new(double.NegativeInfinity, double.NegativeInfinity);
private readonly ITreeDataGridSource<TModel> _source;
private EventHandler _viewSelectionChanged;
private EventHandler _rowDoubleTapped;
private Point _pressedPoint = s_InvalidPoint;
private bool _raiseViewSelectionChanged;
private Func<TModel, IEnumerable<TModel>> _childrenGetter;
public TreeDataGridSelectionModel(ITreeDataGridSource<TModel> source, Func<TModel, IEnumerable<TModel>> childrenGetter)
: base(source.Items)
{
_source = source;
_childrenGetter = childrenGetter;
SelectionChanged += (s, e) =>
{
if (!IsSourceCollectionChanging)
_viewSelectionChanged?.Invoke(this, e);
else
_raiseViewSelectionChanged = true;
};
}
public void Select(IEnumerable<TModel> items)
{
using (BatchUpdate())
{
Clear();
foreach (var selected in items)
{
var idx = GetModelIndex(_source.Items, selected, IndexPath.Unselected);
if (!idx.Equals(IndexPath.Unselected))
Select(idx);
}
}
}
event EventHandler ITreeDataGridSelectionInteraction.SelectionChanged
{
add => _viewSelectionChanged += value;
remove => _viewSelectionChanged -= value;
}
public event EventHandler RowDoubleTapped
{
add => _rowDoubleTapped += value;
remove => _rowDoubleTapped -= value;
}
IEnumerable ITreeDataGridSelection.Source
{
get => Source;
set => Source = value;
}
bool ITreeDataGridSelectionInteraction.IsRowSelected(IRow rowModel)
{
if (rowModel is IModelIndexableRow indexable)
return IsSelected(indexable.ModelIndexPath);
return false;
}
bool ITreeDataGridSelectionInteraction.IsRowSelected(int rowIndex)
{
if (rowIndex >= 0 && rowIndex < _source.Rows.Count)
{
if (_source.Rows[rowIndex] is IModelIndexableRow indexable)
return IsSelected(indexable.ModelIndexPath);
}
return false;
}
void ITreeDataGridSelectionInteraction.OnKeyDown(TreeDataGrid sender, KeyEventArgs e)
{
if (sender.RowsPresenter is null)
return;
if (!e.Handled)
{
var ctrl = e.KeyModifiers.HasFlag(KeyModifiers.Control);
if (e.Key == Key.A && ctrl && !SingleSelect)
{
using (BatchUpdate())
{
Clear();
int num = _source.Rows.Count;
for (int i = 0; i < num; ++i)
{
var m = _source.Rows.RowIndexToModelIndex(i);
Select(m);
}
}
e.Handled = true;
}
var direction = e.Key.ToNavigationDirection();
var shift = e.KeyModifiers.HasFlag(KeyModifiers.Shift);
if (direction.HasValue)
{
var anchorRowIndex = _source.Rows.ModelIndexToRowIndex(AnchorIndex);
sender.RowsPresenter.BringIntoView(anchorRowIndex);
var anchor = sender.TryGetRow(anchorRowIndex);
if (anchor is not null && !ctrl)
{
e.Handled = TryKeyExpandCollapse(sender, direction.Value, anchor);
}
if (!e.Handled && (!ctrl || shift))
{
e.Handled = MoveSelection(sender, direction.Value, shift, anchor);
}
if (!e.Handled && direction == NavigationDirection.Left
&& anchor?.Rows is HierarchicalRows<TModel> hierarchicalRows && anchorRowIndex > 0)
{
var newIndex = hierarchicalRows.GetParentRowIndex(AnchorIndex);
UpdateSelection(sender, newIndex, true);
FocusRow(sender, sender.RowsPresenter.BringIntoView(newIndex));
}
if (!e.Handled && direction == NavigationDirection.Right
&& anchor?.Rows is HierarchicalRows<TModel> hierarchicalRows2 && hierarchicalRows2[anchorRowIndex].IsExpanded)
{
var newIndex = anchorRowIndex + 1;
UpdateSelection(sender, newIndex, true);
sender.RowsPresenter.BringIntoView(newIndex);
}
}
}
}
void ITreeDataGridSelectionInteraction.OnPointerPressed(TreeDataGrid sender, PointerPressedEventArgs e)
{
if (!e.Handled &&
e.Pointer.Type == PointerType.Mouse &&
e.Source is Control source &&
sender.TryGetRow(source, out var row) &&
_source.Rows.RowIndexToModelIndex(row.RowIndex) is { } modelIndex)
{
if (!IsSelected(modelIndex))
{
PointerSelect(sender, row, e);
_pressedPoint = s_InvalidPoint;
}
else
{
var point = e.GetCurrentPoint(sender);
if (point.Properties.IsRightButtonPressed)
{
_pressedPoint = s_InvalidPoint;
return;
}
if (e.KeyModifiers == KeyModifiers.Control)
{
Deselect(modelIndex);
}
else if (e.ClickCount % 2 == 0)
{
var focus = _source.Rows[row.RowIndex];
if (focus is IExpander expander && HasChildren(focus))
expander.IsExpanded = !expander.IsExpanded;
else
_rowDoubleTapped?.Invoke(this, e);
e.Handled = true;
}
else if (sender.RowSelection.Count > 1)
{
using (BatchUpdate())
{
Clear();
Select(modelIndex);
}
}
_pressedPoint = s_InvalidPoint;
}
}
else
{
if (!sender.TryGetRow(e.Source as Control, out var test))
Clear();
_pressedPoint = e.GetPosition(sender);
}
}
void ITreeDataGridSelectionInteraction.OnPointerReleased(TreeDataGrid sender, PointerReleasedEventArgs e)
{
if (!e.Handled &&
_pressedPoint != s_InvalidPoint &&
e.Source is Control source &&
sender.TryGetRow(source, out var row) &&
_source.Rows.RowIndexToModelIndex(row.RowIndex) is { } modelIndex)
{
if (!IsSelected(modelIndex))
{
var p = e.GetPosition(sender);
if (Math.Abs(p.X - _pressedPoint.X) <= 3 || Math.Abs(p.Y - _pressedPoint.Y) <= 3)
PointerSelect(sender, row, e);
}
}
}
protected override void OnSourceCollectionChangeFinished()
{
if (_raiseViewSelectionChanged)
{
_viewSelectionChanged?.Invoke(this, EventArgs.Empty);
_raiseViewSelectionChanged = false;
}
}
private void PointerSelect(TreeDataGrid sender, TreeDataGridRow row, PointerEventArgs e)
{
var point = e.GetCurrentPoint(sender);
var commandModifiers = TopLevel.GetTopLevel(sender)?.PlatformSettings?.HotkeyConfiguration.CommandModifiers;
var toggleModifier = commandModifiers is not null && e.KeyModifiers.HasFlag(commandModifiers);
var isRightButton = point.Properties.PointerUpdateKind is PointerUpdateKind.RightButtonPressed or
PointerUpdateKind.RightButtonReleased;
UpdateSelection(
sender,
row.RowIndex,
select: true,
rangeModifier: e.KeyModifiers.HasFlag(KeyModifiers.Shift),
toggleModifier: toggleModifier,
rightButton: isRightButton);
e.Handled = true;
}
private void UpdateSelection(TreeDataGrid treeDataGrid, int rowIndex, bool select = true, bool rangeModifier = false, bool toggleModifier = false, bool rightButton = false)
{
var modelIndex = _source.Rows.RowIndexToModelIndex(rowIndex);
if (modelIndex == default)
return;
var mode = SingleSelect ? SelectionMode.Single : SelectionMode.Multiple;
var multi = (mode & SelectionMode.Multiple) != 0;
var toggle = (toggleModifier || (mode & SelectionMode.Toggle) != 0);
var range = multi && rangeModifier;
if (!select)
{
if (IsSelected(modelIndex) && !treeDataGrid.QueryCancelSelection())
Deselect(modelIndex);
}
else if (rightButton)
{
if (IsSelected(modelIndex) == false && !treeDataGrid.QueryCancelSelection())
SelectedIndex = modelIndex;
}
else if (range)
{
if (!treeDataGrid.QueryCancelSelection())
{
var anchor = RangeAnchorIndex;
var i = Math.Max(_source.Rows.ModelIndexToRowIndex(anchor), 0);
var step = i < rowIndex ? 1 : -1;
using (BatchUpdate())
{
Clear();
while (true)
{
var m = _source.Rows.RowIndexToModelIndex(i);
Select(m);
anchor = m;
if (i == rowIndex)
break;
i += step;
}
}
}
}
else if (multi && toggle)
{
if (!treeDataGrid.QueryCancelSelection())
{
if (IsSelected(modelIndex) == true)
Deselect(modelIndex);
else
Select(modelIndex);
}
}
else if (toggle)
{
if (!treeDataGrid.QueryCancelSelection())
SelectedIndex = (SelectedIndex == modelIndex) ? -1 : modelIndex;
}
else if (SelectedIndex != modelIndex || Count > 1)
{
if (!treeDataGrid.QueryCancelSelection())
SelectedIndex = modelIndex;
}
}
private bool TryKeyExpandCollapse(TreeDataGrid treeDataGrid, NavigationDirection direction, TreeDataGridRow focused)
{
if (treeDataGrid.RowsPresenter is null || focused.RowIndex < 0)
return false;
var row = _source.Rows[focused.RowIndex];
if (row is IExpander expander)
{
if (direction == NavigationDirection.Right && !expander.IsExpanded)
{
expander.IsExpanded = true;
return true;
}
else if (direction == NavigationDirection.Left && expander.IsExpanded)
{
expander.IsExpanded = false;
return true;
}
}
return false;
}
private bool MoveSelection(TreeDataGrid treeDataGrid, NavigationDirection direction, bool rangeModifier, TreeDataGridRow focused)
{
if (treeDataGrid.RowsPresenter is null || _source.Columns.Count == 0 || _source.Rows.Count == 0)
return false;
var currentRowIndex = focused?.RowIndex ?? _source.Rows.ModelIndexToRowIndex(SelectedIndex);
int newRowIndex;
if (direction == NavigationDirection.First || direction == NavigationDirection.Last)
{
newRowIndex = direction == NavigationDirection.First ? 0 : _source.Rows.Count - 1;
}
else
{
(var x, var y) = direction switch
{
NavigationDirection.Up => (0, -1),
NavigationDirection.Down => (0, 1),
NavigationDirection.Left => (-1, 0),
NavigationDirection.Right => (1, 0),
_ => (0, 0)
};
newRowIndex = Math.Max(0, Math.Min(currentRowIndex + y, _source.Rows.Count - 1));
}
if (newRowIndex != currentRowIndex)
UpdateSelection(treeDataGrid, newRowIndex, true, rangeModifier);
if (newRowIndex != currentRowIndex)
{
treeDataGrid.RowsPresenter?.BringIntoView(newRowIndex);
FocusRow(treeDataGrid, treeDataGrid.TryGetRow(newRowIndex));
return true;
}
else
{
return false;
}
}
private static void FocusRow(TreeDataGrid owner, Control control)
{
if (!owner.TryGetRow(control, out var row) || row.CellsPresenter is null)
return;
// Get the column index of the currently focused cell if possible: we'll try to focus the
// same column in the new row.
if (TopLevel.GetTopLevel(owner)?.FocusManager is { } focusManager &&
focusManager.GetFocusedElement() is Control currentFocus &&
owner.TryGetCell(currentFocus, out var currentCell) &&
row.TryGetCell(currentCell.ColumnIndex) is { } newCell &&
newCell.Focusable)
{
newCell.Focus();
}
else
{
// Otherwise, just focus the first focusable cell in the row.
foreach (var cell in row.CellsPresenter.GetRealizedElements())
{
if (cell.Focusable)
{
cell.Focus();
break;
}
}
}
}
protected override IEnumerable<TModel> GetChildren(TModel node)
{
if (node == null)
return null;
return _childrenGetter?.Invoke(node);
}
private IndexPath GetModelIndex(IEnumerable<TModel> collection, TModel model, IndexPath parent)
{
int i = 0;
foreach (var item in collection)
{
var index = parent.Append(i);
if (item != null && item == model)
return index;
var children = GetChildren(item);
if (children != null)
{
var findInChildren = GetModelIndex(children, model, index);
if (!findInChildren.Equals(IndexPath.Unselected))
return findInChildren;
}
i++;
}
return IndexPath.Unselected;
}
private bool HasChildren(IRow row)
{
var children = GetChildren(row.Model as TModel);
if (children != null)
{
foreach (var c in children)
return true;
}
return false;
}
}
}