-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckBoxExtensions.cs
More file actions
89 lines (78 loc) · 3.16 KB
/
Copy pathCheckBoxExtensions.cs
File metadata and controls
89 lines (78 loc) · 3.16 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
using Terminal.Gui.App;
using Terminal.Gui.Views;
namespace TerminalGui.Extensions.Extensions.ViewExtensions;
public static class CheckBoxExtensions
{
#region Checkbox
extension(CheckBox checkbox)
{
/// <summary>
/// Gets or sets the checked state of the checkbox control.
/// </summary>
/// <remarks>
/// The value is <see langword="true" /> if the checkbox is checked,
/// <see langword="false" /> if it is unchecked, or <see langword="null" /> if it is None.
/// of the checkbox accordingly.
/// </remarks>
public bool? IsChecked {
get => checkbox.Value.IsChecked;
set => checkbox.Value = CheckState.ConvertCheckState(value);
}
/// <summary>
/// Subscribes to the <see cref="CheckBox.ValueChanged" /> event.
/// </summary>
/// <param name="callback">
/// The callback to invoke with the <see cref="ValueChangedEventArgs{T}" /> containing old and new
/// values.
/// </param>
/// <returns>The <see cref="CheckBox" /> instance.</returns>
public CheckBox OnValueChanged(Action<ValueChangedEventArgs<CheckState>> callback)
{
checkbox.ValueChanged += (_, e) => callback(e);
return checkbox;
}
/// <summary>
/// Subscribes to the <see cref="CheckBox.ValueChanging" /> event.
/// Set <see cref="ValueChangingEventArgs{T}.Handled" /> to <see langword="true" /> in the callback to cancel the
/// change.
/// </summary>
/// <param name="callback">
/// The callback to invoke with the <see cref="ValueChangingEventArgs{T}" /> containing current and
/// proposed values.
/// </param>
/// <returns>The <see cref="CheckBox" /> instance.</returns>
public CheckBox OnValueChanging(Action<ValueChangingEventArgs<CheckState>> callback)
{
checkbox.ValueChanging += (_, e) => callback(e);
return checkbox;
}
}
#endregion
#region CheckState
extension(CheckState)
{
public static CheckState ConvertCheckState(bool? state) => state switch
{
true => CheckState.Checked,
false => CheckState.UnChecked,
null => CheckState.None
};
public static bool? ConvertCheckState(CheckState state) => state switch
{
CheckState.Checked => true,
CheckState.UnChecked => false,
_ => null
};
}
extension(CheckState checkState)
{
/// <summary>
/// The <see cref="CheckState" /> converted to a nullable <see cref="bool" />.
/// <see cref="CheckState.Checked" /> returns <see langword="true" />,
/// <see cref="CheckState.UnChecked" /> returns <see langword="false" />,
/// and <see cref="CheckState.None" /> returns <see langword="null" />.
/// </summary>
public bool? IsChecked => CheckState.ConvertCheckState(checkState);
}
#endregion
}