Label.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. // Things this view knows how to do
  29. AddCommand (Command.HotKey, InvokeHotKeyOnNext);
  30. TitleChanged += Label_TitleChanged;
  31. MouseClick += Label_MouseClick;
  32. }
  33. private void Label_MouseClick (object sender, MouseEventEventArgs e)
  34. {
  35. if (!CanFocus)
  36. {
  37. e.Handled = InvokeCommand (Command.HotKey) == true;
  38. }
  39. }
  40. private void Label_TitleChanged (object sender, EventArgs<string> e)
  41. {
  42. base.Text = e.CurrentValue;
  43. TextFormatter.HotKeySpecifier = HotKeySpecifier;
  44. }
  45. /// <inheritdoc/>
  46. public override string Text
  47. {
  48. get => Title;
  49. set => base.Text = Title = value;
  50. }
  51. /// <inheritdoc/>
  52. public override Rune HotKeySpecifier
  53. {
  54. get => base.HotKeySpecifier;
  55. set => TextFormatter.HotKeySpecifier = base.HotKeySpecifier = value;
  56. }
  57. private bool? InvokeHotKeyOnNext (CommandContext context)
  58. {
  59. int me = SuperView?.Subviews.IndexOf (this) ?? -1;
  60. if (me != -1 && me < SuperView?.Subviews.Count - 1)
  61. {
  62. SuperView?.Subviews [me + 1].InvokeCommand (Command.HotKey, context.Key, context.KeyBinding);
  63. }
  64. return true;
  65. }
  66. }