CheckBox.cs 5.9 KB

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