Button.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. /// <inheritdoc />
  34. private bool _wantContinuousButtonPressed;
  35. /// <summary>Initializes a new instance of <see cref="Button"/> using <see cref="LayoutStyle.Computed"/> layout.</summary>
  36. /// <remarks>The width of the <see cref="Button"/> is computed based on the text length. The height will always be 1.</remarks>
  37. public Button ()
  38. {
  39. TextAlignment = TextAlignment.Centered;
  40. VerticalTextAlignment = VerticalTextAlignment.Middle;
  41. _leftBracket = Glyphs.LeftBracket;
  42. _rightBracket = Glyphs.RightBracket;
  43. _leftDefault = Glyphs.LeftDefaultIndicator;
  44. _rightDefault = Glyphs.RightDefaultIndicator;
  45. // Ensures a height of 1 if AutoSize is set to false
  46. Height = 1;
  47. CanFocus = true;
  48. AutoSize = true;
  49. HighlightStyle |= HighlightStyle.Pressed;
  50. #if HOVER
  51. HighlightStyle |= HighlightStyle.Hover;
  52. #endif
  53. // Override default behavior of View
  54. AddCommand (Command.HotKey, () =>
  55. {
  56. SetFocus ();
  57. return !OnAccept ();
  58. });
  59. KeyBindings.Add (Key.Space, Command.HotKey);
  60. KeyBindings.Add (Key.Enter, Command.HotKey);
  61. TitleChanged += Button_TitleChanged;
  62. MouseClick += Button_MouseClick;
  63. }
  64. /// <inheritdoc />
  65. public override bool WantContinuousButtonPressed
  66. {
  67. get => _wantContinuousButtonPressed;
  68. set
  69. {
  70. if (value == _wantContinuousButtonPressed)
  71. {
  72. return;
  73. }
  74. _wantContinuousButtonPressed = value;
  75. if (_wantContinuousButtonPressed)
  76. {
  77. HighlightStyle |= HighlightStyle.PressedOutside;
  78. }
  79. else
  80. {
  81. HighlightStyle &= ~HighlightStyle.PressedOutside;
  82. }
  83. }
  84. }
  85. private void Button_MouseClick (object sender, MouseEventEventArgs e)
  86. {
  87. e.Handled = InvokeCommand (Command.HotKey) == true;
  88. }
  89. private void Button_TitleChanged (object sender, StateEventArgs<string> e)
  90. {
  91. base.Text = e.NewValue;
  92. TextFormatter.HotKeySpecifier = HotKeySpecifier;
  93. }
  94. /// <inheritdoc />
  95. public override string Text
  96. {
  97. get => base.Title;
  98. set => base.Text = base.Title = value;
  99. }
  100. /// <inheritdoc />
  101. public override Rune HotKeySpecifier
  102. {
  103. get => base.HotKeySpecifier;
  104. set => TextFormatter.HotKeySpecifier = base.HotKeySpecifier = value;
  105. }
  106. /// <summary>Gets or sets whether the <see cref="Button"/> is the default action to activate in a dialog.</summary>
  107. /// <value><c>true</c> if is default; otherwise, <c>false</c>.</value>
  108. public bool IsDefault
  109. {
  110. get => _isDefault;
  111. set
  112. {
  113. _isDefault = value;
  114. UpdateTextFormatterText ();
  115. OnResizeNeeded ();
  116. }
  117. }
  118. /// <summary></summary>
  119. public bool NoDecorations { get; set; }
  120. /// <summary></summary>
  121. public bool NoPadding { get; set; }
  122. /// <inheritdoc/>
  123. public override bool OnEnter (View view)
  124. {
  125. Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
  126. return base.OnEnter (view);
  127. }
  128. /// <inheritdoc/>
  129. public override void PositionCursor ()
  130. {
  131. if (HotKey.IsValid && Text != "")
  132. {
  133. for (var i = 0; i < TextFormatter.Text.GetRuneCount (); i++)
  134. {
  135. if (TextFormatter.Text [i] == Text [0])
  136. {
  137. Move (i, 0);
  138. return;
  139. }
  140. }
  141. }
  142. base.PositionCursor ();
  143. }
  144. /// <inheritdoc/>
  145. protected override void UpdateTextFormatterText ()
  146. {
  147. if (NoDecorations)
  148. {
  149. TextFormatter.Text = Text;
  150. }
  151. else if (IsDefault)
  152. {
  153. TextFormatter.Text = $"{_leftBracket}{_leftDefault} {Text} {_rightDefault}{_rightBracket}";
  154. }
  155. else
  156. {
  157. if (NoPadding)
  158. {
  159. TextFormatter.Text = $"{_leftBracket}{Text}{_rightBracket}";
  160. }
  161. else
  162. {
  163. TextFormatter.Text = $"{_leftBracket} {Text} {_rightBracket}";
  164. }
  165. }
  166. }
  167. }