Button.cs 5.6 KB

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