Button.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 where IsDefault changes
  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 show an indicator indicating it is the default Button. If <see langword="true"/>
  128. /// <see cref="Command.Accept"/> will be invoked when the user presses <c>Enter</c> and no other peer-<see cref="View"/> processes the key.
  129. /// If <see cref="View.Accept"/> is not handled, the Gets or sets whether the <see cref="Button"/> will show an indicator indicating it is the default Button. If <see langword="true"/>
  130. /// <see cref="Command.Accept"/> command on the <see cref="View.SuperView"/> will be invoked.
  131. /// </summary>
  132. public bool IsDefault
  133. {
  134. get => _isDefault;
  135. set
  136. {
  137. if (_isDefault == value)
  138. {
  139. return;
  140. }
  141. _isDefault = value;
  142. UpdateTextFormatterText ();
  143. OnResizeNeeded ();
  144. }
  145. }
  146. /// <summary></summary>
  147. public bool NoDecorations { get; set; }
  148. /// <summary></summary>
  149. public bool NoPadding { get; set; }
  150. /// <inheritdoc/>
  151. public override Point? PositionCursor ()
  152. {
  153. if (HotKey.IsValid && Text != "")
  154. {
  155. for (var i = 0; i < TextFormatter.Text.GetRuneCount (); i++)
  156. {
  157. if (TextFormatter.Text [i] == Text [0])
  158. {
  159. Move (i, 0);
  160. return null; // Don't show the cursor
  161. }
  162. }
  163. }
  164. return base.PositionCursor ();
  165. }
  166. /// <inheritdoc/>
  167. protected override void UpdateTextFormatterText ()
  168. {
  169. base.UpdateTextFormatterText ();
  170. if (NoDecorations)
  171. {
  172. TextFormatter.Text = Text;
  173. }
  174. else if (IsDefault)
  175. {
  176. TextFormatter.Text = $"{_leftBracket}{_leftDefault} {Text} {_rightDefault}{_rightBracket}";
  177. }
  178. else
  179. {
  180. if (NoPadding)
  181. {
  182. TextFormatter.Text = $"{_leftBracket}{Text}{_rightBracket}";
  183. }
  184. else
  185. {
  186. TextFormatter.Text = $"{_leftBracket} {Text} {_rightBracket}";
  187. }
  188. }
  189. }
  190. /// <inheritdoc/>
  191. public bool EnableForDesign ()
  192. {
  193. Title = "_Button";
  194. return true;
  195. }
  196. }