Label.cs 1.8 KB

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