2
0

Button.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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, IDesignable
  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>
  34. /// Gets or sets whether <see cref="Button"/>s are shown with a shadow effect by default.
  35. /// </summary>
  36. [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  37. public static ShadowStyle DefaultShadow { get; set; } = ShadowStyle.None;
  38. /// <summary>Initializes a new instance of <see cref="Button"/>.</summary>
  39. public Button ()
  40. {
  41. TextAlignment = Alignment.Center;
  42. VerticalTextAlignment = Alignment.Center;
  43. _leftBracket = Glyphs.LeftBracket;
  44. _rightBracket = Glyphs.RightBracket;
  45. _leftDefault = Glyphs.LeftDefaultIndicator;
  46. _rightDefault = Glyphs.RightDefaultIndicator;
  47. Height = Dim.Auto (DimAutoStyle.Text);
  48. Width = Dim.Auto (DimAutoStyle.Text);
  49. CanFocus = true;
  50. HighlightStyle |= HighlightStyle.Pressed;
  51. #if HOVER
  52. HighlightStyle |= HighlightStyle.Hover;
  53. #endif
  54. // Override default behavior of View
  55. AddCommand (Command.HotKey, () =>
  56. {
  57. SetFocus ();
  58. return !OnAccept ();
  59. });
  60. KeyBindings.Add (Key.Space, Command.HotKey);
  61. KeyBindings.Add (Key.Enter, Command.HotKey);
  62. TitleChanged += Button_TitleChanged;
  63. MouseClick += Button_MouseClick;
  64. ShadowStyle = DefaultShadow;
  65. }
  66. private bool _wantContinuousButtonPressed;
  67. /// <inheritdoc />
  68. public override bool WantContinuousButtonPressed
  69. {
  70. get => _wantContinuousButtonPressed;
  71. set
  72. {
  73. if (value == _wantContinuousButtonPressed)
  74. {
  75. return;
  76. }
  77. _wantContinuousButtonPressed = value;
  78. if (_wantContinuousButtonPressed)
  79. {
  80. HighlightStyle |= HighlightStyle.PressedOutside;
  81. }
  82. else
  83. {
  84. HighlightStyle &= ~HighlightStyle.PressedOutside;
  85. }
  86. }
  87. }
  88. private void Button_MouseClick (object sender, MouseEventEventArgs e)
  89. {
  90. e.Handled = InvokeCommand (Command.HotKey) == true;
  91. }
  92. private void Button_TitleChanged (object sender, EventArgs<string> e)
  93. {
  94. base.Text = e.CurrentValue;
  95. TextFormatter.HotKeySpecifier = HotKeySpecifier;
  96. }
  97. /// <inheritdoc />
  98. public override string Text
  99. {
  100. get => base.Title;
  101. set => base.Text = base.Title = value;
  102. }
  103. /// <inheritdoc />
  104. public override Rune HotKeySpecifier
  105. {
  106. get => base.HotKeySpecifier;
  107. set => TextFormatter.HotKeySpecifier = base.HotKeySpecifier = value;
  108. }
  109. /// <summary>Gets or sets whether the <see cref="Button"/> is the default action to activate in a dialog.</summary>
  110. /// <value><c>true</c> if is default; otherwise, <c>false</c>.</value>
  111. public bool IsDefault
  112. {
  113. get => _isDefault;
  114. set
  115. {
  116. _isDefault = value;
  117. UpdateTextFormatterText ();
  118. OnResizeNeeded ();
  119. }
  120. }
  121. /// <summary></summary>
  122. public bool NoDecorations { get; set; }
  123. /// <summary></summary>
  124. public bool NoPadding { get; set; }
  125. /// <inheritdoc/>
  126. public override Point? PositionCursor ()
  127. {
  128. if (HotKey.IsValid && Text != "")
  129. {
  130. for (var i = 0; i < TextFormatter.Text.GetRuneCount (); i++)
  131. {
  132. if (TextFormatter.Text [i] == Text [0])
  133. {
  134. Move (i, 0);
  135. return null; // Don't show the cursor
  136. }
  137. }
  138. }
  139. return base.PositionCursor ();
  140. }
  141. /// <inheritdoc/>
  142. protected override void UpdateTextFormatterText ()
  143. {
  144. base.UpdateTextFormatterText();
  145. if (NoDecorations)
  146. {
  147. TextFormatter.Text = Text;
  148. }
  149. else if (IsDefault)
  150. {
  151. TextFormatter.Text = $"{_leftBracket}{_leftDefault} {Text} {_rightDefault}{_rightBracket}";
  152. }
  153. else
  154. {
  155. if (NoPadding)
  156. {
  157. TextFormatter.Text = $"{_leftBracket}{Text}{_rightBracket}";
  158. }
  159. else
  160. {
  161. TextFormatter.Text = $"{_leftBracket} {Text} {_rightBracket}";
  162. }
  163. }
  164. }
  165. /// <inheritdoc />
  166. public bool EnableForDesign ()
  167. {
  168. Title = "_Button";
  169. return true;
  170. }
  171. }