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. // 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. e.Handled = InvokeCommand (Command.HotKey) == true;
  27. }
  28. private void Label_TitleChanged (object sender, EventArgs<string> e)
  29. {
  30. base.Text = e.CurrentValue;
  31. TextFormatter.HotKeySpecifier = HotKeySpecifier;
  32. }
  33. /// <inheritdoc />
  34. public override string Text
  35. {
  36. get => base.Title;
  37. set => base.Text = base.Title = value;
  38. }
  39. /// <inheritdoc />
  40. public override Rune HotKeySpecifier
  41. {
  42. get => base.HotKeySpecifier;
  43. set => TextFormatter.HotKeySpecifier = base.HotKeySpecifier = value;
  44. }
  45. private new bool? FocusNext ()
  46. {
  47. var me = SuperView?.Subviews.IndexOf (this) ?? -1;
  48. if (me != -1 && me < SuperView?.Subviews.Count - 1)
  49. {
  50. SuperView?.Subviews [me + 1].SetFocus ();
  51. }
  52. return true;
  53. }
  54. }