forked from nodegui/nodegui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnutils.cpp
More file actions
140 lines (116 loc) · 3.98 KB
/
nutils.cpp
File metadata and controls
140 lines (116 loc) · 3.98 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "Extras/Utils/nutils.h"
#include <QApplication>
#include <QMetaType>
#include <QWidget>
#include <string>
#include "core/Component/component_wrap.h"
#include "core/FlexLayout/flexutils.h"
bool extrautils::isNapiValueInt(Napi::Env& env, Napi::Value& num) {
return env.Global()
.Get("Number")
.ToObject()
.Get("isInteger")
.As<Napi::Function>()
.Call({num})
.ToBoolean()
.Value();
}
std::string extrautils::getNapiObjectClassName(Napi::Object& object) {
return object.Get("constructor")
.As<Napi::Object>()
.Get("name")
.As<Napi::String>()
.Utf8Value();
}
QVariant* extrautils::convertToQVariant(Napi::Env& env, Napi::Value& value) {
// Warning: Make sure you delete the QVariant fron this function upon use.
if (value.IsBoolean()) {
return new QVariant(value.As<Napi::Boolean>().Value());
} else if (value.IsNumber()) {
if (isNapiValueInt(env, value)) {
return new QVariant(value.As<Napi::Number>().Int32Value());
} else {
return new QVariant(value.As<Napi::Number>().DoubleValue());
}
} else if (value.IsString()) {
std::string stringValue = value.As<Napi::String>().Utf8Value();
return new QVariant(stringValue.c_str());
} else if (value.IsSymbol()) {
return new QVariant();
} else if (value.IsArray()) {
// Note: This assumes an array of strings.
Napi::Array array = value.As<Napi::Array>();
QStringList value;
uint32_t len = array.Length();
for (uint32_t i = 0; i < len; i++) {
if (array.Get(i).IsString()) {
std::string stringValue = array.Get(i).As<Napi::String>().Utf8Value();
value.append(QString::fromUtf8(stringValue.c_str()));
}
}
return new QVariant(value);
} else if (value.IsArrayBuffer()) {
// TODO: fix this
return new QVariant();
} else if (value.IsTypedArray()) {
// TODO: fix this
return new QVariant();
} else if (value.IsObject()) {
Napi::Object object = value.As<Napi::Object>();
std::string className = getNapiObjectClassName(object);
int typeId = QMetaType::type(className.c_str());
ComponentWrap* componentWrap =
Napi::ObjectWrap<ComponentWrap>::Unwrap(object);
return new QVariant(typeId, componentWrap->rawData);
} else if (value.IsFunction()) {
return new QVariant();
} else if (value.IsPromise()) {
return new QVariant();
} else if (value.IsUndefined()) {
return new QVariant();
} else if (value.IsNull()) {
return new QVariant();
} else if (value.IsBuffer()) {
// TODO: fix this
return new QVariant();
} else if (value.IsExternal()) {
QVariant* variant = value.As<Napi::External<QVariant>>().Data();
return variant;
} else {
return new QVariant();
}
}
void* extrautils::configureComponent(void* component) { return component; }
void* extrautils::configureQObject(QObject* object) {
return configureComponent(object);
}
void* extrautils::configureQWidget(QWidget* widget, YGNodeRef node,
bool isLeafNode) {
flexutils::configureFlexNode(widget, node, isLeafNode);
return configureQObject(widget);
}
Napi::FunctionReference NUtilsWrap::constructor;
Napi::Object NUtilsWrap::init(Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);
char CLASSNAME[] = "NUtils";
Napi::Function func =
DefineClass(env, CLASSNAME,
{
StaticMethod("isNapiExternal",
&StaticNUtilsWrapMethods::isNapiExternal),
});
constructor = Napi::Persistent(func);
exports.Set(CLASSNAME, func);
return exports;
}
NUtilsWrap::NUtilsWrap(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<NUtilsWrap>(info) {}
Napi::Value StaticNUtilsWrapMethods::isNapiExternal(
const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
if (info.Length() > 0 && info[0].IsExternal()) {
return Napi::Boolean::New(env, true);
}
return Napi::Boolean::New(env, false);
}