Label.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. namespace Terminal.Gui;
  2. /// <summary>
  3. /// The Label <see cref="View"/> displays text that describes the View next in the <see cref="View.Subviews"/>. When
  4. /// Label
  5. /// recieves a <see cref="Command.HotKey"/> command it will pass it to the next <see cref="View"/> in
  6. /// <see cref="View.Subviews"/>.
  7. /// </summary>
  8. /// <remarks>
  9. /// <para>
  10. /// Title and Text are the same property. When Title is set Text s also set. When Text is set Title is also set.
  11. /// </para>
  12. /// <para>
  13. /// If <see cref="View.CanFocus"/> is <see langword="false"/> and the use clicks on the Label,
  14. /// the <see cref="Command.HotKey"/> will be invoked on the next <see cref="View"/> in
  15. /// <see cref="View.Subviews"/>."
  16. /// </para>
  17. /// </remarks>
  18. public class Label : View, IDesignable
  19. {
  20. /// <inheritdoc/>
  21. public Label ()
  22. {
  23. Height = Dim.Auto (DimAutoStyle.Text);
  24. Width = Dim.Auto (DimAutoStyle.Text);
  25. // On HoKey, pass it to the next view
  26. AddCommand (Command.HotKey, InvokeHotKeyOnNext);
  27. TitleChanged += Label_TitleChanged;
  28. MouseClick += Label_MouseClick;
  29. }
  30. // TODO: base raises Select, but we want to raise HotKey. This can be simplified?
  31. private void Label_MouseClick (object sender, MouseEventEventArgs e)
  32. {
  33. if (!CanFocus)
  34. {
  35. e.Handled = InvokeCommand (Command.HotKey, ctx: new (Command.HotKey, key: null, data: this)) == true;
  36. }
  37. }
  38. private void Label_TitleChanged (object sender, EventArgs<string> e)
  39. {
  40. base.Text = e.CurrentValue;
  41. TextFormatter.HotKeySpecifier = HotKeySpecifier;
  42. }
  43. /// <inheritdoc/>
  44. public override string Text
  45. {
  46. get => Title;
  47. set => base.Text = Title = value;
  48. }
  49. /// <inheritdoc/>
  50. public override Rune HotKeySpecifier
  51. {
  52. get => base.HotKeySpecifier;
  53. set => TextFormatter.HotKeySpecifier = base.HotKeySpecifier = value;
  54. }
  55. private bool? InvokeHotKeyOnNext (CommandContext context)
  56. {
  57. if (RaiseHotKeyHandled () == true)
  58. {
  59. return true;
  60. }
  61. if (CanFocus)
  62. {
  63. SetFocus ();
  64. return true;
  65. }
  66. int me = SuperView?.Subviews.IndexOf (this) ?? -1;
  67. if (me != -1 && me < SuperView?.Subviews.Count - 1)
  68. {
  69. return SuperView?.Subviews [me + 1].InvokeCommand (Command.HotKey, context.Key, context.KeyBinding) == true;
  70. }
  71. return false;
  72. }
  73. /// <inheritdoc/>
  74. bool IDesignable.EnableForDesign ()
  75. {
  76. Text = "_Label";
  77. return true;
  78. }
  79. }