From 3bb3ae20c005cde2307d9632b6852156c3f967ff Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Tue, 2 Apr 2024 13:36:24 +0200 Subject: [PATCH] further improved performance of panels when removing a large amount of children --- MLEM.Ui/Elements/Panel.cs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/MLEM.Ui/Elements/Panel.cs b/MLEM.Ui/Elements/Panel.cs index 5c87a01..2f1dc9d 100644 --- a/MLEM.Ui/Elements/Panel.cs +++ b/MLEM.Ui/Elements/Panel.cs @@ -64,6 +64,7 @@ namespace MLEM.Ui.Elements { private float scrollBarChildOffset; private StyleProp scrollBarOffset; private float lastScrollOffset; + private bool childrenDirtyForScroll; /// /// Creates a new panel with the given settings. @@ -157,8 +158,16 @@ namespace MLEM.Ui.Elements { // when removing children, our scroll bar might have to be hidden // if we don't do this before adding children again, they might incorrectly assume that the scroll bar will still be visible and adjust their size accordingly - if (this.System != null) + this.childrenDirtyForScroll = true; + } + + /// + public override T AddChild(T element, int index = -1) { + // if children were recently removed, make sure to update the scroll bar before adding new ones so that they can't incorrectly assume the scroll bar will be visible + if (this.childrenDirtyForScroll && this.System != null) this.ScrollSetup(); + + return base.AddChild(element, index); } /// @@ -277,6 +286,8 @@ namespace MLEM.Ui.Elements { /// Prepares the panel for auto-scrolling, creating the render target and setting up the scroll bar's maximum value. /// protected virtual void ScrollSetup() { + this.childrenDirtyForScroll = false; + if (!this.scrollOverflow || this.IsHidden) return;