Button.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. Key hotKey = Key.Null;
  38. Rune hotKeySpecifier;
  39. /// <summary>
  40. /// Initializes a new instance of <see cref="Button"/> using <see cref="LayoutStyle.Computed"/> layout.
  41. /// </summary>
  42. /// <remarks>
  43. /// The width of the <see cref="Button"/> is computed based on the
  44. /// text length. The height will always be 1.
  45. /// </remarks>
  46. public Button () : this (text: string.Empty, is_default: false) { }
  47. /// <summary>
  48. /// Initializes a new instance of <see cref="Button"/> using <see cref="LayoutStyle.Computed"/> layout.
  49. /// </summary>
  50. /// <remarks>
  51. /// The width of the <see cref="Button"/> is computed based on the
  52. /// text length. The height will always be 1.
  53. /// </remarks>
  54. /// <param name="text">The button's text</param>
  55. /// <param name="is_default">
  56. /// If <c>true</c>, a special decoration is used, and the user pressing the enter key
  57. /// in a <see cref="Dialog"/> will implicitly activate this button.
  58. /// </param>
  59. public Button (ustring text, bool is_default = false) : base (text)
  60. {
  61. Initialize (text, is_default);
  62. }
  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. public Button (int x, int y, ustring text) : this (x, y, text, false) { }
  74. /// <summary>
  75. /// Initializes a new instance of <see cref="Button"/> using <see cref="LayoutStyle.Absolute"/> layout, based on the given text.
  76. /// </summary>
  77. /// <remarks>
  78. /// The width of the <see cref="Button"/> is computed based on the
  79. /// text length. The height will always be 1.
  80. /// </remarks>
  81. /// <param name="x">X position where the button will be shown.</param>
  82. /// <param name="y">Y position where the button will be shown.</param>
  83. /// <param name="text">The button's text</param>
  84. /// <param name="is_default">
  85. /// If <c>true</c>, a special decoration is used, and the user pressing the enter key
  86. /// in a <see cref="Dialog"/> will implicitly activate this button.
  87. /// </param>
  88. public Button (int x, int y, ustring text, bool is_default)
  89. : base (new Rect (x, y, text.RuneCount + 4 + (is_default ? 2 : 0), 1), text)
  90. {
  91. Initialize (text, is_default);
  92. }
  93. void Initialize (ustring text, bool is_default)
  94. {
  95. TextAlignment = TextAlignment.Centered;
  96. VerticalTextAlignment = VerticalTextAlignment.Middle;
  97. HotKeySpecifier = new Rune ('_');
  98. _leftBracket = new Rune (Driver != null ? Driver.LeftBracket : '[');
  99. _rightBracket = new Rune (Driver != null ? Driver.RightBracket : ']');
  100. _leftDefault = new Rune (Driver != null ? Driver.LeftDefaultIndicator : '<');
  101. _rightDefault = new Rune (Driver != null ? Driver.RightDefaultIndicator : '>');
  102. CanFocus = true;
  103. AutoSize = true;
  104. this.is_default = is_default;
  105. Text = text ?? string.Empty;
  106. UpdateTextFormatterText ();
  107. ProcessResizeView ();
  108. // Things this view knows how to do
  109. AddCommand (Command.Accept, () => AcceptKey ());
  110. // Default keybindings for this view
  111. AddKeyBinding (Key.Enter, Command.Accept);
  112. AddKeyBinding (Key.Space, Command.Accept);
  113. if (HotKey != Key.Null) {
  114. AddKeyBinding (Key.Space | HotKey, Command.Accept);
  115. }
  116. }
  117. /// <summary>
  118. /// Gets or sets whether the <see cref="Button"/> is the default action to activate in a dialog.
  119. /// </summary>
  120. /// <value><c>true</c> if is default; otherwise, <c>false</c>.</value>
  121. public bool IsDefault {
  122. get => is_default;
  123. set {
  124. is_default = value;
  125. UpdateTextFormatterText ();
  126. ProcessResizeView ();
  127. }
  128. }
  129. /// <inheritdoc/>
  130. public override Key HotKey {
  131. get => hotKey;
  132. set {
  133. if (hotKey != value) {
  134. var v = value == Key.Unknown ? Key.Null : value;
  135. if (hotKey != Key.Null && ContainsKeyBinding (Key.Space | hotKey)) {
  136. if (v == Key.Null) {
  137. ClearKeybinding (Key.Space | hotKey);
  138. } else {
  139. ReplaceKeyBinding (Key.Space | hotKey, Key.Space | v);
  140. }
  141. } else if (v != Key.Null) {
  142. AddKeyBinding (Key.Space | v, Command.Accept);
  143. }
  144. hotKey = v;
  145. }
  146. }
  147. }
  148. /// <inheritdoc/>
  149. public override Rune HotKeySpecifier {
  150. get => hotKeySpecifier;
  151. set {
  152. hotKeySpecifier = TextFormatter.HotKeySpecifier = value;
  153. }
  154. }
  155. /// <inheritdoc/>
  156. protected override void UpdateTextFormatterText ()
  157. {
  158. if (IsDefault)
  159. TextFormatter.Text = ustring.Make (_leftBracket) + ustring.Make (_leftDefault) + " " + Text + " " + ustring.Make (_rightDefault) + ustring.Make (_rightBracket);
  160. else
  161. TextFormatter.Text = ustring.Make (_leftBracket) + " " + Text + " " + ustring.Make (_rightBracket);
  162. }
  163. ///<inheritdoc/>
  164. public override bool ProcessHotKey (KeyEvent kb)
  165. {
  166. if (!Enabled) {
  167. return false;
  168. }
  169. return ExecuteHotKey (kb);
  170. }
  171. ///<inheritdoc/>
  172. public override bool ProcessColdKey (KeyEvent kb)
  173. {
  174. if (!Enabled) {
  175. return false;
  176. }
  177. return ExecuteColdKey (kb);
  178. }
  179. ///<inheritdoc/>
  180. public override bool ProcessKey (KeyEvent kb)
  181. {
  182. if (!Enabled) {
  183. return false;
  184. }
  185. var result = InvokeKeybindings (kb);
  186. if (result != null)
  187. return (bool)result;
  188. return base.ProcessKey (kb);
  189. }
  190. bool ExecuteHotKey (KeyEvent ke)
  191. {
  192. if (ke.Key == (Key.AltMask | HotKey)) {
  193. return AcceptKey ();
  194. }
  195. return false;
  196. }
  197. bool ExecuteColdKey (KeyEvent ke)
  198. {
  199. if (IsDefault && ke.KeyValue == '\n') {
  200. return AcceptKey ();
  201. }
  202. return ExecuteHotKey (ke);
  203. }
  204. bool AcceptKey ()
  205. {
  206. if (!HasFocus) {
  207. SetFocus ();
  208. }
  209. OnClicked ();
  210. return true;
  211. }
  212. /// <summary>
  213. /// Virtual method to invoke the <see cref="Clicked"/> event.
  214. /// </summary>
  215. public virtual void OnClicked ()
  216. {
  217. Clicked?.Invoke ();
  218. }
  219. /// <summary>
  220. /// Clicked <see cref="Action"/>, raised when the user clicks the primary mouse button within the Bounds of this <see cref="View"/>
  221. /// or if the user presses the action key while this view is focused. (TODO: IsDefault)
  222. /// </summary>
  223. /// <remarks>
  224. /// Client code can hook up to this event, it is
  225. /// raised when the button is activated either with
  226. /// the mouse or the keyboard.
  227. /// </remarks>
  228. public event Action Clicked;
  229. ///<inheritdoc/>
  230. public override bool MouseEvent (MouseEvent me)
  231. {
  232. if (me.Flags == MouseFlags.Button1Clicked || me.Flags == MouseFlags.Button1DoubleClicked ||
  233. me.Flags == MouseFlags.Button1TripleClicked) {
  234. if (CanFocus && Enabled) {
  235. if (!HasFocus) {
  236. SetFocus ();
  237. SetNeedsDisplay ();
  238. Redraw (Bounds);
  239. }
  240. OnClicked ();
  241. }
  242. return true;
  243. }
  244. return false;
  245. }
  246. ///<inheritdoc/>
  247. public override void PositionCursor ()
  248. {
  249. if (HotKey == Key.Unknown && Text != "") {
  250. for (int i = 0; i < TextFormatter.Text.RuneCount; i++) {
  251. if (TextFormatter.Text [i] == Text [0]) {
  252. Move (i, 0);
  253. return;
  254. }
  255. }
  256. }
  257. base.PositionCursor ();
  258. }
  259. ///<inheritdoc/>
  260. public override bool OnEnter (View view)
  261. {
  262. Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
  263. return base.OnEnter (view);
  264. }
  265. }
  266. }