Button.cs 7.8 KB

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