From 285710360c12e3885998e38edfeb998f0e038938 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Thu, 18 Apr 2024 15:26:52 +0200 Subject: [PATCH] Fix margins issues in context menus --- Source/Editor/GUI/ContextMenu/ContextMenu.cs | 11 ++++++----- .../GUI/ContextMenu/ContextMenuChildMenu.cs | 16 +++++++++++----- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/Source/Editor/GUI/ContextMenu/ContextMenu.cs b/Source/Editor/GUI/ContextMenu/ContextMenu.cs index 05d62d3f2..cb197e141 100644 --- a/Source/Editor/GUI/ContextMenu/ContextMenu.cs +++ b/Source/Editor/GUI/ContextMenu/ContextMenu.cs @@ -42,15 +42,14 @@ namespace FlaxEditor.GUI.ContextMenu // Arrange controls Margin margin = _menu._itemsMargin; - float y = margin.Top; - float x = margin.Left; + float y = 0; float width = Width - margin.Width; for (int i = 0; i < _children.Count; i++) { if (_children[i] is ContextMenuItem item && item.Visible) { var height = item.Height; - item.Bounds = new Rectangle(x, y, width, height); + item.Bounds = new Rectangle(margin.Left, y, width, height); y += height + margin.Height; } } @@ -300,7 +299,6 @@ namespace FlaxEditor.GUI.ContextMenu if (_panel.Children[i] is ContextMenuChildMenu menu && menu.Text == text) return menu; } - return null; } @@ -319,7 +317,6 @@ namespace FlaxEditor.GUI.ContextMenu Parent = _panel }; } - return item; } @@ -396,10 +393,12 @@ namespace FlaxEditor.GUI.ContextMenu float height = _itemsAreaMargin.Height; int itemsLeft = MaximumItemsInViewCount; int overflowItemCount = 0; + int itemsCount = 0; for (int i = 0; i < _panel.Children.Count; i++) { if (_panel.Children[i] is ContextMenuItem item && item.Visible) { + itemsCount++; if (itemsLeft > 0) { height += item.Height + _itemsMargin.Height; @@ -412,6 +411,8 @@ namespace FlaxEditor.GUI.ContextMenu maxWidth = Mathf.Max(maxWidth, item.MinimumWidth); } } + if (itemsCount != 0) + height -= _itemsMargin.Height; // Remove item margin from top and bottom maxWidth = Mathf.Max(maxWidth + 20, MinimumWidth); // Move child arrows to accommodate scroll bar showing diff --git a/Source/Editor/GUI/ContextMenu/ContextMenuChildMenu.cs b/Source/Editor/GUI/ContextMenu/ContextMenuChildMenu.cs index 3e422f98f..af0308184 100644 --- a/Source/Editor/GUI/ContextMenu/ContextMenuChildMenu.cs +++ b/Source/Editor/GUI/ContextMenu/ContextMenuChildMenu.cs @@ -29,6 +29,15 @@ namespace FlaxEditor.GUI.ContextMenu CloseMenuOnClick = false; } + private void ShowChild(ContextMenu parentContextMenu) + { + // Hide parent CM popups and set itself as child + var vAlign = parentContextMenu.ItemsAreaMargin.Top; + var location = new Float2(Width, -vAlign); + location = PointToParent(parentContextMenu, location); + parentContextMenu.ShowChild(ContextMenu, location); + } + /// public override void Draw() { @@ -58,14 +67,12 @@ namespace FlaxEditor.GUI.ContextMenu var parentContextMenu = ParentContextMenu; if (parentContextMenu == ContextMenu) return; - if (ContextMenu.IsOpened) return; base.OnMouseEnter(location); - // Hide parent CM popups and set itself as child - parentContextMenu.ShowChild(ContextMenu, PointToParent(ParentContextMenu, new Float2(Width, 0))); + ShowChild(parentContextMenu); } /// @@ -78,8 +85,7 @@ namespace FlaxEditor.GUI.ContextMenu if (ContextMenu.IsOpened) return true; - // Hide parent CM popups and set itself as child - parentContextMenu.ShowChild(ContextMenu, PointToParent(ParentContextMenu, new Float2(Width, 0))); + ShowChild(parentContextMenu); return base.OnMouseUp(location, button); } }