forked from nodegui/nodegui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflexlayout_wrap.cpp
More file actions
98 lines (80 loc) · 3.6 KB
/
flexlayout_wrap.cpp
File metadata and controls
98 lines (80 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "core/FlexLayout/flexlayout_wrap.h"
#include "Extras/Utils/nutils.h"
#include "QtWidgets/QWidget/qwidget_wrap.h"
Napi::FunctionReference FlexLayoutWrap::constructor;
Napi::Object FlexLayoutWrap::init(Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);
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),
QLAYOUT_WRAPPED_METHODS_EXPORT_DEFINE(FlexLayoutWrap)});
constructor = Napi::Persistent(func);
exports.Set(CLASSNAME, func);
return exports;
}
FlexLayout* FlexLayoutWrap::getInternalInstance() { return this->instance; }
FlexLayoutWrap::~FlexLayoutWrap() { extrautils::safeDelete(this->instance); }
FlexLayoutWrap::FlexLayoutWrap(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<FlexLayoutWrap>(info) {
Napi::Env env = info.Env();
if (info.Length() == 1) {
Napi::Object parentObject = info[0].As<Napi::Object>();
NodeWidgetWrap* parentWidgetWrap =
Napi::ObjectWrap<NodeWidgetWrap>::Unwrap(parentObject);
this->instance = new FlexLayout(parentWidgetWrap->getInternalInstance());
} else if (info.Length() == 0) {
this->instance = new FlexLayout();
} else {
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
}
this->rawData = extrautils::configureQObject(this->getInternalInstance());
}
Napi::Value FlexLayoutWrap::addWidget(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::Object qwidgetObject = info[0].As<Napi::Object>();
Napi::External<YGNode> childFlexNodeObject =
info[1].As<Napi::External<YGNode>>();
NodeWidgetWrap* widget =
Napi::ObjectWrap<NodeWidgetWrap>::Unwrap(qwidgetObject);
YGNodeRef childNodeRef = childFlexNodeObject.Data();
this->instance->addWidget(widget->getInternalInstance(), childNodeRef);
return env.Null();
}
Napi::Value FlexLayoutWrap::insertChildBefore(const Napi::CallbackInfo& info) {
Napi::Env env = info.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>>();
NodeWidgetWrap* widget =
Napi::ObjectWrap<NodeWidgetWrap>::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::Object qwidgetObject = info[0].As<Napi::Object>();
Napi::External<YGNode> childFlexNodeObject =
info[1].As<Napi::External<YGNode>>();
NodeWidgetWrap* widget =
Napi::ObjectWrap<NodeWidgetWrap>::Unwrap(qwidgetObject);
YGNodeRef childNodeRef = childFlexNodeObject.Data();
this->instance->removeWidget(widget->getInternalInstance(), childNodeRef);
return env.Null();
}
Napi::Value FlexLayoutWrap::setFlexNode(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::External<YGNode> flexNodeObject = info[0].As<Napi::External<YGNode>>();
YGNodeRef flexNodeRef = flexNodeObject.Data();
this->instance->setFlexNode(flexNodeRef);
return env.Null();
}