From 0fab7fe85910cd648d387aa07de48ae06e5cc455 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sat, 11 Nov 2023 12:06:28 +0100 Subject: [PATCH] Don't unnecessarily set areas dirty when removing a root element from the ui --- CHANGELOG.md | 1 + MLEM.Ui/Elements/Element.cs | 6 ++++-- MLEM.Ui/Elements/Paragraph.cs | 2 +- MLEM.Ui/UiSystem.cs | 8 +++----- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4c025d8..c3c18cd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,7 @@ Additions Improvements - Allow scrolling panels to contain other scrolling panels - Allow dropdowns to have scrolling panels +- Don't unnecessarily set areas dirty when removing a root element from the ui Fixes - Fixed panels updating their relevant children too much when the scroll bar is hidden diff --git a/MLEM.Ui/Elements/Element.cs b/MLEM.Ui/Elements/Element.cs index 3daa80d..f5ecbc5 100644 --- a/MLEM.Ui/Elements/Element.cs +++ b/MLEM.Ui/Elements/Element.cs @@ -588,10 +588,12 @@ namespace MLEM.Ui.Elements { index = this.children.Count; this.children.Insert(index, element); element.Parent = this; - element.AndChildren(e => e.AddedToUi(this.System, this.Root)); + element.AndChildren(e => { + e.SetAreaDirty(); + e.AddedToUi(this.System, this.Root); + }); this.OnChildAdded?.Invoke(this, element); this.SetSortedChildrenDirty(); - element.SetAreaDirty(); return element; } diff --git a/MLEM.Ui/Elements/Paragraph.cs b/MLEM.Ui/Elements/Paragraph.cs index a6950f8..c508fa0 100644 --- a/MLEM.Ui/Elements/Paragraph.cs +++ b/MLEM.Ui/Elements/Paragraph.cs @@ -232,7 +232,7 @@ namespace MLEM.Ui.Elements { private void SetTextDirty() { this.tokenizedText = null; // only set our area dirty if our size changed as a result of this action - if (!this.AreaDirty && !this.CalcActualSize(this.ParentArea).Equals(this.DisplayArea.Size, Element.Epsilon)) + if (!this.AreaDirty && (this.System == null || !this.CalcActualSize(this.ParentArea).Equals(this.DisplayArea.Size, Element.Epsilon))) this.SetAreaDirty(); } diff --git a/MLEM.Ui/UiSystem.cs b/MLEM.Ui/UiSystem.cs index 2e80d72..65dbd73 100644 --- a/MLEM.Ui/UiSystem.cs +++ b/MLEM.Ui/UiSystem.cs @@ -352,8 +352,8 @@ namespace MLEM.Ui { var root = new RootElement(name, element, this); this.rootElements.Add(root); root.Element.AndChildren(e => { - e.AddedToUi(this, root); e.SetAreaDirty(); + e.AddedToUi(this, root); }); this.OnRootAdded?.Invoke(root); root.InvokeOnAddedToUi(this); @@ -371,10 +371,8 @@ namespace MLEM.Ui { return; this.rootElements.Remove(root); this.Controls.SelectElement(root, null); - root.Element.AndChildren(e => { - e.RemovedFromUi(); - e.SetAreaDirty(); - }); + // we don't need to set dirty here since re-adding to a ui will cause dirty state anyway + root.Element.AndChildren(e => e.RemovedFromUi()); this.OnRootRemoved?.Invoke(root); root.InvokeOnRemovedFromUi(this); }