-
Notifications
You must be signed in to change notification settings - Fork 672
/
ListView.cs
1014 lines (905 loc) · 29.1 KB
/
ListView.cs
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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using NStack;
namespace Terminal.Gui {
/// <summary>
/// Implement <see cref="IListDataSource"/> to provide custom rendering for a <see cref="ListView"/>.
/// </summary>
public interface IListDataSource {
/// <summary>
/// Returns the number of elements to display
/// </summary>
int Count { get; }
/// <summary>
/// Returns the maximum length of elements to display
/// </summary>
int Length { get; }
/// <summary>
/// This method is invoked to render a specified item, the method should cover the entire provided width.
/// </summary>
/// <returns>The render.</returns>
/// <param name="container">The list view to render.</param>
/// <param name="driver">The console driver to render.</param>
/// <param name="selected">Describes whether the item being rendered is currently selected by the user.</param>
/// <param name="item">The index of the item to render, zero for the first item and so on.</param>
/// <param name="col">The column where the rendering will start</param>
/// <param name="line">The line where the rendering will be done.</param>
/// <param name="width">The width that must be filled out.</param>
/// <param name="start">The index of the string to be displayed.</param>
/// <remarks>
/// The default color will be set before this method is invoked, and will be based on whether the item is selected or not.
/// </remarks>
void Render (ListView container, ConsoleDriver driver, bool selected, int item, int col, int line, int width, int start = 0);
/// <summary>
/// Should return whether the specified item is currently marked.
/// </summary>
/// <returns><see langword="true"/>, if marked, <see langword="false"/> otherwise.</returns>
/// <param name="item">Item index.</param>
bool IsMarked (int item);
/// <summary>
/// Flags the item as marked.
/// </summary>
/// <param name="item">Item index.</param>
/// <param name="value">If set to <see langword="true"/> value.</param>
void SetMark (int item, bool value);
/// <summary>
/// Return the source as IList.
/// </summary>
/// <returns></returns>
IList ToList ();
}
/// <summary>
/// ListView <see cref="View"/> renders a scrollable list of data where each item can be activated to perform an action.
/// </summary>
/// <remarks>
/// <para>
/// The <see cref="ListView"/> displays lists of data and allows the user to scroll through the data.
/// Items in the can be activated firing an event (with the ENTER key or a mouse double-click).
/// If the <see cref="AllowsMarking"/> property is true, elements of the list can be marked by the user.
/// </para>
/// <para>
/// By default <see cref="ListView"/> uses <see cref="object.ToString"/> to render the items of any
/// <see cref="IList"/> object (e.g. arrays, <see cref="List{T}"/>,
/// and other collections). Alternatively, an object that implements <see cref="IListDataSource"/>
/// can be provided giving full control of what is rendered.
/// </para>
/// <para>
/// <see cref="ListView"/> can display any object that implements the <see cref="IList"/> interface.
/// <see cref="string"/> values are converted into <see cref="ustring"/> values before rendering, and other values are
/// converted into <see cref="string"/> by calling <see cref="object.ToString"/> and then converting to <see cref="ustring"/> .
/// </para>
/// <para>
/// To change the contents of the ListView, set the <see cref="Source"/> property (when
/// providing custom rendering via <see cref="IListDataSource"/>) or call <see cref="SetSource"/>
/// an <see cref="IList"/> is being used.
/// </para>
/// <para>
/// When <see cref="AllowsMarking"/> is set to true the rendering will prefix the rendered items with
/// [x] or [ ] and bind the SPACE key to toggle the selection. To implement a different
/// marking style set <see cref="AllowsMarking"/> to false and implement custom rendering.
/// </para>
/// <para>
/// Searching the ListView with the keyboard is supported. Users type the
/// first characters of an item, and the first item that starts with what the user types will be selected.
/// </para>
/// </remarks>
public class ListView : View {
int top, left;
int selected;
IListDataSource source;
/// <summary>
/// Gets or sets the <see cref="IListDataSource"/> backing this <see cref="ListView"/>, enabling custom rendering.
/// </summary>
/// <value>The source.</value>
/// <remarks>
/// Use <see cref="SetSource"/> to set a new <see cref="IList"/> source.
/// </remarks>
public IListDataSource Source {
get => source;
set {
source = value;
KeystrokeNavigator.Collection = source?.ToList ()?.Cast<object> ();
top = 0;
selected = 0;
lastSelectedItem = -1;
SetNeedsDisplay ();
}
}
/// <summary>
/// Sets the source of the <see cref="ListView"/> to an <see cref="IList"/>.
/// </summary>
/// <value>An object implementing the IList interface.</value>
/// <remarks>
/// Use the <see cref="Source"/> property to set a new <see cref="IListDataSource"/> source and use custome rendering.
/// </remarks>
public void SetSource (IList source)
{
if (source == null && (Source == null || !(Source is ListWrapper)))
Source = null;
else {
Source = MakeWrapper (source);
}
}
/// <summary>
/// Sets the source to an <see cref="IList"/> value asynchronously.
/// </summary>
/// <value>An item implementing the IList interface.</value>
/// <remarks>
/// Use the <see cref="Source"/> property to set a new <see cref="IListDataSource"/> source and use custom rendering.
/// </remarks>
public Task SetSourceAsync (IList source)
{
return Task.Factory.StartNew (() => {
if (source == null && (Source == null || !(Source is ListWrapper)))
Source = null;
else
Source = MakeWrapper (source);
return source;
}, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);
}
bool allowsMarking;
/// <summary>
/// Gets or sets whether this <see cref="ListView"/> allows items to be marked.
/// </summary>
/// <value>Set to <see langword="true"/> to allow marking elements of the list.</value>
/// <remarks>
/// If set to <see langword="true"/>, <see cref="ListView"/> will render items marked items with "[x]", and unmarked items with "[ ]"
/// spaces. SPACE key will toggle marking. The default is <see langword="false"/>.
/// </remarks>
public bool AllowsMarking {
get => allowsMarking;
set {
allowsMarking = value;
if (allowsMarking) {
AddKeyBinding (Key.Space, Command.ToggleChecked);
} else {
ClearKeybinding (Key.Space);
}
SetNeedsDisplay ();
}
}
/// <summary>
/// If set to <see langword="true"/> more than one item can be selected. If <see langword="false"/> selecting
/// an item will cause all others to be un-selected. The default is <see langword="false"/>.
/// </summary>
public bool AllowsMultipleSelection {
get => allowsMultipleSelection;
set {
allowsMultipleSelection = value;
if (Source != null && !allowsMultipleSelection) {
// Clear all selections except selected
for (int i = 0; i < Source.Count; i++) {
if (Source.IsMarked (i) && i != selected) {
Source.SetMark (i, false);
}
}
}
SetNeedsDisplay ();
}
}
/// <summary>
/// Gets or sets the item that is displayed at the top of the <see cref="ListView"/>.
/// </summary>
/// <value>The top item.</value>
public int TopItem {
get => top;
set {
if (source == null)
return;
if (value < 0 || (source.Count > 0 && value >= source.Count))
throw new ArgumentException ("value");
top = value;
SetNeedsDisplay ();
}
}
/// <summary>
/// Gets or sets the leftmost column that is currently visible (when scrolling horizontally).
/// </summary>
/// <value>The left position.</value>
public int LeftItem {
get => left;
set {
if (source == null)
return;
if (value < 0 || (Maxlength > 0 && value >= Maxlength))
throw new ArgumentException ("value");
left = value;
SetNeedsDisplay ();
}
}
/// <summary>
/// Gets the widest item in the list.
/// </summary>
public int Maxlength => (source?.Length) ?? 0;
/// <summary>
/// Gets or sets the index of the currently selected item.
/// </summary>
/// <value>The selected item.</value>
public int SelectedItem {
get => selected;
set {
if (source == null || source.Count == 0) {
return;
}
if (value < 0 || value >= source.Count) {
throw new ArgumentException ("value");
}
selected = value;
OnSelectedChanged ();
}
}
static IListDataSource MakeWrapper (IList source)
{
return new ListWrapper (source);
}
/// <summary>
/// Initializes a new instance of <see cref="ListView"/> that will display the
/// contents of the object implementing the <see cref="IList"/> interface,
/// with relative positioning.
/// </summary>
/// <param name="source">An <see cref="IList"/> data source, if the elements are strings or ustrings,
/// the string is rendered, otherwise the ToString() method is invoked on the result.</param>
public ListView (IList source) : this (MakeWrapper (source))
{
}
/// <summary>
/// Initializes a new instance of <see cref="ListView"/> that will display the provided data source, using relative positioning.
/// </summary>
/// <param name="source"><see cref="IListDataSource"/> object that provides a mechanism to render the data.
/// The number of elements on the collection should not change, if you must change, set
/// the "Source" property to reset the internal settings of the ListView.</param>
public ListView (IListDataSource source) : base ()
{
this.source = source;
Initialize ();
}
/// <summary>
/// Initializes a new instance of <see cref="ListView"/>. Set the <see cref="Source"/> property to display something.
/// </summary>
public ListView () : base ()
{
Initialize ();
}
/// <summary>
/// Initializes a new instance of <see cref="ListView"/> that will display the contents of the object implementing the <see cref="IList"/> interface with an absolute position.
/// </summary>
/// <param name="rect">Frame for the listview.</param>
/// <param name="source">An IList data source, if the elements of the IList are strings or ustrings,
/// the string is rendered, otherwise the ToString() method is invoked on the result.</param>
public ListView (Rect rect, IList source) : this (rect, MakeWrapper (source))
{
Initialize ();
}
/// <summary>
/// Initializes a new instance of <see cref="ListView"/> with the provided data source and an absolute position
/// </summary>
/// <param name="rect">Frame for the listview.</param>
/// <param name="source">IListDataSource object that provides a mechanism to render the data.
/// The number of elements on the collection should not change, if you must change,
/// set the "Source" property to reset the internal settings of the ListView.</param>
public ListView (Rect rect, IListDataSource source) : base (rect)
{
this.source = source;
Initialize ();
}
void Initialize ()
{
Source = source;
CanFocus = true;
// Things this view knows how to do
AddCommand (Command.LineUp, () => MoveUp ());
AddCommand (Command.LineDown, () => MoveDown ());
AddCommand (Command.ScrollUp, () => ScrollUp (1));
AddCommand (Command.ScrollDown, () => ScrollDown (1));
AddCommand (Command.PageUp, () => MovePageUp ());
AddCommand (Command.PageDown, () => MovePageDown ());
AddCommand (Command.TopHome, () => MoveHome ());
AddCommand (Command.BottomEnd, () => MoveEnd ());
AddCommand (Command.OpenSelectedItem, () => OnOpenSelectedItem ());
AddCommand (Command.ToggleChecked, () => MarkUnmarkRow ());
// Default keybindings for all ListViews
AddKeyBinding (Key.CursorUp, Command.LineUp);
AddKeyBinding (Key.P | Key.CtrlMask, Command.LineUp);
AddKeyBinding (Key.CursorDown, Command.LineDown);
AddKeyBinding (Key.N | Key.CtrlMask, Command.LineDown);
AddKeyBinding (Key.PageUp, Command.PageUp);
AddKeyBinding (Key.PageDown, Command.PageDown);
AddKeyBinding (Key.V | Key.CtrlMask, Command.PageDown);
AddKeyBinding (Key.Home, Command.TopHome);
AddKeyBinding (Key.End, Command.BottomEnd);
AddKeyBinding (Key.Enter, Command.OpenSelectedItem);
}
///<inheritdoc/>
public override void Redraw (Rect bounds)
{
var current = ColorScheme.Focus;
Driver.SetAttribute (current);
Move (0, 0);
var f = Frame;
var item = top;
bool focused = HasFocus;
int col = allowsMarking ? 2 : 0;
int start = left;
for (int row = 0; row < f.Height; row++, item++) {
bool isSelected = item == selected;
var newcolor = focused ? (isSelected ? ColorScheme.Focus : GetNormalColor ())
: (isSelected ? ColorScheme.HotNormal : GetNormalColor ());
if (newcolor != current) {
Driver.SetAttribute (newcolor);
current = newcolor;
}
Move (0, row);
if (source == null || item >= source.Count) {
for (int c = 0; c < f.Width; c++)
Driver.AddRune (' ');
} else {
var rowEventArgs = new ListViewRowEventArgs (item);
OnRowRender (rowEventArgs);
if (rowEventArgs.RowAttribute != null && current != rowEventArgs.RowAttribute) {
current = (Attribute)rowEventArgs.RowAttribute;
Driver.SetAttribute (current);
}
if (allowsMarking) {
Driver.AddRune (source.IsMarked (item) ? (AllowsMultipleSelection ? Driver.Checked : Driver.Selected) :
(AllowsMultipleSelection ? Driver.UnChecked : Driver.UnSelected));
Driver.AddRune (' ');
}
Source.Render (this, Driver, isSelected, item, col, row, f.Width - col, start);
}
}
}
/// <summary>
/// This event is raised when the selected item in the <see cref="ListView"/> has changed.
/// </summary>
public event Action<ListViewItemEventArgs> SelectedItemChanged;
/// <summary>
/// This event is raised when the user Double Clicks on an item or presses ENTER to open the selected item.
/// </summary>
public event Action<ListViewItemEventArgs> OpenSelectedItem;
/// <summary>
/// This event is invoked when this <see cref="ListView"/> is being drawn before rendering.
/// </summary>
public event Action<ListViewRowEventArgs> RowRender;
/// <summary>
/// Gets the <see cref="CollectionNavigator"/> that searches the <see cref="ListView.Source"/> collection as
/// the user types.
/// </summary>
public CollectionNavigator KeystrokeNavigator { get; private set; } = new CollectionNavigator ();
///<inheritdoc/>
public override bool ProcessKey (KeyEvent kb)
{
if (source == null) {
return base.ProcessKey (kb);
}
var result = InvokeKeybindings (kb);
if (result != null) {
return (bool)result;
}
// Enable user to find & select an item by typing text
if (CollectionNavigator.IsCompatibleKey (kb)) {
var newItem = KeystrokeNavigator?.GetNextMatchingItem (SelectedItem, (char)kb.KeyValue);
if (newItem is int && newItem != -1) {
SelectedItem = (int)newItem;
EnsureSelectedItemVisible ();
SetNeedsDisplay ();
return true;
}
}
return false;
}
/// <summary>
/// If <see cref="AllowsMarking"/> and <see cref="AllowsMultipleSelection"/> are both <see langword="true"/>,
/// unmarks all marked items other than the currently selected.
/// </summary>
/// <returns><see langword="true"/> if unmarking was successful.</returns>
public virtual bool AllowsAll ()
{
if (!allowsMarking)
return false;
if (!AllowsMultipleSelection) {
for (int i = 0; i < Source.Count; i++) {
if (Source.IsMarked (i) && i != selected) {
Source.SetMark (i, false);
return true;
}
}
}
return true;
}
/// <summary>
/// Marks the <see cref="SelectedItem"/> if it is not already marked.
/// </summary>
/// <returns><see langword="true"/> if the <see cref="SelectedItem"/> was marked.</returns>
public virtual bool MarkUnmarkRow ()
{
if (AllowsAll ()) {
Source.SetMark (SelectedItem, !Source.IsMarked (SelectedItem));
SetNeedsDisplay ();
return true;
}
return false;
}
/// <summary>
/// Changes the <see cref="SelectedItem"/> to the item at the top of the visible list.
/// </summary>
/// <returns></returns>
public virtual bool MovePageUp ()
{
int n = (selected - Frame.Height);
if (n < 0)
n = 0;
if (n != selected) {
selected = n;
top = selected;
OnSelectedChanged ();
SetNeedsDisplay ();
}
return true;
}
/// <summary>
/// Changes the <see cref="SelectedItem"/> to the item just below the bottom
/// of the visible list, scrolling if needed.
/// </summary>
/// <returns></returns>
public virtual bool MovePageDown ()
{
var n = (selected + Frame.Height);
if (n >= source.Count)
n = source.Count - 1;
if (n != selected) {
selected = n;
if (source.Count >= Frame.Height)
top = selected;
else
top = 0;
OnSelectedChanged ();
SetNeedsDisplay ();
}
return true;
}
/// <summary>
/// Changes the <see cref="SelectedItem"/> to the next item in the list,
/// scrolling the list if needed.
/// </summary>
/// <returns></returns>
public virtual bool MoveDown ()
{
if (source.Count == 0) {
// Do we set lastSelectedItem to -1 here?
return false; //Nothing for us to move to
}
if (selected >= source.Count) {
// If for some reason we are currently outside of the
// valid values range, we should select the bottommost valid value.
// This can occur if the backing data source changes.
selected = source.Count - 1;
OnSelectedChanged ();
SetNeedsDisplay ();
} else if (selected + 1 < source.Count) { //can move by down by one.
selected++;
if (selected >= top + Frame.Height) {
top++;
} else if (selected < top) {
top = selected;
} else if (selected < top) {
top = selected;
}
OnSelectedChanged ();
SetNeedsDisplay ();
} else if (selected == 0) {
OnSelectedChanged ();
SetNeedsDisplay ();
} else if (selected >= top + Frame.Height) {
top = source.Count - Frame.Height;
SetNeedsDisplay ();
}
return true;
}
/// <summary>
/// Changes the <see cref="SelectedItem"/> to the previous item in the list,
/// scrolling the list if needed.
/// </summary>
/// <returns></returns>
public virtual bool MoveUp ()
{
if (source.Count == 0) {
// Do we set lastSelectedItem to -1 here?
return false; //Nothing for us to move to
}
if (selected >= source.Count) {
// If for some reason we are currently outside of the
// valid values range, we should select the bottommost valid value.
// This can occur if the backing data source changes.
selected = source.Count - 1;
OnSelectedChanged ();
SetNeedsDisplay ();
} else if (selected > 0) {
selected--;
if (selected > Source.Count) {
selected = Source.Count - 1;
}
if (selected < top) {
top = selected;
} else if (selected > top + Frame.Height) {
top = Math.Max (selected - Frame.Height + 1, 0);
}
OnSelectedChanged ();
SetNeedsDisplay ();
} else if (selected < top) {
top = selected;
SetNeedsDisplay ();
}
return true;
}
/// <summary>
/// Changes the <see cref="SelectedItem"/> to last item in the list,
/// scrolling the list if needed.
/// </summary>
/// <returns></returns>
public virtual bool MoveEnd ()
{
if (source?.Count > 0 && selected != source.Count - 1) {
selected = source.Count - 1;
if (top + selected > Frame.Height - 1) {
top = selected < Frame.Height - 1
? Math.Max (Frame.Height - selected + 1, 0)
: Math.Max (selected - Frame.Height + 1, 0);
}
OnSelectedChanged ();
SetNeedsDisplay ();
}
return true;
}
/// <summary>
/// Changes the <see cref="SelectedItem"/> to the first item in the list,
/// scrolling the list if needed.
/// </summary>
/// <returns></returns>
public virtual bool MoveHome ()
{
if (selected != 0) {
selected = 0;
top = selected;
OnSelectedChanged ();
SetNeedsDisplay ();
}
return true;
}
/// <summary>
/// Scrolls the view down by <paramref name="items"/> items.
/// </summary>
/// <param name="items">Number of items to scroll down.</param>
public virtual bool ScrollDown (int items)
{
top = Math.Max (Math.Min (top + items, source.Count - 1), 0);
SetNeedsDisplay ();
return true;
}
/// <summary>
/// Scrolls the view up by <paramref name="items"/> items.
/// </summary>
/// <param name="items">Number of items to scroll up.</param>
public virtual bool ScrollUp (int items)
{
top = Math.Max (top - items, 0);
SetNeedsDisplay ();
return true;
}
/// <summary>
/// Scrolls the view right.
/// </summary>
/// <param name="cols">Number of columns to scroll right.</param>
public virtual bool ScrollRight (int cols)
{
left = Math.Max (Math.Min (left + cols, Maxlength - 1), 0);
SetNeedsDisplay ();
return true;
}
/// <summary>
/// Scrolls the view left.
/// </summary>
/// <param name="cols">Number of columns to scroll left.</param>
public virtual bool ScrollLeft (int cols)
{
left = Math.Max (left - cols, 0);
SetNeedsDisplay ();
return true;
}
int lastSelectedItem = -1;
private bool allowsMultipleSelection = true;
/// <summary>
/// Invokes the <see cref="SelectedItemChanged"/> event if it is defined.
/// </summary>
/// <returns></returns>
public virtual bool OnSelectedChanged ()
{
if (selected != lastSelectedItem) {
var value = source?.Count > 0 ? source.ToList () [selected] : null;
SelectedItemChanged?.Invoke (new ListViewItemEventArgs (selected, value));
if (HasFocus) {
lastSelectedItem = selected;
}
return true;
}
return false;
}
/// <summary>
/// Invokes the <see cref="OpenSelectedItem"/> event if it is defined.
/// </summary>
/// <returns></returns>
public virtual bool OnOpenSelectedItem ()
{
if (source.Count <= selected || selected < 0 || OpenSelectedItem == null) {
return false;
}
var value = source.ToList () [selected];
OpenSelectedItem?.Invoke (new ListViewItemEventArgs (selected, value));
return true;
}
/// <summary>
/// Virtual method that will invoke the <see cref="RowRender"/>.
/// </summary>
/// <param name="rowEventArgs"></param>
public virtual void OnRowRender (ListViewRowEventArgs rowEventArgs)
{
RowRender?.Invoke (rowEventArgs);
}
///<inheritdoc/>
public override bool OnEnter (View view)
{
Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
if (lastSelectedItem == -1) {
EnsureSelectedItemVisible ();
}
return base.OnEnter (view);
}
///<inheritdoc/>
public override bool OnLeave (View view)
{
if (lastSelectedItem > -1) {
lastSelectedItem = -1;
}
return base.OnLeave (view);
}
/// <summary>
/// Ensures the selected item is always visible on the screen.
/// </summary>
public void EnsureSelectedItemVisible ()
{
SuperView?.LayoutSubviews ();
// If last item is selected and is removed, ensures a valid selected item
if (Source != null && selected > Source.Count - 1) {
SelectedItem = Source.Count - 1;
SetNeedsDisplay ();
}
if (selected < top) {
top = selected;
} else if (Frame.Height > 0 && selected >= top + Frame.Height) {
top = Math.Max (selected - Frame.Height + 1, 0);
}
}
///<inheritdoc/>
public override void PositionCursor ()
{
if (allowsMarking)
Move (0, selected - top);
else
Move (Bounds.Width - 1, selected - top);
}
///<inheritdoc/>
public override bool MouseEvent (MouseEvent me)
{
if (!me.Flags.HasFlag (MouseFlags.Button1Clicked) && !me.Flags.HasFlag (MouseFlags.Button1DoubleClicked) &&
me.Flags != MouseFlags.WheeledDown && me.Flags != MouseFlags.WheeledUp &&
me.Flags != MouseFlags.WheeledRight && me.Flags != MouseFlags.WheeledLeft)
return false;
if (!HasFocus && CanFocus) {
SetFocus ();
}
if (source == null) {
return false;
}
if (me.Flags == MouseFlags.WheeledDown) {
ScrollDown (1);
return true;
} else if (me.Flags == MouseFlags.WheeledUp) {
ScrollUp (1);
return true;
} else if (me.Flags == MouseFlags.WheeledRight) {
ScrollRight (1);
return true;
} else if (me.Flags == MouseFlags.WheeledLeft) {
ScrollLeft (1);
return true;
}
if (me.Y + top >= source.Count) {
return true;
}
selected = top + me.Y;
if (AllowsAll ()) {
Source.SetMark (SelectedItem, !Source.IsMarked (SelectedItem));
}
OnSelectedChanged ();
SetNeedsDisplay ();
if (me.Flags == MouseFlags.Button1DoubleClicked) {
OnOpenSelectedItem ();
}
return true;
}
}
/// <inheritdoc/>
public class ListWrapper : IListDataSource {
IList src;
BitArray marks;
int count, len;
/// <inheritdoc/>
public ListWrapper (IList source)
{
if (source != null) {
count = source.Count;
marks = new BitArray (count);
src = source;
len = GetMaxLengthItem ();
}
}
/// <inheritdoc/>
public int Count {
get {
CheckAndResizeMarksIfRequired ();
return src?.Count ?? 0;
}
}
/// <inheritdoc/>
public int Length => len;
void CheckAndResizeMarksIfRequired ()
{
if (src != null && count != src.Count) {
count = src.Count;
BitArray newMarks = new BitArray (count);
for (var i = 0; i < Math.Min (marks.Length, newMarks.Length); i++) {
newMarks [i] = marks [i];
}
marks = newMarks;
len = GetMaxLengthItem ();
}
}
int GetMaxLengthItem ()
{
if (src == null || src?.Count == 0) {
return 0;
}
int maxLength = 0;
for (int i = 0; i < src.Count; i++) {
var t = src [i];
int l;
if (t is ustring u) {
l = TextFormatter.GetTextWidth (u);
} else if (t is string s) {
l = s.Length;
} else {
l = t.ToString ().Length;
}
if (l > maxLength) {
maxLength = l;
}
}
return maxLength;
}
void RenderUstr (ConsoleDriver driver, ustring ustr, int col, int line, int width, int start = 0)
{
ustring str = start > ustr.ConsoleWidth ? string.Empty : ustr.Substring (Math.Min (start, ustr.ToRunes ().Length - 1));
ustring u = TextFormatter.ClipAndJustify (str, width, TextAlignment.Left);
driver.AddStr (u);
width -= TextFormatter.GetTextWidth (u);
while (width-- + start > 0) {
driver.AddRune (' ');
}
}
/// <inheritdoc/>
public void Render (ListView container, ConsoleDriver driver, bool marked, int item, int col, int line, int width, int start = 0)
{
var savedClip = container.ClipToBounds ();
container.Move (Math.Max (col - start, 0), line);
var t = src? [item];
if (t == null) {
RenderUstr (driver, ustring.Make (""), col, line, width);
} else {
if (t is ustring u) {
RenderUstr (driver, u, col, line, width, start);
} else if (t is string s) {
RenderUstr (driver, s, col, line, width, start);
} else {
RenderUstr (driver, t.ToString (), col, line, width, start);
}
}
driver.Clip = savedClip;
}
/// <inheritdoc/>
public bool IsMarked (int item)
{
if (item >= 0 && item < Count)
return marks [item];
return false;
}
/// <inheritdoc/>
public void SetMark (int item, bool value)
{
if (item >= 0 && item < Count)
marks [item] = value;
}
/// <inheritdoc/>
public IList ToList ()
{
return src;
}
/// <inheritdoc/>
public int StartsWith (string search)
{
if (src == null || src?.Count == 0) {
return -1;
}
for (int i = 0; i < src.Count; i++) {
var t = src [i];
if (t is ustring u) {
if (u.ToUpper ().StartsWith (search.ToUpperInvariant ())) {
return i;
}
} else if (t is string s) {
if (s.StartsWith (search, StringComparison.InvariantCultureIgnoreCase)) {
return i;
}
}
}
return -1;
}
}
/// <summary>
/// <see cref="EventArgs"/> for <see cref="ListView"/> events.
/// </summary>
public class ListViewItemEventArgs : EventArgs {
/// <summary>
/// The index of the <see cref="ListView"/> item.
/// </summary>
public int Item { get; }
/// <summary>
/// The <see cref="ListView"/> item.
/// </summary>
public object Value { get; }
/// <summary>
/// Initializes a new instance of <see cref="ListViewItemEventArgs"/>
/// </summary>
/// <param name="item">The index of the <see cref="ListView"/> item.</param>
/// <param name="value">The <see cref="ListView"/> item</param>
public ListViewItemEventArgs (int item, object value)
{
Item = item;
Value = value;
}
}
/// <summary>
/// <see cref="EventArgs"/> used by the <see cref="ListView.RowRender"/> event.
/// </summary>
public class ListViewRowEventArgs : EventArgs {
/// <summary>
/// The current row being rendered.
/// </summary>
public int Row { get; }
/// <summary>
/// The <see cref="Attribute"/> used by current row or