Button.cs 5.3 KB

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