Label.cs 2.7 KB

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