Button.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 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 hotkey specifier from the default of ('_').
  21. /// </para>
  22. /// <para>
  23. /// If no hotkey specifier is found, the first uppercase letter encountered will be used as the hotkey.
  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 <see cref="KeyEvent"/>, the <see cref="Button"/>'s
  28. /// <see cref="Action"/> will be invoked.
  29. /// </para>
  30. /// </remarks>
  31. public class Button : View {
  32. bool is_default;
  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 (ustring text, bool is_default = false) : base (text)
  58. {
  59. Initialize (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, ustring 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, ustring text, bool is_default)
  87. : base (new Rect (x, y, text.RuneCount + 4 + (is_default ? 2 : 0), 1), text)
  88. {
  89. Initialize (text, is_default);
  90. }
  91. void Initialize (ustring text, bool is_default)
  92. {
  93. TextAlignment = TextAlignment.Centered;
  94. VerticalTextAlignment = VerticalTextAlignment.Middle;
  95. HotKeySpecifier = new Rune ('_');
  96. _leftBracket = new Rune (Driver != null ? Driver.LeftBracket : '[');
  97. _rightBracket = new Rune (Driver != null ? Driver.RightBracket : ']');
  98. _leftDefault = new Rune (Driver != null ? Driver.LeftDefaultIndicator : '<');
  99. _rightDefault = new Rune (Driver != null ? Driver.RightDefaultIndicator : '>');
  100. CanFocus = true;
  101. AutoSize = true;
  102. this.is_default = is_default;
  103. Text = text ?? string.Empty;
  104. UpdateTextFormatterText ();
  105. ProcessResizeView ();
  106. // Things this view knows how to do
  107. AddCommand (Command.Accept, () => AcceptKey ());
  108. // Default keybindings for this view
  109. AddKeyBinding (Key.Enter, Command.Accept);
  110. AddKeyBinding (Key.Space, Command.Accept);
  111. if (HotKey != Key.Null) {
  112. AddKeyBinding (Key.Space | HotKey, Command.Accept);
  113. }
  114. }
  115. /// <summary>
  116. /// Gets or sets whether the <see cref="Button"/> is the default action to activate in a dialog.
  117. /// </summary>
  118. /// <value><c>true</c> if is default; otherwise, <c>false</c>.</value>
  119. /// <remarks>
  120. /// If is <see langword="true"/> the current focused view
  121. /// will remain focused if the window is not closed.
  122. /// </remarks>
  123. public bool IsDefault {
  124. get => is_default;
  125. set {
  126. is_default = value;
  127. UpdateTextFormatterText ();
  128. ProcessResizeView ();
  129. }
  130. }
  131. /// <inheritdoc/>
  132. public override Key HotKey {
  133. get => base.HotKey;
  134. set {
  135. if (base.HotKey != value) {
  136. var v = value == Key.Unknown ? Key.Null : value;
  137. if (base.HotKey != Key.Null && ContainsKeyBinding (Key.Space | base.HotKey)) {
  138. if (v == Key.Null) {
  139. ClearKeybinding (Key.Space | base.HotKey);
  140. } else {
  141. ReplaceKeyBinding (Key.Space | base.HotKey, Key.Space | v);
  142. }
  143. } else if (v != Key.Null) {
  144. AddKeyBinding (Key.Space | v, Command.Accept);
  145. }
  146. base.HotKey = TextFormatter.HotKey = v;
  147. }
  148. }
  149. }
  150. /// <inheritdoc/>
  151. protected override void UpdateTextFormatterText ()
  152. {
  153. if (IsDefault)
  154. TextFormatter.Text = ustring.Make (_leftBracket) + ustring.Make (_leftDefault) + " " + Text + " " + ustring.Make (_rightDefault) + ustring.Make (_rightBracket);
  155. else
  156. TextFormatter.Text = ustring.Make (_leftBracket) + " " + Text + " " + ustring.Make (_rightBracket);
  157. }
  158. ///<inheritdoc/>
  159. public override bool ProcessHotKey (KeyEvent kb)
  160. {
  161. if (!Enabled) {
  162. return false;
  163. }
  164. return ExecuteHotKey (kb);
  165. }
  166. ///<inheritdoc/>
  167. public override bool ProcessColdKey (KeyEvent kb)
  168. {
  169. if (!Enabled) {
  170. return false;
  171. }
  172. return ExecuteColdKey (kb);
  173. }
  174. ///<inheritdoc/>
  175. public override bool ProcessKey (KeyEvent kb)
  176. {
  177. if (!Enabled) {
  178. return false;
  179. }
  180. var result = InvokeKeybindings (kb);
  181. if (result != null)
  182. return (bool)result;
  183. return base.ProcessKey (kb);
  184. }
  185. bool ExecuteHotKey (KeyEvent ke)
  186. {
  187. if (ke.Key == (Key.AltMask | HotKey)) {
  188. return AcceptKey ();
  189. }
  190. return false;
  191. }
  192. bool ExecuteColdKey (KeyEvent ke)
  193. {
  194. if (IsDefault && ke.KeyValue == '\n') {
  195. return AcceptKey ();
  196. }
  197. return ExecuteHotKey (ke);
  198. }
  199. bool AcceptKey ()
  200. {
  201. if (!IsDefault && !HasFocus) {
  202. SetFocus ();
  203. }
  204. OnClicked ();
  205. return true;
  206. }
  207. /// <summary>
  208. /// Virtual method to invoke the <see cref="Clicked"/> event.
  209. /// </summary>
  210. public virtual void OnClicked ()
  211. {
  212. Clicked?.Invoke ();
  213. }
  214. /// <summary>
  215. /// Clicked <see cref="Action"/>, raised when the user clicks the primary mouse button within the Bounds of this <see cref="View"/>
  216. /// or if the user presses the action key while this view is focused. (TODO: IsDefault)
  217. /// </summary>
  218. /// <remarks>
  219. /// Client code can hook up to this event, it is
  220. /// raised when the button is activated either with
  221. /// the mouse or the keyboard.
  222. /// </remarks>
  223. public event Action Clicked;
  224. ///<inheritdoc/>
  225. public override bool MouseEvent (MouseEvent me)
  226. {
  227. if (me.Flags == MouseFlags.Button1Clicked) {
  228. if (CanFocus && Enabled) {
  229. if (!HasFocus) {
  230. SetFocus ();
  231. SetNeedsDisplay ();
  232. Redraw (Bounds);
  233. }
  234. OnClicked ();
  235. }
  236. return true;
  237. }
  238. return false;
  239. }
  240. ///<inheritdoc/>
  241. public override void PositionCursor ()
  242. {
  243. if (HotKey == Key.Unknown && Text != "") {
  244. for (int i = 0; i < TextFormatter.Text.RuneCount; i++) {
  245. if (TextFormatter.Text [i] == Text [0]) {
  246. Move (i, 0);
  247. return;
  248. }
  249. }
  250. }
  251. base.PositionCursor ();
  252. }
  253. ///<inheritdoc/>
  254. public override bool OnEnter (View view)
  255. {
  256. Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
  257. return base.OnEnter (view);
  258. }
  259. }
  260. }