Button.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. namespace Terminal.Gui;
  2. /// <summary>
  3. /// A button View that can be pressed with the mouse or keybaord.
  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. [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  33. public static ShadowStyle DefaultShadow { get; set; } = ShadowStyle.None;
  34. /// <summary>
  35. /// Gets or sets the default Highlight Style.
  36. /// </summary>
  37. [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  38. public static HighlightStyle DefaultHighlightStyle { get; set; } = HighlightStyle.Pressed | HighlightStyle.Hover;
  39. /// <summary>Initializes a new instance of <see cref="Button"/>.</summary>
  40. public Button ()
  41. {
  42. TextAlignment = Alignment.Center;
  43. 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. ShadowStyle = DefaultShadow;
  59. HighlightStyle = DefaultHighlightStyle;
  60. }
  61. private bool? HandleHotKeyCommand (CommandContext ctx)
  62. {
  63. bool cachedIsDefault = IsDefault; // Supports "Swap Default" in Buttons scenario where IsDefault changes
  64. if (RaiseSelecting (ctx) is true)
  65. {
  66. return true;
  67. }
  68. bool? handled = RaiseAccepting (ctx);
  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 bool _wantContinuousButtonPressed;
  84. /// <inheritdoc/>
  85. public override bool WantContinuousButtonPressed
  86. {
  87. get => _wantContinuousButtonPressed;
  88. set
  89. {
  90. if (value == _wantContinuousButtonPressed)
  91. {
  92. return;
  93. }
  94. _wantContinuousButtonPressed = value;
  95. if (_wantContinuousButtonPressed)
  96. {
  97. HighlightStyle |= HighlightStyle.PressedOutside;
  98. }
  99. else
  100. {
  101. HighlightStyle &= ~HighlightStyle.PressedOutside;
  102. }
  103. }
  104. }
  105. private void Button_MouseClick (object sender, MouseEventArgs e)
  106. {
  107. if (e.Handled)
  108. {
  109. return;
  110. }
  111. // TODO: With https://github.com/gui-cs/Terminal.Gui/issues/3778 we won't have to pass data:
  112. e.Handled = InvokeCommand (Command.HotKey, new (Command.HotKey, null, data: this)) == true;
  113. }
  114. private void Button_TitleChanged (object sender, EventArgs<string> e)
  115. {
  116. base.Text = e.CurrentValue;
  117. TextFormatter.HotKeySpecifier = HotKeySpecifier;
  118. }
  119. /// <inheritdoc/>
  120. public override string Text
  121. {
  122. get => Title;
  123. set => base.Text = Title = value;
  124. }
  125. /// <inheritdoc/>
  126. public override Rune HotKeySpecifier
  127. {
  128. get => base.HotKeySpecifier;
  129. set => TextFormatter.HotKeySpecifier = base.HotKeySpecifier = value;
  130. }
  131. /// <summary>
  132. /// Gets or sets whether the <see cref="Button"/> will act as the default handler for <see cref="Command.Accept"/>
  133. /// commands on the <see cref="View.SuperView"/>.
  134. /// </summary>
  135. /// <remarks>
  136. /// <para>
  137. /// If <see langword="true"/>:
  138. /// </para>
  139. /// <para>
  140. /// - the Button will display an indicator that it is the default Button.
  141. /// </para>
  142. /// <para>
  143. /// - when clicked, if the Accepting event is not handled, <see cref="Command.Accept"/> will be
  144. /// invoked on the SuperView.
  145. /// </para>
  146. /// <para>
  147. /// - If a peer-View receives <see cref="Command.Accept"/> and does not handle it, the command will be passed to
  148. /// the
  149. /// first Button in the SuperView that has <see cref="IsDefault"/> set to <see langword="true"/>. See
  150. /// <see cref="View.RaiseAccepting"/> for more information.
  151. /// </para>
  152. /// </remarks>
  153. public bool IsDefault
  154. {
  155. get => _isDefault;
  156. set
  157. {
  158. if (_isDefault == value)
  159. {
  160. return;
  161. }
  162. _isDefault = value;
  163. UpdateTextFormatterText ();
  164. SetNeedsLayout ();
  165. }
  166. }
  167. /// <summary>
  168. /// Gets or sets whether the Button will show decorations or not. If <see langword="true"/> the glyphs that normally
  169. /// brakcet the Button Title and the <see cref="IsDefault"/> indicator will not be shown.
  170. /// </summary>
  171. public bool NoDecorations { get; set; }
  172. /// <summary>
  173. /// Gets or sets whether the Button will include padding on each side of the Title.
  174. /// </summary>
  175. public bool NoPadding { get; set; }
  176. /// <inheritdoc/>
  177. public override Point? PositionCursor ()
  178. {
  179. if (HotKey.IsValid && Text != "")
  180. {
  181. for (var i = 0; i < TextFormatter.Text.GetRuneCount (); i++)
  182. {
  183. if (TextFormatter.Text [i] == Text [0])
  184. {
  185. Move (i, 0);
  186. return null; // Don't show the cursor
  187. }
  188. }
  189. }
  190. return base.PositionCursor ();
  191. }
  192. /// <inheritdoc/>
  193. protected override void UpdateTextFormatterText ()
  194. {
  195. base.UpdateTextFormatterText ();
  196. if (NoDecorations)
  197. {
  198. TextFormatter.Text = Text;
  199. }
  200. else if (IsDefault)
  201. {
  202. TextFormatter.Text = $"{_leftBracket}{_leftDefault} {Text} {_rightDefault}{_rightBracket}";
  203. }
  204. else
  205. {
  206. if (NoPadding)
  207. {
  208. TextFormatter.Text = $"{_leftBracket}{Text}{_rightBracket}";
  209. }
  210. else
  211. {
  212. TextFormatter.Text = $"{_leftBracket} {Text} {_rightBracket}";
  213. }
  214. }
  215. }
  216. /// <inheritdoc/>
  217. public bool EnableForDesign ()
  218. {
  219. Title = "_Button";
  220. return true;
  221. }
  222. }