CheckBox.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. TitleChanged += Checkbox_TitleChanged;
  23. HighlightStyle = DefaultHighlightStyle;
  24. MouseClick += CheckBox_MouseClick;
  25. }
  26. private void CheckBox_MouseClick (object? sender, MouseEventEventArgs e)
  27. {
  28. e.Handled = AdvanceCheckState () == true;
  29. }
  30. private void Checkbox_TitleChanged (object? sender, EventArgs<string> e)
  31. {
  32. base.Text = e.CurrentValue;
  33. TextFormatter.HotKeySpecifier = HotKeySpecifier;
  34. }
  35. /// <inheritdoc />
  36. public override string Text
  37. {
  38. get => base.Title;
  39. set => base.Text = base.Title = value;
  40. }
  41. /// <inheritdoc />
  42. public override Rune HotKeySpecifier
  43. {
  44. get => base.HotKeySpecifier;
  45. set => TextFormatter.HotKeySpecifier = base.HotKeySpecifier = value;
  46. }
  47. private bool _allowNone = false;
  48. /// <summary>
  49. /// If <see langword="true"/> allows <see cref="CheckedState"/> to be <see cref="CheckState.None"/>. The default is <see langword="false"/>.
  50. /// </summary>
  51. public bool AllowCheckStateNone
  52. {
  53. get => _allowNone;
  54. set
  55. {
  56. if (_allowNone == value)
  57. {
  58. return;
  59. }
  60. _allowNone = value;
  61. if (CheckedState == CheckState.None)
  62. {
  63. CheckedState = CheckState.UnChecked;
  64. }
  65. }
  66. }
  67. private CheckState _checkedState = CheckState.UnChecked;
  68. /// <summary>
  69. /// The state of the <see cref="CheckBox"/>.
  70. /// </summary>
  71. /// <remarks>
  72. /// <para>
  73. /// If <see cref="AllowCheckStateNone"/> is <see langword="true"/> and <see cref="CheckState.None"/>, the <see cref="CheckBox"/>
  74. /// will display the <c>ConfigurationManager.Glyphs.CheckStateNone</c> character (☒).
  75. /// </para>
  76. /// <para>
  77. /// If <see cref="CheckState.UnChecked"/>, the <see cref="CheckBox"/>
  78. /// will display the <c>ConfigurationManager.Glyphs.CheckStateUnChecked</c> character (☐).
  79. /// </para>
  80. /// <para>
  81. /// If <see cref="CheckState.Checked"/>, the <see cref="CheckBox"/>
  82. /// will display the <c>ConfigurationManager.Glyphs.CheckStateChecked</c> character (☑).
  83. /// </para>
  84. /// </remarks>
  85. public CheckState CheckedState
  86. {
  87. get => _checkedState;
  88. set
  89. {
  90. if (_checkedState == value || (value is CheckState.None && !AllowCheckStateNone))
  91. {
  92. return;
  93. }
  94. _checkedState = value;
  95. UpdateTextFormatterText ();
  96. OnResizeNeeded ();
  97. }
  98. }
  99. /// <summary>
  100. /// Advances <see cref="CheckedState"/> to the next value. Invokes the cancelable <see cref="CheckedStateChanging"/> event.
  101. /// </summary>
  102. /// <remarks>
  103. /// </remarks>
  104. /// <returns>If <see langword="true"/> the <see cref="CheckedStateChanging"/> event was canceled.</returns>
  105. /// <remarks>
  106. /// <para>
  107. /// Cycles through the states <see cref="CheckState.None"/>, <see cref="CheckState.Checked"/>, and <see cref="CheckState.UnChecked"/>.
  108. /// </para>
  109. /// <para>
  110. /// 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.
  111. /// </para>
  112. /// </remarks>
  113. public bool? AdvanceCheckState ()
  114. {
  115. CheckState oldValue = CheckedState;
  116. CancelEventArgs<CheckState> e = new (in _checkedState, ref oldValue);
  117. switch (CheckedState)
  118. {
  119. case CheckState.None:
  120. e.NewValue = CheckState.Checked;
  121. break;
  122. case CheckState.Checked:
  123. e.NewValue = CheckState.UnChecked;
  124. break;
  125. case CheckState.UnChecked:
  126. if (AllowCheckStateNone)
  127. {
  128. e.NewValue = CheckState.None;
  129. }
  130. else
  131. {
  132. e.NewValue = CheckState.Checked;
  133. }
  134. break;
  135. }
  136. CheckedStateChanging?.Invoke (this, e);
  137. if (e.Cancel)
  138. {
  139. return e.Cancel;
  140. }
  141. // By default, Command.Accept calls OnAccept, so we need to call it here to ensure that the Accept event is fired.
  142. if (RaiseAcceptEvent () == true)
  143. {
  144. return true;
  145. }
  146. CheckedState = e.NewValue;
  147. return true;
  148. }
  149. /// <summary>Raised when the <see cref="CheckBox"/> state is changing.</summary>
  150. /// <remarks>
  151. /// <para>
  152. /// This event can be cancelled. If cancelled, the <see cref="CheckBox"/> will not change its state.
  153. /// </para>
  154. /// </remarks>
  155. public event EventHandler<CancelEventArgs<CheckState>>? CheckedStateChanging;
  156. /// <inheritdoc/>
  157. protected override void UpdateTextFormatterText ()
  158. {
  159. base.UpdateTextFormatterText ();
  160. switch (TextAlignment)
  161. {
  162. case Alignment.Start:
  163. case Alignment.Center:
  164. case Alignment.Fill:
  165. TextFormatter.Text = $"{GetCheckedGlyph ()} {Text}";
  166. break;
  167. case Alignment.End:
  168. TextFormatter.Text = $"{Text} {GetCheckedGlyph ()}";
  169. break;
  170. }
  171. }
  172. private Rune GetCheckedGlyph ()
  173. {
  174. return CheckedState switch
  175. {
  176. CheckState.Checked => Glyphs.CheckStateChecked,
  177. CheckState.UnChecked => Glyphs.CheckStateUnChecked,
  178. CheckState.None => Glyphs.CheckStateNone,
  179. _ => throw new ArgumentOutOfRangeException ()
  180. };
  181. }
  182. }