Button.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. /// Gets or sets whether the <see cref="Button"/> is the default action to activate in a dialog.
  30. /// </summary>
  31. /// <value><c>true</c> if is default; otherwise, <c>false</c>.</value>
  32. public bool IsDefault {
  33. get => is_default;
  34. set {
  35. is_default = value;
  36. SetWidthHeight (Text, is_default);
  37. Update ();
  38. }
  39. }
  40. /// <summary>
  41. /// Initializes a new instance of <see cref="Button"/> using <see cref="LayoutStyle.Computed"/> layout.
  42. /// </summary>
  43. /// <remarks>
  44. /// The width of the <see cref="Button"/> is computed based on the
  45. /// text length. The height will always be 1.
  46. /// </remarks>
  47. public Button () : this (text: string.Empty, is_default: false) { }
  48. /// <summary>
  49. /// Initializes a new instance of <see cref="Button"/> using <see cref="LayoutStyle.Computed"/> layout.
  50. /// </summary>
  51. /// <remarks>
  52. /// The width of the <see cref="Button"/> is computed based on the
  53. /// text length. The height will always be 1.
  54. /// </remarks>
  55. /// <param name="text">The button's text</param>
  56. /// <param name="is_default">
  57. /// If <c>true</c>, a special decoration is used, and the user pressing the enter key
  58. /// in a <see cref="Dialog"/> will implicitly activate this button.
  59. /// </param>
  60. public Button (ustring text, bool is_default = false) : base ()
  61. {
  62. Init (text, is_default);
  63. }
  64. /// <summary>
  65. /// Initializes a new instance of <see cref="Button"/> using <see cref="LayoutStyle.Absolute"/> layout, based on the given text
  66. /// </summary>
  67. /// <remarks>
  68. /// The width of the <see cref="Button"/> is computed based on the
  69. /// text length. The height will always be 1.
  70. /// </remarks>
  71. /// <param name="x">X position where the button will be shown.</param>
  72. /// <param name="y">Y position where the button will be shown.</param>
  73. /// <param name="text">The button's text</param>
  74. public Button (int x, int y, ustring text) : this (x, y, text, false) { }
  75. /// <summary>
  76. /// Initializes a new instance of <see cref="Button"/> using <see cref="LayoutStyle.Absolute"/> layout, based on the given text.
  77. /// </summary>
  78. /// <remarks>
  79. /// The width of the <see cref="Button"/> is computed based on the
  80. /// text length. The height will always be 1.
  81. /// </remarks>
  82. /// <param name="x">X position where the button will be shown.</param>
  83. /// <param name="y">Y position where the button will be shown.</param>
  84. /// <param name="text">The button's text</param>
  85. /// <param name="is_default">
  86. /// If <c>true</c>, a special decoration is used, and the user pressing the enter key
  87. /// in a <see cref="Dialog"/> will implicitly activate this button.
  88. /// </param>
  89. public Button (int x, int y, ustring text, bool is_default)
  90. : base (new Rect (x, y, text.Length + 4 + (is_default ? 2 : 0), 1))
  91. {
  92. Init (text, is_default);
  93. }
  94. Rune _leftBracket;
  95. Rune _rightBracket;
  96. Rune _leftDefault;
  97. Rune _rightDefault;
  98. void Init (ustring text, bool is_default)
  99. {
  100. HotKeySpecifier = new Rune ('_');
  101. _leftBracket = new Rune (Driver != null ? Driver.LeftBracket : '[');
  102. _rightBracket = new Rune (Driver != null ? Driver.RightBracket : ']');
  103. _leftDefault = new Rune (Driver != null ? Driver.LeftDefaultIndicator : '<');
  104. _rightDefault = new Rune (Driver != null ? Driver.RightDefaultIndicator : '>');
  105. CanFocus = true;
  106. Text = text ?? string.Empty;
  107. this.IsDefault = is_default;
  108. int w = SetWidthHeight (text, is_default);
  109. Frame = new Rect (Frame.Location, new Size (w, 1));
  110. }
  111. int SetWidthHeight (ustring text, bool is_default)
  112. {
  113. int w = text.Length + 4 + (is_default ? 2 : 0);
  114. Width = w;
  115. Height = 1;
  116. Frame = new Rect (Frame.Location, new Size (w, 1));
  117. return w;
  118. }
  119. /// <summary>
  120. /// The text displayed by this <see cref="Button"/>.
  121. /// </summary>
  122. public new ustring Text {
  123. get {
  124. return text;
  125. }
  126. set {
  127. SetWidthHeight (value, is_default);
  128. text = value;
  129. Update ();
  130. }
  131. }
  132. internal void Update ()
  133. {
  134. if (IsDefault)
  135. base.Text = ustring.Make (_leftBracket) + ustring.Make (_leftDefault) + " " + text + " " + ustring.Make (_rightDefault) + ustring.Make (_rightBracket);
  136. else
  137. base.Text = ustring.Make (_leftBracket) + " " + text + " " + ustring.Make (_rightBracket);
  138. SetNeedsDisplay ();
  139. }
  140. bool CheckKey (KeyEvent key)
  141. {
  142. if ((char)key.KeyValue == HotKey) {
  143. this.SuperView.SetFocus (this);
  144. Clicked?.Invoke ();
  145. return true;
  146. }
  147. return false;
  148. }
  149. ///<inheritdoc/>
  150. public override bool ProcessHotKey (KeyEvent kb)
  151. {
  152. if (kb.IsAlt)
  153. return CheckKey (kb);
  154. return false;
  155. }
  156. ///<inheritdoc/>
  157. public override bool ProcessColdKey (KeyEvent kb)
  158. {
  159. if (IsDefault && kb.KeyValue == '\n') {
  160. Clicked?.Invoke ();
  161. return true;
  162. }
  163. return CheckKey (kb);
  164. }
  165. ///<inheritdoc/>
  166. public override bool ProcessKey (KeyEvent kb)
  167. {
  168. var c = kb.KeyValue;
  169. if (c == '\n' || c == ' ' || Rune.ToUpper ((uint)c) == HotKey) {
  170. Clicked?.Invoke ();
  171. return true;
  172. }
  173. return base.ProcessKey (kb);
  174. }
  175. }
  176. }