forked from nodegui/nodegui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqmenu_wrap.cpp
More file actions
125 lines (101 loc) · 3.99 KB
/
qmenu_wrap.cpp
File metadata and controls
125 lines (101 loc) · 3.99 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
#include "QtWidgets/QMenu/qmenu_wrap.h"
#include <nodegui/Extras/Utils/nutils.h>
#include <nodegui/QtWidgets/QWidget/qwidget_wrap.h>
#include <QWidget>
#include "QtCore/QPoint/qpoint_wrap.h"
#include "QtWidgets/QAction/qaction_wrap.h"
Napi::FunctionReference QMenuWrap::constructor;
Napi::Object QMenuWrap::init(Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);
char CLASSNAME[] = "QMenu";
Napi::Function func =
DefineClass(env, CLASSNAME,
{InstanceMethod("setTitle", &QMenuWrap::setTitle),
InstanceMethod("clear", &QMenuWrap::clear),
InstanceMethod("addSeparator", &QMenuWrap::addSeparator),
InstanceMethod("exec", &QMenuWrap::exec),
InstanceMethod("popup", &QMenuWrap::popup),
QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QMenuWrap)});
constructor = Napi::Persistent(func);
exports.Set(CLASSNAME, func);
return exports;
}
NMenu* QMenuWrap::getInternalInstance() { return this->instance; }
QMenuWrap::QMenuWrap(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<QMenuWrap>(info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
if (info.Length() == 1) {
Napi::Object parentObject = info[0].As<Napi::Object>();
NodeWidgetWrap* parentWidgetWrap =
Napi::ObjectWrap<NodeWidgetWrap>::Unwrap(parentObject);
this->instance = new NMenu(parentWidgetWrap->getInternalInstance());
} else if (info.Length() == 0) {
this->instance = new NMenu();
} else {
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
}
this->rawData = extrautils::configureQWidget(
this->getInternalInstance(), this->getInternalInstance()->getFlexNode(),
true);
}
QMenuWrap::~QMenuWrap() { extrautils::safeDelete(this->instance); }
Napi::Value QMenuWrap::clear(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
this->instance->clear();
return env.Null();
}
Napi::Value QMenuWrap::setTitle(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
Napi::String message = info[0].As<Napi::String>();
this->instance->setTitle(QString::fromStdString(message.Utf8Value()));
return env.Null();
}
Napi::Value QMenuWrap::addSeparator(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
auto value =
Napi::External<QAction>::New(env, this->instance->addSeparator());
return Napi::Value::From(env, value);
}
Napi::Value QMenuWrap::exec(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
if (info.Length() > 0) {
Napi::Object pointObject = info[0].As<Napi::Object>();
QPointWrap* pointWrap = Napi::ObjectWrap<QPointWrap>::Unwrap(pointObject);
QAction* action = nullptr;
if (info.Length() == 2) {
Napi::Object actionObject = info[1].As<Napi::Object>();
QActionWrap* actionWrap =
Napi::ObjectWrap<QActionWrap>::Unwrap(actionObject);
action = actionWrap->getInternalInstance();
}
this->instance->exec(*pointWrap->getInternalInstance(), action);
} else if (info.Length() == 0) {
this->instance->exec();
} else {
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
}
return env.Null();
}
Napi::Value QMenuWrap::popup(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
Napi::Object pointObject = info[0].As<Napi::Object>();
QPointWrap* pointWrap = Napi::ObjectWrap<QPointWrap>::Unwrap(pointObject);
QPoint* qpoint = pointWrap->getInternalInstance();
Napi::Object actionObject = info[1].As<Napi::Object>();
QAction* action = nullptr;
if (!actionObject.IsUndefined() && !actionObject.IsNull()) {
QActionWrap* actionWrap =
Napi::ObjectWrap<QActionWrap>::Unwrap(actionObject);
action = actionWrap->getInternalInstance();
}
this->instance->popup(*qpoint, action);
return env.Null();
}