Button.cs 6.9 KB

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