Skip to content

Commit 3fed29d

Browse files
committed
Adds insertbefore method on flex layout
1 parent 687fcc1 commit 3fed29d

File tree

5 files changed

+58
-0
lines changed

5 files changed

+58
-0
lines changed

src/cpp/core/FlexLayout/flexlayout.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,27 @@ void FlexLayout::removeWidget(QWidget* childWidget, YGNodeRef childNode)
115115
QLayout::removeWidget(childWidget);
116116
}
117117

118+
void FlexLayout::insertChildBefore(QWidget* childWidget, YGNodeRef beforeChildNode, YGNodeRef childNode)
119+
{
120+
if(!this->node){
121+
spdlog::warn("Flex layout's parent yoga node not set yet. Set it using setFlexNode. childwidget cant be inserted");
122+
return;
123+
}
124+
uint count = YGNodeGetChildCount(this->node);
125+
uint indexToInsert = 0;
126+
for(uint i=0; i<count; i+=1){
127+
if(beforeChildNode == YGNodeGetChild(this->node, i)){
128+
indexToInsert = i;
129+
break;
130+
}
131+
}
132+
YGNodeInsertChild(this->node, childNode, indexToInsert);
133+
QLayoutItem* layoutItem = new QWidgetItem(childWidget);
134+
NodeContext* childContext = new NodeContext(layoutItem);
135+
YGNodeSetContext(childNode, static_cast<void *>(childContext));
136+
QLayout::addWidget(childWidget);
137+
}
138+
118139
void FlexLayout::setGeometry(const QRect &rect)
119140
{
120141
if(!this->node){

src/cpp/core/FlexLayout/flexlayout.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class FlexLayout: public QLayout
4040
QLayoutItem *takeAt(int index) override;
4141
int count() const override;
4242
void addWidget(QWidget* childWidget, YGNodeRef childNode);
43+
void insertChildBefore(QWidget* childWidget, YGNodeRef beforeChildNode, YGNodeRef childNode);
4344
void removeWidget(QWidget* childWidget, YGNodeRef childNode);
4445
void setGeometry(const QRect &rect) override;
4546
void setFlexNode(YGNodeRef parentNode);

src/cpp/core/FlexLayout/flexlayout_wrap.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Napi::Object FlexLayoutWrap::init(Napi::Env env, Napi::Object exports) {
99
char CLASSNAME[] = "FlexLayout";
1010
Napi::Function func = DefineClass(env, CLASSNAME, {
1111
InstanceMethod("addWidget", &FlexLayoutWrap::addWidget),
12+
InstanceMethod("insertChildBefore", &FlexLayoutWrap::insertChildBefore),
1213
InstanceMethod("removeWidget", &FlexLayoutWrap::removeWidget),
1314
InstanceMethod("setFlexNode", &FlexLayoutWrap::setFlexNode),
1415
});
@@ -57,6 +58,22 @@ Napi::Value FlexLayoutWrap::addWidget(const Napi::CallbackInfo& info) {
5758
return env.Null();
5859
}
5960

61+
Napi::Value FlexLayoutWrap::insertChildBefore(const Napi::CallbackInfo& info) {
62+
Napi::Env env = info.Env();
63+
Napi::HandleScope scope(env);
64+
65+
Napi::Object qwidgetObject = info[0].As<Napi::Object>();
66+
Napi::External<YGNode> beforeChildFlexNodeObject = info[1].As<Napi::External<YGNode>>();
67+
Napi::External<YGNode> childFlexNodeObject = info[2].As<Napi::External<YGNode>>();
68+
QWidgetWrap* widget = Napi::ObjectWrap<QWidgetWrap>::Unwrap(qwidgetObject);
69+
YGNodeRef childNodeRef = childFlexNodeObject.Data();
70+
YGNodeRef beforeChildNodeRef = beforeChildFlexNodeObject.Data();
71+
72+
this->instance->insertChildBefore(widget->getInternalInstance(), beforeChildNodeRef, childNodeRef);
73+
74+
return env.Null();
75+
}
76+
6077
Napi::Value FlexLayoutWrap::removeWidget(const Napi::CallbackInfo& info) {
6178
Napi::Env env = info.Env();
6279
Napi::HandleScope scope(env);

src/cpp/core/FlexLayout/flexlayout_wrap.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class FlexLayoutWrap : public Napi::ObjectWrap<FlexLayoutWrap>{
1717
static Napi::FunctionReference constructor;
1818
//wrapped methods
1919
Napi::Value addWidget(const Napi::CallbackInfo& info);
20+
Napi::Value insertChildBefore(const Napi::CallbackInfo& info);
2021
Napi::Value removeWidget(const Napi::CallbackInfo& info);
2122
Napi::Value setFlexNode(const Napi::CallbackInfo& info);
2223
};

src/lib/core/FlexLayout/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,24 @@ export class FlexLayout extends NodeLayout {
1313
this.children.add(childWidget);
1414
this.native.addWidget(childWidget.native, childYogaNode);
1515
};
16+
17+
insertChildBefore = (
18+
childWidget: NodeWidget,
19+
beforeChildWidget: NodeWidget,
20+
childFlexNode?: FlexNode,
21+
beforeChildFlexNode?: FlexNode
22+
) => {
23+
const childYogaNode = childFlexNode || childWidget.getFlexNode();
24+
const beforeChildYogaNode =
25+
beforeChildFlexNode || beforeChildWidget.getFlexNode();
26+
this.children.add(childWidget); // No orderer required yet, so just inserting at the end.
27+
this.native.insertChildBefore(
28+
childWidget.native,
29+
beforeChildYogaNode,
30+
childYogaNode
31+
);
32+
};
33+
1634
removeWidget = (childWidget: NodeWidget, childFlexNode?: FlexNode) => {
1735
const childYogaNode = childFlexNode || childWidget.getFlexNode();
1836
this.native.removeWidget(childWidget.native, childYogaNode);

0 commit comments

Comments
 (0)