CheckBox.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 = View.DefaultAutoSize;
  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. HighlightStyle = Gui.HighlightStyle.PressedOutside | Gui.HighlightStyle.Pressed;
  31. MouseClick += CheckBox_MouseClick;
  32. }
  33. private void CheckBox_MouseClick (object? sender, MouseEventEventArgs e)
  34. {
  35. e.Handled = OnToggled () == true;
  36. }
  37. private void Checkbox_TitleChanged (object? sender, StateEventArgs<string> e)
  38. {
  39. base.Text = e.NewValue;
  40. TextFormatter.HotKeySpecifier = HotKeySpecifier;
  41. }
  42. /// <inheritdoc />
  43. public override string Text
  44. {
  45. get => base.Title;
  46. set => base.Text = base.Title = value;
  47. }
  48. /// <inheritdoc />
  49. public override Rune HotKeySpecifier
  50. {
  51. get => base.HotKeySpecifier;
  52. set => TextFormatter.HotKeySpecifier = base.HotKeySpecifier = value;
  53. }
  54. /// <summary>
  55. /// If <see langword="true"/> allows <see cref="Checked"/> to be null, true or false. If <see langword="false"/>
  56. /// only allows <see cref="Checked"/> to be true or false.
  57. /// </summary>
  58. public bool AllowNullChecked
  59. {
  60. get => _allowNullChecked;
  61. set
  62. {
  63. _allowNullChecked = value;
  64. Checked ??= false;
  65. }
  66. }
  67. /// <summary>The state of the <see cref="CheckBox"/></summary>
  68. public bool? Checked
  69. {
  70. get => _checked;
  71. set
  72. {
  73. if (value is null && !AllowNullChecked)
  74. {
  75. return;
  76. }
  77. _checked = value;
  78. UpdateTextFormatterText ();
  79. OnResizeNeeded ();
  80. }
  81. }
  82. /// <inheritdoc/>
  83. public override bool OnEnter (View view)
  84. {
  85. Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
  86. return base.OnEnter (view);
  87. }
  88. /// <summary>Called when the <see cref="Checked"/> property changes. Invokes the <see cref="Toggled"/> event.</summary>
  89. /// <remarks>
  90. /// </remarks>
  91. /// <returns>If <see langword="true"/> the <see cref="Toggled"/> event was canceled.</returns>
  92. public bool? OnToggled ()
  93. {
  94. StateEventArgs<bool?> e = new (Checked, false);
  95. if (AllowNullChecked)
  96. {
  97. switch (Checked)
  98. {
  99. case null:
  100. e.NewValue = true;
  101. break;
  102. case true:
  103. e.NewValue = false;
  104. break;
  105. case false:
  106. e.NewValue = null;
  107. break;
  108. }
  109. }
  110. else
  111. {
  112. e.NewValue = !Checked;
  113. }
  114. Toggled?.Invoke (this, e);
  115. if (e.Cancel)
  116. {
  117. return e.Cancel;
  118. }
  119. // By default, Command.Accept calls OnAccept, so we need to call it here to ensure that the event is fired.
  120. if (OnAccept () == true)
  121. {
  122. return true;
  123. }
  124. Checked = e.NewValue;
  125. return true;
  126. }
  127. /// <inheritdoc/>
  128. public override void PositionCursor () { Move (0, 0); }
  129. /// <summary>Toggled event, raised when the <see cref="CheckBox"/> is toggled.</summary>
  130. /// <remarks>
  131. /// <para>
  132. /// This event can be cancelled. If cancelled, the <see cref="CheckBox"/> will not change its state.
  133. /// </para>
  134. /// </remarks>
  135. public event EventHandler<StateEventArgs<bool?>>? Toggled;
  136. /// <inheritdoc/>
  137. protected override void UpdateTextFormatterText ()
  138. {
  139. switch (TextAlignment)
  140. {
  141. case TextAlignment.Left:
  142. case TextAlignment.Centered:
  143. case TextAlignment.Justified:
  144. TextFormatter.Text = $"{GetCheckedState ()} {GetFormatterText ()}";
  145. break;
  146. case TextAlignment.Right:
  147. TextFormatter.Text = $"{GetFormatterText ()} {GetCheckedState ()}";
  148. break;
  149. }
  150. }
  151. private Rune GetCheckedState ()
  152. {
  153. return Checked switch
  154. {
  155. true => _charChecked,
  156. false => _charUnChecked,
  157. var _ => _charNullChecked
  158. };
  159. }
  160. private string GetFormatterText ()
  161. {
  162. if (AutoSize || string.IsNullOrEmpty (Title) || Frame.Width <= 2)
  163. {
  164. return Text;
  165. }
  166. return Text [..Math.Min (Frame.Width - 2, Text.GetRuneCount ())];
  167. }
  168. }