Label.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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, InvokeHotKeyOnNextPeer);
  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? InvokeHotKeyOnNextPeer (ICommandContext commandContext)
  55. {
  56. if (RaiseHandlingHotKey () == true)
  57. {
  58. return true;
  59. }
  60. if (CanFocus)
  61. {
  62. SetFocus ();
  63. // Always return true on hotkey, even if SetFocus fails because
  64. // hotkeys are always handled by the View (unless RaiseHandlingHotKey cancels).
  65. // This is the same behavior as the base (View).
  66. return true;
  67. }
  68. if (HotKey.IsValid)
  69. {
  70. // If the Label has a hotkey, we need to find the next view in the subview list
  71. int me = SuperView?.SubViews.IndexOf (this) ?? -1;
  72. if (me != -1 && me < SuperView?.SubViews.Count - 1)
  73. {
  74. return SuperView?.SubViews.ElementAt (me + 1).InvokeCommand (Command.HotKey) == true;
  75. }
  76. }
  77. return false;
  78. }
  79. /// <inheritdoc/>
  80. bool IDesignable.EnableForDesign ()
  81. {
  82. Text = "_Label";
  83. return true;
  84. }
  85. }