-
Notifications
You must be signed in to change notification settings - Fork 673
/
CheckBox.cs
191 lines (166 loc) · 4.93 KB
/
CheckBox.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
//
// Checkbox.cs: Checkbox control
//
// Authors:
// Miguel de Icaza (miguel@gnome.org)
//
using System;
using NStack;
namespace Terminal.Gui {
/// <summary>
/// The <see cref="CheckBox"/> <see cref="View"/> shows an on/off toggle that the user can set
/// </summary>
public class CheckBox : View {
Rune charChecked;
Rune charUnChecked;
bool @checked;
/// <summary>
/// Toggled event, raised when the <see cref="CheckBox"/> is toggled.
/// </summary>
/// <remarks>
/// Client code can hook up to this event, it is
/// raised when the <see cref="CheckBox"/> is activated either with
/// the mouse or the keyboard. The passed <c>bool</c> contains the previous state.
/// </remarks>
public event Action<bool> Toggled;
/// <summary>
/// Called when the <see cref="Checked"/> property changes. Invokes the <see cref="Toggled"/> event.
/// </summary>
public virtual void OnToggled (bool previousChecked)
{
Toggled?.Invoke (previousChecked);
}
/// <summary>
/// Initializes a new instance of <see cref="CheckBox"/> based on the given text, using <see cref="LayoutStyle.Computed"/> layout.
/// </summary>
public CheckBox () : this (string.Empty) { }
/// <summary>
/// Initializes a new instance of <see cref="CheckBox"/> based on the given text, using <see cref="LayoutStyle.Computed"/> layout.
/// </summary>
/// <param name="s">S.</param>
/// <param name="is_checked">If set to <c>true</c> is checked.</param>
public CheckBox (ustring s, bool is_checked = false) : base ()
{
Initialize (s, is_checked);
}
/// <summary>
/// Initializes a new instance of <see cref="CheckBox"/> using <see cref="LayoutStyle.Absolute"/> layout.
/// </summary>
/// <remarks>
/// The size of <see cref="CheckBox"/> is computed based on the
/// text length. This <see cref="CheckBox"/> is not toggled.
/// </remarks>
public CheckBox (int x, int y, ustring s) : this (x, y, s, false)
{
}
/// <summary>
/// Initializes a new instance of <see cref="CheckBox"/> using <see cref="LayoutStyle.Absolute"/> layout.
/// </summary>
/// <remarks>
/// The size of <see cref="CheckBox"/> is computed based on the
/// text length.
/// </remarks>
public CheckBox (int x, int y, ustring s, bool is_checked) : base (new Rect (x, y, s.Length, 1))
{
Initialize (s, is_checked);
}
void Initialize (ustring s, bool is_checked)
{
charChecked = new Rune (Driver != null ? Driver.Checked : '√');
charUnChecked = new Rune (Driver != null ? Driver.UnChecked : '╴');
Checked = is_checked;
HotKeySpecifier = new Rune ('_');
CanFocus = true;
AutoSize = true;
Text = s;
UpdateTextFormatterText ();
ProcessResizeView ();
// Things this view knows how to do
AddCommand (Command.ToggleChecked, () => ToggleChecked ());
// Default keybindings for this view
AddKeyBinding ((Key)' ', Command.ToggleChecked);
AddKeyBinding (Key.Space, Command.ToggleChecked);
}
/// <inheritdoc/>
protected override void UpdateTextFormatterText ()
{
switch (TextAlignment) {
case TextAlignment.Left:
case TextAlignment.Centered:
case TextAlignment.Justified:
TextFormatter.Text = ustring.Make (Checked ? charChecked : charUnChecked) + " " + GetFormatterText ();
break;
case TextAlignment.Right:
TextFormatter.Text = GetFormatterText () + " " + ustring.Make (Checked ? charChecked : charUnChecked);
break;
}
}
ustring GetFormatterText ()
{
if (AutoSize || ustring.IsNullOrEmpty (Text) || Frame.Width <= 2) {
return Text;
}
return Text.RuneSubstring (0, Math.Min (Frame.Width - 2, Text.RuneCount));
}
/// <summary>
/// The state of the <see cref="CheckBox"/>
/// </summary>
public bool Checked {
get => @checked;
set {
@checked = value;
UpdateTextFormatterText ();
ProcessResizeView ();
}
}
///<inheritdoc/>
public override void PositionCursor ()
{
Move (0, 0);
}
///<inheritdoc/>
public override bool ProcessKey (KeyEvent kb)
{
var result = InvokeKeybindings (kb);
if (result != null)
return (bool)result;
return base.ProcessKey (kb);
}
///<inheritdoc/>
public override bool ProcessHotKey (KeyEvent kb)
{
if (kb.Key == (Key.AltMask | HotKey))
return ToggleChecked ();
return false;
}
bool ToggleChecked ()
{
if (!HasFocus) {
SetFocus ();
}
var previousChecked = Checked;
Checked = !Checked;
OnToggled (previousChecked);
SetNeedsDisplay ();
return true;
}
///<inheritdoc/>
public override bool MouseEvent (MouseEvent me)
{
if (!me.Flags.HasFlag (MouseFlags.Button1Clicked) || !CanFocus)
return false;
SetFocus ();
var previousChecked = Checked;
Checked = !Checked;
OnToggled (previousChecked);
SetNeedsDisplay ();
return true;
}
///<inheritdoc/>
public override bool OnEnter (View view)
{
Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
return base.OnEnter (view);
}
}
}