CheckBox.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. OnResizeNeeded ();
  80. // Things this view knows how to do
  81. AddCommand (Command.ToggleChecked, () => ToggleChecked ());
  82. AddCommand (Command.Accept, () => {
  83. if (!HasFocus) {
  84. SetFocus ();
  85. }
  86. ToggleChecked ();
  87. return true;
  88. });
  89. // Default keybindings for this view
  90. KeyBindings.Add (Key.Space, Command.ToggleChecked);
  91. }
  92. /// <inheritdoc/>
  93. public override Key HotKey {
  94. get => base.HotKey;
  95. set {
  96. if (value is null || value.KeyCode is KeyCode.Unknown) {
  97. throw new ArgumentException (nameof (value));
  98. }
  99. var prev = base.HotKey;
  100. if (prev != value) {
  101. var v = value == KeyCode.Unknown ? Key.Empty: value;
  102. base.HotKey = TextFormatter.HotKey = v;
  103. // Also add Alt+HotKey
  104. if (prev != (Key)KeyCode.Null && KeyBindings.TryGet (prev.WithAlt, out _)) {
  105. if (v.KeyCode == KeyCode.Null) {
  106. KeyBindings.Remove (prev.WithAlt);
  107. } else {
  108. KeyBindings.Replace (prev.WithAlt, v.WithAlt);
  109. }
  110. } else if (v.KeyCode != KeyCode.Null) {
  111. KeyBindings.Add (v.WithAlt, Command.Accept);
  112. }
  113. }
  114. }
  115. }
  116. /// <inheritdoc/>
  117. protected override void UpdateTextFormatterText ()
  118. {
  119. switch (TextAlignment) {
  120. case TextAlignment.Left:
  121. case TextAlignment.Centered:
  122. case TextAlignment.Justified:
  123. TextFormatter.Text = $"{GetCheckedState ()} {GetFormatterText ()}";
  124. break;
  125. case TextAlignment.Right:
  126. TextFormatter.Text = $"{GetFormatterText ()} {GetCheckedState ()}";
  127. break;
  128. }
  129. }
  130. Rune GetCheckedState ()
  131. {
  132. return Checked switch {
  133. true => _charChecked,
  134. false => _charUnChecked,
  135. var _ => _charNullChecked
  136. };
  137. }
  138. string GetFormatterText ()
  139. {
  140. if (AutoSize || string.IsNullOrEmpty (Text) || Frame.Width <= 2) {
  141. return Text;
  142. }
  143. return Text [..Math.Min (Frame.Width - 2, Text.GetRuneCount ())];
  144. }
  145. /// <summary>
  146. /// The state of the <see cref="CheckBox"/>
  147. /// </summary>
  148. public bool? Checked {
  149. get => @_checked;
  150. set {
  151. if (value == null && !AllowNullChecked) {
  152. return;
  153. }
  154. @_checked = value;
  155. UpdateTextFormatterText ();
  156. OnResizeNeeded ();
  157. }
  158. }
  159. /// <summary>
  160. /// If <see langword="true"/> allows <see cref="Checked"/> to be null, true or false.
  161. /// If <see langword="false"/> only allows <see cref="Checked"/> to be true or false.
  162. /// </summary>
  163. public bool AllowNullChecked {
  164. get => _allowNullChecked;
  165. set {
  166. _allowNullChecked = value;
  167. Checked ??= false;
  168. }
  169. }
  170. ///<inheritdoc/>
  171. public override void PositionCursor ()
  172. {
  173. Move (0, 0);
  174. }
  175. bool ToggleChecked ()
  176. {
  177. if (!HasFocus) {
  178. SetFocus ();
  179. }
  180. var previousChecked = Checked;
  181. if (AllowNullChecked) {
  182. switch (previousChecked) {
  183. case null:
  184. Checked = true;
  185. break;
  186. case true:
  187. Checked = false;
  188. break;
  189. case false:
  190. Checked = null;
  191. break;
  192. }
  193. } else {
  194. Checked = !Checked;
  195. }
  196. OnToggled (new ToggleEventArgs (previousChecked, Checked));
  197. SetNeedsDisplay ();
  198. return true;
  199. }
  200. ///<inheritdoc/>
  201. public override bool MouseEvent (MouseEvent me)
  202. {
  203. if (!me.Flags.HasFlag (MouseFlags.Button1Clicked) || !CanFocus)
  204. return false;
  205. ToggleChecked ();
  206. return true;
  207. }
  208. ///<inheritdoc/>
  209. public override bool OnEnter (View view)
  210. {
  211. Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
  212. return base.OnEnter (view);
  213. }
  214. }