CheckBox.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #nullable enable
  2. using System.Reflection.Metadata;
  3. namespace Terminal.Gui;
  4. /// <summary>Shows a check box that can be cycled between 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, AdvanceCheckState);
  22. // Accept (Enter key and double-click) - Raise Accept event - DO NOT advance state
  23. AddCommand (Command.Accept, RaiseAcceptEvent);
  24. // Hotkey - Advance state and raise Select event - DO NOT raise Accept
  25. AddCommand (Command.HotKey, AdvanceCheckState);
  26. TitleChanged += Checkbox_TitleChanged;
  27. HighlightStyle = DefaultHighlightStyle;
  28. MouseClick += CheckBox_MouseClick;
  29. }
  30. private void CheckBox_MouseClick (object? sender, MouseEventEventArgs e)
  31. {
  32. e.Handled = AdvanceCheckState () == true;
  33. }
  34. private void Checkbox_TitleChanged (object? sender, EventArgs<string> e)
  35. {
  36. base.Text = e.CurrentValue;
  37. TextFormatter.HotKeySpecifier = HotKeySpecifier;
  38. }
  39. /// <inheritdoc />
  40. public override string Text
  41. {
  42. get => base.Title;
  43. set => base.Text = base.Title = value;
  44. }
  45. /// <inheritdoc />
  46. public override Rune HotKeySpecifier
  47. {
  48. get => base.HotKeySpecifier;
  49. set => TextFormatter.HotKeySpecifier = base.HotKeySpecifier = value;
  50. }
  51. private bool _allowNone = false;
  52. /// <summary>
  53. /// If <see langword="true"/> allows <see cref="CheckedState"/> to be <see cref="CheckState.None"/>. The default is <see langword="false"/>.
  54. /// </summary>
  55. public bool AllowCheckStateNone
  56. {
  57. get => _allowNone;
  58. set
  59. {
  60. if (_allowNone == value)
  61. {
  62. return;
  63. }
  64. _allowNone = value;
  65. if (CheckedState == CheckState.None)
  66. {
  67. CheckedState = CheckState.UnChecked;
  68. }
  69. }
  70. }
  71. private CheckState _checkedState = CheckState.UnChecked;
  72. /// <summary>
  73. /// The state of the <see cref="CheckBox"/>.
  74. /// </summary>
  75. /// <remarks>
  76. /// <para>
  77. /// If <see cref="AllowCheckStateNone"/> is <see langword="true"/> and <see cref="CheckState.None"/>, the <see cref="CheckBox"/>
  78. /// will display the <c>ConfigurationManager.Glyphs.CheckStateNone</c> character (☒).
  79. /// </para>
  80. /// <para>
  81. /// If <see cref="CheckState.UnChecked"/>, the <see cref="CheckBox"/>
  82. /// will display the <c>ConfigurationManager.Glyphs.CheckStateUnChecked</c> character (☐).
  83. /// </para>
  84. /// <para>
  85. /// If <see cref="CheckState.Checked"/>, the <see cref="CheckBox"/>
  86. /// will display the <c>ConfigurationManager.Glyphs.CheckStateChecked</c> character (☑).
  87. /// </para>
  88. /// </remarks>
  89. public CheckState CheckedState
  90. {
  91. get => _checkedState;
  92. set => ChangeCheckedState (value);
  93. }
  94. /// <summary>
  95. /// INTERNAL Sets CheckedState.
  96. /// </summary>
  97. /// <param name="value"></param>
  98. /// <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>
  99. private bool? ChangeCheckedState (CheckState value)
  100. {
  101. if (_checkedState == value || (value is CheckState.None && !AllowCheckStateNone))
  102. {
  103. return null;
  104. }
  105. if (RaiseSelectEvent () == true)
  106. {
  107. return true;
  108. }
  109. CancelEventArgs<CheckState> e = new (in _checkedState, ref value);
  110. CheckedStateChanging?.Invoke (this, e);
  111. if (e.Cancel)
  112. {
  113. return e.Cancel;
  114. }
  115. _checkedState = value;
  116. UpdateTextFormatterText ();
  117. OnResizeNeeded ();
  118. return false;
  119. }
  120. /// <summary>
  121. /// Advances <see cref="CheckedState"/> to the next value. Invokes the cancelable <see cref="CheckedStateChanging"/> event.
  122. /// </summary>
  123. /// <remarks>
  124. /// <para>
  125. /// Cycles through the states <see cref="CheckState.None"/>, <see cref="CheckState.Checked"/>, and <see cref="CheckState.UnChecked"/>.
  126. /// </para>
  127. /// <para>
  128. /// 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.
  129. /// </para>
  130. /// </remarks>
  131. /// <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>
  132. public bool? AdvanceCheckState ()
  133. {
  134. CheckState oldValue = CheckedState;
  135. CancelEventArgs<CheckState> e = new (in _checkedState, ref oldValue);
  136. switch (CheckedState)
  137. {
  138. case CheckState.None:
  139. e.NewValue = CheckState.Checked;
  140. break;
  141. case CheckState.Checked:
  142. e.NewValue = CheckState.UnChecked;
  143. break;
  144. case CheckState.UnChecked:
  145. if (AllowCheckStateNone)
  146. {
  147. e.NewValue = CheckState.None;
  148. }
  149. else
  150. {
  151. e.NewValue = CheckState.Checked;
  152. }
  153. break;
  154. }
  155. bool? cancelled = ChangeCheckedState (e.NewValue);
  156. return !cancelled;
  157. }
  158. /// <summary>Raised when the <see cref="CheckBox"/> state is changing.</summary>
  159. /// <remarks>
  160. /// <para>
  161. /// This event can be cancelled. If cancelled, the <see cref="CheckBox"/> will not change its state.
  162. /// </para>
  163. /// </remarks>
  164. public event EventHandler<CancelEventArgs<CheckState>>? CheckedStateChanging;
  165. /// <inheritdoc/>
  166. protected override void UpdateTextFormatterText ()
  167. {
  168. base.UpdateTextFormatterText ();
  169. switch (TextAlignment)
  170. {
  171. case Alignment.Start:
  172. case Alignment.Center:
  173. case Alignment.Fill:
  174. TextFormatter.Text = $"{GetCheckedGlyph ()} {Text}";
  175. break;
  176. case Alignment.End:
  177. TextFormatter.Text = $"{Text} {GetCheckedGlyph ()}";
  178. break;
  179. }
  180. }
  181. private Rune GetCheckedGlyph ()
  182. {
  183. return CheckedState switch
  184. {
  185. CheckState.Checked => Glyphs.CheckStateChecked,
  186. CheckState.UnChecked => Glyphs.CheckStateUnChecked,
  187. CheckState.None => Glyphs.CheckStateNone,
  188. _ => throw new ArgumentOutOfRangeException ()
  189. };
  190. }
  191. }