Button.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //
  2. // Button.cs: Button control
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. using System.Text.Json.Serialization;
  8. namespace Terminal.Gui;
  9. /// <summary>Button is a <see cref="View"/> that provides an item that invokes raises the <see cref="View.Accept"/> event.</summary>
  10. /// <remarks>
  11. /// <para>
  12. /// Provides a button showing text that raises the <see cref="View.Accept"/> event when clicked on with a mouse or
  13. /// when the user presses SPACE, ENTER, or the <see cref="View.HotKey"/>. The hot key is the first letter or digit
  14. /// following the first underscore ('_') in the button text.
  15. /// </para>
  16. /// <para>Use <see cref="View.HotKeySpecifier"/> to change the hot key specifier from the default of ('_').</para>
  17. /// <para>
  18. /// When the button is configured as the default (<see cref="IsDefault"/>) and the user presses the ENTER key, if
  19. /// no other <see cref="View"/> processes the key, the <see cref="Button"/>'s <see cref="View.Accept"/> event will
  20. /// be fired.
  21. /// </para>
  22. /// <para>
  23. /// Set <see cref="View.WantContinuousButtonPressed"/> to <see langword="true"/> to have the <see cref="View.Accept"/> event
  24. /// invoked repeatedly while the button is pressed.
  25. /// </para>
  26. /// </remarks>
  27. public class Button : View, IDesignable
  28. {
  29. private readonly Rune _leftBracket;
  30. private readonly Rune _leftDefault;
  31. private readonly Rune _rightBracket;
  32. private readonly Rune _rightDefault;
  33. private bool _isDefault;
  34. /// <summary>
  35. /// Gets or sets whether <see cref="Button"/>s are shown with a shadow effect by default.
  36. /// </summary>
  37. [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  38. [JsonConverter (typeof (JsonStringEnumConverter<ShadowStyle>))]
  39. public static ShadowStyle DefaultShadow { get; set; } = ShadowStyle.None;
  40. /// <summary>Initializes a new instance of <see cref="Button"/>.</summary>
  41. public Button ()
  42. {
  43. TextAlignment = Alignment.Center;
  44. VerticalTextAlignment = Alignment.Center;
  45. _leftBracket = Glyphs.LeftBracket;
  46. _rightBracket = Glyphs.RightBracket;
  47. _leftDefault = Glyphs.LeftDefaultIndicator;
  48. _rightDefault = Glyphs.RightDefaultIndicator;
  49. Height = Dim.Auto (DimAutoStyle.Text);
  50. Width = Dim.Auto (DimAutoStyle.Text);
  51. CanFocus = true;
  52. HighlightStyle |= HighlightStyle.Pressed;
  53. #if HOVER
  54. HighlightStyle |= HighlightStyle.Hover;
  55. #endif
  56. // Override default behavior of View
  57. AddCommand (Command.HotKey, () =>
  58. {
  59. SetFocus ();
  60. return !OnAccept ();
  61. });
  62. KeyBindings.Add (Key.Space, Command.HotKey);
  63. KeyBindings.Add (Key.Enter, Command.HotKey);
  64. TitleChanged += Button_TitleChanged;
  65. MouseClick += Button_MouseClick;
  66. ShadowStyle = DefaultShadow;
  67. }
  68. private bool _wantContinuousButtonPressed;
  69. /// <inheritdoc />
  70. public override bool WantContinuousButtonPressed
  71. {
  72. get => _wantContinuousButtonPressed;
  73. set
  74. {
  75. if (value == _wantContinuousButtonPressed)
  76. {
  77. return;
  78. }
  79. _wantContinuousButtonPressed = value;
  80. if (_wantContinuousButtonPressed)
  81. {
  82. HighlightStyle |= HighlightStyle.PressedOutside;
  83. }
  84. else
  85. {
  86. HighlightStyle &= ~HighlightStyle.PressedOutside;
  87. }
  88. }
  89. }
  90. private void Button_MouseClick (object sender, MouseEventEventArgs e)
  91. {
  92. e.Handled = InvokeCommand (Command.HotKey) == true;
  93. }
  94. private void Button_TitleChanged (object sender, EventArgs<string> e)
  95. {
  96. base.Text = e.CurrentValue;
  97. TextFormatter.HotKeySpecifier = HotKeySpecifier;
  98. }
  99. /// <inheritdoc />
  100. public override string Text
  101. {
  102. get => base.Title;
  103. set => base.Text = base.Title = value;
  104. }
  105. /// <inheritdoc />
  106. public override Rune HotKeySpecifier
  107. {
  108. get => base.HotKeySpecifier;
  109. set => TextFormatter.HotKeySpecifier = base.HotKeySpecifier = value;
  110. }
  111. /// <summary>Gets or sets whether the <see cref="Button"/> is the default action to activate in a dialog.</summary>
  112. /// <value><c>true</c> if is default; otherwise, <c>false</c>.</value>
  113. public bool IsDefault
  114. {
  115. get => _isDefault;
  116. set
  117. {
  118. _isDefault = value;
  119. UpdateTextFormatterText ();
  120. OnResizeNeeded ();
  121. }
  122. }
  123. /// <summary></summary>
  124. public bool NoDecorations { get; set; }
  125. /// <summary></summary>
  126. public bool NoPadding { get; set; }
  127. /// <inheritdoc/>
  128. public override Point? 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 null; // Don't show the cursor
  138. }
  139. }
  140. }
  141. return base.PositionCursor ();
  142. }
  143. /// <inheritdoc/>
  144. protected override void UpdateTextFormatterText ()
  145. {
  146. base.UpdateTextFormatterText();
  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. /// <inheritdoc />
  168. public bool EnableForDesign ()
  169. {
  170. Title = "_Button";
  171. return true;
  172. }
  173. }