Button.cs 7.9 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. // Override default behavior of View
  62. AddCommand (
  63. Command.HotKey,
  64. (ctx) =>
  65. {
  66. bool cachedIsDefault = IsDefault; // Supports "Swap Default" in Buttons scenario where IsDefault changes
  67. if (RaiseSelected (ctx) is true)
  68. {
  69. return true;
  70. }
  71. bool? handled = RaiseAccepted ();
  72. if (handled == true)
  73. {
  74. return true;
  75. }
  76. SetFocus ();
  77. // TODO: If `IsDefault` were a property on `View` *any* View could work this way. That's theoretical as
  78. // TODO: no use-case has been identified for any View other than Button to act like this.
  79. // If Accept was not handled...
  80. if (cachedIsDefault && SuperView is { })
  81. {
  82. return SuperView.InvokeCommand (Command.Accept);
  83. }
  84. return false;
  85. });
  86. KeyBindings.Remove (Key.Space);
  87. KeyBindings.Add (Key.Space, Command.HotKey);
  88. KeyBindings.Remove (Key.Enter);
  89. KeyBindings.Add (Key.Enter, Command.HotKey);
  90. TitleChanged += Button_TitleChanged;
  91. MouseClick += Button_MouseClick;
  92. ShadowStyle = DefaultShadow;
  93. HighlightStyle = DefaultHighlightStyle;
  94. }
  95. private bool _wantContinuousButtonPressed;
  96. /// <inheritdoc/>
  97. public override bool WantContinuousButtonPressed
  98. {
  99. get => _wantContinuousButtonPressed;
  100. set
  101. {
  102. if (value == _wantContinuousButtonPressed)
  103. {
  104. return;
  105. }
  106. _wantContinuousButtonPressed = value;
  107. if (_wantContinuousButtonPressed)
  108. {
  109. HighlightStyle |= HighlightStyle.PressedOutside;
  110. }
  111. else
  112. {
  113. HighlightStyle &= ~HighlightStyle.PressedOutside;
  114. }
  115. }
  116. }
  117. private void Button_MouseClick (object sender, MouseEventEventArgs e)
  118. {
  119. if (e.Handled)
  120. {
  121. return;
  122. }
  123. e.Handled = InvokeCommand (Command.HotKey) == true;
  124. }
  125. private void Button_TitleChanged (object sender, EventArgs<string> e)
  126. {
  127. base.Text = e.CurrentValue;
  128. TextFormatter.HotKeySpecifier = HotKeySpecifier;
  129. }
  130. /// <inheritdoc/>
  131. public override string Text
  132. {
  133. get => Title;
  134. set => base.Text = Title = value;
  135. }
  136. /// <inheritdoc/>
  137. public override Rune HotKeySpecifier
  138. {
  139. get => base.HotKeySpecifier;
  140. set => TextFormatter.HotKeySpecifier = base.HotKeySpecifier = value;
  141. }
  142. /// <summary>
  143. /// Gets or sets whether the <see cref="Button"/> will show an indicator indicating it is the default Button. If
  144. /// <see langword="true"/>
  145. /// <see cref="Command.Accept"/> will be invoked when the user presses <c>Enter</c> and no other peer-
  146. /// <see cref="View"/> processes the key.
  147. /// If <see cref="View.Accepted"/> is not handled, the Gets or sets whether the <see cref="Button"/> will show an
  148. /// indicator indicating it is the default Button. If <see langword="true"/>
  149. /// <see cref="Command.Accept"/> command on the <see cref="View.SuperView"/> will be invoked.
  150. /// </summary>
  151. public bool IsDefault
  152. {
  153. get => _isDefault;
  154. set
  155. {
  156. if (_isDefault == value)
  157. {
  158. return;
  159. }
  160. _isDefault = value;
  161. UpdateTextFormatterText ();
  162. OnResizeNeeded ();
  163. }
  164. }
  165. /// <summary></summary>
  166. public bool NoDecorations { get; set; }
  167. /// <summary></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. }