Button.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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="Clicked"/> event.</summary>
  9. /// <remarks>
  10. /// <para>
  11. /// Provides a button showing text that raises the <see cref="Clicked"/> 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="Clicked"/> event will 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. HotKeySpecifier = new Rune ('_');
  36. _leftBracket = Glyphs.LeftBracket;
  37. _rightBracket = Glyphs.RightBracket;
  38. _leftDefault = Glyphs.LeftDefaultIndicator;
  39. _rightDefault = Glyphs.RightDefaultIndicator;
  40. // Ensures a height of 1 if AutoSize is set to false
  41. Height = 1;
  42. CanFocus = true;
  43. AutoSize = true;
  44. // Override default behavior of View
  45. // Command.Default sets focus
  46. AddCommand (
  47. Command.Accept,
  48. () =>
  49. {
  50. OnClicked ();
  51. return true;
  52. }
  53. );
  54. KeyBindings.Add (Key.Space, Command.Default, Command.Accept);
  55. KeyBindings.Add (Key.Enter, Command.Default, Command.Accept);
  56. }
  57. /// <summary>Gets or sets whether the <see cref="Button"/> is the default action to activate in a dialog.</summary>
  58. /// <value><c>true</c> if is default; otherwise, <c>false</c>.</value>
  59. public bool IsDefault
  60. {
  61. get => _isDefault;
  62. set
  63. {
  64. _isDefault = value;
  65. UpdateTextFormatterText ();
  66. OnResizeNeeded ();
  67. }
  68. }
  69. /// <summary></summary>
  70. public bool NoDecorations { get; set; }
  71. /// <summary></summary>
  72. public bool NoPadding { get; set; }
  73. /// <summary>
  74. /// The event fired when the user clicks the primary mouse button within the Bounds of this <see cref="View"/> or
  75. /// if the user presses the action key while this view is focused. (TODO: IsDefault)
  76. /// </summary>
  77. /// <remarks>
  78. /// Client code can hook up to this event, it is raised when the button is activated either with the mouse or the
  79. /// keyboard.
  80. /// </remarks>
  81. public event EventHandler Clicked;
  82. /// <inheritdoc/>
  83. public override bool MouseEvent (MouseEvent me)
  84. {
  85. if (me.Flags == MouseFlags.Button1Clicked)
  86. {
  87. if (CanFocus && Enabled)
  88. {
  89. if (!HasFocus)
  90. {
  91. SetFocus ();
  92. SetNeedsDisplay ();
  93. Draw ();
  94. }
  95. OnClicked ();
  96. }
  97. return true;
  98. }
  99. return false;
  100. }
  101. /// <summary>Virtual method to invoke the <see cref="Clicked"/> event.</summary>
  102. public virtual void OnClicked () { Clicked?.Invoke (this, EventArgs.Empty); }
  103. /// <inheritdoc/>
  104. public override bool OnEnter (View view)
  105. {
  106. Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
  107. return base.OnEnter (view);
  108. }
  109. /// <inheritdoc/>
  110. public override void PositionCursor ()
  111. {
  112. if (HotKey.IsValid && Text != "")
  113. {
  114. for (var i = 0; i < TextFormatter.Text.GetRuneCount (); i++)
  115. {
  116. if (TextFormatter.Text [i] == Text [0])
  117. {
  118. Move (i, 0);
  119. return;
  120. }
  121. }
  122. }
  123. base.PositionCursor ();
  124. }
  125. /// <inheritdoc/>
  126. protected override void UpdateTextFormatterText ()
  127. {
  128. if (NoDecorations)
  129. {
  130. TextFormatter.Text = Text;
  131. }
  132. else if (IsDefault)
  133. {
  134. TextFormatter.Text = $"{_leftBracket}{_leftDefault} {Text} {_rightDefault}{_rightBracket}";
  135. }
  136. else
  137. {
  138. if (NoPadding)
  139. {
  140. TextFormatter.Text = $"{_leftBracket}{Text}{_rightBracket}";
  141. }
  142. else
  143. {
  144. TextFormatter.Text = $"{_leftBracket} {Text} {_rightBracket}";
  145. }
  146. }
  147. }
  148. }