-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJSEUIBase.cpp
More file actions
179 lines (149 loc) · 5.02 KB
/
JSEUIBase.cpp
File metadata and controls
179 lines (149 loc) · 5.02 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include "JSEUIBase.h"
using namespace v8;
namespace JSE { namespace UI {
JSEUIBase::JSEUIBase(void)
{
static class MagicNumberHelper
{
public:
MagicNumberHelper(void *&MagicNumber)
{
MagicNumber = this;
}
} magicNumberHelper(MagicNumber);
}
void JSEUIBase::WeakCallback(const WeakCallbackData<Object, JSEUIBase> &data)
{
Isolate *isolate = data.GetIsolate();
HandleScope scope(isolate);
JSEUIBase *widget = data.GetParameter();
widget->objThis.ClearWeak();
widget->objThis.Reset();
if (widget->pImpl != nullptr)
{
// Detach all children
const QObjectList &children = widget->pImpl->children();
for (auto iter = children.cbegin(); iter != children.cend(); iter++)
{
(*iter)->setParent(nullptr);
}
// Delete itself
widget->pImpl->deleteLater();
}
for (auto iter = widget->EventListeners.cbegin(); iter != widget->EventListeners.cend(); iter++)
{
for (auto viter = iter->second.cbegin(); viter != iter->second.cend(); viter++)
{
(**viter).Reset();
delete (*viter);
}
}
delete widget;
}
JSEUIBase *JSEUIBase::GetImpl(void)
{
return static_cast<JSEUIBase *>(pImpl);
}
void JSEUIBase::Initialize(Isolate *&isolate, Handle<FunctionTemplate> &tpl)
{
JSE_SET_PROTOTYPE_METHOD(tpl, "appendTo", Proto_appendTo);
JSE_SET_PROTOTYPE_METHOD(tpl, "addEventListener", Proto_addEventListener);
}
void JSEUIBase::Invoke(JSEUIBase *self, std::string evt, int argc, Handle<Value> *argv)
{
if (self->EventListeners.count(evt) == 0)
{
return;
}
for (auto iter = self->EventListeners[evt].cbegin(); iter != self->EventListeners[evt].cend(); iter++)
{
Local<Function>::New(Isolate::GetCurrent(), **iter)->Call(Local<Object>::New(Isolate::GetCurrent(), self->objThis), argc, argv);
}
}
JSEUIBase *JSEUIBase::GetInternal(Local<Object> &obj)
{
if (obj->InternalFieldCount() != 2)
{
return nullptr;
}
if (obj->GetAlignedPointerFromInternalField(1) != MagicNumber)
{
return nullptr;
}
return static_cast<JSEUIBase *>(obj->GetAlignedPointerFromInternalField(0));
}
void *JSEUIBase::MagicNumber = nullptr;
void JSEUIBase::Proto_appendTo(const FunctionCallbackInfo<Value> &args)
{
Isolate *isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
JSEUIBase *widget = static_cast<JSEUIBase *>(args.This()->GetAlignedPointerFromInternalField(0));
if (args.Length() == 0)
{
widget->pImpl->setParent(nullptr);
args.GetReturnValue().Set(Boolean::New(isolate, true));
return;
}
if (args.Length() > 1)
{
args.GetReturnValue().Set(Exception::Error(String::NewFromUtf8(isolate, "There is one optional parameter.")));
return;
}
Local<Object> objParent = args[0]->ToObject();
JSEUIBase *parent = GetInternal(objParent);
if (parent == nullptr)
{
args.GetReturnValue().Set(Exception::Error(String::NewFromUtf8(isolate, "Invalid parent object.")));
return;
}
widget->objParent.Reset(isolate, objParent);
// Special case: JSEUIWindow, need to append to centralWidget()
JSEUIWindowImpl *parentWindow = dynamic_cast<JSEUIWindowImpl *>(parent->pImpl);
if (parentWindow != nullptr)
{
widget->pImpl->setParent(parentWindow->centralWidget());
}
else
{
widget->pImpl->setParent(parent->pImpl);
}
widget->pImpl->setVisible(true);
args.GetReturnValue().Set(Boolean::New(isolate, true));
}
void JSEUIBase::Proto_addEventListener(const FunctionCallbackInfo<Value> &args)
{
Isolate *isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
JSEUIBase *widget = static_cast<JSEUIBase *>(args.This()->GetAlignedPointerFromInternalField(0));
if (args.Length() != 2)
{
args.GetReturnValue().Set(Exception::Error(String::NewFromUtf8(isolate, "There must be exact two parameters.")));
return;
}
if (args[0]->IsString() == false)
{
args.GetReturnValue().Set(Exception::TypeError(String::NewFromUtf8(isolate, "Argument 1 must be a string.")));
return;
}
if (args[1]->IsFunction() == false)
{
args.GetReturnValue().Set(Exception::TypeError(String::NewFromUtf8(isolate, "Argument 2 must be a function.")));
return;
}
Persistent<Function> *callback = new Persistent<Function>;
callback->Reset(isolate, Local<Function>::Cast(args[1]));
if (widget->EventListeners.count(*String::Utf8Value(args[0]->ToString())) == 0)
{
// Create new listener vector
std::vector<Persistent<Function> *> listeners;
listeners.push_back(callback);
widget->EventListeners[*String::Utf8Value(args[0]->ToString())] = listeners;
}
else
{
// Push to existing listener vector
widget->EventListeners[*String::Utf8Value(args[0]->ToString())].push_back(callback);
}
args.GetReturnValue().Set(Boolean::New(isolate, true));
}
}}