Label.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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
  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 (CanFocus)
  61. {
  62. return SetFocus ();
  63. }
  64. int me = SuperView?.Subviews.IndexOf (this) ?? -1;
  65. if (me != -1 && me < SuperView?.Subviews.Count - 1)
  66. {
  67. SuperView?.Subviews [me + 1].InvokeCommand (Command.HotKey, context.Key, context.KeyBinding);
  68. }
  69. return true;
  70. }
  71. }