-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListViewExtensions.cs
More file actions
107 lines (97 loc) · 4.46 KB
/
Copy pathListViewExtensions.cs
File metadata and controls
107 lines (97 loc) · 4.46 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
using System.Collections.Specialized;
using Terminal.Gui.App;
using Terminal.Gui.Views;
namespace TerminalGui.Extensions.Extensions.ViewExtensions;
/// <summary>
/// Extension methods for <see cref="ListView" />.
/// </summary>
public static class ListViewExtensions
{
extension(ListView listView)
{
/// <summary>
/// Returns the currently selected item from the given source list,
/// or <see langword="default" /> when no item is selected or the source is empty.
/// </summary>
/// <typeparam name="T">The type of items in the source list.</typeparam>
/// <param name="source">The source list backing the <see cref="ListView" />.</param>
/// <returns>The selected item, or <see langword="default" />.</returns>
public T? GetSelectedItem<T>(IList<T> source)
{
if (source.Count == 0 || listView.SelectedItem is null)
{
return default;
}
return source[listView.SelectedItem!.Value];
}
/// <summary>
/// Subscribes to the <see cref="ListView.ValueChanged" /> event.
/// </summary>
/// <param name="callback">
/// The callback to invoke with the <see cref="ValueChangedEventArgs{T}" /> containing old and new
/// selected indices.
/// </param>
/// <returns>The <see cref="ListView" /> instance.</returns>
public ListView OnValueChanged(Action<ValueChangedEventArgs<int?>> callback)
{
listView.ValueChanged += (_, e) => callback(e);
return listView;
}
/// <summary>
/// Subscribes to the <see cref="ListView.CollectionChanged" /> event.
/// </summary>
/// <param name="callback">The callback to invoke with the <see cref="NotifyCollectionChangedEventArgs" />.</param>
/// <returns>The <see cref="ListView" /> instance.</returns>
public ListView OnCollectionChanged(Action<NotifyCollectionChangedEventArgs> callback)
{
listView.CollectionChanged += (_, e) => callback(e);
return listView;
}
/// <summary>
/// Subscribes to the <see cref="ListView.SourceChanged" /> event.
/// </summary>
/// <param name="callback">The callback to invoke when the source changes.</param>
/// <returns>The <see cref="ListView" /> instance.</returns>
public ListView OnSourceChanged(Action callback)
{
listView.SourceChanged += (_, _) => callback();
return listView;
}
/// <summary>
/// Subscribes to the <see cref="ListView.ValueChanging" /> event.
/// Set <see cref="ValueChangingEventArgs{T}.Handled" /> to <see langword="true" /> to cancel the change.
/// </summary>
/// <param name="callback">
/// The callback to invoke with the <see cref="ValueChangingEventArgs{T}" /> containing current and
/// proposed selected indices.
/// </param>
/// <returns>The <see cref="ListView" /> instance.</returns>
public ListView OnValueChanging(Action<ValueChangingEventArgs<int?>> callback)
{
listView.ValueChanging += (_, e) => callback(e);
return listView;
}
/// <summary>
/// Subscribes to the <see cref="ListView.RowRender" /> event.
/// </summary>
/// <param name="callback">The callback to invoke with the <see cref="ListViewRowEventArgs" />.</param>
/// <returns>The <see cref="ListView" /> instance.</returns>
public ListView OnRowRender(Action<ListViewRowEventArgs> callback)
{
listView.RowRender += (_, e) => callback(e);
return listView;
}
/// <summary>
/// Configures scroll bar visibility for the <see cref="ListView" />.
/// </summary>
/// <param name="vertical">Whether to show the vertical scroll bar.</param>
/// <param name="horizontal">Whether to show the horizontal scroll bar.</param>
/// <returns>The <see cref="ListView" /> instance.</returns>
public ListView WithScrollBars(bool vertical = true, bool horizontal = false)
{
listView.VerticalScrollBar.Visible = vertical;
listView.HorizontalScrollBar.Visible = horizontal;
return listView;
}
}
}