Adds all button event listeners
This commit is contained in:
parent
120e45f583
commit
d0ef8472a3
15
demo.ts
15
demo.ts
@ -25,12 +25,19 @@ const testGridLayout = () => {
|
||||
label.setStyleSheet("background-color:blue; color:white;");
|
||||
|
||||
const button1 = new QPushButton();
|
||||
button1.setEventListener(QPushButtonEvents.clicked, () => {
|
||||
console.log("THIS IS Yolo from JS");
|
||||
button1.setEventListener(QPushButtonEvents.clicked, (...args) => {
|
||||
console.log("clicked", ...args);
|
||||
});
|
||||
button1.setEventListener(QPushButtonEvents.pressed, () => {
|
||||
console.log("THIS IS PRESED FROM JS");
|
||||
button1.setEventListener(QPushButtonEvents.pressed, (...args) => {
|
||||
console.log("pressed", ...args);
|
||||
});
|
||||
button1.setEventListener(QPushButtonEvents.released, (...args) => {
|
||||
console.log("released", ...args);
|
||||
});
|
||||
button1.setEventListener(QPushButtonEvents.toggled, (...args) => {
|
||||
console.log("toggled", ...args);
|
||||
});
|
||||
|
||||
button1.setText("Yolo");
|
||||
|
||||
const checkbox = new QCheckBox();
|
||||
|
||||
@ -9,7 +9,7 @@ Napi::Object QPushButtonWrap::init(Napi::Env env, Napi::Object exports) {
|
||||
char CLASSNAME[] = "QPushButton";
|
||||
Napi::Function func = DefineClass(env, CLASSNAME, {
|
||||
InstanceMethod("setText", &QPushButtonWrap::setText),
|
||||
InstanceMethod("setNodeEventEmiiter",&QPushButtonWrap::setNodeEventEmiiter),
|
||||
InstanceMethod("setupEventListeners",&QPushButtonWrap::setupEventListeners),
|
||||
QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QPushButtonWrap)
|
||||
});
|
||||
constructor = Napi::Persistent(func);
|
||||
@ -37,7 +37,7 @@ QPushButtonWrap::QPushButtonWrap(const Napi::CallbackInfo& info): Napi::ObjectWr
|
||||
}
|
||||
|
||||
QPushButtonWrap::~QPushButtonWrap() {
|
||||
this->emitterEmit.release();
|
||||
this->nodeEmit.release();
|
||||
delete this->instance;
|
||||
}
|
||||
|
||||
|
||||
@ -8,7 +8,7 @@
|
||||
class QPushButtonWrap : public Napi::ObjectWrap<QPushButtonWrap> {
|
||||
private:
|
||||
NPushButton* instance;
|
||||
std::unique_ptr<ThreadSafeCallback> emitterEmit;
|
||||
std::unique_ptr<ThreadSafeCallback> nodeEmit;
|
||||
public:
|
||||
static Napi::Object init(Napi::Env env, Napi::Object exports);
|
||||
QPushButtonWrap(const Napi::CallbackInfo& info);
|
||||
@ -19,17 +19,38 @@ class QPushButtonWrap : public Napi::ObjectWrap<QPushButtonWrap> {
|
||||
//wrapped methods
|
||||
Napi::Value setText(const Napi::CallbackInfo& info);
|
||||
|
||||
Napi::Value setNodeEventEmiiter(const Napi::CallbackInfo& info) {
|
||||
Napi::Value setupEventListeners(const Napi::CallbackInfo& info) {
|
||||
Napi::Env env = info.Env();
|
||||
this->emitterEmit = std::make_unique<ThreadSafeCallback>(info[0].As<Napi::Function>());
|
||||
this->nodeEmit = std::make_unique<ThreadSafeCallback>(info[0].As<Napi::Function>());
|
||||
|
||||
NPushButton::connect(this->instance, &QPushButton::clicked,
|
||||
[=](bool val) {
|
||||
this->emitterEmit->call([=](Napi::Env env, std::vector<napi_value>& args) {
|
||||
args = { Napi::String::New(env, "clicked"), Napi::Boolean::New(env, val) };
|
||||
this->nodeEmit->call([=](Napi::Env env, std::vector<napi_value>& args) {
|
||||
args = { Napi::String::New(env, "clicked"), Napi::Value::From(env,val) };
|
||||
});
|
||||
}
|
||||
);
|
||||
NPushButton::connect(this->instance, &QPushButton::released,
|
||||
[=]() {
|
||||
this->nodeEmit->call([=](Napi::Env env, std::vector<napi_value>& args) {
|
||||
args = { Napi::String::New(env, "released") };
|
||||
});
|
||||
}
|
||||
);
|
||||
NPushButton::connect(this->instance, &QPushButton::pressed,
|
||||
[=]() {
|
||||
this->nodeEmit->call([=](Napi::Env env, std::vector<napi_value>& args) {
|
||||
args = { Napi::String::New(env, "pressed") };
|
||||
});
|
||||
}
|
||||
);
|
||||
NPushButton::connect(this->instance, &QPushButton::toggled,
|
||||
[=](bool val) {
|
||||
this->nodeEmit->call([=](Napi::Env env, std::vector<napi_value>& args) {
|
||||
args = { Napi::String::New(env, "toggled"), Napi::Value::From(env,val) };
|
||||
});
|
||||
}
|
||||
);
|
||||
return env.Null();
|
||||
}
|
||||
|
||||
|
||||
@ -12,7 +12,7 @@ export enum QPushButtonEvents {
|
||||
type EventCallback = (payload?: any) => void;
|
||||
export class QPushButton extends NodeWidget {
|
||||
native: any;
|
||||
private eventEmiiter: EventEmitter;
|
||||
private emitter: EventEmitter;
|
||||
constructor(parent?: NodeWidget) {
|
||||
super();
|
||||
if (parent) {
|
||||
@ -21,17 +21,14 @@ export class QPushButton extends NodeWidget {
|
||||
} else {
|
||||
this.native = new addon.QPushButton();
|
||||
}
|
||||
this.eventEmiiter = this.initEventEmitter(this.native);
|
||||
}
|
||||
private initEventEmitter(native: any): EventEmitter {
|
||||
const emitter = new EventEmitter();
|
||||
native.setNodeEventEmiiter(emitter.emit.bind(emitter));
|
||||
return emitter;
|
||||
this.emitter = new EventEmitter();
|
||||
this.native.setupEventListeners(this.emitter.emit.bind(this.emitter));
|
||||
}
|
||||
|
||||
setText(text: string) {
|
||||
this.native.setText(text);
|
||||
}
|
||||
setEventListener(eventType: QPushButtonEvents, callback: EventCallback) {
|
||||
this.eventEmiiter.on(eventType, callback);
|
||||
this.emitter.on(eventType, callback);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user