forked from nodegui/nodegui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqsize_wrap.cpp
More file actions
86 lines (77 loc) · 3.04 KB
/
qsize_wrap.cpp
File metadata and controls
86 lines (77 loc) · 3.04 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
#include "QtCore/QSize/qsize_wrap.h"
#include "Extras/Utils/nutils.h"
#include "QtCore/QVariant/qvariant_wrap.h"
Napi::FunctionReference QSizeWrap::constructor;
Napi::Object QSizeWrap::init(Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);
char CLASSNAME[] = "QSize";
Napi::Function func = DefineClass(
env, CLASSNAME,
{InstanceMethod("setHeight", &QSizeWrap::setHeight),
InstanceMethod("setWidth", &QSizeWrap::setWidth),
InstanceMethod("height", &QSizeWrap::height),
InstanceMethod("width", &QSizeWrap::width),
StaticMethod("fromQVariant", &StaticQSizeWrapMethods::fromQVariant),
COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE(QSizeWrap)});
constructor = Napi::Persistent(func);
exports.Set(CLASSNAME, func);
return exports;
}
QSizeWrap::QSizeWrap(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<QSizeWrap>(info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
if (info.Length() == 2) {
int width = info[0].As<Napi::Number>().Int32Value();
int height = info[1].As<Napi::Number>().Int32Value();
this->instance = std::make_unique<QSize>(width, height);
} else if (info.Length() == 1) {
this->instance =
std::unique_ptr<QSize>(info[0].As<Napi::External<QSize>>().Data());
} else if (info.Length() == 0) {
this->instance = std::make_unique<QSize>();
} else {
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
}
this->rawData = extrautils::configureComponent(this->getInternalInstance());
}
QSizeWrap::~QSizeWrap() { this->instance.reset(); }
QSize* QSizeWrap::getInternalInstance() { return this->instance.get(); }
Napi::Value QSizeWrap::setHeight(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
int height = info[0].As<Napi::Number>().Int32Value();
this->instance->setHeight(height);
return env.Null();
}
Napi::Value QSizeWrap::setWidth(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
int width = info[0].As<Napi::Number>().Int32Value();
this->instance->setWidth(width);
return env.Null();
}
Napi::Value QSizeWrap::height(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
return Napi::Value::From(env, this->instance->height());
}
Napi::Value QSizeWrap::width(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
return Napi::Value::From(env, this->instance->width());
}
Napi::Value StaticQSizeWrapMethods::fromQVariant(
const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
Napi::Object variantObject = info[0].As<Napi::Object>();
QVariantWrap* variantWrap =
Napi::ObjectWrap<QVariantWrap>::Unwrap(variantObject);
QVariant* variant = variantWrap->getInternalInstance();
QSize size = variant->value<QSize>();
auto instance = QSizeWrap::constructor.New({Napi::External<QSize>::New(
env, new QSize(size.width(), size.height()))});
return instance;
}