2
0

CheckBox.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #nullable enable
  2. namespace Terminal.Gui;
  3. /// <summary>Shows a check box that can be cycled between three states.</summary>
  4. public class CheckBox : View
  5. {
  6. /// <summary>
  7. /// Gets or sets the default Highlight Style.
  8. /// </summary>
  9. [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  10. public static HighlightStyle DefaultHighlightStyle { get; set; } = HighlightStyle.PressedOutside | HighlightStyle.Pressed | HighlightStyle.Hover;
  11. /// <summary>
  12. /// Initializes a new instance of <see cref="CheckBox"/>.
  13. /// </summary>
  14. public CheckBox ()
  15. {
  16. Width = Dim.Auto (DimAutoStyle.Text);
  17. Height = Dim.Auto (DimAutoStyle.Text, minimumContentDim: 1);
  18. CanFocus = true;
  19. // Things this view knows how to do
  20. AddCommand (Command.Accept, AdvanceCheckState);
  21. AddCommand (Command.HotKey, AdvanceCheckState);
  22. // Default keybindings for this view
  23. KeyBindings.Add (Key.Space, Command.Accept);
  24. TitleChanged += Checkbox_TitleChanged;
  25. HighlightStyle = DefaultHighlightStyle;
  26. MouseClick += CheckBox_MouseClick;
  27. }
  28. private void CheckBox_MouseClick (object? sender, MouseEventEventArgs e)
  29. {
  30. e.Handled = AdvanceCheckState () == true;
  31. }
  32. private void Checkbox_TitleChanged (object? sender, EventArgs<string> e)
  33. {
  34. base.Text = e.CurrentValue;
  35. TextFormatter.HotKeySpecifier = HotKeySpecifier;
  36. }
  37. /// <inheritdoc />
  38. public override string Text
  39. {
  40. get => base.Title;
  41. set => base.Text = base.Title = value;
  42. }
  43. /// <inheritdoc />
  44. public override Rune HotKeySpecifier
  45. {
  46. get => base.HotKeySpecifier;
  47. set => TextFormatter.HotKeySpecifier = base.HotKeySpecifier = value;
  48. }
  49. private bool _allowNone = false;
  50. /// <summary>
  51. /// If <see langword="true"/> allows <see cref="CheckedState"/> to be <see cref="CheckState.None"/>. The default is <see langword="false"/>.
  52. /// </summary>
  53. public bool AllowCheckStateNone
  54. {
  55. get => _allowNone;
  56. set
  57. {
  58. if (_allowNone == value)
  59. {
  60. return;
  61. }
  62. _allowNone = value;
  63. if (CheckedState == CheckState.None)
  64. {
  65. CheckedState = CheckState.UnChecked;
  66. }
  67. }
  68. }
  69. private CheckState _checkedState = CheckState.UnChecked;
  70. /// <summary>
  71. /// The state of the <see cref="CheckBox"/>.
  72. /// </summary>
  73. /// <remarks>
  74. /// <para>
  75. /// If <see cref="AllowCheckStateNone"/> is <see langword="true"/> and <see cref="CheckState.None"/>, the <see cref="CheckBox"/>
  76. /// will display the <c>ConfigurationManager.Glyphs.CheckStateNone</c> character (☒).
  77. /// </para>
  78. /// <para>
  79. /// If <see cref="CheckState.UnChecked"/>, the <see cref="CheckBox"/>
  80. /// will display the <c>ConfigurationManager.Glyphs.CheckStateUnChecked</c> character (☐).
  81. /// </para>
  82. /// <para>
  83. /// If <see cref="CheckState.Checked"/>, the <see cref="CheckBox"/>
  84. /// will display the <c>ConfigurationManager.Glyphs.CheckStateChecked</c> character (☑).
  85. /// </para>
  86. /// </remarks>
  87. public CheckState CheckedState
  88. {
  89. get => _checkedState;
  90. set
  91. {
  92. if (_checkedState == value || (value is CheckState.None && !AllowCheckStateNone))
  93. {
  94. return;
  95. }
  96. _checkedState = value;
  97. UpdateTextFormatterText ();
  98. OnResizeNeeded ();
  99. }
  100. }
  101. /// <summary>
  102. /// Advances <see cref="CheckedState"/> to the next value. Invokes the cancelable <see cref="CheckedStateChanging"/> event.
  103. /// </summary>
  104. /// <remarks>
  105. /// </remarks>
  106. /// <returns>If <see langword="true"/> the <see cref="CheckedStateChanging"/> event was canceled.</returns>
  107. /// <remarks>
  108. /// <para>
  109. /// Cycles through the states <see cref="CheckState.None"/>, <see cref="CheckState.Checked"/>, and <see cref="CheckState.UnChecked"/>.
  110. /// </para>
  111. /// <para>
  112. /// If the <see cref="CheckedStateChanging"/> event is not canceled, the <see cref="CheckedState"/> will be updated and the <see cref="Command.Accept"/> event will be raised.
  113. /// </para>
  114. /// </remarks>
  115. public bool? AdvanceCheckState ()
  116. {
  117. CheckState oldValue = CheckedState;
  118. CancelEventArgs<CheckState> e = new (in _checkedState, ref oldValue);
  119. switch (CheckedState)
  120. {
  121. case CheckState.None:
  122. e.NewValue = CheckState.Checked;
  123. break;
  124. case CheckState.Checked:
  125. e.NewValue = CheckState.UnChecked;
  126. break;
  127. case CheckState.UnChecked:
  128. if (AllowCheckStateNone)
  129. {
  130. e.NewValue = CheckState.None;
  131. }
  132. else
  133. {
  134. e.NewValue = CheckState.Checked;
  135. }
  136. break;
  137. }
  138. CheckedStateChanging?.Invoke (this, e);
  139. if (e.Cancel)
  140. {
  141. return e.Cancel;
  142. }
  143. // By default, Command.Accept calls OnAccept, so we need to call it here to ensure that the Accept event is fired.
  144. if (OnAccept () == true)
  145. {
  146. return true;
  147. }
  148. CheckedState = e.NewValue;
  149. return true;
  150. }
  151. /// <summary>Raised when the <see cref="CheckBox"/> state is changing.</summary>
  152. /// <remarks>
  153. /// <para>
  154. /// This event can be cancelled. If cancelled, the <see cref="CheckBox"/> will not change its state.
  155. /// </para>
  156. /// </remarks>
  157. public event EventHandler<CancelEventArgs<CheckState>>? CheckedStateChanging;
  158. /// <inheritdoc/>
  159. protected override void UpdateTextFormatterText ()
  160. {
  161. base.UpdateTextFormatterText ();
  162. switch (TextAlignment)
  163. {
  164. case Alignment.Start:
  165. case Alignment.Center:
  166. case Alignment.Fill:
  167. TextFormatter.Text = $"{GetCheckedGlyph ()} {Text}";
  168. break;
  169. case Alignment.End:
  170. TextFormatter.Text = $"{Text} {GetCheckedGlyph ()}";
  171. break;
  172. }
  173. }
  174. private Rune GetCheckedGlyph ()
  175. {
  176. return CheckedState switch
  177. {
  178. CheckState.Checked => Glyphs.CheckStateChecked,
  179. CheckState.UnChecked => Glyphs.CheckStateUnChecked,
  180. CheckState.None => Glyphs.CheckStateNone,
  181. _ => throw new ArgumentOutOfRangeException ()
  182. };
  183. }
  184. }