-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathTableViewRow.cs
More file actions
657 lines (577 loc) · 20.6 KB
/
Copy pathTableViewRow.cs
File metadata and controls
657 lines (577 loc) · 20.6 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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Media.Animation;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using Windows.Foundation;
using WinUI.TableView.Extensions;
using WinUI.TableView.Helpers;
namespace WinUI.TableView;
/// <summary>
/// Represents a row in a TableView.
/// </summary>
#if WINDOWS
[WinRT.GeneratedBindableCustomProperty]
#endif
public partial class TableViewRow : ListViewItem
{
private const string Selection_Background = "SelectionBackground";
private const double Selection_IndicatorHeight = 16d;
private const string Check_Mark = "\uE73E";
private Thickness _focusVisualMargin = new(1);
private readonly Thickness _selectionBackgroundMargin = new(4, 2, 4, 2);
private readonly Thickness _selectionIndicatorMargin = new(4, 0, 0, 0);
private TableView? _tableView;
private ListViewItemPresenter? _itemPresenter;
private Border? _selectionBackground;
private bool _ensureCells = true;
private Brush? _cellPresenterBackground;
private Brush? _cellPresenterForeground;
/// <summary>
/// Initializes a new instance of the TableViewRow class.
/// </summary>
public TableViewRow()
{
DefaultStyleKey = typeof(TableViewRow);
SizeChanged += OnSizeChanged;
Loaded += TableViewRow_Loaded;
#if WINDOWS
ContextRequested += OnContextRequested;
RegisterPropertyChangedCallback(IsSelectedProperty, delegate { OnIsSelectedChanged(); });
#endif
RegisterPropertyChangedCallback(ForegroundProperty, delegate { OnForegroundChanged(); });
RegisterPropertyChangedCallback(BackgroundProperty, delegate { OnBackgroundChanged(); });
}
#if !WINDOWS
/// <inheritdoc/>
protected override void OnRightTapped(RightTappedRoutedEventArgs e)
{
base.OnRightTapped(e);
var position = e.GetPosition(this);
#else
/// <summary>
/// Handles the ContextRequested event.
/// </summary>
private void OnContextRequested(UIElement sender, ContextRequestedEventArgs e)
{
if (!e.TryGetPosition(sender, out var position)) return;
#endif
e.Handled = TableView?.ShowRowContext(this, position) is true;
}
#if WINDOWS
/// <summary>
/// Handles the IsSelected property changed.
/// </summary>
private void OnIsSelectedChanged()
{
EnsureLayout();
RowPresenter?.SetRowDetailsVisibility();
}
#endif
/// <summary>
/// Handles the Foreground property changed.
/// </summary>
private void OnForegroundChanged()
{
_cellPresenterForeground = Foreground;
EnsureAlternateColors();
}
/// <summary>
/// Handles the Background property changed.
/// </summary>
private void OnBackgroundChanged()
{
_cellPresenterBackground = Background;
EnsureAlternateColors();
}
/// <summary>
/// Handles the Loaded event.
/// </summary>
private void TableViewRow_Loaded(object sender, RoutedEventArgs e)
{
_focusVisualMargin = FocusVisualMargin;
RowPresenter?.EnsureGridLines();
EnsureLayout();
}
/// <inheritdoc/>
protected override void OnApplyTemplate()
{
base.OnApplyTemplate();
_cellPresenterBackground = Background;
_cellPresenterForeground = Foreground;
_itemPresenter = GetTemplateChild("Root") as ListViewItemPresenter;
#if !WINDOWS
RowPresenter = GetTemplateChild("RowPresenter") as TableViewRowPresenter;
_selectionBackground = GetTemplateChild("SelectionBackground") as Border;
#endif
}
/// <inheritdoc/>
protected override void OnContentChanged(object oldContent, object newContent)
{
base.OnContentChanged(oldContent, newContent);
if (_ensureCells)
{
EnsureCells();
}
else
{
foreach (var cell in Cells)
{
cell.RefreshElement();
}
}
RowPresenter?.InvalidateMeasure(); // The cells presenter does not measure every time.
_tableView?.EnsureAlternateRowColors();
}
/// <inheritdoc/>
protected override void OnPointerPressed(PointerRoutedEventArgs e)
{
if (TableView is { IsEditing: false })
{
base.OnPointerPressed(e);
}
if (!KeyboardHelper.IsShiftKeyDown() && TableView is not null)
{
TableView.SelectionStartRowIndex = Index;
}
}
/// <inheritdoc/>
protected override void OnPointerReleased(PointerRoutedEventArgs e)
{
base.OnPointerReleased(e);
if (!KeyboardHelper.IsShiftKeyDown() && TableView is not null)
{
TableView.SelectionStartCellSlot = null;
TableView.SelectionStartRowIndex = Index;
}
}
/// <inheritdoc/>
protected override void OnTapped(TappedRoutedEventArgs e)
{
base.OnTapped(e);
if (TableView?.SelectionUnit is TableViewSelectionUnit.Row or TableViewSelectionUnit.CellOrRow)
{
TableView.CurrentRowIndex = Index;
TableView.LastSelectionUnit = TableViewSelectionUnit.Row;
}
}
/// <inheritdoc/>
protected override void OnDoubleTapped(DoubleTappedRoutedEventArgs e)
{
var eventArgs = new TableViewRowDoubleTappedEventArgs(Index, this, Content);
TableView?.OnRowDoubleTapped(eventArgs);
e.Handled = eventArgs.Handled;
base.OnDoubleTapped(e);
}
/// <inheritdoc/>
protected override Size ArrangeOverride(Size finalSize)
{
finalSize = base.ArrangeOverride(finalSize);
var cornerRadius = _itemPresenter?.CornerRadius ?? new();
var left = Math.Max(cornerRadius.TopLeft, cornerRadius.BottomLeft);
_itemPresenter?.Arrange(new Rect(-left, 0, _itemPresenter.ActualWidth + left, _itemPresenter.ActualHeight));
return finalSize;
}
/// <summary>
/// Ensures cells are created for the row.
/// </summary>
internal void EnsureCells()
{
if (TableView is null)
{
return;
}
if (RowPresenter is not null && _ensureCells)
{
RowPresenter.ClearCells();
AddCells(TableView.Columns.VisibleColumns);
_ensureCells = false;
}
}
/// <summary>
/// Handles the SizeChanged event.
/// </summary>
private async void OnSizeChanged(object sender, SizeChangedEventArgs e)
{
if (TableView?.CurrentCellSlot?.Row == Index)
{
_ = await TableView.ScrollCellIntoView(TableView.CurrentCellSlot.Value);
}
}
/// <summary>
/// Handles the collection changed event for the columns.
/// </summary>
private void OnColumnsCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add && e.NewItems?.OfType<TableViewColumn>() is IEnumerable<TableViewColumn> newItems)
{
AddCells(newItems.Where(x => x.Visibility == Visibility.Visible));
}
else if (e.Action == NotifyCollectionChangedAction.Remove && e.OldItems?.OfType<TableViewColumn>() is IEnumerable<TableViewColumn> oldItems)
{
RemoveCells(oldItems);
}
else if (e.Action == NotifyCollectionChangedAction.Move && e.NewItems?.Count > 0)
{
RowPresenter?.MoveCells(e.NewItems.OfType<TableViewColumn>().First(), e.NewStartingIndex);
}
else if (e.Action == NotifyCollectionChangedAction.Reset && RowPresenter is not null)
{
RowPresenter.ClearCells();
}
}
/// <summary>
/// Handles the property changed event for a column.
/// </summary>
private void OnColumnPropertyChanged(object? sender, TableViewColumnPropertyChangedEventArgs e)
{
if (e.PropertyName is nameof(TableViewColumn.Visibility))
{
if (e.Column.Visibility == Visibility.Visible)
{
AddCells([e.Column]);
}
else
{
RemoveCells([e.Column]);
}
}
else if ((e.PropertyName is nameof(TableViewColumn.Order) ||
e.PropertyName is nameof(TableViewColumn.IsFrozen)) &&
e.Column.Visibility is Visibility.Visible)
{
RemoveCells([e.Column]);
AddCells([e.Column]);
}
else if (e.PropertyName is nameof(TableViewColumn.ActualWidth))
{
if (Cells.FirstOrDefault(x => x.Column == e.Column) is { } cell)
{
cell.Width = e.Column.ActualWidth;
}
}
else if (e.PropertyName is nameof(TableViewColumn.IsReadOnly))
{
UpdateCellsState();
}
else if (e.PropertyName is nameof(TableViewColumn.CellStyle))
{
EnsureCellsStyle(e.Column);
}
else if (e.PropertyName is nameof(TableViewBoundColumn.ElementStyle))
{
EnsureElementStyle(e.Column);
}
else if (e.PropertyName is nameof(TableViewBoundColumn.EditingElementStyle))
{
EnsureEditingElementStyle(e.Column);
}
}
/// <summary>
/// Removes cells for the specified columns.
/// </summary>
private void RemoveCells(IEnumerable<TableViewColumn> columns)
{
if (RowPresenter is not null)
{
foreach (var column in columns)
{
var cell = RowPresenter.Cells.FirstOrDefault(x => x.Column == column);
if (cell is not null)
{
RowPresenter.RemoveCell(cell);
}
}
}
}
/// <summary>
/// Adds cells for the specified columns.
/// </summary>
private void AddCells(IEnumerable<TableViewColumn> columns)
{
if (RowPresenter is not null && TableView is not null)
{
foreach (var column in columns)
{
var cell = new TableViewCell
{
Row = this,
Column = column,
TableView = TableView,
Index = TableView.Columns.VisibleColumns.IndexOf(column),
Width = column.ActualWidth
};
cell.SetBinding(HeightProperty, new Binding
{
Path = new PropertyPath($"{nameof(TableViewCell.TableView)}.{nameof(TableView.RowHeight)}"),
RelativeSource = new RelativeSource { Mode = RelativeSourceMode.Self }
});
cell.SetBinding(MaxHeightProperty, new Binding
{
Path = new PropertyPath($"{nameof(TableViewCell.TableView)}.{nameof(TableView.RowMaxHeight)}"),
RelativeSource = new RelativeSource { Mode = RelativeSourceMode.Self }
});
cell.SetBinding(MinHeightProperty, new Binding
{
Path = new PropertyPath($"{nameof(TableViewCell.TableView)}.{nameof(TableView.RowMinHeight)}"),
RelativeSource = new RelativeSource { Mode = RelativeSourceMode.Self }
});
RowPresenter.InsertCell(cell);
}
}
}
/// <summary>
/// Handles the TableView changing event.
/// </summary>
private void OnTableViewChanging()
{
if (TableView is not null)
{
TableView.IsReadOnlyChanged -= OnTableViewIsReadOnlyChanged;
if (TableView.Columns is not null)
{
TableView.Columns.CollectionChanged -= OnColumnsCollectionChanged;
TableView.Columns.ColumnPropertyChanged -= OnColumnPropertyChanged;
}
}
}
/// <summary>
/// Handles the TableView changed event.
/// </summary>
private void OnTableViewChanged()
{
if (TableView is not null)
{
TableView.IsReadOnlyChanged += OnTableViewIsReadOnlyChanged;
if (TableView.Columns is not null)
{
TableView.Columns.CollectionChanged += OnColumnsCollectionChanged;
TableView.Columns.ColumnPropertyChanged += OnColumnPropertyChanged;
}
}
}
/// <summary>
/// Handles the IsReadOnly property changed event for the TableView.
/// </summary>
private void OnTableViewIsReadOnlyChanged(object sender, DependencyPropertyChangedEventArgs e)
{
UpdateCellsState();
}
/// <summary>
/// Updates the state of the cells.
/// </summary>
private void UpdateCellsState()
{
foreach (var cell in Cells)
{
cell.UpdateElementState();
}
}
private void EnsureElementStyle(TableViewColumn column)
{
foreach (var cell in Cells)
{
if (cell.Column == column
&& cell.Content is FrameworkElement element
&& cell.Column is TableViewBoundColumn boundColumn
&& (TableView?.IsEditing is false || TableView?.CurrentCellSlot != cell.Slot))
{
element.Style = boundColumn.ElementStyle;
}
}
}
private void EnsureEditingElementStyle(TableViewColumn column)
{
if (TableView?.IsEditing is true
&& TableView.CurrentCellSlot is not null
&& column is TableViewBoundColumn boundColumn
&& TableView.GetCellFromSlot(TableView.CurrentCellSlot.Value) is { } cell
&& cell.Column == column
&& cell.Content is FrameworkElement element)
{
element.Style = boundColumn.EditingElementStyle;
}
}
/// <summary>
/// Ensures the cells style is applied.
/// </summary>
internal void EnsureCellsStyle(TableViewColumn? column = null, object? dataItem = null)
{
var cells = Cells.Where(x => column is null || x.Column == column);
foreach (var cell in cells)
{
cell.EnsureStyle(dataItem ?? Content);
}
}
/// <summary>
/// Applies the current cell state to the specified slot.
/// </summary>
internal void ApplyCurrentCellState(TableViewCellSlot slot)
{
if (slot.Column >= 0 && slot.Column < Cells.Count)
{
var cell = Cells[slot.Column];
cell.ApplyCurrentCellState();
}
}
/// <summary>
/// Applies the selection state to the cells.
/// </summary>
internal void ApplyCellsSelectionState()
{
foreach (var cell in Cells)
{
cell.ApplySelectionState();
}
}
/// <summary>
/// Ensures the layout of the row.
/// </summary>
internal void EnsureLayout()
{
var cornerRadius = _itemPresenter?.CornerRadius ?? new();
var left = Math.Max(cornerRadius.TopLeft, cornerRadius.BottomLeft) / 2;
var detailsHeight = RowPresenter?.GetDetailsContentHeight() ?? 0d;
#if WINDOWS
var selectionIndicator = _itemPresenter?.FindDescendants()
.OfType<Border>()
.FirstOrDefault(x => x is { Width: 3 });
var cellsHeight = ActualHeight - detailsHeight;
var selectionIndicatorHeight = Math.Max(Selection_IndicatorHeight, cellsHeight - 40);
if (selectionIndicator is not null)
{
selectionIndicator.MaxHeight = selectionIndicatorHeight;
selectionIndicator.Margin = new Thickness(
_selectionIndicatorMargin.Left + left,
_selectionIndicatorMargin.Top,
_selectionIndicatorMargin.Right,
_selectionIndicatorMargin.Bottom);
}
if (TableView is ListView { SelectionMode: ListViewSelectionMode.Multiple })
{
var fontIcon = this.FindDescendant<FontIcon>(x => x.Glyph == Check_Mark);
selectionIndicator = fontIcon?.Parent as Border;
}
if (TableView is ListView { SelectionMode: ListViewSelectionMode.Multiple })
{
var fontIcon = this.FindDescendant<FontIcon>(x => x.Glyph == Check_Mark);
selectionIndicator = fontIcon?.Parent as Border;
}
_selectionBackground ??= _itemPresenter?.FindDescendants()
.OfType<Border>()
.FirstOrDefault(x => x.Name is not Selection_Background && x.Margin == _selectionBackgroundMargin);
FocusVisualMargin = new Thickness(
_focusVisualMargin.Left + left,
_focusVisualMargin.Top,
_focusVisualMargin.Right,
_focusVisualMargin.Bottom + GetHorizontalGridlineHeight());
EnsureSelectionIndicatorPosition(detailsHeight, selectionIndicator);
#endif
if (_selectionBackground is not null)
{
_selectionBackground.Name = Selection_Background;
_selectionBackground.Margin = new Thickness(
_selectionBackgroundMargin.Left + left,
_selectionBackgroundMargin.Top,
_selectionBackgroundMargin.Right,
_selectionBackgroundMargin.Bottom + GetHorizontalGridlineHeight() + detailsHeight);
}
}
/// <summary>
/// Ensures the position of the selection indicator.
/// </summary>
private async void EnsureSelectionIndicatorPosition(double detailsHeight, Border? selectionIndicator)
{
await Task.Yield(); // let the animations and visual state changes complete
if (selectionIndicator is not null)
{
// Assign a TranslateTransform for animation
var translateTransform = new TranslateTransform();
selectionIndicator.RenderTransform = translateTransform;
var toValue = RowPresenter?.IsDetailsPanelVisible ?? false ? Math.Round(-detailsHeight / 2) : 0; // move up or down
var animation = new DoubleAnimation
{
To = toValue,
Duration = new Duration(TimeSpan.Zero)
};
var storyboard = new Storyboard();
Storyboard.SetTarget(animation, translateTransform);
Storyboard.SetTargetProperty(animation, "Y"); // vertical movement
storyboard.Children.Add(animation);
storyboard.Begin();
}
}
/// <summary>
/// Ensures alternate colors are applied to the row.
/// </summary>
internal void EnsureAlternateColors()
{
if (TableView is null || RowPresenter is null) return;
RowPresenter.Background =
Index % 2 == 1 && TableView.AlternateRowBackground is not null ? TableView.AlternateRowBackground : _cellPresenterBackground;
RowPresenter.Foreground =
Index % 2 == 1 && TableView.AlternateRowForeground is not null ? TableView.AlternateRowForeground : _cellPresenterForeground;
}
internal void UpdateSelectCheckMarkOpacity()
{
var fontIcon = this.FindDescendant<FontIcon>(x => x.Glyph == Check_Mark);
if (fontIcon?.Parent is Border border)
{
border.Opacity = TableView?.IsEditing is true ? 0.3 : 1;
}
}
/// <summary>
/// Highlights or unhighlights the row to indicate that a cell is being edited.
/// </summary>
internal void ApplyEditingHighlight(bool isEditing)
{
RowPresenter?.ApplyEditingHighlight(isEditing);
}
/// <summary>
/// Gets the height of the horizontal gridlines.
/// </summary>
private double GetHorizontalGridlineHeight()
{
return TableView?.GridLinesVisibility is TableViewGridLinesVisibility.All or TableViewGridLinesVisibility.Horizontal
? TableView.HorizontalGridLinesStrokeThickness : 0d;
}
/// <summary>
/// Gets the list of cells in the row.
/// </summary>
public IReadOnlyList<TableViewCell> Cells => RowPresenter?.Cells ?? [];
/// <summary>
/// Gets the index of the row.
/// </summary>
public int Index => TableView?.IndexFromContainer(this) ?? -1;
/// <summary>
/// Gets or sets the TableView associated with the row.
/// </summary>
public TableView? TableView
{
get => _tableView;
internal set
{
if (_tableView != value)
{
OnTableViewChanging();
_tableView = value;
OnTableViewChanged();
}
}
}
/// <inheritdoc/>
public TableViewRowPresenter? RowPresenter
#if WINDOWS
=> ContentTemplateRoot as TableViewRowPresenter;
#else
{ get; private set; }
#endif
}