Button.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. //
  2. // Button.cs: Button control
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. namespace Terminal.Gui;
  8. /// <summary>
  9. /// A View that raises the <see cref="View.Accepted"/> event when clicked with the mouse or when the
  10. /// <see cref="View.HotKey"/>, <c>Enter</c>, or <c>Space</c> key is pressed.
  11. /// </summary>
  12. /// <remarks>
  13. /// <para>
  14. /// Provides a button showing text that raises the <see cref="View.Accepted"/> event when clicked on with a mouse or
  15. /// when the user presses <c>Enter</c>, <c>Space</c> or the <see cref="View.HotKey"/>. The hot key is the first
  16. /// letter or digit
  17. /// following the first underscore ('_') in the button text.
  18. /// </para>
  19. /// <para>Use <see cref="View.HotKeySpecifier"/> to change the hot key specifier from the default of ('_').</para>
  20. /// <para>
  21. /// When the button is configured as the default (<see cref="IsDefault"/>) and the user causes the button to be
  22. /// accepted the <see cref="Button"/>'s <see cref="View.Accepted"/> event will be raised. If the Accept event is not
  23. /// handled, the Accept event on the <see cref="View.SuperView"/>. will be raised. This enables default Accept
  24. /// behavior.
  25. /// </para>
  26. /// <para>
  27. /// Set <see cref="View.WantContinuousButtonPressed"/> to <see langword="true"/> to have the
  28. /// <see cref="View.Accepted"/> event
  29. /// invoked repeatedly while the button is pressed.
  30. /// </para>
  31. /// </remarks>
  32. public class Button : View, IDesignable
  33. {
  34. private readonly Rune _leftBracket;
  35. private readonly Rune _leftDefault;
  36. private readonly Rune _rightBracket;
  37. private readonly Rune _rightDefault;
  38. private bool _isDefault;
  39. /// <summary>
  40. /// Gets or sets whether <see cref="Button"/>s are shown with a shadow effect by default.
  41. /// </summary>
  42. [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  43. public static ShadowStyle DefaultShadow { get; set; } = ShadowStyle.None;
  44. /// <summary>
  45. /// Gets or sets the default Highlight Style.
  46. /// </summary>
  47. [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  48. public static HighlightStyle DefaultHighlightStyle { get; set; } = HighlightStyle.Pressed | HighlightStyle.Hover;
  49. /// <summary>Initializes a new instance of <see cref="Button"/>.</summary>
  50. public Button ()
  51. {
  52. TextAlignment = Alignment.Center;
  53. VerticalTextAlignment = Alignment.Center;
  54. _leftBracket = Glyphs.LeftBracket;
  55. _rightBracket = Glyphs.RightBracket;
  56. _leftDefault = Glyphs.LeftDefaultIndicator;
  57. _rightDefault = Glyphs.RightDefaultIndicator;
  58. Height = Dim.Auto (DimAutoStyle.Text);
  59. Width = Dim.Auto (DimAutoStyle.Text);
  60. CanFocus = true;
  61. AddCommand (Command.HotKey, HandleHotKeyCommand);
  62. KeyBindings.Remove (Key.Space);
  63. KeyBindings.Add (Key.Space, Command.HotKey);
  64. KeyBindings.Remove (Key.Enter);
  65. KeyBindings.Add (Key.Enter, Command.HotKey);
  66. TitleChanged += Button_TitleChanged;
  67. MouseClick += Button_MouseClick;
  68. ShadowStyle = DefaultShadow;
  69. HighlightStyle = DefaultHighlightStyle;
  70. }
  71. private bool? HandleHotKeyCommand (CommandContext ctx)
  72. {
  73. bool cachedIsDefault = IsDefault; // Supports "Swap Default" in Buttons scenario where IsDefault changes
  74. if (RaiseSelected (ctx) is true)
  75. {
  76. return true;
  77. }
  78. bool? handled = RaiseAccepted ();
  79. if (handled == true)
  80. {
  81. return true;
  82. }
  83. SetFocus ();
  84. // TODO: If `IsDefault` were a property on `View` *any* View could work this way. That's theoretical as
  85. // TODO: no use-case has been identified for any View other than Button to act like this.
  86. // If Accept was not handled...
  87. if (cachedIsDefault && SuperView is { })
  88. {
  89. return SuperView.InvokeCommand (Command.Accept);
  90. }
  91. return false;
  92. }
  93. private bool _wantContinuousButtonPressed;
  94. /// <inheritdoc/>
  95. public override bool WantContinuousButtonPressed
  96. {
  97. get => _wantContinuousButtonPressed;
  98. set
  99. {
  100. if (value == _wantContinuousButtonPressed)
  101. {
  102. return;
  103. }
  104. _wantContinuousButtonPressed = value;
  105. if (_wantContinuousButtonPressed)
  106. {
  107. HighlightStyle |= HighlightStyle.PressedOutside;
  108. }
  109. else
  110. {
  111. HighlightStyle &= ~HighlightStyle.PressedOutside;
  112. }
  113. }
  114. }
  115. private void Button_MouseClick (object sender, MouseEventEventArgs e)
  116. {
  117. if (e.Handled)
  118. {
  119. return;
  120. }
  121. e.Handled = InvokeCommand (Command.HotKey, ctx: new (Command.HotKey, key: null, data: this)) == true;
  122. }
  123. private void Button_TitleChanged (object sender, EventArgs<string> e)
  124. {
  125. base.Text = e.CurrentValue;
  126. TextFormatter.HotKeySpecifier = HotKeySpecifier;
  127. }
  128. /// <inheritdoc/>
  129. public override string Text
  130. {
  131. get => Title;
  132. set => base.Text = Title = value;
  133. }
  134. /// <inheritdoc/>
  135. public override Rune HotKeySpecifier
  136. {
  137. get => base.HotKeySpecifier;
  138. set => TextFormatter.HotKeySpecifier = base.HotKeySpecifier = value;
  139. }
  140. /// <summary>
  141. /// Gets or sets whether the <see cref="Button"/> will show an indicator indicating it is the default Button. If
  142. /// <see langword="true"/>
  143. /// <see cref="Command.Accept"/> will be invoked when the user presses <c>Enter</c> and no other peer-
  144. /// <see cref="View"/> processes the key.
  145. /// If <see cref="View.Accepted"/> is not handled, the Gets or sets whether the <see cref="Button"/> will show an
  146. /// indicator indicating it is the default Button. If <see langword="true"/>
  147. /// <see cref="Command.Accept"/> command on the <see cref="View.SuperView"/> will be invoked.
  148. /// </summary>
  149. public bool IsDefault
  150. {
  151. get => _isDefault;
  152. set
  153. {
  154. if (_isDefault == value)
  155. {
  156. return;
  157. }
  158. _isDefault = value;
  159. UpdateTextFormatterText ();
  160. OnResizeNeeded ();
  161. }
  162. }
  163. /// <summary></summary>
  164. public bool NoDecorations { get; set; }
  165. /// <summary></summary>
  166. public bool NoPadding { get; set; }
  167. /// <inheritdoc/>
  168. public override Point? PositionCursor ()
  169. {
  170. if (HotKey.IsValid && Text != "")
  171. {
  172. for (var i = 0; i < TextFormatter.Text.GetRuneCount (); i++)
  173. {
  174. if (TextFormatter.Text [i] == Text [0])
  175. {
  176. Move (i, 0);
  177. return null; // Don't show the cursor
  178. }
  179. }
  180. }
  181. return base.PositionCursor ();
  182. }
  183. /// <inheritdoc/>
  184. protected override void UpdateTextFormatterText ()
  185. {
  186. base.UpdateTextFormatterText ();
  187. if (NoDecorations)
  188. {
  189. TextFormatter.Text = Text;
  190. }
  191. else if (IsDefault)
  192. {
  193. TextFormatter.Text = $"{_leftBracket}{_leftDefault} {Text} {_rightDefault}{_rightBracket}";
  194. }
  195. else
  196. {
  197. if (NoPadding)
  198. {
  199. TextFormatter.Text = $"{_leftBracket}{Text}{_rightBracket}";
  200. }
  201. else
  202. {
  203. TextFormatter.Text = $"{_leftBracket} {Text} {_rightBracket}";
  204. }
  205. }
  206. }
  207. /// <inheritdoc/>
  208. public bool EnableForDesign ()
  209. {
  210. Title = "_Button";
  211. return true;
  212. }
  213. }