Button.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. //
  2. // Button.cs: Button control
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. using System;
  8. using NStack;
  9. namespace Terminal.Gui {
  10. /// <summary>
  11. /// Button is a <see cref="View"/> that provides an item that invokes an <see cref="Action"/> when activated by the user.
  12. /// </summary>
  13. /// <remarks>
  14. /// <para>
  15. /// Provides a button showing text invokes an <see cref="Action"/> when clicked on with a mouse
  16. /// or when the user presses SPACE, ENTER, or hotkey. The hotkey is specified by the first uppercase
  17. /// letter in the button.
  18. /// </para>
  19. /// <para>
  20. /// When the button is configured as the default (<see cref="IsDefault"/>) and the user presses
  21. /// the ENTER key, if no other <see cref="View"/> processes the <see cref="KeyEvent"/>, the <see cref="Button"/>'s
  22. /// <see cref="Action"/> will be invoked.
  23. /// </para>
  24. /// </remarks>
  25. public class Button : View {
  26. ustring text;
  27. bool is_default;
  28. /// <summary>
  29. /// Initializes a new instance of <see cref="Button"/> using <see cref="LayoutStyle.Computed"/> layout.
  30. /// </summary>
  31. /// <remarks>
  32. /// The width of the <see cref="Button"/> is computed based on the
  33. /// text length. The height will always be 1.
  34. /// </remarks>
  35. public Button () : this (text: string.Empty, is_default: false) { }
  36. /// <summary>
  37. /// Initializes a new instance of <see cref="Button"/> using <see cref="LayoutStyle.Computed"/> layout.
  38. /// </summary>
  39. /// <remarks>
  40. /// The width of the <see cref="Button"/> is computed based on the
  41. /// text length. The height will always be 1.
  42. /// </remarks>
  43. /// <param name="text">The button's text</param>
  44. /// <param name="is_default">
  45. /// If <c>true</c>, a special decoration is used, and the user pressing the enter key
  46. /// in a <see cref="Dialog"/> will implicitly activate this button.
  47. /// </param>
  48. public Button (ustring text, bool is_default = false) : base ()
  49. {
  50. Init (text, is_default);
  51. }
  52. /// <summary>
  53. /// Initializes a new instance of <see cref="Button"/> using <see cref="LayoutStyle.Absolute"/> layout, based on the given text
  54. /// </summary>
  55. /// <remarks>
  56. /// The width of the <see cref="Button"/> is computed based on the
  57. /// text length. The height will always be 1.
  58. /// </remarks>
  59. /// <param name="x">X position where the button will be shown.</param>
  60. /// <param name="y">Y position where the button will be shown.</param>
  61. /// <param name="text">The button's text</param>
  62. public Button (int x, int y, ustring text) : this (x, y, text, false) { }
  63. /// <summary>
  64. /// Initializes a new instance of <see cref="Button"/> using <see cref="LayoutStyle.Absolute"/> layout, based on the given text.
  65. /// </summary>
  66. /// <remarks>
  67. /// The width of the <see cref="Button"/> is computed based on the
  68. /// text length. The height will always be 1.
  69. /// </remarks>
  70. /// <param name="x">X position where the button will be shown.</param>
  71. /// <param name="y">Y position where the button will be shown.</param>
  72. /// <param name="text">The button's text</param>
  73. /// <param name="is_default">
  74. /// If <c>true</c>, a special decoration is used, and the user pressing the enter key
  75. /// in a <see cref="Dialog"/> will implicitly activate this button.
  76. /// </param>
  77. public Button (int x, int y, ustring text, bool is_default)
  78. : base (new Rect (x, y, text.RuneCount + 4 + (is_default ? 2 : 0), 1))
  79. {
  80. Init (text, is_default);
  81. }
  82. Rune _leftBracket;
  83. Rune _rightBracket;
  84. Rune _leftDefault;
  85. Rune _rightDefault;
  86. void Init (ustring text, bool is_default)
  87. {
  88. HotKeySpecifier = new Rune ('_');
  89. _leftBracket = new Rune (Driver != null ? Driver.LeftBracket : '[');
  90. _rightBracket = new Rune (Driver != null ? Driver.RightBracket : ']');
  91. _leftDefault = new Rune (Driver != null ? Driver.LeftDefaultIndicator : '<');
  92. _rightDefault = new Rune (Driver != null ? Driver.RightDefaultIndicator : '>');
  93. CanFocus = true;
  94. this.IsDefault = is_default;
  95. Text = text ?? string.Empty;
  96. //int w = SetWidthHeight (text, is_default);
  97. //Frame = new Rect (Frame.Location, new Size (w, 1));
  98. }
  99. //int SetWidthHeight (ustring text, bool is_default)
  100. //{
  101. // int w = text.RuneCount;// + 4 + (is_default ? 2 : 0);
  102. // Width = w;
  103. // Height = 1;
  104. // Frame = new Rect (Frame.Location, new Size (w, 1));
  105. // return w;
  106. //}
  107. /// <summary>
  108. /// The text displayed by this <see cref="Button"/>.
  109. /// </summary>
  110. public new ustring Text {
  111. get {
  112. return text;
  113. }
  114. set {
  115. text = value;
  116. Update ();
  117. }
  118. }
  119. /// <summary>
  120. /// Gets or sets whether the <see cref="Button"/> is the default action to activate in a dialog.
  121. /// </summary>
  122. /// <value><c>true</c> if is default; otherwise, <c>false</c>.</value>
  123. public bool IsDefault {
  124. get => is_default;
  125. set {
  126. is_default = value;
  127. Update ();
  128. }
  129. }
  130. internal void Update ()
  131. {
  132. if (IsDefault)
  133. base.Text = ustring.Make (_leftBracket) + ustring.Make (_leftDefault) + " " + text + " " + ustring.Make (_rightDefault) + ustring.Make (_rightBracket);
  134. else
  135. base.Text = ustring.Make (_leftBracket) + " " + text + " " + ustring.Make (_rightBracket);
  136. int w = base.Text.RuneCount - (base.Text.Contains (HotKeySpecifier) ? 1 : 0);
  137. Width = w;
  138. Height = 1;
  139. Frame = new Rect (Frame.Location, new Size (w, 1));
  140. SetNeedsDisplay ();
  141. }
  142. bool CheckKey (KeyEvent key)
  143. {
  144. if (key.Key == HotKey) {
  145. this.SuperView.SetFocus (this);
  146. Clicked?.Invoke ();
  147. return true;
  148. }
  149. return false;
  150. }
  151. ///<inheritdoc/>
  152. public override bool ProcessHotKey (KeyEvent kb)
  153. {
  154. if (kb.IsAlt)
  155. return CheckKey (kb);
  156. return false;
  157. }
  158. ///<inheritdoc/>
  159. public override bool ProcessColdKey (KeyEvent kb)
  160. {
  161. if (IsDefault && kb.KeyValue == '\n') {
  162. Clicked?.Invoke ();
  163. return true;
  164. }
  165. return CheckKey (kb);
  166. }
  167. ///<inheritdoc/>
  168. public override bool ProcessKey (KeyEvent kb)
  169. {
  170. var c = kb.KeyValue;
  171. if (c == '\n' || c == ' ' || kb.Key == HotKey) {
  172. Clicked?.Invoke ();
  173. return true;
  174. }
  175. return base.ProcessKey (kb);
  176. }
  177. }
  178. }