Button.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. //
  2. // Button.cs: Button control
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. namespace Terminal.Gui;
  8. /// <summary>Button is a <see cref="View"/> that provides an item that invokes raises the <see cref="View.Accept"/> event.</summary>
  9. /// <remarks>
  10. /// <para>
  11. /// Provides a button showing text that raises the <see cref="View.Accept"/> event when clicked on with a mouse or
  12. /// when the user presses SPACE, ENTER, or the <see cref="View.HotKey"/>. The hot key is the first letter or digit
  13. /// following the first underscore ('_') in the button text.
  14. /// </para>
  15. /// <para>Use <see cref="View.HotKeySpecifier"/> to change the hot key specifier from the default of ('_').</para>
  16. /// <para>
  17. /// When the button is configured as the default (<see cref="IsDefault"/>) and the user presses the ENTER key, if
  18. /// no other <see cref="View"/> processes the key, the <see cref="Button"/>'s <see cref="View.Accept"/> event will
  19. /// be fired.
  20. /// </para>
  21. /// </remarks>
  22. public class Button : View
  23. {
  24. private readonly Rune _leftBracket;
  25. private readonly Rune _leftDefault;
  26. private readonly Rune _rightBracket;
  27. private readonly Rune _rightDefault;
  28. private bool _isDefault;
  29. /// <summary>Initializes a new instance of <see cref="Button"/> using <see cref="LayoutStyle.Computed"/> layout.</summary>
  30. /// <remarks>The width of the <see cref="Button"/> is computed based on the text length. The height will always be 1.</remarks>
  31. public Button ()
  32. {
  33. TextAlignment = TextAlignment.Centered;
  34. VerticalTextAlignment = VerticalTextAlignment.Middle;
  35. _leftBracket = Glyphs.LeftBracket;
  36. _rightBracket = Glyphs.RightBracket;
  37. _leftDefault = Glyphs.LeftDefaultIndicator;
  38. _rightDefault = Glyphs.RightDefaultIndicator;
  39. // Ensures a height of 1 if AutoSize is set to false
  40. Height = 1;
  41. CanFocus = true;
  42. AutoSize = true;
  43. // Override default behavior of View
  44. AddCommand (Command.HotKey, () =>
  45. {
  46. SetFocus ();
  47. return !OnAccept ();
  48. });
  49. KeyBindings.Add (Key.Space, Command.HotKey);
  50. KeyBindings.Add (Key.Enter, Command.HotKey);
  51. TitleChanged += Button_TitleChanged;
  52. MouseEvent += Button_MouseEvent;
  53. //MouseClick += Button_MouseClick;
  54. }
  55. [CanBeNull]
  56. private ColorScheme _savedColorScheme;
  57. private void Button_MouseEvent (object sender, MouseEventEventArgs e)
  58. {
  59. // Default behavior is to invoke Accept (via HotKey) on clicked.
  60. if (!WantContinuousButtonPressed &&
  61. Application.MouseGrabView != this &&
  62. e.MouseEvent.Flags.HasFlag (MouseFlags.Button1Clicked))
  63. {
  64. e.Handled = InvokeCommand (Command.HotKey) == true;
  65. return;
  66. }
  67. if (e.MouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed))
  68. {
  69. // If WantContinuousButtonPressed is true, and this is not the first pressed event,
  70. // invoke Accept (via HotKey)
  71. if (WantContinuousButtonPressed && Application.MouseGrabView == this)
  72. {
  73. e.Handled = InvokeCommand (Command.HotKey) == true;
  74. return;
  75. }
  76. // The first time we get pressed event, grab the mouse and invert the colors
  77. if (Application.MouseGrabView != this)
  78. {
  79. Application.GrabMouse (this);
  80. _savedColorScheme = ColorScheme;
  81. var cs = new ColorScheme (new Attribute (ColorScheme.Normal.Background, ColorScheme.Normal.Foreground));
  82. ColorScheme = cs;
  83. // Set the focus, but don't invoke Accept
  84. SetFocus ();
  85. }
  86. }
  87. if (e.MouseEvent.Flags.HasFlag (MouseFlags.Button1Released))
  88. {
  89. // When the mouse is released, if WantContinuousButtonPressed is set, invoke Accept one last time.
  90. if (WantContinuousButtonPressed)
  91. {
  92. e.Handled = InvokeCommand (Command.HotKey) == true;
  93. }
  94. if (Application.MouseGrabView == this)
  95. {
  96. Application.UngrabMouse ();
  97. if (_savedColorScheme is { })
  98. {
  99. ColorScheme = _savedColorScheme;
  100. _savedColorScheme = null;
  101. }
  102. }
  103. }
  104. }
  105. /// <inheritdoc />
  106. protected internal override bool OnMouseLeave (MouseEvent e)
  107. {
  108. return base.OnMouseLeave (e);
  109. }
  110. private void Button_MouseClick (object sender, MouseEventEventArgs e)
  111. {
  112. e.Handled = InvokeCommand (Command.HotKey) == true;
  113. }
  114. private void Button_TitleChanged (object sender, StateEventArgs<string> e)
  115. {
  116. base.Text = e.NewValue;
  117. TextFormatter.HotKeySpecifier = HotKeySpecifier;
  118. }
  119. /// <inheritdoc />
  120. public override string Text
  121. {
  122. get => base.Title;
  123. set => base.Text = base.Title = value;
  124. }
  125. /// <inheritdoc />
  126. public override Rune HotKeySpecifier
  127. {
  128. get => base.HotKeySpecifier;
  129. set => TextFormatter.HotKeySpecifier = base.HotKeySpecifier = value;
  130. }
  131. /// <summary>Gets or sets whether the <see cref="Button"/> is the default action to activate in a dialog.</summary>
  132. /// <value><c>true</c> if is default; otherwise, <c>false</c>.</value>
  133. public bool IsDefault
  134. {
  135. get => _isDefault;
  136. set
  137. {
  138. _isDefault = value;
  139. UpdateTextFormatterText ();
  140. OnResizeNeeded ();
  141. }
  142. }
  143. /// <summary></summary>
  144. public bool NoDecorations { get; set; }
  145. /// <summary></summary>
  146. public bool NoPadding { get; set; }
  147. /// <inheritdoc/>
  148. public override bool OnEnter (View view)
  149. {
  150. Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
  151. return base.OnEnter (view);
  152. }
  153. /// <inheritdoc/>
  154. public override void PositionCursor ()
  155. {
  156. if (HotKey.IsValid && Text != "")
  157. {
  158. for (var i = 0; i < TextFormatter.Text.GetRuneCount (); i++)
  159. {
  160. if (TextFormatter.Text [i] == Text [0])
  161. {
  162. Move (i, 0);
  163. return;
  164. }
  165. }
  166. }
  167. base.PositionCursor ();
  168. }
  169. /// <inheritdoc/>
  170. protected override void UpdateTextFormatterText ()
  171. {
  172. if (NoDecorations)
  173. {
  174. TextFormatter.Text = Text;
  175. }
  176. else if (IsDefault)
  177. {
  178. TextFormatter.Text = $"{_leftBracket}{_leftDefault} {Text} {_rightDefault}{_rightBracket}";
  179. }
  180. else
  181. {
  182. if (NoPadding)
  183. {
  184. TextFormatter.Text = $"{_leftBracket}{Text}{_rightBracket}";
  185. }
  186. else
  187. {
  188. TextFormatter.Text = $"{_leftBracket} {Text} {_rightBracket}";
  189. }
  190. }
  191. }
  192. }