Button.cs 6.8 KB

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