Button.cs 5.5 KB

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