Label.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System.Reflection.Metadata.Ecma335;
  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
  5. /// newline characters. Multi-line Labels support word wrap.
  6. /// </summary>
  7. /// <remarks>
  8. /// The <see cref="Label"/> view is functionality identical to <see cref="View"/> and is included for API
  9. /// backwards compatibility.
  10. /// </remarks>
  11. public class Label : View
  12. {
  13. /// <inheritdoc/>
  14. public Label ()
  15. {
  16. Height = 1;
  17. AutoSize = true;
  18. // Things this view knows how to do
  19. AddCommand (Command.HotKey, FocusNext);
  20. // Default key bindings for this view
  21. KeyBindings.Add (Key.Space, Command.Accept);
  22. TitleChanged += Label_TitleChanged;
  23. }
  24. private void Label_TitleChanged (object sender, StateEventArgs<string> e)
  25. {
  26. base.Text = e.NewValue;
  27. TextFormatter.HotKeySpecifier = HotKeySpecifier;
  28. }
  29. /// <inheritdoc />
  30. public override string Text
  31. {
  32. get => base.Title;
  33. set => base.Text = base.Title = value;
  34. }
  35. /// <inheritdoc />
  36. public override Rune HotKeySpecifier
  37. {
  38. get => base.HotKeySpecifier;
  39. set => TextFormatter.HotKeySpecifier = base.HotKeySpecifier = value;
  40. }
  41. private new bool? FocusNext ()
  42. {
  43. var me = SuperView?.Subviews.IndexOf (this) ?? -1;
  44. if (me != -1 && me < SuperView?.Subviews.Count - 1)
  45. {
  46. SuperView?.Subviews [me + 1].SetFocus ();
  47. }
  48. return true;
  49. }
  50. /// <inheritdoc/>
  51. public override bool OnEnter (View view)
  52. {
  53. Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
  54. return base.OnEnter (view);
  55. }
  56. /// <summary>Method invoked when a mouse event is generated</summary>
  57. /// <param name="mouseEvent"></param>
  58. /// <returns><c>true</c>, if the event was handled, <c>false</c> otherwise.</returns>
  59. public override bool OnMouseEvent (MouseEvent mouseEvent)
  60. {
  61. var args = new MouseEventEventArgs (mouseEvent);
  62. if (OnMouseClick (args))
  63. {
  64. return true;
  65. }
  66. if (MouseEvent (mouseEvent))
  67. {
  68. return true;
  69. }
  70. if (mouseEvent.Flags == MouseFlags.Button1Clicked)
  71. {
  72. if (!CanFocus)
  73. {
  74. FocusNext ();
  75. }
  76. if (!HasFocus && SuperView is { })
  77. {
  78. if (!SuperView.HasFocus)
  79. {
  80. SuperView.SetFocus ();
  81. }
  82. SetFocus ();
  83. SetNeedsDisplay ();
  84. }
  85. OnAccept ();
  86. return true;
  87. }
  88. return false;
  89. }
  90. }