Button.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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.Accept"/> 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.Accept"/> 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.Accept"/> 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.Accept"/> 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. () =>
  65. {
  66. bool cachedIsDefault = IsDefault; // Supports "Swap Default" in Buttons scenario where IsDefault changes
  67. bool? handled = RaiseAcceptEvent ();
  68. if (handled == true)
  69. {
  70. return true;
  71. }
  72. SetFocus ();
  73. // TODO: If `IsDefault` were a property on `View` *any* View could work this way. That's theoretical as
  74. // TODO: no use-case has been identified for any View other than Button to act like this.
  75. // If Accept was not handled...
  76. if (cachedIsDefault && SuperView is { })
  77. {
  78. return SuperView.InvokeCommand (Command.Accept);
  79. }
  80. return false;
  81. });
  82. KeyBindings.Remove (Key.Space);
  83. KeyBindings.Add (Key.Space, Command.HotKey);
  84. KeyBindings.Remove (Key.Enter);
  85. KeyBindings.Add (Key.Enter, Command.HotKey);
  86. TitleChanged += Button_TitleChanged;
  87. MouseClick += Button_MouseClick;
  88. ShadowStyle = DefaultShadow;
  89. HighlightStyle = DefaultHighlightStyle;
  90. }
  91. private bool _wantContinuousButtonPressed;
  92. /// <inheritdoc/>
  93. public override bool WantContinuousButtonPressed
  94. {
  95. get => _wantContinuousButtonPressed;
  96. set
  97. {
  98. if (value == _wantContinuousButtonPressed)
  99. {
  100. return;
  101. }
  102. _wantContinuousButtonPressed = value;
  103. if (_wantContinuousButtonPressed)
  104. {
  105. HighlightStyle |= HighlightStyle.PressedOutside;
  106. }
  107. else
  108. {
  109. HighlightStyle &= ~HighlightStyle.PressedOutside;
  110. }
  111. }
  112. }
  113. private void Button_MouseClick (object sender, MouseEventEventArgs e) { e.Handled = InvokeCommand (Command.HotKey) == true; }
  114. private void Button_TitleChanged (object sender, EventArgs<string> e)
  115. {
  116. base.Text = e.CurrentValue;
  117. TextFormatter.HotKeySpecifier = HotKeySpecifier;
  118. }
  119. /// <inheritdoc/>
  120. public override string Text
  121. {
  122. get => Title;
  123. set => base.Text = Title = value;
  124. }
  125. /// <inheritdoc/>
  126. public override Rune HotKeySpecifier
  127. {
  128. get => base.HotKeySpecifier;
  129. set => TextFormatter.HotKeySpecifier = base.HotKeySpecifier = value;
  130. }
  131. /// <summary>
  132. /// Gets or sets whether the <see cref="Button"/> will show an indicator indicating it is the default Button. If
  133. /// <see langword="true"/>
  134. /// <see cref="Command.Accept"/> will be invoked when the user presses <c>Enter</c> and no other peer-
  135. /// <see cref="View"/> processes the key.
  136. /// If <see cref="View.Accept"/> is not handled, the Gets or sets whether the <see cref="Button"/> will show an
  137. /// indicator indicating it is the default Button. If <see langword="true"/>
  138. /// <see cref="Command.Accept"/> command on the <see cref="View.SuperView"/> will be invoked.
  139. /// </summary>
  140. public bool IsDefault
  141. {
  142. get => _isDefault;
  143. set
  144. {
  145. if (_isDefault == value)
  146. {
  147. return;
  148. }
  149. _isDefault = value;
  150. UpdateTextFormatterText ();
  151. OnResizeNeeded ();
  152. }
  153. }
  154. /// <summary></summary>
  155. public bool NoDecorations { get; set; }
  156. /// <summary></summary>
  157. public bool NoPadding { get; set; }
  158. /// <inheritdoc/>
  159. public override Point? PositionCursor ()
  160. {
  161. if (HotKey.IsValid && Text != "")
  162. {
  163. for (var i = 0; i < TextFormatter.Text.GetRuneCount (); i++)
  164. {
  165. if (TextFormatter.Text [i] == Text [0])
  166. {
  167. Move (i, 0);
  168. return null; // Don't show the cursor
  169. }
  170. }
  171. }
  172. return base.PositionCursor ();
  173. }
  174. /// <inheritdoc/>
  175. protected override void UpdateTextFormatterText ()
  176. {
  177. base.UpdateTextFormatterText ();
  178. if (NoDecorations)
  179. {
  180. TextFormatter.Text = Text;
  181. }
  182. else if (IsDefault)
  183. {
  184. TextFormatter.Text = $"{_leftBracket}{_leftDefault} {Text} {_rightDefault}{_rightBracket}";
  185. }
  186. else
  187. {
  188. if (NoPadding)
  189. {
  190. TextFormatter.Text = $"{_leftBracket}{Text}{_rightBracket}";
  191. }
  192. else
  193. {
  194. TextFormatter.Text = $"{_leftBracket} {Text} {_rightBracket}";
  195. }
  196. }
  197. }
  198. /// <inheritdoc/>
  199. public bool EnableForDesign ()
  200. {
  201. Title = "_Button";
  202. return true;
  203. }
  204. }