CheckBox.cs 5.9 KB

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