Button.cs 7.4 KB

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