Adds custom keyevent handler. And remove dependency on napi-thread-safe-callback

This commit is contained in:
Atul R
2019-06-23 17:34:54 +02:00
parent ba409c8c14
commit e15d6b14ac
16 changed files with 124 additions and 48 deletions
+12 -12
View File
@@ -13,24 +13,24 @@ public:
void connectWidgetSignalsToEventEmitter() {
// Qt Connects: Implement all signal connects here
QObject::connect(this, &QPushButton::clicked, [=](bool checked) {
this->emitOnNode->call([=](Napi::Env env, std::vector<napi_value>& args) {
args = { Napi::String::New(env, "clicked"), Napi::Value::From(env, checked) };
});
Napi::Env env = this->emitOnNode.Env();
Napi::HandleScope scope(env);
this->emitOnNode.Call({ Napi::String::New(env, "clicked"), Napi::Value::From(env, checked) });
});
QObject::connect(this, &QPushButton::released, [=]() {
this->emitOnNode->call([=](Napi::Env env, std::vector<napi_value>& args) {
args = { Napi::String::New(env, "released") };
});
Napi::Env env = this->emitOnNode.Env();
Napi::HandleScope scope(env);
this->emitOnNode.Call({ Napi::String::New(env, "released") });
});
QObject::connect(this, &QPushButton::pressed, [=]() {
this->emitOnNode->call([=](Napi::Env env, std::vector<napi_value>& args) {
args = { Napi::String::New(env, "pressed") };
});
Napi::Env env = this->emitOnNode.Env();
Napi::HandleScope scope(env);
this->emitOnNode.Call({ Napi::String::New(env, "pressed") });
});
QObject::connect(this, &QPushButton::toggled, [=](bool checked) {
this->emitOnNode->call([=](Napi::Env env, std::vector<napi_value>& args) {
args = { Napi::String::New(env, "toggled"), Napi::Value::From(env, checked) };
});
Napi::Env env = this->emitOnNode.Env();
Napi::HandleScope scope(env);
this->emitOnNode.Call({ Napi::String::New(env, "toggled"), Napi::Value::From(env, checked) });
});
}
};
@@ -9,7 +9,6 @@
class QPushButtonWrap : public Napi::ObjectWrap<QPushButtonWrap> {
private:
NPushButton* instance;
// std::unique_ptr<ThreadSafeCallback> emitOnNode;
public:
static Napi::Object init(Napi::Env env, Napi::Object exports);
QPushButtonWrap(const Napi::CallbackInfo& info);
+9 -5
View File
@@ -17,10 +17,14 @@ void EventWidget::event(QEvent* event){
try {
QEvent::Type evtType = event->type();
std::string eventTypeString = subscribedEvents.at(evtType);
this->emitOnNode->call([=](Napi::Env env, std::vector<napi_value>& args) {
Napi::Value nativeEvent = Napi::External<QEvent>::New(env, event);
args = { Napi::String::New(env, eventTypeString), nativeEvent };
});
Napi::Env env = this->emitOnNode.Env();
Napi::HandleScope scope(env);
Napi::Value nativeEvent = Napi::External<QEvent>::New(env, event);
std::vector<napi_value> args = { Napi::String::New(env, eventTypeString), nativeEvent };
this->emitOnNode.Call(args);
} catch (...) {
// Do nothing
}
@@ -34,6 +38,6 @@ void EventWidget::connectWidgetSignalsToEventEmitter(){
EventWidget::~EventWidget(){
if(this->emitOnNode){
this->emitOnNode.release();
this->emitOnNode.Reset();
}
}
+2 -2
View File
@@ -1,12 +1,12 @@
#pragma once
#include <QEvent>
#include <napi-thread-safe-callback.hpp>
#include "src/cpp/core/Events/eventsmap.h"
#include <napi.h>
class EventWidget {
public:
std::unique_ptr<ThreadSafeCallback> emitOnNode = nullptr;
Napi::FunctionReference emitOnNode;
std::unordered_map<QEvent::Type, std::string> subscribedEvents;
void subscribeToQtEvent(std::string evtString);
+1 -1
View File
@@ -14,7 +14,7 @@
\
Napi::Value initNodeEventEmitter(const Napi::CallbackInfo& info) { \
Napi::Env env = info.Env(); \
this->instance->emitOnNode = std::make_unique<ThreadSafeCallback>(info[0].As<Napi::Function>()); \
this->instance->emitOnNode = Napi::Persistent(info[0].As<Napi::Function>()); \
this->instance->connectWidgetSignalsToEventEmitter(); \
return env.Null(); \
} \
@@ -0,0 +1,44 @@
#include "keyevent_wrap.h"
#include "src/cpp/Extras/Utils/nutils.h"
#include <QString>
#include "deps/spdlog/spdlog.h"
Napi::FunctionReference QKeyEventWrap::constructor;
Napi::Object QKeyEventWrap::init(Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);
char CLASSNAME[] = "QKeyEvent";
Napi::Function func = DefineClass(env, CLASSNAME, {
InstanceMethod("text", &QKeyEventWrap::text),
});
constructor = Napi::Persistent(func);
exports.Set(CLASSNAME, func);
return exports;
}
QKeyEvent* QKeyEventWrap::getInternalInstance() {
return this->instance;
}
QKeyEventWrap::QKeyEventWrap(const Napi::CallbackInfo& info): Napi::ObjectWrap<QKeyEventWrap>(info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
if(info.Length() == 1) {
Napi::External<QKeyEvent> eventObject = info[0].As<Napi::External<QKeyEvent>>();
this->instance = eventObject.Data();
} else {
extrautils::throwTypeError(env, "Wrong number of arguments");
}
}
QKeyEventWrap::~QKeyEventWrap() {
// Do not destroy instance here. It will be done by Qt Event loop.
}
Napi::Value QKeyEventWrap::text(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
int keyText = this->instance->key();
Napi::String keyValue = Napi::String::New(env, std::to_string(keyText));
return keyValue;
}
@@ -0,0 +1,20 @@
#pragma once
#include <napi.h>
#include <QKeyEvent>
class QKeyEventWrap : public Napi::ObjectWrap<QKeyEventWrap>{
private:
QKeyEvent* instance;
public:
static Napi::Object init(Napi::Env env, Napi::Object exports);
QKeyEventWrap(const Napi::CallbackInfo& info);
~QKeyEventWrap();
QKeyEvent* getInternalInstance();
//class constructor
static Napi::FunctionReference constructor;
//wrapped methods
Napi::Value text(const Napi::CallbackInfo& info);
// Napi::Value setFlexNode(const Napi::CallbackInfo& info);
};
+2
View File
@@ -10,6 +10,7 @@
#include "src/cpp/QtWidgets/QRadioButton/qradiobutton_wrap.h"
#include "src/cpp/QtWidgets/QLineEdit/qlineedit_wrap.h"
#include "src/cpp/core/FlexLayout/flexlayout_wrap.h"
#include "src/cpp/core/Events/types/KeyEvent/keyevent_wrap.h"
#include <napi.h>
// These cant be instantiated in JS Side
@@ -29,6 +30,7 @@ Napi::Object Main(Napi::Env env, Napi::Object exports) {
QProgressBarWrap::init(env, exports);
QRadioButtonWrap::init(env, exports);
QLineEditWrap::init(env, exports);
QKeyEventWrap::init(env, exports);
return QLabelWrap::init(env, exports);
}
+13
View File
@@ -0,0 +1,13 @@
import addon from "../../core/addon";
import { NativeElement } from "../../core/Component";
import { NativeEvent } from "../../core/EventWidget";
export class KeyEvent {
native: NativeElement;
constructor(event: NativeEvent) {
this.native = new addon.QKeyEvent(event);
}
text = (): string => {
return this.native.text();
};
}
+1 -1
View File
@@ -2,7 +2,7 @@ import { EventEmitter } from "events";
import { YogaWidget } from "../YogaWidget";
import { NativeElement } from "../Component";
type NativeEvent = {};
export type NativeEvent = {};
export abstract class EventWidget extends YogaWidget {
private emitter: EventEmitter;
constructor(native: NativeElement) {