Label.cs 3.2 KB

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