1
0

ContextMenuPopup.xaml.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System.Numerics;
  2. using Robust.Client.AutoGenerated;
  3. using Robust.Client.Graphics;
  4. using Robust.Client.UserInterface.Controls;
  5. using Robust.Client.UserInterface.XAML;
  6. using Robust.Shared.Utility;
  7. namespace Content.Client.ContextMenu.UI
  8. {
  9. /// <summary>
  10. /// The base context-menu pop-up window used for both the entity and verb menus.
  11. /// </summary>
  12. [GenerateTypedNameReferences]
  13. public sealed partial class ContextMenuPopup : Popup
  14. {
  15. public const string StyleClassContextMenuPopup = "contextMenuPopup";
  16. /// <summary>
  17. /// How many items to list before limiting the size and adding a scroll bar.
  18. /// </summary>
  19. public const int MaxItemsBeforeScroll = 10;
  20. /// <summary>
  21. /// If this pop-up is created by hovering over some element in another pop-up, this is that element.
  22. /// </summary>
  23. public ContextMenuElement? ParentElement;
  24. /// <summary>
  25. /// This is the main body of the menu. The menu entries should be added to this object.
  26. /// </summary>
  27. public GridContainer MenuBody = new();
  28. private ContextMenuUIController _uiController;
  29. public ContextMenuPopup (ContextMenuUIController uiController, ContextMenuElement? parentElement) : base()
  30. {
  31. RobustXamlLoader.Load(this);
  32. MenuPanel.SetOnlyStyleClass(StyleClassContextMenuPopup);
  33. _uiController = uiController;
  34. ParentElement = parentElement;
  35. // TODO xaml controls now have the access options -> re-xamlify all this.
  36. //XAML controls are private. So defining and adding MenuBody here instead.
  37. Scroll.AddChild(MenuBody);
  38. // Set Max Height based on MaxItemsBeforeScroll and the panel's style box
  39. MenuPanel.ForceRunStyleUpdate();
  40. MenuPanel.TryGetStyleProperty<StyleBox>(PanelContainer.StylePropertyPanel, out var box);
  41. var styleSize = (box?.MinimumSize ?? Vector2.Zero) / UIScale;
  42. MenuPanel.MaxHeight = MaxItemsBeforeScroll * (ContextMenuElement.ElementHeight + 2 * ContextMenuElement.ElementMargin) + styleSize.Y;
  43. UserInterfaceManager.ModalRoot.AddChild(this);
  44. MenuBody.OnChildRemoved += ctrl => _uiController.OnRemoveElement(this, ctrl);
  45. MenuBody.VSeparationOverride = 0;
  46. MenuBody.HSeparationOverride = 0;
  47. if (ParentElement != null)
  48. {
  49. DebugTools.Assert(ParentElement.SubMenu == null);
  50. ParentElement.SubMenu = this;
  51. }
  52. // ensure the menu-stack is properly updated when a pop-up looses focus or otherwise closes without going
  53. // through the menu presenter.
  54. OnPopupHide += () =>
  55. {
  56. if (ParentElement != null)
  57. _uiController.CloseSubMenus(ParentElement.ParentMenu);
  58. };
  59. }
  60. protected override void Dispose(bool disposing)
  61. {
  62. MenuBody.OnChildRemoved -= ctrl => _uiController.OnRemoveElement(this, ctrl);
  63. ParentElement = null;
  64. base.Dispose(disposing);
  65. }
  66. }
  67. }