Adds insertbefore method on flex layout

This commit is contained in:
Atul R 2019-07-10 00:08:39 +02:00
parent 687fcc1052
commit 3fed29d6fe
5 changed files with 58 additions and 0 deletions

View File

@ -115,6 +115,27 @@ void FlexLayout::removeWidget(QWidget* childWidget, YGNodeRef childNode)
QLayout::removeWidget(childWidget); 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; i<count; i+=1){
if(beforeChildNode == YGNodeGetChild(this->node, i)){
indexToInsert = i;
break;
}
}
YGNodeInsertChild(this->node, childNode, indexToInsert);
QLayoutItem* layoutItem = new QWidgetItem(childWidget);
NodeContext* childContext = new NodeContext(layoutItem);
YGNodeSetContext(childNode, static_cast<void *>(childContext));
QLayout::addWidget(childWidget);
}
void FlexLayout::setGeometry(const QRect &rect) void FlexLayout::setGeometry(const QRect &rect)
{ {
if(!this->node){ if(!this->node){

View File

@ -40,6 +40,7 @@ public:
QLayoutItem *takeAt(int index) override; QLayoutItem *takeAt(int index) override;
int count() const override; int count() const override;
void addWidget(QWidget* childWidget, YGNodeRef childNode); void addWidget(QWidget* childWidget, YGNodeRef childNode);
void insertChildBefore(QWidget* childWidget, YGNodeRef beforeChildNode, YGNodeRef childNode);
void removeWidget(QWidget* childWidget, YGNodeRef childNode); void removeWidget(QWidget* childWidget, YGNodeRef childNode);
void setGeometry(const QRect &rect) override; void setGeometry(const QRect &rect) override;
void setFlexNode(YGNodeRef parentNode); void setFlexNode(YGNodeRef parentNode);

View File

@ -9,6 +9,7 @@ Napi::Object FlexLayoutWrap::init(Napi::Env env, Napi::Object exports) {
char CLASSNAME[] = "FlexLayout"; char CLASSNAME[] = "FlexLayout";
Napi::Function func = DefineClass(env, CLASSNAME, { Napi::Function func = DefineClass(env, CLASSNAME, {
InstanceMethod("addWidget", &FlexLayoutWrap::addWidget), InstanceMethod("addWidget", &FlexLayoutWrap::addWidget),
InstanceMethod("insertChildBefore", &FlexLayoutWrap::insertChildBefore),
InstanceMethod("removeWidget", &FlexLayoutWrap::removeWidget), InstanceMethod("removeWidget", &FlexLayoutWrap::removeWidget),
InstanceMethod("setFlexNode", &FlexLayoutWrap::setFlexNode), InstanceMethod("setFlexNode", &FlexLayoutWrap::setFlexNode),
}); });
@ -57,6 +58,22 @@ Napi::Value FlexLayoutWrap::addWidget(const Napi::CallbackInfo& info) {
return env.Null(); 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::Object>();
Napi::External<YGNode> beforeChildFlexNodeObject = info[1].As<Napi::External<YGNode>>();
Napi::External<YGNode> childFlexNodeObject = info[2].As<Napi::External<YGNode>>();
QWidgetWrap* widget = Napi::ObjectWrap<QWidgetWrap>::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::Value FlexLayoutWrap::removeWidget(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env(); Napi::Env env = info.Env();
Napi::HandleScope scope(env); Napi::HandleScope scope(env);

View File

@ -17,6 +17,7 @@ class FlexLayoutWrap : public Napi::ObjectWrap<FlexLayoutWrap>{
static Napi::FunctionReference constructor; static Napi::FunctionReference constructor;
//wrapped methods //wrapped methods
Napi::Value addWidget(const Napi::CallbackInfo& info); Napi::Value addWidget(const Napi::CallbackInfo& info);
Napi::Value insertChildBefore(const Napi::CallbackInfo& info);
Napi::Value removeWidget(const Napi::CallbackInfo& info); Napi::Value removeWidget(const Napi::CallbackInfo& info);
Napi::Value setFlexNode(const Napi::CallbackInfo& info); Napi::Value setFlexNode(const Napi::CallbackInfo& info);
}; };

View File

@ -13,6 +13,24 @@ export class FlexLayout extends NodeLayout {
this.children.add(childWidget); this.children.add(childWidget);
this.native.addWidget(childWidget.native, childYogaNode); 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) => { removeWidget = (childWidget: NodeWidget, childFlexNode?: FlexNode) => {
const childYogaNode = childFlexNode || childWidget.getFlexNode(); const childYogaNode = childFlexNode || childWidget.getFlexNode();
this.native.removeWidget(childWidget.native, childYogaNode); this.native.removeWidget(childWidget.native, childYogaNode);