Button.cs 7.7 KB

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