Label.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. namespace Terminal.Gui;
  2. /// <summary>
  3. /// The Label <see cref="View"/> displays a string at a given position and supports multiple lines separated by
  4. /// newline characters. Multi-line Labels support word wrap.
  5. /// </summary>
  6. /// <remarks>
  7. /// The <see cref="Label"/> view is functionality identical to <see cref="View"/> and is included for API
  8. /// backwards compatibility.
  9. /// </remarks>
  10. public class Label : View
  11. {
  12. /// <inheritdoc/>
  13. public Label ()
  14. {
  15. Height = 1;
  16. AutoSize = true;
  17. // Things this view knows how to do
  18. AddCommand (
  19. Command.Default,
  20. () =>
  21. {
  22. // BUGBUG: This is a hack, but it does work.
  23. bool can = CanFocus;
  24. CanFocus = true;
  25. SetFocus ();
  26. SuperView.FocusNext ();
  27. CanFocus = can;
  28. return true;
  29. }
  30. );
  31. AddCommand (Command.Accept, () => AcceptKey ());
  32. // Default key bindings for this view
  33. KeyBindings.Add (KeyCode.Space, Command.Accept);
  34. }
  35. /// <summary>
  36. /// The event fired when the user clicks the primary mouse button within the Bounds of this <see cref="View"/> or
  37. /// if the user presses the action key while this view is focused. (TODO: IsDefault)
  38. /// </summary>
  39. /// <remarks>
  40. /// Client code can hook up to this event, it is raised when the button is activated either with the mouse or the
  41. /// keyboard.
  42. /// </remarks>
  43. public event EventHandler Clicked;
  44. /// <summary>Virtual method to invoke the <see cref="Clicked"/> event.</summary>
  45. public virtual void OnClicked () { Clicked?.Invoke (this, EventArgs.Empty); }
  46. /// <inheritdoc/>
  47. public override bool OnEnter (View view)
  48. {
  49. Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
  50. return base.OnEnter (view);
  51. }
  52. /// <summary>Method invoked when a mouse event is generated</summary>
  53. /// <param name="mouseEvent"></param>
  54. /// <returns><c>true</c>, if the event was handled, <c>false</c> otherwise.</returns>
  55. public override bool OnMouseEvent (MouseEvent mouseEvent)
  56. {
  57. var args = new MouseEventEventArgs (mouseEvent);
  58. if (OnMouseClick (args))
  59. {
  60. return true;
  61. }
  62. if (MouseEvent (mouseEvent))
  63. {
  64. return true;
  65. }
  66. if (mouseEvent.Flags == MouseFlags.Button1Clicked)
  67. {
  68. if (!HasFocus && SuperView is { })
  69. {
  70. if (!SuperView.HasFocus)
  71. {
  72. SuperView.SetFocus ();
  73. }
  74. SetFocus ();
  75. SetNeedsDisplay ();
  76. }
  77. OnClicked ();
  78. return true;
  79. }
  80. return false;
  81. }
  82. private bool AcceptKey ()
  83. {
  84. if (!HasFocus)
  85. {
  86. SetFocus ();
  87. }
  88. OnClicked ();
  89. return true;
  90. }
  91. }