Button.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. /// <para>
  22. /// Set <see cref="View.WantContinuousButtonPressed"/> to <see langword="true"/> to have the <see cref="View.Accept"/> event
  23. /// invoked repeatedly while the button is pressed.
  24. /// </para>
  25. /// </remarks>
  26. public class Button : View
  27. {
  28. private readonly Rune _leftBracket;
  29. private readonly Rune _leftDefault;
  30. private readonly Rune _rightBracket;
  31. private readonly Rune _rightDefault;
  32. private bool _isDefault;
  33. /// <summary>Initializes a new instance of <see cref="Button"/> using <see cref="LayoutStyle.Computed"/> layout.</summary>
  34. /// <remarks>The width of the <see cref="Button"/> is computed based on the text length. The height will always be 1.</remarks>
  35. public Button ()
  36. {
  37. TextAlignment = TextAlignment.Centered;
  38. VerticalTextAlignment = VerticalTextAlignment.Middle;
  39. _leftBracket = Glyphs.LeftBracket;
  40. _rightBracket = Glyphs.RightBracket;
  41. _leftDefault = Glyphs.LeftDefaultIndicator;
  42. _rightDefault = Glyphs.RightDefaultIndicator;
  43. // Ensures a height of 1 if AutoSize is set to false
  44. Height = 1;
  45. CanFocus = true;
  46. AutoSize = true;
  47. HighlightOnPress = true;
  48. // Override default behavior of View
  49. AddCommand (Command.HotKey, () =>
  50. {
  51. SetFocus ();
  52. return !OnAccept ();
  53. });
  54. KeyBindings.Add (Key.Space, Command.HotKey);
  55. KeyBindings.Add (Key.Enter, Command.HotKey);
  56. TitleChanged += Button_TitleChanged;
  57. MouseEvent += Button_MouseEvent;
  58. //MouseClick += Button_MouseClick;
  59. }
  60. [CanBeNull]
  61. private ColorScheme _savedColorScheme;
  62. private void Button_MouseEvent (object sender, MouseEventEventArgs e)
  63. {
  64. if (e.MouseEvent.Flags.HasFlag(MouseFlags.Button1Clicked))
  65. {
  66. if (Application.MouseGrabView != this)
  67. {
  68. e.Handled = InvokeCommand (Command.HotKey) == true;
  69. return;
  70. }
  71. }
  72. if (e.MouseEvent.Flags == MouseFlags.Button1Pressed)
  73. {
  74. if (Application.MouseGrabView == this)
  75. {
  76. e.Handled = InvokeCommand (Command.HotKey) == true;
  77. return;
  78. }
  79. SetFocus();
  80. Application.GrabMouse(this);
  81. _savedColorScheme = ColorScheme;
  82. var cs = new ColorScheme (new Attribute (ColorScheme.Normal.Background, ColorScheme.Normal.Foreground));
  83. ColorScheme = cs;
  84. }
  85. if (e.MouseEvent.Flags.HasFlag(MouseFlags.Button1Released))
  86. {
  87. Application.UngrabMouse ();
  88. e.Handled = InvokeCommand (Command.HotKey) == true;
  89. if (_savedColorScheme is { })
  90. {
  91. ColorScheme = _savedColorScheme;
  92. }
  93. _savedColorScheme = null;
  94. }
  95. }
  96. /// <inheritdoc />
  97. public override bool OnLeave (View view)
  98. {
  99. //Application.UngrabMouse();
  100. return base.OnLeave (view);
  101. }
  102. private void Button_MouseClick (object sender, MouseEventEventArgs e)
  103. {
  104. e.Handled = InvokeCommand (Command.HotKey) == true;
  105. }
  106. private void Button_TitleChanged (object sender, StateEventArgs<string> e)
  107. {
  108. base.Text = e.NewValue;
  109. TextFormatter.HotKeySpecifier = HotKeySpecifier;
  110. }
  111. /// <inheritdoc />
  112. public override string Text
  113. {
  114. get => base.Title;
  115. set => base.Text = base.Title = value;
  116. }
  117. /// <inheritdoc />
  118. public override Rune HotKeySpecifier
  119. {
  120. get => base.HotKeySpecifier;
  121. set => TextFormatter.HotKeySpecifier = base.HotKeySpecifier = value;
  122. }
  123. /// <summary>Gets or sets whether the <see cref="Button"/> is the default action to activate in a dialog.</summary>
  124. /// <value><c>true</c> if is default; otherwise, <c>false</c>.</value>
  125. public bool IsDefault
  126. {
  127. get => _isDefault;
  128. set
  129. {
  130. _isDefault = value;
  131. UpdateTextFormatterText ();
  132. OnResizeNeeded ();
  133. }
  134. }
  135. /// <summary></summary>
  136. public bool NoDecorations { get; set; }
  137. /// <summary></summary>
  138. public bool NoPadding { get; set; }
  139. /// <inheritdoc/>
  140. public override bool OnEnter (View view)
  141. {
  142. Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
  143. return base.OnEnter (view);
  144. }
  145. /// <inheritdoc/>
  146. public override void PositionCursor ()
  147. {
  148. if (HotKey.IsValid && Text != "")
  149. {
  150. for (var i = 0; i < TextFormatter.Text.GetRuneCount (); i++)
  151. {
  152. if (TextFormatter.Text [i] == Text [0])
  153. {
  154. Move (i, 0);
  155. return;
  156. }
  157. }
  158. }
  159. base.PositionCursor ();
  160. }
  161. /// <inheritdoc/>
  162. protected override void UpdateTextFormatterText ()
  163. {
  164. if (NoDecorations)
  165. {
  166. TextFormatter.Text = Text;
  167. }
  168. else if (IsDefault)
  169. {
  170. TextFormatter.Text = $"{_leftBracket}{_leftDefault} {Text} {_rightDefault}{_rightBracket}";
  171. }
  172. else
  173. {
  174. if (NoPadding)
  175. {
  176. TextFormatter.Text = $"{_leftBracket}{Text}{_rightBracket}";
  177. }
  178. else
  179. {
  180. TextFormatter.Text = $"{_leftBracket} {Text} {_rightBracket}";
  181. }
  182. }
  183. }
  184. }