Button.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. //
  2. // Button.cs: Button control
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. namespace Terminal.Gui;
  8. /// <summary>Button is a <see cref="View"/> that provides an item that invokes raises the <see cref="View.Accept"/> event.</summary>
  9. /// <remarks>
  10. /// <para>
  11. /// Provides a button showing text that raises the <see cref="View.Accept"/> event when clicked on with a mouse or
  12. /// when the user presses SPACE, ENTER, or the <see cref="View.HotKey"/>. The hot key is the first letter or digit
  13. /// following the first underscore ('_') in the button text.
  14. /// </para>
  15. /// <para>Use <see cref="View.HotKeySpecifier"/> to change the hot key specifier from the default of ('_').</para>
  16. /// <para>
  17. /// When the button is configured as the default (<see cref="IsDefault"/>) and the user presses the ENTER key, if
  18. /// no other <see cref="View"/> processes the key, the <see cref="Button"/>'s <see cref="View.Accept"/> event will
  19. /// be fired.
  20. /// </para>
  21. /// <para>
  22. /// Set <see cref="View.WantContinuousButtonPressed"/> to <see langword="true"/> to have the
  23. /// <see cref="View.Accept"/> event
  24. /// invoked repeatedly while the button is pressed.
  25. /// </para>
  26. /// </remarks>
  27. public class Button : View, IDesignable
  28. {
  29. private readonly Rune _leftBracket;
  30. private readonly Rune _leftDefault;
  31. private readonly Rune _rightBracket;
  32. private readonly Rune _rightDefault;
  33. private bool _isDefault;
  34. /// <summary>
  35. /// Gets or sets whether <see cref="Button"/>s are shown with a shadow effect by default.
  36. /// </summary>
  37. [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  38. public static ShadowStyle DefaultShadow { get; set; } = ShadowStyle.None;
  39. /// <summary>
  40. /// Gets or sets the default Highlight Style.
  41. /// </summary>
  42. [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  43. public static HighlightStyle DefaultHighlightStyle { get; set; } = HighlightStyle.Pressed | HighlightStyle.Hover;
  44. /// <summary>Initializes a new instance of <see cref="Button"/>.</summary>
  45. public Button ()
  46. {
  47. TextAlignment = Alignment.Center;
  48. VerticalTextAlignment = Alignment.Center;
  49. _leftBracket = Glyphs.LeftBracket;
  50. _rightBracket = Glyphs.RightBracket;
  51. _leftDefault = Glyphs.LeftDefaultIndicator;
  52. _rightDefault = Glyphs.RightDefaultIndicator;
  53. Height = Dim.Auto (DimAutoStyle.Text);
  54. Width = Dim.Auto (DimAutoStyle.Text);
  55. CanFocus = true;
  56. // Override default behavior of View
  57. AddCommand (
  58. Command.HotKey,
  59. () =>
  60. {
  61. bool cachedIsDefault = IsDefault; // Supports "Swap Default" in Buttons scenario
  62. bool? handled = RaiseAcceptEvent ();
  63. if (handled == true)
  64. {
  65. return true;
  66. }
  67. SetFocus ();
  68. // TODO: If `IsDefault` were a property on `View` *any* View could work this way. That's theoretical as
  69. // TODO: no use-case has been identified for any View other than Button to act like this.
  70. // If Accept was not handled...
  71. if (cachedIsDefault && SuperView is { })
  72. {
  73. return SuperView.InvokeCommand (Command.Accept);
  74. }
  75. return false;
  76. });
  77. KeyBindings.Add (Key.Space, Command.HotKey);
  78. KeyBindings.Add (Key.Enter, Command.HotKey);
  79. TitleChanged += Button_TitleChanged;
  80. MouseClick += Button_MouseClick;
  81. ShadowStyle = DefaultShadow;
  82. HighlightStyle = DefaultHighlightStyle;
  83. }
  84. private bool _wantContinuousButtonPressed;
  85. /// <inheritdoc/>
  86. public override bool WantContinuousButtonPressed
  87. {
  88. get => _wantContinuousButtonPressed;
  89. set
  90. {
  91. if (value == _wantContinuousButtonPressed)
  92. {
  93. return;
  94. }
  95. _wantContinuousButtonPressed = value;
  96. if (_wantContinuousButtonPressed)
  97. {
  98. HighlightStyle |= HighlightStyle.PressedOutside;
  99. }
  100. else
  101. {
  102. HighlightStyle &= ~HighlightStyle.PressedOutside;
  103. }
  104. }
  105. }
  106. private void Button_MouseClick (object sender, MouseEventEventArgs e) { e.Handled = InvokeCommand (Command.HotKey) == true; }
  107. private void Button_TitleChanged (object sender, EventArgs<string> e)
  108. {
  109. base.Text = e.CurrentValue;
  110. TextFormatter.HotKeySpecifier = HotKeySpecifier;
  111. }
  112. /// <inheritdoc/>
  113. public override string Text
  114. {
  115. get => Title;
  116. set => base.Text = Title = value;
  117. }
  118. /// <inheritdoc/>
  119. public override Rune HotKeySpecifier
  120. {
  121. get => base.HotKeySpecifier;
  122. set => TextFormatter.HotKeySpecifier = base.HotKeySpecifier = value;
  123. }
  124. /// <summary>
  125. /// Gets or sets whether the <see cref="Button"/> will invoke the <see cref="Command.Accept"/>
  126. /// command on the <see cref="View.SuperView"/> if <see cref="View.Accept"/> is not handled by a subscriber.
  127. /// </summary>
  128. public bool IsDefault
  129. {
  130. get => _isDefault;
  131. set
  132. {
  133. _isDefault = value;
  134. UpdateTextFormatterText ();
  135. OnResizeNeeded ();
  136. }
  137. }
  138. /// <summary></summary>
  139. public bool NoDecorations { get; set; }
  140. /// <summary></summary>
  141. public bool NoPadding { get; set; }
  142. /// <inheritdoc/>
  143. public override Point? PositionCursor ()
  144. {
  145. if (HotKey.IsValid && Text != "")
  146. {
  147. for (var i = 0; i < TextFormatter.Text.GetRuneCount (); i++)
  148. {
  149. if (TextFormatter.Text [i] == Text [0])
  150. {
  151. Move (i, 0);
  152. return null; // Don't show the cursor
  153. }
  154. }
  155. }
  156. return base.PositionCursor ();
  157. }
  158. /// <inheritdoc/>
  159. protected override void UpdateTextFormatterText ()
  160. {
  161. base.UpdateTextFormatterText ();
  162. if (NoDecorations)
  163. {
  164. TextFormatter.Text = Text;
  165. }
  166. else if (IsDefault)
  167. {
  168. TextFormatter.Text = $"{_leftBracket}{_leftDefault} {Text} {_rightDefault}{_rightBracket}";
  169. }
  170. else
  171. {
  172. if (NoPadding)
  173. {
  174. TextFormatter.Text = $"{_leftBracket}{Text}{_rightBracket}";
  175. }
  176. else
  177. {
  178. TextFormatter.Text = $"{_leftBracket} {Text} {_rightBracket}";
  179. }
  180. }
  181. }
  182. /// <inheritdoc/>
  183. public bool EnableForDesign ()
  184. {
  185. Title = "_Button";
  186. return true;
  187. }
  188. }