Button.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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
  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))]
  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. if (!CanFocus)
  93. {
  94. return;
  95. }
  96. e.Handled = InvokeCommand (Command.HotKey) == true;
  97. }
  98. private void Button_TitleChanged (object sender, StateEventArgs<string> e)
  99. {
  100. base.Text = e.NewValue;
  101. TextFormatter.HotKeySpecifier = HotKeySpecifier;
  102. }
  103. /// <inheritdoc />
  104. public override string Text
  105. {
  106. get => base.Title;
  107. set => base.Text = base.Title = value;
  108. }
  109. /// <inheritdoc />
  110. public override Rune HotKeySpecifier
  111. {
  112. get => base.HotKeySpecifier;
  113. set => TextFormatter.HotKeySpecifier = base.HotKeySpecifier = value;
  114. }
  115. /// <summary>Gets or sets whether the <see cref="Button"/> is the default action to activate in a dialog.</summary>
  116. /// <value><c>true</c> if is default; otherwise, <c>false</c>.</value>
  117. public bool IsDefault
  118. {
  119. get => _isDefault;
  120. set
  121. {
  122. _isDefault = value;
  123. UpdateTextFormatterText ();
  124. OnResizeNeeded ();
  125. }
  126. }
  127. /// <summary></summary>
  128. public bool NoDecorations { get; set; }
  129. /// <summary></summary>
  130. public bool NoPadding { get; set; }
  131. /// <inheritdoc/>
  132. public override Point? PositionCursor ()
  133. {
  134. if (HotKey.IsValid && Text != "")
  135. {
  136. for (var i = 0; i < TextFormatter.Text.GetRuneCount (); i++)
  137. {
  138. if (TextFormatter.Text [i] == Text [0])
  139. {
  140. Move (i, 0);
  141. return null; // Don't show the cursor
  142. }
  143. }
  144. }
  145. return base.PositionCursor ();
  146. }
  147. /// <inheritdoc/>
  148. protected override void UpdateTextFormatterText ()
  149. {
  150. if (NoDecorations)
  151. {
  152. TextFormatter.Text = Text;
  153. }
  154. else if (IsDefault)
  155. {
  156. TextFormatter.Text = $"{_leftBracket}{_leftDefault} {Text} {_rightDefault}{_rightBracket}";
  157. }
  158. else
  159. {
  160. if (NoPadding)
  161. {
  162. TextFormatter.Text = $"{_leftBracket}{Text}{_rightBracket}";
  163. }
  164. else
  165. {
  166. TextFormatter.Text = $"{_leftBracket} {Text} {_rightBracket}";
  167. }
  168. }
  169. }
  170. }