CheckBox.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 (ICommandContext commandContext)
  29. {
  30. if (commandContext is not CommandContext<KeyBinding> ctx)
  31. {
  32. return false;
  33. }
  34. bool? cancelled = AdvanceCheckState ();
  35. if (cancelled is true)
  36. {
  37. return true;
  38. }
  39. if (RaiseSelecting (ctx) is true)
  40. {
  41. return true;
  42. }
  43. return ctx.Command == Command.HotKey ? cancelled : cancelled is false;
  44. }
  45. private void Checkbox_TitleChanged (object? sender, EventArgs<string> e)
  46. {
  47. base.Text = e.CurrentValue;
  48. TextFormatter.HotKeySpecifier = HotKeySpecifier;
  49. }
  50. /// <inheritdoc/>
  51. public override string Text
  52. {
  53. get => Title;
  54. set => base.Text = Title = value;
  55. }
  56. /// <inheritdoc/>
  57. public override Rune HotKeySpecifier
  58. {
  59. get => base.HotKeySpecifier;
  60. set => TextFormatter.HotKeySpecifier = base.HotKeySpecifier = value;
  61. }
  62. private bool _allowNone;
  63. /// <summary>
  64. /// If <see langword="true"/> allows <see cref="CheckedState"/> to be <see cref="CheckState.None"/>. The default is
  65. /// <see langword="false"/>.
  66. /// </summary>
  67. public bool AllowCheckStateNone
  68. {
  69. get => _allowNone;
  70. set
  71. {
  72. if (_allowNone == value)
  73. {
  74. return;
  75. }
  76. _allowNone = value;
  77. if (CheckedState == CheckState.None)
  78. {
  79. CheckedState = CheckState.UnChecked;
  80. }
  81. }
  82. }
  83. private CheckState _checkedState = CheckState.UnChecked;
  84. /// <summary>
  85. /// The state of the <see cref="CheckBox"/>.
  86. /// </summary>
  87. /// <remarks>
  88. /// <para>
  89. /// If <see cref="AllowCheckStateNone"/> is <see langword="true"/> and <see cref="CheckState.None"/>, the
  90. /// <see cref="CheckBox"/>
  91. /// will display the <c>ConfigurationManager.Glyphs.CheckStateNone</c> character (☒).
  92. /// </para>
  93. /// <para>
  94. /// If <see cref="CheckState.UnChecked"/>, the <see cref="CheckBox"/>
  95. /// will display the <c>ConfigurationManager.Glyphs.CheckStateUnChecked</c> character (☐).
  96. /// </para>
  97. /// <para>
  98. /// If <see cref="CheckState.Checked"/>, the <see cref="CheckBox"/>
  99. /// will display the <c>ConfigurationManager.Glyphs.CheckStateChecked</c> character (☑).
  100. /// </para>
  101. /// </remarks>
  102. public CheckState CheckedState
  103. {
  104. get => _checkedState;
  105. set => ChangeCheckedState (value);
  106. }
  107. /// <summary>
  108. /// INTERNAL Sets CheckedState.
  109. /// </summary>
  110. /// <param name="value"></param>
  111. /// <returns>
  112. /// <see langword="true"/> if state change was canceled, <see langword="false"/> if the state changed, and
  113. /// <see langword="null"/> if the state was not changed for some other reason.
  114. /// </returns>
  115. private bool? ChangeCheckedState (CheckState value)
  116. {
  117. if (_checkedState == value || (value is CheckState.None && !AllowCheckStateNone))
  118. {
  119. return null;
  120. }
  121. CancelEventArgs<CheckState> e = new (in _checkedState, ref value);
  122. if (OnCheckedStateChanging (e))
  123. {
  124. return true;
  125. }
  126. CheckedStateChanging?.Invoke (this, e);
  127. if (e.Cancel)
  128. {
  129. return e.Cancel;
  130. }
  131. _checkedState = value;
  132. UpdateTextFormatterText ();
  133. SetNeedsLayout ();
  134. EventArgs<CheckState> args = new (in _checkedState);
  135. OnCheckedStateChanged (args);
  136. CheckedStateChanged?.Invoke (this, args);
  137. return false;
  138. }
  139. /// <summary>Called when the <see cref="CheckBox"/> state is changing.</summary>
  140. /// <remarks>
  141. /// <para>
  142. /// The state cahnge can be cancelled by setting the args.Cancel to <see langword="true"/>.
  143. /// </para>
  144. /// </remarks>
  145. protected virtual bool OnCheckedStateChanging (CancelEventArgs<CheckState> args) { return false; }
  146. /// <summary>Raised when the <see cref="CheckBox"/> state is changing.</summary>
  147. /// <remarks>
  148. /// <para>
  149. /// This event can be cancelled. If cancelled, the <see cref="CheckBox"/> will not change its state.
  150. /// </para>
  151. /// </remarks>
  152. public event EventHandler<CancelEventArgs<CheckState>>? CheckedStateChanging;
  153. /// <summary>Called when the <see cref="CheckBox"/> state has changed.</summary>
  154. protected virtual void OnCheckedStateChanged (EventArgs<CheckState> args) { }
  155. /// <summary>Raised when the <see cref="CheckBox"/> state has changed.</summary>
  156. public event EventHandler<EventArgs<CheckState>>? CheckedStateChanged;
  157. /// <summary>
  158. /// Advances <see cref="CheckedState"/> to the next value. Invokes the cancelable <see cref="CheckedStateChanging"/>
  159. /// event.
  160. /// </summary>
  161. /// <remarks>
  162. /// <para>
  163. /// Cycles through the states <see cref="CheckState.None"/>, <see cref="CheckState.Checked"/>, and
  164. /// <see cref="CheckState.UnChecked"/>.
  165. /// </para>
  166. /// <para>
  167. /// If the <see cref="CheckedStateChanging"/> event is not canceled, the <see cref="CheckedState"/> will be updated
  168. /// and the <see cref="Command.Accept"/> event will be raised.
  169. /// </para>
  170. /// </remarks>
  171. /// <returns>
  172. /// <see langword="true"/> if state change was canceled, <see langword="false"/> if the state changed, and
  173. /// <see langword="null"/> if the state was not changed for some other reason.
  174. /// </returns>
  175. public bool? AdvanceCheckState ()
  176. {
  177. CheckState oldValue = CheckedState;
  178. CancelEventArgs<CheckState> e = new (in _checkedState, ref oldValue);
  179. switch (CheckedState)
  180. {
  181. case CheckState.None:
  182. e.NewValue = CheckState.Checked;
  183. break;
  184. case CheckState.Checked:
  185. e.NewValue = CheckState.UnChecked;
  186. break;
  187. case CheckState.UnChecked:
  188. if (AllowCheckStateNone)
  189. {
  190. e.NewValue = CheckState.None;
  191. }
  192. else
  193. {
  194. e.NewValue = CheckState.Checked;
  195. }
  196. break;
  197. }
  198. bool? cancelled = ChangeCheckedState (e.NewValue);
  199. return cancelled;
  200. }
  201. /// <inheritdoc/>
  202. protected override void UpdateTextFormatterText ()
  203. {
  204. base.UpdateTextFormatterText ();
  205. switch (TextAlignment)
  206. {
  207. case Alignment.Start:
  208. case Alignment.Center:
  209. case Alignment.Fill:
  210. TextFormatter.Text = $"{GetCheckedGlyph ()} {Text}";
  211. break;
  212. case Alignment.End:
  213. TextFormatter.Text = $"{Text} {GetCheckedGlyph ()}";
  214. break;
  215. }
  216. }
  217. private Rune GetCheckedGlyph ()
  218. {
  219. return CheckedState switch
  220. {
  221. CheckState.Checked => Glyphs.CheckStateChecked,
  222. CheckState.UnChecked => Glyphs.CheckStateUnChecked,
  223. CheckState.None => Glyphs.CheckStateNone,
  224. _ => throw new ArgumentOutOfRangeException ()
  225. };
  226. }
  227. }