Label.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 = Dim.Auto (DimAutoStyle.Text);
  16. Width = Dim.Auto (DimAutoStyle.Text);
  17. // Things this view knows how to do
  18. AddCommand (Command.HotKey, FocusNext);
  19. // Default key bindings for this view
  20. KeyBindings.Add (Key.Space, Command.Accept);
  21. TitleChanged += Label_TitleChanged;
  22. MouseClick += Label_MouseClick;
  23. }
  24. private void Label_MouseClick (object sender, MouseEventEventArgs e)
  25. {
  26. if (!CanFocus)
  27. {
  28. e.Handled = InvokeCommand (Command.HotKey) == true;
  29. }
  30. }
  31. private void Label_TitleChanged (object sender, EventArgs<string> e)
  32. {
  33. base.Text = e.CurrentValue;
  34. TextFormatter.HotKeySpecifier = HotKeySpecifier;
  35. }
  36. /// <inheritdoc />
  37. public override string Text
  38. {
  39. get => base.Title;
  40. set => base.Text = base.Title = value;
  41. }
  42. /// <inheritdoc />
  43. public override Rune HotKeySpecifier
  44. {
  45. get => base.HotKeySpecifier;
  46. set => TextFormatter.HotKeySpecifier = base.HotKeySpecifier = value;
  47. }
  48. private bool? FocusNext ()
  49. {
  50. int me = SuperView?.Subviews.IndexOf (this) ?? -1;
  51. if (me != -1 && me < SuperView?.Subviews.Count - 1)
  52. {
  53. SuperView?.Subviews [me + 1].SetFocus ();
  54. }
  55. return true;
  56. }
  57. }