CheckBox.cs 9.3 KB

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