Button.cs 8.0 KB

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