CheckBox.cs 5.3 KB

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