Button.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. if (MouseHeldDown != null)
  61. {
  62. MouseHeldDown.MouseIsHeldDownTick += (_,_) => RaiseAccepting (null);
  63. }
  64. }
  65. private bool? HandleHotKeyCommand (ICommandContext commandContext)
  66. {
  67. bool cachedIsDefault = IsDefault; // Supports "Swap Default" in Buttons scenario where IsDefault changes
  68. if (RaiseSelecting (commandContext) is true)
  69. {
  70. return true;
  71. }
  72. bool? handled = RaiseAccepting (commandContext);
  73. if (handled == true)
  74. {
  75. return true;
  76. }
  77. SetFocus ();
  78. // TODO: If `IsDefault` were a property on `View` *any* View could work this way. That's theoretical as
  79. // TODO: no use-case has been identified for any View other than Button to act like this.
  80. // If Accept was not handled...
  81. if (cachedIsDefault && SuperView is { })
  82. {
  83. return SuperView.InvokeCommand (Command.Accept);
  84. }
  85. return false;
  86. }
  87. private void Button_MouseClick (object sender, MouseEventArgs e)
  88. {
  89. if (e.Handled)
  90. {
  91. return;
  92. }
  93. // TODO: With https://github.com/gui-cs/Terminal.Gui/issues/3778 we won't have to pass data:
  94. e.Handled = InvokeCommand<KeyBinding> (Command.HotKey, new KeyBinding ([Command.HotKey], this, data: null)) == true;
  95. }
  96. private void Button_TitleChanged (object sender, EventArgs<string> e)
  97. {
  98. base.Text = e.Value;
  99. TextFormatter.HotKeySpecifier = HotKeySpecifier;
  100. }
  101. /// <inheritdoc/>
  102. public override string Text
  103. {
  104. get => Title;
  105. set => base.Text = Title = value;
  106. }
  107. /// <inheritdoc/>
  108. public override Rune HotKeySpecifier
  109. {
  110. get => base.HotKeySpecifier;
  111. set => TextFormatter.HotKeySpecifier = base.HotKeySpecifier = value;
  112. }
  113. /// <summary>
  114. /// Gets or sets whether the <see cref="Button"/> will act as the default handler for <see cref="Command.Accept"/>
  115. /// commands on the <see cref="View.SuperView"/>.
  116. /// </summary>
  117. /// <remarks>
  118. /// <para>
  119. /// If <see langword="true"/>:
  120. /// </para>
  121. /// <para>
  122. /// - The Button will display an indicator that it is the default Button.
  123. /// </para>
  124. /// <para>
  125. /// - When clicked, if the Accepting event is not handled, <see cref="Command.Accept"/> will be
  126. /// invoked on the SuperView.
  127. /// </para>
  128. /// <para>
  129. /// - If a peer-View receives <see cref="Command.Accept"/> and does not handle it, the command will be passed to
  130. /// the
  131. /// first Button in the SuperView that has <see cref="IsDefault"/> set to <see langword="true"/>. See
  132. /// <see cref="View.RaiseAccepting"/> for more information.
  133. /// </para>
  134. /// </remarks>
  135. public bool IsDefault
  136. {
  137. get => _isDefault;
  138. set
  139. {
  140. if (_isDefault == value)
  141. {
  142. return;
  143. }
  144. _isDefault = value;
  145. UpdateTextFormatterText ();
  146. SetNeedsLayout ();
  147. }
  148. }
  149. /// <summary>
  150. /// Gets or sets whether the Button will show decorations or not. If <see langword="true"/> the glyphs that normally
  151. /// bracket the Button Title and the <see cref="IsDefault"/> indicator will not be shown.
  152. /// </summary>
  153. public bool NoDecorations { get; set; }
  154. /// <summary>
  155. /// Gets or sets whether the Button will include padding on each side of the Title.
  156. /// </summary>
  157. public bool NoPadding { get; set; }
  158. /// <inheritdoc/>
  159. public override Point? PositionCursor ()
  160. {
  161. if (HotKey.IsValid && Text != "")
  162. {
  163. for (var i = 0; i < TextFormatter.Text.GetRuneCount (); i++)
  164. {
  165. if (TextFormatter.Text [i] == Text [0])
  166. {
  167. Move (i, 0);
  168. return null; // Don't show the cursor
  169. }
  170. }
  171. }
  172. return base.PositionCursor ();
  173. }
  174. /// <inheritdoc/>
  175. protected override void UpdateTextFormatterText ()
  176. {
  177. base.UpdateTextFormatterText ();
  178. if (NoDecorations)
  179. {
  180. TextFormatter.Text = Text;
  181. }
  182. else if (IsDefault)
  183. {
  184. TextFormatter.Text = $"{_leftBracket}{_leftDefault} {Text} {_rightDefault}{_rightBracket}";
  185. }
  186. else
  187. {
  188. if (NoPadding)
  189. {
  190. TextFormatter.Text = $"{_leftBracket}{Text}{_rightBracket}";
  191. }
  192. else
  193. {
  194. TextFormatter.Text = $"{_leftBracket} {Text} {_rightBracket}";
  195. }
  196. }
  197. }
  198. /// <inheritdoc/>
  199. public bool EnableForDesign ()
  200. {
  201. Title = "_Button";
  202. return true;
  203. }
  204. }