From 40e8a0a74352c7ca0b1b81cf9b2057bd55eeef1b Mon Sep 17 00:00:00 2001 From: Atul R Date: Tue, 21 May 2019 20:55:49 +0200 Subject: [PATCH] refactored a bit and changed name --- binding.gyp | 2 +- demo.js => demo.ts | 9 +++++---- package.json | 4 ++-- src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.cpp | 5 +++++ src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.h | 1 + src/lib/QtWidgets/QGridLayout/index.ts | 2 +- src/lib/QtWidgets/QPushButton/index.ts | 7 +++++-- src/lib/core/Component/index.ts | 4 ++-- src/lib/core/EventComponent/index.ts | 7 +++++++ src/lib/core/addon.ts | 2 +- 10 files changed, 30 insertions(+), 13 deletions(-) rename demo.js => demo.ts (75%) create mode 100644 src/lib/core/EventComponent/index.ts diff --git a/binding.gyp b/binding.gyp index 9a1f27563..fad11236e 100644 --- a/binding.gyp +++ b/binding.gyp @@ -1,6 +1,6 @@ { "targets": [{ - "target_name": "node_desktop", + "target_name": "node_gui", "cflags!": ["-fno-exceptions"], "cflags_cc!": ["-fno-exceptions"], "sources": [ diff --git a/demo.js b/demo.ts similarity index 75% rename from demo.js rename to demo.ts index 5eb66002c..0846bf93c 100644 --- a/demo.js +++ b/demo.ts @@ -1,6 +1,4 @@ -const { QtGui, QtWidgets } = require("."); - -// const app = new QtGui.QApplication(); +import { QtGui, QtWidgets } from "./src"; const win = new QtWidgets.QMainWindow(); const view = new QtGui.QWidget(); win.setCentralWidget(view); @@ -16,6 +14,9 @@ label2.setStyleSheet("background-color:blue; color:white;"); const button1 = new QtWidgets.QPushButton(); button1.setText("Yolo"); +// button1.setEventListener("click", () => { +// console.log("button clicked"); +// }); gridLayout.addWidget(label); gridLayout.addWidget(label2); @@ -26,4 +27,4 @@ view.setLayout(gridLayout); win.show(); // app.exec(); -global.win = win; //to keep gc from collecting +(global as any).win = win; //to keep gc from collecting diff --git a/package.json b/package.json index 903d765cd..469a15615 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "node_desktop", + "name": "node-gui", "version": "1.0.0", "description": "A cross platform library to build native desktop apps. Based on Qt5.", "main": "dist/", @@ -15,7 +15,7 @@ "scripts": { "build:addon": "node-gyp -j 8 rebuild", "build:lib": "rm -rf ./dist/ && tsc", - "dev": "qode demo.js" + "dev": "yarn build:lib && qode dist/demo.js" }, "dependencies": { "node-addon-api": "^1.6.3" diff --git a/src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.cpp b/src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.cpp index 30f738ca7..f8fbb3106 100644 --- a/src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.cpp +++ b/src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.cpp @@ -67,3 +67,8 @@ Napi::Value QPushButtonWrap::setText(const Napi::CallbackInfo& info) { return env.Null(); } +Napi::Value QPushButtonWrap::setEventListener(const Napi::CallbackInfo& info){ + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + return env.Null(); +} diff --git a/src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.h b/src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.h index ae0a456e1..30ef79802 100644 --- a/src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.h +++ b/src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.h @@ -16,6 +16,7 @@ class QPushButtonWrap : public Napi::ObjectWrap { //wrapped methods Napi::Value setStyleSheet(const Napi::CallbackInfo& info); Napi::Value setText(const Napi::CallbackInfo& info); + Napi::Value setEventListener(const Napi::CallbackInfo& info); }; #endif diff --git a/src/lib/QtWidgets/QGridLayout/index.ts b/src/lib/QtWidgets/QGridLayout/index.ts index 8f050b7b6..1aed8d846 100644 --- a/src/lib/QtWidgets/QGridLayout/index.ts +++ b/src/lib/QtWidgets/QGridLayout/index.ts @@ -13,7 +13,7 @@ export class QGridLayout extends Component { this.native = new addon.QGridLayout(); } } - addWidget(widget: QWidget) { + addWidget(widget: Component) { this.native.addWidget(widget.native); this.children.add(widget); } diff --git a/src/lib/QtWidgets/QPushButton/index.ts b/src/lib/QtWidgets/QPushButton/index.ts index e2f887492..af88a3a59 100644 --- a/src/lib/QtWidgets/QPushButton/index.ts +++ b/src/lib/QtWidgets/QPushButton/index.ts @@ -1,9 +1,12 @@ import addon from "../../core/addon"; import { QWidget } from "../../QtGui/QWidget"; -import { Component } from "../../core/Component"; +import { EventComponent } from "../../core/EventComponent"; -export class QPushButton extends Component { +export class QPushButton extends EventComponent { native: any; + setEventListener(event: string, callback: () => void): void { + throw new Error("Method not implemented."); + } constructor(parent?: QWidget) { super(); if (parent) { diff --git a/src/lib/core/Component/index.ts b/src/lib/core/Component/index.ts index 15de30fd3..918bc8698 100644 --- a/src/lib/core/Component/index.ts +++ b/src/lib/core/Component/index.ts @@ -1,5 +1,5 @@ export abstract class Component { - children = new Set(); //TODO if react stub these as react will manage the instances from beings gc'ed better. - parent: Component | null = null; + protected children = new Set(); //TODO if react stub these as react will manage the instances from beings gc'ed better. + protected parent: Component | null = null; abstract native: any; } diff --git a/src/lib/core/EventComponent/index.ts b/src/lib/core/EventComponent/index.ts new file mode 100644 index 000000000..f533d1984 --- /dev/null +++ b/src/lib/core/EventComponent/index.ts @@ -0,0 +1,7 @@ +import { Component } from "../Component"; + +type Callback = () => void; + +export abstract class EventComponent extends Component { + abstract setEventListener(event: string, callback: Callback): void; +} diff --git a/src/lib/core/addon.ts b/src/lib/core/addon.ts index ea5beb852..1d7285bab 100644 --- a/src/lib/core/addon.ts +++ b/src/lib/core/addon.ts @@ -1,2 +1,2 @@ -const addon = require("../../../build/Release/node_desktop.node"); +const addon = require("../../../../build/Release/node_gui.node"); export default addon;