CheckBox.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #nullable enable
  2. namespace Terminal.Gui;
  3. /// <summary>The <see cref="CheckBox"/> <see cref="View"/> shows an on/off toggle that the user can set</summary>
  4. public class CheckBox : View
  5. {
  6. private readonly Rune _charChecked;
  7. private readonly Rune _charNullChecked;
  8. private readonly Rune _charUnChecked;
  9. private bool _allowNullChecked;
  10. private bool? _checked = false;
  11. /// <summary>
  12. /// Initializes a new instance of <see cref="CheckBox"/> based on the given text, using
  13. /// <see cref="LayoutStyle.Computed"/> layout.
  14. /// </summary>
  15. public CheckBox ()
  16. {
  17. _charNullChecked = Glyphs.NullChecked;
  18. _charChecked = Glyphs.Checked;
  19. _charUnChecked = Glyphs.UnChecked;
  20. // Ensures a height of 1 if AutoSize is set to false
  21. Height = 1;
  22. CanFocus = true;
  23. AutoSize = true;
  24. // Things this view knows how to do
  25. AddCommand (Command.Accept, OnToggled);
  26. AddCommand (Command.HotKey, OnToggled);
  27. // Default keybindings for this view
  28. KeyBindings.Add (Key.Space, Command.Accept);
  29. TitleChanged += Checkbox_TitleChanged;
  30. }
  31. private void Checkbox_TitleChanged (object? sender, StateEventArgs<string> e)
  32. {
  33. base.Text = e.NewValue;
  34. TextFormatter.HotKeySpecifier = HotKeySpecifier;
  35. }
  36. /// <inheritdoc />
  37. public override string Text
  38. {
  39. get => base.Title;
  40. set => base.Text = base.Title = value;
  41. }
  42. /// <inheritdoc />
  43. public override Rune HotKeySpecifier
  44. {
  45. get => base.HotKeySpecifier;
  46. set => TextFormatter.HotKeySpecifier = base.HotKeySpecifier = value;
  47. }
  48. /// <summary>
  49. /// If <see langword="true"/> allows <see cref="Checked"/> to be null, true or false. If <see langword="false"/>
  50. /// only allows <see cref="Checked"/> to be true or false.
  51. /// </summary>
  52. public bool AllowNullChecked
  53. {
  54. get => _allowNullChecked;
  55. set
  56. {
  57. _allowNullChecked = value;
  58. Checked ??= false;
  59. }
  60. }
  61. /// <summary>The state of the <see cref="CheckBox"/></summary>
  62. public bool? Checked
  63. {
  64. get => _checked;
  65. set
  66. {
  67. if (value is null && !AllowNullChecked)
  68. {
  69. return;
  70. }
  71. _checked = value;
  72. UpdateTextFormatterText ();
  73. OnResizeNeeded ();
  74. }
  75. }
  76. /// <inheritdoc/>
  77. public override bool OnMouseEvent (MouseEvent me)
  78. {
  79. if (!me.Flags.HasFlag (MouseFlags.Button1Clicked) || !CanFocus)
  80. {
  81. return false;
  82. }
  83. return OnToggled () == true;
  84. }
  85. /// <inheritdoc/>
  86. public override bool OnEnter (View view)
  87. {
  88. Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
  89. return base.OnEnter (view);
  90. }
  91. /// <summary>Called when the <see cref="Checked"/> property changes. Invokes the <see cref="Toggled"/> event.</summary>
  92. /// <remarks>
  93. /// </remarks>
  94. /// <returns>If <see langword="true"/> the <see cref="Toggled"/> event was canceled.</returns>
  95. public bool? OnToggled ()
  96. {
  97. StateEventArgs<bool?> e = new (Checked, false);
  98. if (AllowNullChecked)
  99. {
  100. switch (Checked)
  101. {
  102. case null:
  103. e.NewValue = true;
  104. break;
  105. case true:
  106. e.NewValue = false;
  107. break;
  108. case false:
  109. e.NewValue = null;
  110. break;
  111. }
  112. }
  113. else
  114. {
  115. e.NewValue = !Checked;
  116. }
  117. Toggled?.Invoke (this, e);
  118. if (e.Cancel)
  119. {
  120. return e.Cancel;
  121. }
  122. Checked = e.NewValue;
  123. SetNeedsDisplay ();
  124. SetFocus ();
  125. OnAccept ();
  126. return true;
  127. }
  128. /// <inheritdoc/>
  129. public override void PositionCursor () { Move (0, 0); }
  130. /// <summary>Toggled event, raised when the <see cref="CheckBox"/> is toggled.</summary>
  131. /// <remarks>
  132. /// <para>
  133. /// This event can be cancelled. If cancelled, the <see cref="CheckBox"/> will not change its state.
  134. /// </para>
  135. /// </remarks>
  136. public event EventHandler<StateEventArgs<bool?>>? Toggled;
  137. /// <inheritdoc/>
  138. protected override void UpdateTextFormatterText ()
  139. {
  140. switch (TextAlignment)
  141. {
  142. case TextAlignment.Left:
  143. case TextAlignment.Centered:
  144. case TextAlignment.Justified:
  145. TextFormatter.Text = $"{GetCheckedState ()} {GetFormatterText ()}";
  146. break;
  147. case TextAlignment.Right:
  148. TextFormatter.Text = $"{GetFormatterText ()} {GetCheckedState ()}";
  149. break;
  150. }
  151. }
  152. private Rune GetCheckedState ()
  153. {
  154. return Checked switch
  155. {
  156. true => _charChecked,
  157. false => _charUnChecked,
  158. var _ => _charNullChecked
  159. };
  160. }
  161. private string GetFormatterText ()
  162. {
  163. if (AutoSize || string.IsNullOrEmpty (Title) || Frame.Width <= 2)
  164. {
  165. return Text;
  166. }
  167. return Text [..Math.Min (Frame.Width - 2, Text.GetRuneCount ())];
  168. }
  169. }