CheckBox.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. #nullable enable
  2. namespace Terminal.Gui.Views;
  3. /// <summary>Shows a checkbox that can be cycled between two or three states.</summary>
  4. /// <remarks>
  5. /// <para>
  6. /// <see cref="RadioStyle"/> is used to display radio button style glyphs (●) instead of checkbox style glyphs (☑).
  7. /// </para>
  8. /// </remarks>
  9. public class CheckBox : View
  10. {
  11. /// <summary>
  12. /// Gets or sets the default Highlight Style.
  13. /// </summary>
  14. [ConfigurationProperty (Scope = typeof (ThemeScope))]
  15. public static MouseState DefaultHighlightStates { get; set; } = MouseState.PressedOutside | MouseState.Pressed | MouseState.In;
  16. /// <summary>
  17. /// Initializes a new instance of <see cref="CheckBox"/>.
  18. /// </summary>
  19. public CheckBox ()
  20. {
  21. Width = Dim.Auto (DimAutoStyle.Text);
  22. Height = Dim.Auto (DimAutoStyle.Text, 1);
  23. CanFocus = true;
  24. // Select (Space key and single-click) - Advance state and raise Select event - DO NOT raise Accept
  25. AddCommand (Command.Select, AdvanceAndSelect);
  26. // Hotkey - Advance state and raise Select event - DO NOT raise Accept
  27. AddCommand (Command.HotKey, ctx =>
  28. {
  29. if (RaiseHandlingHotKey () is true)
  30. {
  31. return true;
  32. }
  33. return AdvanceAndSelect (ctx);
  34. });
  35. // Accept (Enter key) - Raise Accept event - DO NOT advance state
  36. AddCommand (Command.Accept, RaiseAccepting);
  37. MouseBindings.Add (MouseFlags.Button1DoubleClicked, Command.Accept);
  38. TitleChanged += Checkbox_TitleChanged;
  39. HighlightStates = DefaultHighlightStates;
  40. }
  41. private bool? AdvanceAndSelect (ICommandContext? commandContext)
  42. {
  43. bool? cancelled = AdvanceCheckState ();
  44. if (cancelled is true)
  45. {
  46. return true;
  47. }
  48. if (RaiseSelecting (commandContext) is true)
  49. {
  50. return true;
  51. }
  52. return commandContext?.Command == Command.HotKey ? cancelled : cancelled is false;
  53. }
  54. private void Checkbox_TitleChanged (object? sender, EventArgs<string> e)
  55. {
  56. base.Text = e.Value;
  57. TextFormatter.HotKeySpecifier = HotKeySpecifier;
  58. }
  59. /// <inheritdoc/>
  60. public override string Text
  61. {
  62. get => Title;
  63. set => base.Text = Title = value;
  64. }
  65. /// <inheritdoc/>
  66. public override Rune HotKeySpecifier
  67. {
  68. get => base.HotKeySpecifier;
  69. set => TextFormatter.HotKeySpecifier = base.HotKeySpecifier = value;
  70. }
  71. private bool _allowNone;
  72. /// <summary>
  73. /// If <see langword="true"/> allows <see cref="CheckedState"/> to be <see cref="CheckState.None"/>. The default is
  74. /// <see langword="false"/>.
  75. /// </summary>
  76. public bool AllowCheckStateNone
  77. {
  78. get => _allowNone;
  79. set
  80. {
  81. if (_allowNone == value)
  82. {
  83. return;
  84. }
  85. _allowNone = value;
  86. if (CheckedState == CheckState.None)
  87. {
  88. CheckedState = CheckState.UnChecked;
  89. }
  90. }
  91. }
  92. private CheckState _checkedState = CheckState.UnChecked;
  93. /// <summary>
  94. /// The state of the <see cref="CheckBox"/>.
  95. /// </summary>
  96. /// <remarks>
  97. /// <para>
  98. /// If <see cref="AllowCheckStateNone"/> is <see langword="true"/> and <see cref="CheckState.None"/>, the
  99. /// <see cref="CheckBox"/>
  100. /// will display the <c>Glyphs.CheckStateNone</c> character (☒).
  101. /// </para>
  102. /// <para>
  103. /// If <see cref="CheckState.UnChecked"/>, the <see cref="CheckBox"/>
  104. /// will display the <c>Glyphs.CheckStateUnChecked</c> character (☐).
  105. /// </para>
  106. /// <para>
  107. /// If <see cref="CheckState.Checked"/>, the <see cref="CheckBox"/>
  108. /// will display the <c>Glyphs.CheckStateChecked</c> character (☑).
  109. /// </para>
  110. /// </remarks>
  111. public CheckState CheckedState
  112. {
  113. get => _checkedState;
  114. set => ChangeCheckedState (value);
  115. }
  116. /// <summary>
  117. /// INTERNAL Sets CheckedState.
  118. /// </summary>
  119. /// <param name="value"></param>
  120. /// <returns>
  121. /// <see langword="true"/> if state change was canceled, <see langword="false"/> if the state changed, and
  122. /// <see langword="null"/> if the state was not changed for some other reason.
  123. /// </returns>
  124. private bool? ChangeCheckedState (CheckState value)
  125. {
  126. if (_checkedState == value || (value is CheckState.None && !AllowCheckStateNone))
  127. {
  128. return null;
  129. }
  130. ResultEventArgs<CheckState> e = new (value);
  131. if (OnCheckedStateChanging (e))
  132. {
  133. return true;
  134. }
  135. CheckedStateChanging?.Invoke (this, e);
  136. if (e.Handled)
  137. {
  138. return e.Handled;
  139. }
  140. _checkedState = value;
  141. UpdateTextFormatterText ();
  142. SetNeedsLayout ();
  143. EventArgs<CheckState> args = new (in _checkedState);
  144. OnCheckedStateChanged (args);
  145. CheckedStateChanged?.Invoke (this, args);
  146. return false;
  147. }
  148. /// <summary>Called when the <see cref="CheckBox"/> state is changing.</summary>
  149. /// <remarks>
  150. /// <para>
  151. /// The state change can be cancelled by setting the args.Cancel to <see langword="true"/>.
  152. /// </para>
  153. /// </remarks>
  154. protected virtual bool OnCheckedStateChanging (ResultEventArgs<CheckState> args) { return false; }
  155. /// <summary>Raised when the <see cref="CheckBox"/> state is changing.</summary>
  156. /// <remarks>
  157. /// <para>
  158. /// This event can be cancelled. If cancelled, the <see cref="CheckBox"/> will not change its state.
  159. /// </para>
  160. /// </remarks>
  161. public event EventHandler<ResultEventArgs<CheckState>>? CheckedStateChanging;
  162. /// <summary>Called when the <see cref="CheckBox"/> state has changed.</summary>
  163. protected virtual void OnCheckedStateChanged (EventArgs<CheckState> args) { }
  164. /// <summary>Raised when the <see cref="CheckBox"/> state has changed.</summary>
  165. public event EventHandler<EventArgs<CheckState>>? CheckedStateChanged;
  166. /// <summary>
  167. /// Advances <see cref="CheckedState"/> to the next value. Invokes the cancelable <see cref="CheckedStateChanging"/>
  168. /// event.
  169. /// </summary>
  170. /// <remarks>
  171. /// <para>
  172. /// Cycles through the states <see cref="CheckState.None"/>, <see cref="CheckState.Checked"/>, and
  173. /// <see cref="CheckState.UnChecked"/>.
  174. /// </para>
  175. /// <para>
  176. /// If the <see cref="CheckedStateChanging"/> event is not canceled, the <see cref="CheckedState"/> will be updated
  177. /// and the <see cref="Command.Accept"/> event will be raised.
  178. /// </para>
  179. /// </remarks>
  180. /// <returns>
  181. /// <see langword="true"/> if state change was canceled, <see langword="false"/> if the state changed, and
  182. /// <see langword="null"/> if the state was not changed for some other reason.
  183. /// </returns>
  184. public bool? AdvanceCheckState ()
  185. {
  186. CheckState oldValue = CheckedState;
  187. ResultEventArgs<CheckState> e = new (oldValue);
  188. switch (CheckedState)
  189. {
  190. case CheckState.None:
  191. e.Result = CheckState.Checked;
  192. break;
  193. case CheckState.Checked:
  194. e.Result = CheckState.UnChecked;
  195. break;
  196. case CheckState.UnChecked:
  197. if (AllowCheckStateNone)
  198. {
  199. e.Result = CheckState.None;
  200. }
  201. else
  202. {
  203. e.Result = CheckState.Checked;
  204. }
  205. break;
  206. }
  207. bool? cancelled = ChangeCheckedState (e.Result);
  208. return cancelled;
  209. }
  210. /// <inheritdoc/>
  211. protected override void UpdateTextFormatterText ()
  212. {
  213. base.UpdateTextFormatterText ();
  214. Rune glyph = RadioStyle ? GetRadioGlyph () : GetCheckGlyph ();
  215. switch (TextAlignment)
  216. {
  217. case Alignment.Start:
  218. case Alignment.Center:
  219. case Alignment.Fill:
  220. TextFormatter.Text = $"{glyph} {Text}";
  221. break;
  222. case Alignment.End:
  223. TextFormatter.Text = $"{Text} {glyph}";
  224. break;
  225. }
  226. }
  227. private Rune GetCheckGlyph ()
  228. {
  229. return CheckedState switch
  230. {
  231. CheckState.Checked => Glyphs.CheckStateChecked,
  232. CheckState.UnChecked => Glyphs.CheckStateUnChecked,
  233. CheckState.None => Glyphs.CheckStateNone,
  234. _ => throw new ArgumentOutOfRangeException ()
  235. };
  236. }
  237. /// <summary>
  238. /// If <see langword="true"/>, the <see cref="CheckBox"/> will display radio button style glyphs (●) instead of
  239. /// checkbox style glyphs (☑).
  240. /// </summary>
  241. public bool RadioStyle { get; set; }
  242. private Rune GetRadioGlyph ()
  243. {
  244. return CheckedState switch
  245. {
  246. CheckState.Checked => Glyphs.Selected,
  247. CheckState.UnChecked => Glyphs.UnSelected,
  248. CheckState.None => Glyphs.Dot,
  249. _ => throw new ArgumentOutOfRangeException ()
  250. };
  251. }
  252. }