From 552086cc1f5627043cef65e732377121b7920295 Mon Sep 17 00:00:00 2001 From: TimeBather Date: Wed, 13 Nov 2024 22:32:07 +0800 Subject: [PATCH] fix(frontend): support addChildBefore method --- .../client/frontend/dom/nodes/DomNode.java | 7 ++++++ .../client/frontend/gui/nodes/GuiDomNode.java | 24 +++++++++++++++---- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/main/java/kasuga/lib/core/client/frontend/dom/nodes/DomNode.java b/src/main/java/kasuga/lib/core/client/frontend/dom/nodes/DomNode.java index 9f27f269..954987bf 100644 --- a/src/main/java/kasuga/lib/core/client/frontend/dom/nodes/DomNode.java +++ b/src/main/java/kasuga/lib/core/client/frontend/dom/nodes/DomNode.java @@ -28,6 +28,13 @@ public DomNode(T context) { this.domContext = context; } + public boolean addChildBefore(DomNode child, DomNode before){ + int index = children.indexOf(before); + if(index == -1) + return false; + return addChildAt(index,child); + } + public boolean addChildAt(int i, DomNode child){ if(child.parent != null) return false; diff --git a/src/main/java/kasuga/lib/core/client/frontend/gui/nodes/GuiDomNode.java b/src/main/java/kasuga/lib/core/client/frontend/gui/nodes/GuiDomNode.java index c898b764..0aa6f130 100644 --- a/src/main/java/kasuga/lib/core/client/frontend/gui/nodes/GuiDomNode.java +++ b/src/main/java/kasuga/lib/core/client/frontend/gui/nodes/GuiDomNode.java @@ -69,18 +69,34 @@ public LayoutContext getLayoutManager() { protected BackgroundRenderer background = new BackgroundRenderer(); + @HostAccess.Export + @Override + public boolean addChildBefore(DomNode child, DomNode before) { + this.domContext.queueDuringRender(()->{ + int index = children.indexOf(before); + if(index == -1) + return; + addChildInstant(index, child); + }); + return true; + } + @HostAccess.Export @Override public boolean addChildAt(int i, DomNode child) { this.domContext.queueDuringRender(()->{ - if(child instanceof GuiDomNode domNode) - getLayoutManager().addChild(i,domNode.getLayoutManager()); - styles.notifyUpdate(); - super.addChildAt(i,child); + addChildInstant(i, child); }); return true; } + protected void addChildInstant(int i, DomNode child){ + if(child instanceof GuiDomNode domNode) + getLayoutManager().addChild(i,domNode.getLayoutManager()); + styles.notifyUpdate(); + super.addChildAt(i,child); + } + @HostAccess.Export @Override public boolean removeChild(DomNode child) {