Button.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. //
  2. // Button.cs: Button control
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. using System;
  8. using System.Text;
  9. namespace Terminal.Gui;
  10. /// <summary>
  11. /// Button is a <see cref="View"/> that provides an item that invokes raises the <see cref="Clicked"/> event.
  12. /// </summary>
  13. /// <remarks>
  14. /// <para>
  15. /// Provides a button showing text that raises the <see cref="Clicked"/> event when clicked on with a mouse
  16. /// or when the user presses SPACE, ENTER, or the <see cref="View.HotKey"/>. The hot key is the first letter or
  17. /// digit following the first underscore ('_')
  18. /// in the button text.
  19. /// </para>
  20. /// <para>
  21. /// Use <see cref="View.HotKeySpecifier"/> to change the hot key specifier from the default of ('_').
  22. /// </para>
  23. /// <para>
  24. /// If no hot key specifier is found, the first uppercase letter encountered will be used as the hot key.
  25. /// </para>
  26. /// <para>
  27. /// When the button is configured as the default (<see cref="IsDefault"/>) and the user presses
  28. /// the ENTER key, if no other <see cref="View"/> processes the key, the <see cref="Button"/>'s
  29. /// <see cref="Clicked"/> event will will be fired.
  30. /// </para>
  31. /// </remarks>
  32. public class Button : View {
  33. bool _isDefault;
  34. Rune _leftBracket;
  35. Rune _leftDefault;
  36. Rune _rightBracket;
  37. Rune _rightDefault;
  38. /// <summary>
  39. /// Initializes a new instance of <see cref="Button"/> using <see cref="LayoutStyle.Computed"/> layout.
  40. /// </summary>
  41. /// <remarks>
  42. /// The width of the <see cref="Button"/> is computed based on the
  43. /// text length. The height will always be 1.
  44. /// </remarks>
  45. public Button () : this (string.Empty, false) { }
  46. /// <summary>
  47. /// Initializes a new instance of <see cref="Button"/> using <see cref="LayoutStyle.Computed"/> layout.
  48. /// </summary>
  49. /// <remarks>
  50. /// The width of the <see cref="Button"/> is computed based on the
  51. /// text length. The height will always be 1.
  52. /// </remarks>
  53. /// <param name="text">The button's text</param>
  54. /// <param name="is_default">
  55. /// If <c>true</c>, a special decoration is used, and the user pressing the enter key
  56. /// in a <see cref="Dialog"/> will implicitly activate this button.
  57. /// </param>
  58. public Button (string text, bool is_default = false) : base (text) => SetInitialProperties (text, is_default);
  59. /// <summary>
  60. /// Initializes a new instance of <see cref="Button"/> using <see cref="LayoutStyle.Absolute"/> layout, based on the given
  61. /// text
  62. /// </summary>
  63. /// <remarks>
  64. /// The width of the <see cref="Button"/> is computed based on the
  65. /// text length. The height will always be 1.
  66. /// </remarks>
  67. /// <param name="x">X position where the button will be shown.</param>
  68. /// <param name="y">Y position where the button will be shown.</param>
  69. /// <param name="text">The button's text</param>
  70. public Button (int x, int y, string text) : this (x, y, text, false) { }
  71. /// <summary>
  72. /// Initializes a new instance of <see cref="Button"/> using <see cref="LayoutStyle.Absolute"/> layout, based on the given
  73. /// text.
  74. /// </summary>
  75. /// <remarks>
  76. /// The width of the <see cref="Button"/> is computed based on the
  77. /// text length. The height will always be 1.
  78. /// </remarks>
  79. /// <param name="x">X position where the button will be shown.</param>
  80. /// <param name="y">Y position where the button will be shown.</param>
  81. /// <param name="text">The button's text</param>
  82. /// <param name="is_default">
  83. /// If <c>true</c>, a special decoration is used, and the user pressing the enter key
  84. /// in a <see cref="Dialog"/> will implicitly activate this button.
  85. /// </param>
  86. public Button (int x, int y, string text, bool is_default)
  87. : base (new Rect (x, y, text.GetRuneCount () + 4 + (is_default ? 2 : 0), 1), text) => SetInitialProperties (text, is_default);
  88. /// <summary>
  89. /// Gets or sets whether the <see cref="Button"/> is the default action to activate in a dialog.
  90. /// </summary>
  91. /// <value><c>true</c> if is default; otherwise, <c>false</c>.</value>
  92. public bool IsDefault {
  93. get => _isDefault;
  94. set {
  95. _isDefault = value;
  96. UpdateTextFormatterText ();
  97. OnResizeNeeded ();
  98. }
  99. }
  100. /// <summary>
  101. ///
  102. /// </summary>
  103. public bool NoDecorations { get; set; }
  104. /// <summary>
  105. ///
  106. /// </summary>
  107. public bool NoPadding { get; set; }
  108. // TODO: v2 - Remove constructors with parameters
  109. /// <summary>
  110. /// Private helper to set the initial properties of the View that were provided via constructors.
  111. /// </summary>
  112. /// <param name="text"></param>
  113. /// <param name="is_default"></param>
  114. void SetInitialProperties (string text, bool is_default)
  115. {
  116. TextAlignment = TextAlignment.Centered;
  117. VerticalTextAlignment = VerticalTextAlignment.Middle;
  118. HotKeySpecifier = new Rune ('_');
  119. _leftBracket = Glyphs.LeftBracket;
  120. _rightBracket = Glyphs.RightBracket;
  121. _leftDefault = Glyphs.LeftDefaultIndicator;
  122. _rightDefault = Glyphs.RightDefaultIndicator;
  123. CanFocus = true;
  124. AutoSize = true;
  125. _isDefault = is_default;
  126. Text = text ?? string.Empty;
  127. // Override default behavior of View
  128. // Command.Default sets focus
  129. AddCommand (Command.Accept, () => {
  130. OnClicked ();
  131. return true;
  132. });
  133. KeyBindings.Add (Key.Space, Command.Default, Command.Accept);
  134. KeyBindings.Add (Key.Enter, Command.Default, Command.Accept);
  135. }
  136. /// <inheritdoc/>
  137. protected override void UpdateTextFormatterText ()
  138. {
  139. if (NoDecorations) {
  140. TextFormatter.Text = Text;
  141. } else if (IsDefault) {
  142. TextFormatter.Text = $"{_leftBracket}{_leftDefault} {Text} {_rightDefault}{_rightBracket}";
  143. } else {
  144. if (NoPadding) {
  145. TextFormatter.Text = $"{_leftBracket}{Text}{_rightBracket}";
  146. } else {
  147. TextFormatter.Text = $"{_leftBracket} {Text} {_rightBracket}";
  148. }
  149. }
  150. }
  151. /// <summary>
  152. /// Virtual method to invoke the <see cref="Clicked"/> event.
  153. /// </summary>
  154. public virtual void OnClicked () => Clicked?.Invoke (this, EventArgs.Empty);
  155. /// <summary>
  156. /// The event fired when the user clicks the primary mouse button within the Bounds of this <see cref="View"/>
  157. /// or if the user presses the action key while this view is focused. (TODO: IsDefault)
  158. /// </summary>
  159. /// <remarks>
  160. /// Client code can hook up to this event, it is
  161. /// raised when the button is activated either with
  162. /// the mouse or the keyboard.
  163. /// </remarks>
  164. public event EventHandler Clicked;
  165. ///<inheritdoc/>
  166. public override bool MouseEvent (MouseEvent me)
  167. {
  168. if (me.Flags == MouseFlags.Button1Clicked) {
  169. if (CanFocus && Enabled) {
  170. if (!HasFocus) {
  171. SetFocus ();
  172. SetNeedsDisplay ();
  173. Draw ();
  174. }
  175. OnClicked ();
  176. }
  177. return true;
  178. }
  179. return false;
  180. }
  181. ///<inheritdoc/>
  182. public override void PositionCursor ()
  183. {
  184. if (HotKey.IsValid && Text != "") {
  185. for (var i = 0; i < TextFormatter.Text.GetRuneCount (); i++) {
  186. if (TextFormatter.Text [i] == Text [0]) {
  187. Move (i, 0);
  188. return;
  189. }
  190. }
  191. }
  192. base.PositionCursor ();
  193. }
  194. ///<inheritdoc/>
  195. public override bool OnEnter (View view)
  196. {
  197. Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
  198. return base.OnEnter (view);
  199. }
  200. }