Label.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. TitleChanged += Label_TitleChanged;
  20. MouseClick += Label_MouseClick;
  21. }
  22. private void Label_MouseClick (object sender, MouseEventEventArgs e)
  23. {
  24. if (!CanFocus)
  25. {
  26. e.Handled = InvokeCommand (Command.HotKey) == true;
  27. }
  28. }
  29. private void Label_TitleChanged (object sender, EventArgs<string> e)
  30. {
  31. base.Text = e.CurrentValue;
  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 bool? FocusNext ()
  47. {
  48. int 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. }