Button.cs 10 KB

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