CheckBox.cs 8.3 KB

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