CheckBox.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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, OnToggled);
  24. AddCommand (Command.HotKey, OnToggled);
  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 = OnToggled () == true;
  34. }
  35. private void Checkbox_TitleChanged (object? sender, StateEventArgs<string> e)
  36. {
  37. base.Text = e.NewValue;
  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>The state of the <see cref="CheckBox"/></summary>
  66. public bool? Checked
  67. {
  68. get => _checked;
  69. set
  70. {
  71. if (value is null && !AllowNullChecked)
  72. {
  73. return;
  74. }
  75. _checked = value;
  76. UpdateTextFormatterText ();
  77. OnResizeNeeded ();
  78. }
  79. }
  80. /// <summary>Called when the <see cref="Checked"/> property changes. Invokes the <see cref="Toggled"/> event.</summary>
  81. /// <remarks>
  82. /// </remarks>
  83. /// <returns>If <see langword="true"/> the <see cref="Toggled"/> event was canceled.</returns>
  84. public bool? OnToggled ()
  85. {
  86. StateEventArgs<bool?> e = new (Checked, false);
  87. if (AllowNullChecked)
  88. {
  89. switch (Checked)
  90. {
  91. case null:
  92. e.NewValue = true;
  93. break;
  94. case true:
  95. e.NewValue = false;
  96. break;
  97. case false:
  98. e.NewValue = null;
  99. break;
  100. }
  101. }
  102. else
  103. {
  104. e.NewValue = !Checked;
  105. }
  106. Toggled?.Invoke (this, e);
  107. if (e.Cancel)
  108. {
  109. return e.Cancel;
  110. }
  111. // By default, Command.Accept calls OnAccept, so we need to call it here to ensure that the event is fired.
  112. if (OnAccept () == true)
  113. {
  114. return true;
  115. }
  116. Checked = e.NewValue;
  117. return true;
  118. }
  119. /// <summary>Toggled event, raised when the <see cref="CheckBox"/> is toggled.</summary>
  120. /// <remarks>
  121. /// <para>
  122. /// This event can be cancelled. If cancelled, the <see cref="CheckBox"/> will not change its state.
  123. /// </para>
  124. /// </remarks>
  125. public event EventHandler<StateEventArgs<bool?>>? Toggled;
  126. /// <inheritdoc/>
  127. protected override void UpdateTextFormatterText ()
  128. {
  129. switch (TextAlignment)
  130. {
  131. case Alignment.Start:
  132. case Alignment.Center:
  133. case Alignment.Fill:
  134. TextFormatter.Text = $"{GetCheckedState ()} {Text}";
  135. break;
  136. case Alignment.End:
  137. TextFormatter.Text = $"{Text} {GetCheckedState ()}";
  138. break;
  139. }
  140. }
  141. private Rune GetCheckedState ()
  142. {
  143. return Checked switch
  144. {
  145. true => _charChecked,
  146. false => _charUnChecked,
  147. var _ => _charNullChecked
  148. };
  149. }
  150. }