Button.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. ustring text;
  33. bool is_default;
  34. /// <summary>
  35. /// Initializes a new instance of <see cref="Button"/> using <see cref="LayoutStyle.Computed"/> layout.
  36. /// </summary>
  37. /// <remarks>
  38. /// The width of the <see cref="Button"/> is computed based on the
  39. /// text length. The height will always be 1.
  40. /// </remarks>
  41. public Button () : this (text: string.Empty, is_default: false) { }
  42. /// <summary>
  43. /// Initializes a new instance of <see cref="Button"/> using <see cref="LayoutStyle.Computed"/> layout.
  44. /// </summary>
  45. /// <remarks>
  46. /// The width of the <see cref="Button"/> is computed based on the
  47. /// text length. The height will always be 1.
  48. /// </remarks>
  49. /// <param name="text">The button's text</param>
  50. /// <param name="is_default">
  51. /// If <c>true</c>, a special decoration is used, and the user pressing the enter key
  52. /// in a <see cref="Dialog"/> will implicitly activate this button.
  53. /// </param>
  54. public Button (ustring text, bool is_default = false) : base (text)
  55. {
  56. Initialize (text, is_default);
  57. }
  58. /// <summary>
  59. /// Initializes a new instance of <see cref="Button"/> using <see cref="LayoutStyle.Absolute"/> layout, based on the given text
  60. /// </summary>
  61. /// <remarks>
  62. /// The width of the <see cref="Button"/> is computed based on the
  63. /// text length. The height will always be 1.
  64. /// </remarks>
  65. /// <param name="x">X position where the button will be shown.</param>
  66. /// <param name="y">Y position where the button will be shown.</param>
  67. /// <param name="text">The button's text</param>
  68. public Button (int x, int y, ustring text) : this (x, y, text, false) { }
  69. /// <summary>
  70. /// Initializes a new instance of <see cref="Button"/> using <see cref="LayoutStyle.Absolute"/> layout, based on the given text.
  71. /// </summary>
  72. /// <remarks>
  73. /// The width of the <see cref="Button"/> is computed based on the
  74. /// text length. The height will always be 1.
  75. /// </remarks>
  76. /// <param name="x">X position where the button will be shown.</param>
  77. /// <param name="y">Y position where the button will be shown.</param>
  78. /// <param name="text">The button's text</param>
  79. /// <param name="is_default">
  80. /// If <c>true</c>, a special decoration is used, and the user pressing the enter key
  81. /// in a <see cref="Dialog"/> will implicitly activate this button.
  82. /// </param>
  83. public Button (int x, int y, ustring text, bool is_default)
  84. : base (new Rect (x, y, text.RuneCount + 4 + (is_default ? 2 : 0), 1), text)
  85. {
  86. Initialize (text, is_default);
  87. }
  88. Rune _leftBracket;
  89. Rune _rightBracket;
  90. Rune _leftDefault;
  91. Rune _rightDefault;
  92. private Key hotKey = Key.Null;
  93. private Rune hotKeySpecifier;
  94. void Initialize (ustring text, bool is_default)
  95. {
  96. TextAlignment = TextAlignment.Centered;
  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. this.is_default = is_default;
  104. this.text = text ?? string.Empty;
  105. Update ();
  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. /// <inheritdoc/>>
  116. public override ustring Text {
  117. get {
  118. return text;
  119. }
  120. set {
  121. text = value;
  122. TextFormatter.FindHotKey (text, HotKeySpecifier, true, out _, out Key hk);
  123. if (hotKey != hk) {
  124. HotKey = hk;
  125. }
  126. if (IsInitialized)
  127. Update ();
  128. }
  129. }
  130. /// <summary>
  131. /// Gets or sets whether the <see cref="Button"/> is the default action to activate in a dialog.
  132. /// </summary>
  133. /// <value><c>true</c> if is default; otherwise, <c>false</c>.</value>
  134. public bool IsDefault {
  135. get => is_default;
  136. set {
  137. is_default = value;
  138. if (IsInitialized)
  139. Update ();
  140. }
  141. }
  142. /// <inheritdoc/>
  143. public override Key HotKey {
  144. get => hotKey;
  145. set {
  146. if (hotKey != value) {
  147. var v = value == Key.Unknown ? Key.Null : value;
  148. if (ContainsKeyBinding (Key.Space | hotKey)) {
  149. if (v == Key.Null) {
  150. ClearKeybinding (Key.Space | hotKey);
  151. } else {
  152. ReplaceKeyBinding (Key.Space | hotKey, Key.Space | v);
  153. }
  154. } else if (v != Key.Null) {
  155. AddKeyBinding (Key.Space | v, Command.Accept);
  156. }
  157. hotKey = v;
  158. }
  159. }
  160. }
  161. /// <inheritdoc/>
  162. public override Rune HotKeySpecifier {
  163. get => hotKeySpecifier;
  164. set {
  165. hotKeySpecifier = TextFormatter.HotKeySpecifier = value;
  166. }
  167. }
  168. /// <inheritdoc/>
  169. public override bool AutoSize {
  170. get => base.AutoSize;
  171. set {
  172. base.AutoSize = value;
  173. if (IsInitialized)
  174. Update ();
  175. }
  176. }
  177. internal void Update ()
  178. {
  179. if (IsDefault)
  180. TextFormatter.Text = ustring.Make (_leftBracket) + ustring.Make (_leftDefault) + " " + text + " " + ustring.Make (_rightDefault) + ustring.Make (_rightBracket);
  181. else
  182. TextFormatter.Text = ustring.Make (_leftBracket) + " " + text + " " + ustring.Make (_rightBracket);
  183. int w = TextFormatter.Size.Width - (TextFormatter.Text.Contains (HotKeySpecifier) ? 1 : 0);
  184. GetCurrentWidth (out int cWidth);
  185. var canSetWidth = SetWidth (w, out int rWidth);
  186. if (canSetWidth && (cWidth < rWidth || AutoSize)) {
  187. Width = rWidth;
  188. w = rWidth;
  189. } else if (!canSetWidth || !AutoSize) {
  190. w = cWidth;
  191. }
  192. var layout = LayoutStyle;
  193. bool layoutChanged = false;
  194. if (!(Height is Dim.DimAbsolute)) {
  195. // The height is always equal to 1 and must be Dim.DimAbsolute.
  196. layoutChanged = true;
  197. LayoutStyle = LayoutStyle.Absolute;
  198. }
  199. Height = 1;
  200. if (layoutChanged) {
  201. LayoutStyle = layout;
  202. }
  203. Frame = new Rect (Frame.Location, new Size (w, 1));
  204. SetNeedsDisplay ();
  205. }
  206. ///<inheritdoc/>
  207. public override bool ProcessHotKey (KeyEvent kb)
  208. {
  209. if (!Enabled) {
  210. return false;
  211. }
  212. return ExecuteHotKey (kb);
  213. }
  214. ///<inheritdoc/>
  215. public override bool ProcessColdKey (KeyEvent kb)
  216. {
  217. if (!Enabled) {
  218. return false;
  219. }
  220. return ExecuteColdKey (kb);
  221. }
  222. ///<inheritdoc/>
  223. public override bool ProcessKey (KeyEvent kb)
  224. {
  225. if (!Enabled) {
  226. return false;
  227. }
  228. var result = InvokeKeybindings (kb);
  229. if (result != null)
  230. return (bool)result;
  231. return base.ProcessKey (kb);
  232. }
  233. bool ExecuteHotKey (KeyEvent ke)
  234. {
  235. if (ke.Key == (Key.AltMask | HotKey)) {
  236. return AcceptKey ();
  237. }
  238. return false;
  239. }
  240. bool ExecuteColdKey (KeyEvent ke)
  241. {
  242. if (IsDefault && ke.KeyValue == '\n') {
  243. return AcceptKey ();
  244. }
  245. return ExecuteHotKey (ke);
  246. }
  247. bool AcceptKey ()
  248. {
  249. if (!HasFocus) {
  250. SetFocus ();
  251. }
  252. OnClicked ();
  253. return true;
  254. }
  255. /// <summary>
  256. /// Virtual method to invoke the <see cref="Clicked"/> event.
  257. /// </summary>
  258. public virtual void OnClicked ()
  259. {
  260. Clicked?.Invoke ();
  261. }
  262. /// <summary>
  263. /// Clicked <see cref="Action"/>, raised when the user clicks the primary mouse button within the Bounds of this <see cref="View"/>
  264. /// or if the user presses the action key while this view is focused. (TODO: IsDefault)
  265. /// </summary>
  266. /// <remarks>
  267. /// Client code can hook up to this event, it is
  268. /// raised when the button is activated either with
  269. /// the mouse or the keyboard.
  270. /// </remarks>
  271. public event Action Clicked;
  272. ///<inheritdoc/>
  273. public override bool MouseEvent (MouseEvent me)
  274. {
  275. if (me.Flags == MouseFlags.Button1Clicked || me.Flags == MouseFlags.Button1DoubleClicked ||
  276. me.Flags == MouseFlags.Button1TripleClicked) {
  277. if (CanFocus && Enabled) {
  278. if (!HasFocus) {
  279. SetFocus ();
  280. SetNeedsDisplay ();
  281. Redraw (Bounds);
  282. }
  283. OnClicked ();
  284. }
  285. return true;
  286. }
  287. return false;
  288. }
  289. ///<inheritdoc/>
  290. public override void PositionCursor ()
  291. {
  292. if (HotKey == Key.Unknown && text != "") {
  293. for (int i = 0; i < TextFormatter.Text.RuneCount; i++) {
  294. if (TextFormatter.Text [i] == text [0]) {
  295. Move (i, 0);
  296. return;
  297. }
  298. }
  299. }
  300. base.PositionCursor ();
  301. }
  302. ///<inheritdoc/>
  303. public override bool OnEnter (View view)
  304. {
  305. Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
  306. return base.OnEnter (view);
  307. }
  308. }
  309. }