Label.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. /// receives 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. private void Label_MouseClick (object sender, MouseEventArgs e)
  31. {
  32. if (!CanFocus)
  33. {
  34. e.Handled = InvokeCommand<KeyBinding> (Command.HotKey, new ([Command.HotKey], this, data: this)) == true;
  35. }
  36. }
  37. private void Label_TitleChanged (object sender, EventArgs<string> e)
  38. {
  39. base.Text = e.CurrentValue;
  40. TextFormatter.HotKeySpecifier = HotKeySpecifier;
  41. }
  42. /// <inheritdoc/>
  43. public override string Text
  44. {
  45. get => Title;
  46. set => base.Text = Title = value;
  47. }
  48. /// <inheritdoc/>
  49. public override Rune HotKeySpecifier
  50. {
  51. get => base.HotKeySpecifier;
  52. set => TextFormatter.HotKeySpecifier = base.HotKeySpecifier = value;
  53. }
  54. private bool? InvokeHotKeyOnNext (ICommandContext commandContext)
  55. {
  56. if (RaiseHandlingHotKey () == true)
  57. {
  58. return true;
  59. }
  60. if (CanFocus)
  61. {
  62. SetFocus ();
  63. return true;
  64. }
  65. if (HotKey.IsValid)
  66. {
  67. int me = SuperView?.Subviews.IndexOf (this) ?? -1;
  68. if (me != -1 && me < SuperView?.Subviews.Count - 1)
  69. {
  70. return SuperView?.Subviews [me + 1].InvokeCommand (Command.HotKey) == true;
  71. }
  72. }
  73. return false;
  74. }
  75. /// <inheritdoc/>
  76. bool IDesignable.EnableForDesign ()
  77. {
  78. Text = "_Label";
  79. return true;
  80. }
  81. }