Button.cs 7.0 KB

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