CheckBox.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. using System;
  2. using System.Text;
  3. namespace Terminal.Gui;
  4. /// <summary>
  5. /// The <see cref="CheckBox"/> <see cref="View"/> shows an on/off toggle that the user can set
  6. /// </summary>
  7. public class CheckBox : View {
  8. Rune _charNullChecked;
  9. Rune _charChecked;
  10. Rune _charUnChecked;
  11. bool? @_checked;
  12. bool _allowNullChecked;
  13. /// <summary>
  14. /// Toggled event, raised when the <see cref="CheckBox"/> is toggled.
  15. /// </summary>
  16. /// <remarks>
  17. /// Client code can hook up to this event, it is
  18. /// raised when the <see cref="CheckBox"/> is activated either with
  19. /// the mouse or the keyboard. The passed <c>bool</c> contains the previous state.
  20. /// </remarks>
  21. public event EventHandler<ToggleEventArgs> Toggled;
  22. /// <summary>
  23. /// Called when the <see cref="Checked"/> property changes. Invokes the <see cref="Toggled"/> event.
  24. /// </summary>
  25. public virtual void OnToggled (ToggleEventArgs e)
  26. {
  27. Toggled?.Invoke (this, e);
  28. }
  29. /// <summary>
  30. /// Initializes a new instance of <see cref="CheckBox"/> based on the given text, using <see cref="LayoutStyle.Computed"/> layout.
  31. /// </summary>
  32. public CheckBox () : this (string.Empty) { }
  33. /// <summary>
  34. /// Initializes a new instance of <see cref="CheckBox"/> based on the given text, using <see cref="LayoutStyle.Computed"/> layout.
  35. /// </summary>
  36. /// <param name="s">S.</param>
  37. /// <param name="is_checked">If set to <c>true</c> is checked.</param>
  38. public CheckBox (string s, bool is_checked = false) : base ()
  39. {
  40. SetInitialProperties (s, is_checked);
  41. }
  42. /// <summary>
  43. /// Initializes a new instance of <see cref="CheckBox"/> using <see cref="LayoutStyle.Absolute"/> layout.
  44. /// </summary>
  45. /// <remarks>
  46. /// The size of <see cref="CheckBox"/> is computed based on the
  47. /// text length. This <see cref="CheckBox"/> is not toggled.
  48. /// </remarks>
  49. public CheckBox (int x, int y, string s) : this (x, y, s, false)
  50. {
  51. }
  52. /// <summary>
  53. /// Initializes a new instance of <see cref="CheckBox"/> using <see cref="LayoutStyle.Absolute"/> layout.
  54. /// </summary>
  55. /// <remarks>
  56. /// The size of <see cref="CheckBox"/> is computed based on the
  57. /// text length.
  58. /// </remarks>
  59. public CheckBox (int x, int y, string s, bool is_checked) : base (new Rect (x, y, s.Length, 1))
  60. {
  61. SetInitialProperties (s, is_checked);
  62. }
  63. // TODO: v2 - Remove constructors with parameters
  64. /// <summary>
  65. /// Private helper to set the initial properties of the View that were provided via constructors.
  66. /// </summary>
  67. /// <param name="s"></param>
  68. /// <param name="is_checked"></param>
  69. void SetInitialProperties (string s, bool is_checked)
  70. {
  71. _charNullChecked = CM.Glyphs.NullChecked;
  72. _charChecked = CM.Glyphs.Checked;
  73. _charUnChecked = CM.Glyphs.UnChecked;
  74. Checked = is_checked;
  75. HotKeySpecifier = (Rune)'_';
  76. CanFocus = true;
  77. AutoSize = true;
  78. Text = s;
  79. // Things this view knows how to do
  80. AddCommand (Command.ToggleChecked, () => ToggleChecked ());
  81. AddCommand (Command.Accept, () => {
  82. if (!HasFocus) {
  83. SetFocus ();
  84. }
  85. ToggleChecked ();
  86. return true;
  87. });
  88. // Default keybindings for this view
  89. KeyBindings.Add (Key.Space, Command.ToggleChecked);
  90. }
  91. /// <inheritdoc/>
  92. public override Key HotKey {
  93. get => base.HotKey;
  94. set {
  95. if (value is null) {
  96. throw new ArgumentException (nameof (value));
  97. }
  98. var prev = base.HotKey;
  99. if (prev != value) {
  100. base.HotKey = TextFormatter.HotKey = value;
  101. // Also add Alt+HotKey
  102. if (prev != Key.Empty && KeyBindings.TryGet (prev.WithAlt, out _)) {
  103. if (value.KeyCode == KeyCode.Null) {
  104. KeyBindings.Remove (prev.WithAlt);
  105. } else {
  106. KeyBindings.Replace (prev.WithAlt, value.WithAlt);
  107. }
  108. } else if (value != Key.Empty) {
  109. KeyBindings.Add (value.WithAlt, Command.Accept);
  110. }
  111. }
  112. }
  113. }
  114. /// <inheritdoc/>
  115. protected override void UpdateTextFormatterText ()
  116. {
  117. switch (TextAlignment) {
  118. case TextAlignment.Left:
  119. case TextAlignment.Centered:
  120. case TextAlignment.Justified:
  121. TextFormatter.Text = $"{GetCheckedState ()} {GetFormatterText ()}";
  122. break;
  123. case TextAlignment.Right:
  124. TextFormatter.Text = $"{GetFormatterText ()} {GetCheckedState ()}";
  125. break;
  126. }
  127. }
  128. Rune GetCheckedState ()
  129. {
  130. return Checked switch {
  131. true => _charChecked,
  132. false => _charUnChecked,
  133. var _ => _charNullChecked
  134. };
  135. }
  136. string GetFormatterText ()
  137. {
  138. if (AutoSize || string.IsNullOrEmpty (Text) || Frame.Width <= 2) {
  139. return Text;
  140. }
  141. return Text [..Math.Min (Frame.Width - 2, Text.GetRuneCount ())];
  142. }
  143. /// <summary>
  144. /// The state of the <see cref="CheckBox"/>
  145. /// </summary>
  146. public bool? Checked {
  147. get => @_checked;
  148. set {
  149. if (value == null && !AllowNullChecked) {
  150. return;
  151. }
  152. @_checked = value;
  153. UpdateTextFormatterText ();
  154. OnResizeNeeded ();
  155. }
  156. }
  157. /// <summary>
  158. /// If <see langword="true"/> allows <see cref="Checked"/> to be null, true or false.
  159. /// If <see langword="false"/> only allows <see cref="Checked"/> to be true or false.
  160. /// </summary>
  161. public bool AllowNullChecked {
  162. get => _allowNullChecked;
  163. set {
  164. _allowNullChecked = value;
  165. Checked ??= false;
  166. }
  167. }
  168. ///<inheritdoc/>
  169. public override void PositionCursor ()
  170. {
  171. Move (0, 0);
  172. }
  173. bool ToggleChecked ()
  174. {
  175. if (!HasFocus) {
  176. SetFocus ();
  177. }
  178. var previousChecked = Checked;
  179. if (AllowNullChecked) {
  180. switch (previousChecked) {
  181. case null:
  182. Checked = true;
  183. break;
  184. case true:
  185. Checked = false;
  186. break;
  187. case false:
  188. Checked = null;
  189. break;
  190. }
  191. } else {
  192. Checked = !Checked;
  193. }
  194. OnToggled (new ToggleEventArgs (previousChecked, Checked));
  195. SetNeedsDisplay ();
  196. return true;
  197. }
  198. ///<inheritdoc/>
  199. public override bool MouseEvent (MouseEvent me)
  200. {
  201. if (!me.Flags.HasFlag (MouseFlags.Button1Clicked) || !CanFocus)
  202. return false;
  203. ToggleChecked ();
  204. return true;
  205. }
  206. ///<inheritdoc/>
  207. public override bool OnEnter (View view)
  208. {
  209. Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
  210. return base.OnEnter (view);
  211. }
  212. }