Button.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. /// </remarks>
  22. public class Button : View
  23. {
  24. private readonly Rune _leftBracket;
  25. private readonly Rune _leftDefault;
  26. private readonly Rune _rightBracket;
  27. private readonly Rune _rightDefault;
  28. private bool _isDefault;
  29. /// <summary>Initializes a new instance of <see cref="Button"/> using <see cref="LayoutStyle.Computed"/> layout.</summary>
  30. /// <remarks>The width of the <see cref="Button"/> is computed based on the text length. The height will always be 1.</remarks>
  31. public Button ()
  32. {
  33. TextAlignment = TextAlignment.Centered;
  34. VerticalTextAlignment = VerticalTextAlignment.Middle;
  35. _leftBracket = Glyphs.LeftBracket;
  36. _rightBracket = Glyphs.RightBracket;
  37. _leftDefault = Glyphs.LeftDefaultIndicator;
  38. _rightDefault = Glyphs.RightDefaultIndicator;
  39. // Ensures a height of 1 if AutoSize is set to false
  40. Height = 1;
  41. CanFocus = true;
  42. AutoSize = true;
  43. // Override default behavior of View
  44. AddCommand (Command.HotKey, () =>
  45. {
  46. SetFocus ();
  47. return !OnAccept ();
  48. });
  49. KeyBindings.Add (Key.Space, Command.HotKey);
  50. KeyBindings.Add (Key.Enter, Command.HotKey);
  51. TitleChanged += Button_TitleChanged;
  52. }
  53. private void Button_TitleChanged (object sender, StateEventArgs<string> e)
  54. {
  55. base.Text = e.NewValue;
  56. TextFormatter.HotKeySpecifier = HotKeySpecifier;
  57. }
  58. /// <inheritdoc />
  59. public override string Text
  60. {
  61. get => base.Title;
  62. set => base.Text = base.Title = value;
  63. }
  64. /// <inheritdoc />
  65. public override Rune HotKeySpecifier
  66. {
  67. get => base.HotKeySpecifier;
  68. set => TextFormatter.HotKeySpecifier = base.HotKeySpecifier = value;
  69. }
  70. /// <summary>Gets or sets whether the <see cref="Button"/> is the default action to activate in a dialog.</summary>
  71. /// <value><c>true</c> if is default; otherwise, <c>false</c>.</value>
  72. public bool IsDefault
  73. {
  74. get => _isDefault;
  75. set
  76. {
  77. _isDefault = value;
  78. UpdateTextFormatterText ();
  79. OnResizeNeeded ();
  80. }
  81. }
  82. /// <summary></summary>
  83. public bool NoDecorations { get; set; }
  84. /// <summary></summary>
  85. public bool NoPadding { get; set; }
  86. /// <inheritdoc/>
  87. public override bool OnMouseEvent (MouseEvent me)
  88. {
  89. if (me.Flags == MouseFlags.Button1Clicked)
  90. {
  91. if (CanFocus && Enabled)
  92. {
  93. if (!HasFocus)
  94. {
  95. SetFocus ();
  96. SetNeedsDisplay ();
  97. Draw ();
  98. }
  99. OnAccept ();
  100. }
  101. return true;
  102. }
  103. return false;
  104. }
  105. /// <inheritdoc/>
  106. public override bool OnEnter (View view)
  107. {
  108. Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
  109. return base.OnEnter (view);
  110. }
  111. /// <inheritdoc/>
  112. public override void PositionCursor ()
  113. {
  114. if (HotKey.IsValid && Text != "")
  115. {
  116. for (var i = 0; i < TextFormatter.Text.GetRuneCount (); i++)
  117. {
  118. if (TextFormatter.Text [i] == Text [0])
  119. {
  120. Move (i, 0);
  121. return;
  122. }
  123. }
  124. }
  125. base.PositionCursor ();
  126. }
  127. /// <inheritdoc/>
  128. protected override void UpdateTextFormatterText ()
  129. {
  130. if (NoDecorations)
  131. {
  132. TextFormatter.Text = Text;
  133. }
  134. else if (IsDefault)
  135. {
  136. TextFormatter.Text = $"{_leftBracket}{_leftDefault} {Text} {_rightDefault}{_rightBracket}";
  137. }
  138. else
  139. {
  140. if (NoPadding)
  141. {
  142. TextFormatter.Text = $"{_leftBracket}{Text}{_rightBracket}";
  143. }
  144. else
  145. {
  146. TextFormatter.Text = $"{_leftBracket} {Text} {_rightBracket}";
  147. }
  148. }
  149. }
  150. }