diff --git a/src/cpp/core/FlexLayout/flexlayout.cpp b/src/cpp/core/FlexLayout/flexlayout.cpp index 109eb4edc..232fdbe95 100644 --- a/src/cpp/core/FlexLayout/flexlayout.cpp +++ b/src/cpp/core/FlexLayout/flexlayout.cpp @@ -115,6 +115,27 @@ void FlexLayout::removeWidget(QWidget* childWidget, YGNodeRef childNode) QLayout::removeWidget(childWidget); } +void FlexLayout::insertChildBefore(QWidget* childWidget, YGNodeRef beforeChildNode, YGNodeRef childNode) +{ + if(!this->node){ + spdlog::warn("Flex layout's parent yoga node not set yet. Set it using setFlexNode. childwidget cant be inserted"); + return; + } + uint count = YGNodeGetChildCount(this->node); + uint indexToInsert = 0; + for(uint i=0; inode, i)){ + indexToInsert = i; + break; + } + } + YGNodeInsertChild(this->node, childNode, indexToInsert); + QLayoutItem* layoutItem = new QWidgetItem(childWidget); + NodeContext* childContext = new NodeContext(layoutItem); + YGNodeSetContext(childNode, static_cast(childContext)); + QLayout::addWidget(childWidget); +} + void FlexLayout::setGeometry(const QRect &rect) { if(!this->node){ diff --git a/src/cpp/core/FlexLayout/flexlayout.h b/src/cpp/core/FlexLayout/flexlayout.h index 47bbe2231..ab35ef896 100644 --- a/src/cpp/core/FlexLayout/flexlayout.h +++ b/src/cpp/core/FlexLayout/flexlayout.h @@ -40,6 +40,7 @@ public: QLayoutItem *takeAt(int index) override; int count() const override; void addWidget(QWidget* childWidget, YGNodeRef childNode); + void insertChildBefore(QWidget* childWidget, YGNodeRef beforeChildNode, YGNodeRef childNode); void removeWidget(QWidget* childWidget, YGNodeRef childNode); void setGeometry(const QRect &rect) override; void setFlexNode(YGNodeRef parentNode); diff --git a/src/cpp/core/FlexLayout/flexlayout_wrap.cpp b/src/cpp/core/FlexLayout/flexlayout_wrap.cpp index fe8ee2850..a54decce5 100644 --- a/src/cpp/core/FlexLayout/flexlayout_wrap.cpp +++ b/src/cpp/core/FlexLayout/flexlayout_wrap.cpp @@ -9,6 +9,7 @@ Napi::Object FlexLayoutWrap::init(Napi::Env env, Napi::Object exports) { char CLASSNAME[] = "FlexLayout"; Napi::Function func = DefineClass(env, CLASSNAME, { InstanceMethod("addWidget", &FlexLayoutWrap::addWidget), + InstanceMethod("insertChildBefore", &FlexLayoutWrap::insertChildBefore), InstanceMethod("removeWidget", &FlexLayoutWrap::removeWidget), InstanceMethod("setFlexNode", &FlexLayoutWrap::setFlexNode), }); @@ -57,6 +58,22 @@ Napi::Value FlexLayoutWrap::addWidget(const Napi::CallbackInfo& info) { return env.Null(); } +Napi::Value FlexLayoutWrap::insertChildBefore(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + + Napi::Object qwidgetObject = info[0].As(); + Napi::External beforeChildFlexNodeObject = info[1].As>(); + Napi::External childFlexNodeObject = info[2].As>(); + QWidgetWrap* widget = Napi::ObjectWrap::Unwrap(qwidgetObject); + YGNodeRef childNodeRef = childFlexNodeObject.Data(); + YGNodeRef beforeChildNodeRef = beforeChildFlexNodeObject.Data(); + + this->instance->insertChildBefore(widget->getInternalInstance(), beforeChildNodeRef, childNodeRef); + + return env.Null(); +} + Napi::Value FlexLayoutWrap::removeWidget(const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); Napi::HandleScope scope(env); diff --git a/src/cpp/core/FlexLayout/flexlayout_wrap.h b/src/cpp/core/FlexLayout/flexlayout_wrap.h index 3e24477a5..37e4ab236 100644 --- a/src/cpp/core/FlexLayout/flexlayout_wrap.h +++ b/src/cpp/core/FlexLayout/flexlayout_wrap.h @@ -17,6 +17,7 @@ class FlexLayoutWrap : public Napi::ObjectWrap{ static Napi::FunctionReference constructor; //wrapped methods Napi::Value addWidget(const Napi::CallbackInfo& info); + Napi::Value insertChildBefore(const Napi::CallbackInfo& info); Napi::Value removeWidget(const Napi::CallbackInfo& info); Napi::Value setFlexNode(const Napi::CallbackInfo& info); }; diff --git a/src/lib/core/FlexLayout/index.ts b/src/lib/core/FlexLayout/index.ts index 7d04f164f..67df800c0 100644 --- a/src/lib/core/FlexLayout/index.ts +++ b/src/lib/core/FlexLayout/index.ts @@ -13,6 +13,24 @@ export class FlexLayout extends NodeLayout { this.children.add(childWidget); this.native.addWidget(childWidget.native, childYogaNode); }; + + insertChildBefore = ( + childWidget: NodeWidget, + beforeChildWidget: NodeWidget, + childFlexNode?: FlexNode, + beforeChildFlexNode?: FlexNode + ) => { + const childYogaNode = childFlexNode || childWidget.getFlexNode(); + const beforeChildYogaNode = + beforeChildFlexNode || beforeChildWidget.getFlexNode(); + this.children.add(childWidget); // No orderer required yet, so just inserting at the end. + this.native.insertChildBefore( + childWidget.native, + beforeChildYogaNode, + childYogaNode + ); + }; + removeWidget = (childWidget: NodeWidget, childFlexNode?: FlexNode) => { const childYogaNode = childFlexNode || childWidget.getFlexNode(); this.native.removeWidget(childWidget.native, childYogaNode);