CheckBox.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #nullable enable
  2. namespace Terminal.Gui;
  3. /// <summary>Shows a check box that can be cycled between three states.</summary>
  4. public class CheckBox : View
  5. {
  6. /// <summary>
  7. /// Initializes a new instance of <see cref="CheckBox"/>.
  8. /// </summary>
  9. public CheckBox ()
  10. {
  11. Width = Dim.Auto (DimAutoStyle.Text);
  12. Height = Dim.Auto (DimAutoStyle.Text, minimumContentDim: 1);
  13. CanFocus = true;
  14. // Things this view knows how to do
  15. AddCommand (Command.Accept, AdvanceCheckState);
  16. AddCommand (Command.HotKey, AdvanceCheckState);
  17. // Default keybindings for this view
  18. KeyBindings.Add (Key.Space, Command.Accept);
  19. TitleChanged += Checkbox_TitleChanged;
  20. HighlightStyle = Gui.HighlightStyle.PressedOutside | Gui.HighlightStyle.Pressed;
  21. MouseClick += CheckBox_MouseClick;
  22. }
  23. private void CheckBox_MouseClick (object? sender, MouseEventEventArgs e)
  24. {
  25. e.Handled = AdvanceCheckState () == true;
  26. }
  27. private void Checkbox_TitleChanged (object? sender, EventArgs<string> e)
  28. {
  29. base.Text = e.CurrentValue;
  30. TextFormatter.HotKeySpecifier = HotKeySpecifier;
  31. }
  32. /// <inheritdoc />
  33. public override string Text
  34. {
  35. get => base.Title;
  36. set => base.Text = base.Title = value;
  37. }
  38. /// <inheritdoc />
  39. public override Rune HotKeySpecifier
  40. {
  41. get => base.HotKeySpecifier;
  42. set => TextFormatter.HotKeySpecifier = base.HotKeySpecifier = value;
  43. }
  44. private bool _allowNone = false;
  45. /// <summary>
  46. /// If <see langword="true"/> allows <see cref="CheckedState"/> to be <see cref="CheckState.None"/>. The default is <see langword="false"/>.
  47. /// </summary>
  48. public bool AllowCheckStateNone
  49. {
  50. get => _allowNone;
  51. set
  52. {
  53. if (_allowNone == value)
  54. {
  55. return;
  56. }
  57. _allowNone = value;
  58. if (CheckedState == CheckState.None)
  59. {
  60. CheckedState = CheckState.UnChecked;
  61. }
  62. }
  63. }
  64. private CheckState _checkedState = CheckState.UnChecked;
  65. /// <summary>
  66. /// The state of the <see cref="CheckBox"/>.
  67. /// </summary>
  68. /// <remarks>
  69. /// <para>
  70. /// If <see cref="AllowCheckStateNone"/> is <see langword="true"/> and <see cref="CheckState.None"/>, the <see cref="CheckBox"/>
  71. /// will display the <c>ConfigurationManager.Glyphs.CheckStateNone</c> character (☒).
  72. /// </para>
  73. /// <para>
  74. /// If <see cref="CheckState.UnChecked"/>, the <see cref="CheckBox"/>
  75. /// will display the <c>ConfigurationManager.Glyphs.CheckStateUnChecked</c> character (☐).
  76. /// </para>
  77. /// <para>
  78. /// If <see cref="CheckState.Checked"/>, the <see cref="CheckBox"/>
  79. /// will display the <c>ConfigurationManager.Glyphs.CheckStateChecked</c> character (☑).
  80. /// </para>
  81. /// </remarks>
  82. public CheckState CheckedState
  83. {
  84. get => _checkedState;
  85. set
  86. {
  87. if (_checkedState == value || (value is CheckState.None && !AllowCheckStateNone))
  88. {
  89. return;
  90. }
  91. _checkedState = value;
  92. UpdateTextFormatterText ();
  93. OnResizeNeeded ();
  94. }
  95. }
  96. /// <summary>
  97. /// Advances <see cref="CheckedState"/> to the next value. Invokes the cancelable <see cref="CheckedStateChanging"/> event.
  98. /// </summary>
  99. /// <remarks>
  100. /// </remarks>
  101. /// <returns>If <see langword="true"/> the <see cref="CheckedStateChanging"/> event was canceled.</returns>
  102. /// <remarks>
  103. /// <para>
  104. /// Cycles through the states <see cref="CheckState.None"/>, <see cref="CheckState.Checked"/>, and <see cref="CheckState.UnChecked"/>.
  105. /// </para>
  106. /// <para>
  107. /// 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.
  108. /// </para>
  109. /// </remarks>
  110. public bool? AdvanceCheckState ()
  111. {
  112. CheckState oldValue = CheckedState;
  113. CancelEventArgs<CheckState> e = new (in _checkedState, ref oldValue);
  114. switch (CheckedState)
  115. {
  116. case CheckState.None:
  117. e.NewValue = CheckState.Checked;
  118. break;
  119. case CheckState.Checked:
  120. e.NewValue = CheckState.UnChecked;
  121. break;
  122. case CheckState.UnChecked:
  123. if (AllowCheckStateNone)
  124. {
  125. e.NewValue = CheckState.None;
  126. }
  127. else
  128. {
  129. e.NewValue = CheckState.Checked;
  130. }
  131. break;
  132. }
  133. CheckedStateChanging?.Invoke (this, e);
  134. if (e.Cancel)
  135. {
  136. return e.Cancel;
  137. }
  138. // By default, Command.Accept calls OnAccept, so we need to call it here to ensure that the Accept event is fired.
  139. if (OnAccept () == true)
  140. {
  141. return true;
  142. }
  143. CheckedState = e.NewValue;
  144. return true;
  145. }
  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. /// <inheritdoc/>
  154. protected override void UpdateTextFormatterText ()
  155. {
  156. base.UpdateTextFormatterText ();
  157. switch (TextAlignment)
  158. {
  159. case Alignment.Start:
  160. case Alignment.Center:
  161. case Alignment.Fill:
  162. TextFormatter.Text = $"{GetCheckedGlyph ()} {Text}";
  163. break;
  164. case Alignment.End:
  165. TextFormatter.Text = $"{Text} {GetCheckedGlyph ()}";
  166. break;
  167. }
  168. }
  169. private Rune GetCheckedGlyph ()
  170. {
  171. return CheckedState switch
  172. {
  173. CheckState.Checked => Glyphs.CheckStateChecked,
  174. CheckState.UnChecked => Glyphs.CheckStateUnChecked,
  175. CheckState.None => Glyphs.CheckStateNone,
  176. _ => throw new ArgumentOutOfRangeException ()
  177. };
  178. }
  179. }