Label.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. MouseClick += Label_MouseClick;
  24. }
  25. private void Label_MouseClick (object sender, MouseEventEventArgs e)
  26. {
  27. e.Handled = InvokeCommand (Command.HotKey) == true;
  28. }
  29. private void Label_TitleChanged (object sender, StateEventArgs<string> e)
  30. {
  31. base.Text = e.NewValue;
  32. TextFormatter.HotKeySpecifier = HotKeySpecifier;
  33. }
  34. /// <inheritdoc />
  35. public override string Text
  36. {
  37. get => base.Title;
  38. set => base.Text = base.Title = value;
  39. }
  40. /// <inheritdoc />
  41. public override Rune HotKeySpecifier
  42. {
  43. get => base.HotKeySpecifier;
  44. set => TextFormatter.HotKeySpecifier = base.HotKeySpecifier = value;
  45. }
  46. private new bool? FocusNext ()
  47. {
  48. var me = SuperView?.Subviews.IndexOf (this) ?? -1;
  49. if (me != -1 && me < SuperView?.Subviews.Count - 1)
  50. {
  51. SuperView?.Subviews [me + 1].SetFocus ();
  52. }
  53. return true;
  54. }
  55. /// <inheritdoc/>
  56. public override bool OnEnter (View view)
  57. {
  58. Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
  59. return base.OnEnter (view);
  60. }
  61. }