ContextMenuElement.xaml.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using Robust.Client.AutoGenerated;
  2. using Robust.Client.Graphics;
  3. using Robust.Client.UserInterface.Controls;
  4. using Robust.Client.UserInterface.XAML;
  5. using Robust.Shared.Utility;
  6. namespace Content.Client.ContextMenu.UI
  7. {
  8. /// <summary>
  9. /// This is a basic entry in a context menu. It has a label and room for some sort of icon on the left.
  10. /// If this entry has a sub-menu, it also shows a little ">" icon on the right.
  11. /// </summary>
  12. [GenerateTypedNameReferences]
  13. [Virtual]
  14. public partial class ContextMenuElement : ContainerButton
  15. {
  16. public const string StyleClassContextMenuButton = "contextMenuButton";
  17. public const string StyleClassContextMenuExpansionTexture = "contextMenuExpansionTexture";
  18. public const string StyleClassEntityMenuIconLabel = "contextMenuIconLabel";
  19. public const float ElementMargin = 2;
  20. public const float ElementHeight = 32;
  21. /// <summary>
  22. /// The menu that contains this element
  23. /// </summary>
  24. public ContextMenuPopup? ParentMenu;
  25. private ContextMenuPopup? _subMenu;
  26. /// <summary>
  27. /// The pop-up menu that is opened when hovering over this element.
  28. /// </summary>
  29. public ContextMenuPopup? SubMenu
  30. {
  31. get => _subMenu;
  32. set
  33. {
  34. _subMenu = value;
  35. ExpansionIndicator.Visible = _subMenu != null;
  36. }
  37. }
  38. /// <summary>
  39. /// Convenience property to set label text.
  40. /// </summary>
  41. public virtual string Text { set => Label.SetMessage(FormattedMessage.FromMarkupPermissive(value.Trim())); }
  42. public ContextMenuElement(string? text = null)
  43. {
  44. RobustXamlLoader.Load(this);
  45. Margin = new Thickness(ElementMargin, ElementMargin, ElementMargin, ElementMargin);
  46. SetOnlyStyleClass(StyleClassContextMenuButton);
  47. if (text != null)
  48. Text = text;
  49. }
  50. protected override void Dispose(bool disposing)
  51. {
  52. base.Dispose(disposing);
  53. _subMenu?.Dispose();
  54. _subMenu = null;
  55. ParentMenu = null;
  56. }
  57. protected override void Draw(DrawingHandleScreen handle)
  58. {
  59. UpdateStyle();
  60. base.Draw(handle);
  61. }
  62. /// <summary>
  63. /// If this element's sub-menu is currently visible, give it the hovered pseudo class.
  64. /// </summary>
  65. /// <remarks>
  66. /// Basically: if we are in a sub menu, keep the element in the parent menu highlighted even though we are
  67. /// not actually hovering over it.
  68. /// </remarks>
  69. protected virtual void UpdateStyle()
  70. {
  71. if ((_subMenu?.Visible ?? false) && !HasStylePseudoClass(StylePseudoClassHover))
  72. {
  73. AddStylePseudoClass(StylePseudoClassHover);
  74. return;
  75. }
  76. if (DrawMode == DrawModeEnum.Hover)
  77. return;
  78. if (_subMenu?.Visible ?? true)
  79. return;
  80. if (HasStylePseudoClass(StylePseudoClassHover))
  81. RemoveStylePseudoClass(StylePseudoClassHover);
  82. }
  83. }
  84. }