CheckBox.cs 5.5 KB

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