2
0

Button.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. namespace Terminal.Gui.Views;
  2. /// <summary>
  3. /// A button View that can be pressed with the mouse or keyboard.
  4. /// </summary>
  5. /// <remarks>
  6. /// <para>
  7. /// The Button will raise the <see cref="View.Accepting"/> event when the user presses <see cref="View.HotKey"/>,
  8. /// <c>Enter</c>, or <c>Space</c>
  9. /// or clicks on the button with the mouse.
  10. /// </para>
  11. /// <para>Use <see cref="View.HotKeySpecifier"/> to change the hot key specifier from the default of ('_').</para>
  12. /// <para>
  13. /// Button can act as the default <see cref="Command.Accept"/> handler for all peer-Views. See
  14. /// <see cref="IsDefault"/>.
  15. /// </para>
  16. /// <para>
  17. /// Set <see cref="View.WantContinuousButtonPressed"/> to <see langword="true"/> to have the
  18. /// <see cref="View.Accepting"/> event
  19. /// invoked repeatedly while the button is pressed.
  20. /// </para>
  21. /// </remarks>
  22. public class Button : View, IDesignable
  23. {
  24. private readonly Rune _leftBracket;
  25. private readonly Rune _leftDefault;
  26. private readonly Rune _rightBracket;
  27. private readonly Rune _rightDefault;
  28. private bool _isDefault;
  29. /// <summary>
  30. /// Gets or sets whether <see cref="Button"/>s are shown with a shadow effect by default.
  31. /// </summary>
  32. [ConfigurationProperty (Scope = typeof (ThemeScope))]
  33. public static ShadowStyle DefaultShadow { get; set; } = ShadowStyle.Opaque;
  34. /// <summary>
  35. /// Gets or sets the default Highlight Style.
  36. /// </summary>
  37. [ConfigurationProperty (Scope = typeof (ThemeScope))]
  38. public static MouseState DefaultHighlightStates { get; set; } = MouseState.In | MouseState.Pressed | MouseState.PressedOutside;
  39. /// <summary>Initializes a new instance of <see cref="Button"/>.</summary>
  40. public Button ()
  41. {
  42. base.TextAlignment = Alignment.Center;
  43. base.VerticalTextAlignment = Alignment.Center;
  44. _leftBracket = Glyphs.LeftBracket;
  45. _rightBracket = Glyphs.RightBracket;
  46. _leftDefault = Glyphs.LeftDefaultIndicator;
  47. _rightDefault = Glyphs.RightDefaultIndicator;
  48. Height = Dim.Auto (DimAutoStyle.Text);
  49. Width = Dim.Auto (DimAutoStyle.Text);
  50. CanFocus = true;
  51. AddCommand (Command.HotKey, HandleHotKeyCommand);
  52. KeyBindings.Remove (Key.Space);
  53. KeyBindings.Add (Key.Space, Command.HotKey);
  54. KeyBindings.Remove (Key.Enter);
  55. KeyBindings.Add (Key.Enter, Command.HotKey);
  56. TitleChanged += Button_TitleChanged;
  57. MouseClick += Button_MouseClick;
  58. base.ShadowStyle = DefaultShadow;
  59. HighlightStates = DefaultHighlightStates;
  60. }
  61. private bool? HandleHotKeyCommand (ICommandContext commandContext)
  62. {
  63. bool cachedIsDefault = IsDefault; // Supports "Swap Default" in Buttons scenario where IsDefault changes
  64. if (RaiseSelecting (commandContext) is true)
  65. {
  66. return true;
  67. }
  68. bool? handled = RaiseAccepting (commandContext);
  69. if (handled == true)
  70. {
  71. return true;
  72. }
  73. SetFocus ();
  74. // TODO: If `IsDefault` were a property on `View` *any* View could work this way. That's theoretical as
  75. // TODO: no use-case has been identified for any View other than Button to act like this.
  76. // If Accept was not handled...
  77. if (cachedIsDefault && SuperView is { })
  78. {
  79. return SuperView.InvokeCommand (Command.Accept);
  80. }
  81. return false;
  82. }
  83. private void Button_MouseClick (object sender, MouseEventArgs e)
  84. {
  85. if (e.Handled)
  86. {
  87. return;
  88. }
  89. // TODO: With https://github.com/gui-cs/Terminal.Gui/issues/3778 we won't have to pass data:
  90. e.Handled = InvokeCommand<KeyBinding> (Command.HotKey, new KeyBinding ([Command.HotKey], this, data: null)) == true;
  91. }
  92. private void Button_TitleChanged (object sender, EventArgs<string> e)
  93. {
  94. base.Text = e.Value;
  95. TextFormatter.HotKeySpecifier = HotKeySpecifier;
  96. }
  97. /// <inheritdoc/>
  98. public override string Text
  99. {
  100. get => Title;
  101. set => base.Text = Title = value;
  102. }
  103. /// <inheritdoc/>
  104. public override Rune HotKeySpecifier
  105. {
  106. get => base.HotKeySpecifier;
  107. set => TextFormatter.HotKeySpecifier = base.HotKeySpecifier = value;
  108. }
  109. /// <summary>
  110. /// Gets or sets whether the <see cref="Button"/> will act as the default handler for <see cref="Command.Accept"/>
  111. /// commands on the <see cref="View.SuperView"/>.
  112. /// </summary>
  113. /// <remarks>
  114. /// <para>
  115. /// If <see langword="true"/>:
  116. /// </para>
  117. /// <para>
  118. /// - The Button will display an indicator that it is the default Button.
  119. /// </para>
  120. /// <para>
  121. /// - When clicked, if the Accepting event is not handled, <see cref="Command.Accept"/> will be
  122. /// invoked on the SuperView.
  123. /// </para>
  124. /// <para>
  125. /// - If a peer-View receives <see cref="Command.Accept"/> and does not handle it, the command will be passed to
  126. /// the
  127. /// first Button in the SuperView that has <see cref="IsDefault"/> set to <see langword="true"/>. See
  128. /// <see cref="View.RaiseAccepting"/> for more information.
  129. /// </para>
  130. /// </remarks>
  131. public bool IsDefault
  132. {
  133. get => _isDefault;
  134. set
  135. {
  136. if (_isDefault == value)
  137. {
  138. return;
  139. }
  140. _isDefault = value;
  141. UpdateTextFormatterText ();
  142. SetNeedsLayout ();
  143. }
  144. }
  145. /// <summary>
  146. /// Gets or sets whether the Button will show decorations or not. If <see langword="true"/> the glyphs that normally
  147. /// bracket the Button Title and the <see cref="IsDefault"/> indicator will not be shown.
  148. /// </summary>
  149. public bool NoDecorations { get; set; }
  150. /// <summary>
  151. /// Gets or sets whether the Button will include padding on each side of the Title.
  152. /// </summary>
  153. public bool NoPadding { get; set; }
  154. /// <inheritdoc/>
  155. public override Point? PositionCursor ()
  156. {
  157. if (HotKey.IsValid && Text != "")
  158. {
  159. for (var i = 0; i < TextFormatter.Text.GetRuneCount (); i++)
  160. {
  161. if (TextFormatter.Text [i] == Text [0])
  162. {
  163. Move (i, 0);
  164. return null; // Don't show the cursor
  165. }
  166. }
  167. }
  168. return base.PositionCursor ();
  169. }
  170. /// <inheritdoc/>
  171. protected override void UpdateTextFormatterText ()
  172. {
  173. base.UpdateTextFormatterText ();
  174. if (NoDecorations)
  175. {
  176. TextFormatter.Text = Text;
  177. }
  178. else if (IsDefault)
  179. {
  180. TextFormatter.Text = $"{_leftBracket}{_leftDefault} {Text} {_rightDefault}{_rightBracket}";
  181. }
  182. else
  183. {
  184. if (NoPadding)
  185. {
  186. TextFormatter.Text = $"{_leftBracket}{Text}{_rightBracket}";
  187. }
  188. else
  189. {
  190. TextFormatter.Text = $"{_leftBracket} {Text} {_rightBracket}";
  191. }
  192. }
  193. }
  194. /// <inheritdoc/>
  195. public bool EnableForDesign ()
  196. {
  197. Title = "_Button";
  198. return true;
  199. }
  200. }