CheckBox.cs 4.8 KB

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