diff --git a/demo.js b/demo.js index 536900487..f47331b2d 100644 --- a/demo.js +++ b/demo.js @@ -1,19 +1,18 @@ -const nodeDesktop = require("./index"); -const {QWidget,QGridLayout,QLabel,QMainWindow} = nodeDesktop; +const { QtGui, QtWidgets } = require("."); -const win = new QMainWindow(); - -const view = new QWidget(); +// const app = new QtGui.QApplication(); +const win = new QtWidgets.QMainWindow(); +const view = new QtGui.QWidget(); win.setCentralWidget(view); -const gridLayout = new QGridLayout(); +const gridLayout = new QtWidgets.QGridLayout(); -const label = new QLabel(); -label.setText('Testing1234'); +const label = new QtWidgets.QLabel(); +label.setText("Testing1234"); -const label2 = new QLabel(); -label2.setText('Hello12321'); -label2.setStyleSheet('background-color:blue; color:white;'); +const label2 = new QtWidgets.QLabel(); +label2.setText("Hello12321"); +label2.setStyleSheet("background-color:blue; color:white;"); gridLayout.addWidget(label); gridLayout.addWidget(label2); @@ -21,6 +20,6 @@ gridLayout.addWidget(label2); view.setLayout(gridLayout); win.show(); +// app.exec(); -console.log("THIS RAN"); -setInterval(()=>console.log("SHOW OFF"), 1000); \ No newline at end of file +global.win = win; //to keep gc from collecting diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index b3caa7b45..000000000 --- a/index.d.ts +++ /dev/null @@ -1,5 +0,0 @@ -interface NodeDesktop { - myMethod1: (arg1: number) => null; -} - -export default NodeDesktop; diff --git a/index.js b/index.js deleted file mode 100644 index 3316d85e7..000000000 --- a/index.js +++ /dev/null @@ -1,3 +0,0 @@ -const addon = require("./build/Release/node_desktop.node"); - -module.exports = addon; diff --git a/package.json b/package.json index edf345677..903d765cd 100644 --- a/package.json +++ b/package.json @@ -2,16 +2,19 @@ "name": "node_desktop", "version": "1.0.0", "description": "A cross platform library to build native desktop apps. Based on Qt5.", - "main": "src/index.js", + "main": "dist/", "author": "Atul R ", "license": "MIT", "private": false, "devDependencies": { - "node-gyp": "^4.0.0" + "@types/lodash": "^4.14.130", + "@types/node": "^12.0.2", + "node-gyp": "^4.0.0", + "typescript": "^3.4.5" }, - "types": "index.d.ts", "scripts": { - "build": "node-gyp -j 8 rebuild", + "build:addon": "node-gyp -j 8 rebuild", + "build:lib": "rm -rf ./dist/ && tsc", "dev": "qode demo.js" }, "dependencies": { diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 000000000..b2e844373 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,18 @@ +import { QApplication } from "./lib/QtGui/QApplication"; +import { QWidget } from "./lib/QtGui/QWidget"; +import { QGridLayout } from "./lib/QtWidgets/QGridLayout"; +import { QLabel } from "./lib/QtWidgets/QLabel"; +import { QLayout } from "./lib/QtWidgets/QLayout"; +import { QMainWindow } from "./lib/QtWidgets/QMainWindow"; + +export const QtGui = { + QApplication, + QWidget +}; + +export const QtWidgets = { + QGridLayout, + QLabel, + QMainWindow, + QLayout +}; diff --git a/src/lib/QtGui/QApplication/index.ts b/src/lib/QtGui/QApplication/index.ts new file mode 100644 index 000000000..5a4adb019 --- /dev/null +++ b/src/lib/QtGui/QApplication/index.ts @@ -0,0 +1,12 @@ +import addon from "../../core/addon"; +import { Component } from "../../core/Component"; + +export class QApplication extends Component { + native = new addon.QApplication(); + processEvents() { + this.native.processEvents(); + } + exec() { + this.native.exec(); + } +} diff --git a/src/lib/QtGui/QWidget/index.ts b/src/lib/QtGui/QWidget/index.ts new file mode 100644 index 000000000..2178d9815 --- /dev/null +++ b/src/lib/QtGui/QWidget/index.ts @@ -0,0 +1,32 @@ +import addon from "../../core/addon"; +import { QLayout } from "../../QtWidgets/QLayout"; +import { Component } from "../../core/Component"; + +export class QWidget extends Component { + native: any; + constructor(parent?: QWidget) { + super(); + if (parent) { + this.native = new addon.QWidget(parent.native); + this.parent = parent; + } else { + this.native = new addon.QWidget(); + } + } + show() { + this.native.show(); + } + resize(width: number, height: number) { + this.native.resize(width, height); + } + close() { + this.native.close(); + } + setLayout(parentLayout: QLayout) { + this.native.setLayout(parentLayout.native); + this.parent = parentLayout; + } + setStyleSheet(style: string) { + this.native.setStyleSheet(style); + } +} diff --git a/src/lib/QtWidgets/QGridLayout/index.ts b/src/lib/QtWidgets/QGridLayout/index.ts new file mode 100644 index 000000000..8f050b7b6 --- /dev/null +++ b/src/lib/QtWidgets/QGridLayout/index.ts @@ -0,0 +1,20 @@ +import addon from "../../core/addon"; +import { QWidget } from "../../QtGui/QWidget"; +import { Component } from "../../core/Component"; + +export class QGridLayout extends Component { + native: any; + constructor(parent?: QWidget) { + super(); + if (parent) { + this.native = new addon.QGridLayout(parent.native); + this.parent = parent; + } else { + this.native = new addon.QGridLayout(); + } + } + addWidget(widget: QWidget) { + this.native.addWidget(widget.native); + this.children.add(widget); + } +} diff --git a/src/lib/QtWidgets/QLabel/index.ts b/src/lib/QtWidgets/QLabel/index.ts new file mode 100644 index 000000000..eb4277fbf --- /dev/null +++ b/src/lib/QtWidgets/QLabel/index.ts @@ -0,0 +1,25 @@ +import addon from "../../core/addon"; +import { QWidget } from "../../QtGui/QWidget"; +import { Component } from "../../core/Component"; + +export class QLabel extends Component { + native: any; + constructor(parent?: QWidget) { + super(); + if (parent) { + this.native = new addon.QLabel(parent.native); + this.parent = parent; + } else { + this.native = new addon.QLabel(); + } + } + setWordWrap(on: boolean) { + this.native.setWordWrap(on); + } + setText(text: string) { + this.native.setText(text); + } + setStyleSheet(style: string) { + this.native.setStyleSheet(style); + } +} diff --git a/src/lib/QtWidgets/QLayout/index.ts b/src/lib/QtWidgets/QLayout/index.ts new file mode 100644 index 000000000..9fe4304c1 --- /dev/null +++ b/src/lib/QtWidgets/QLayout/index.ts @@ -0,0 +1,6 @@ +import addon from "../../core/addon"; +import { Component } from "../../core/Component"; + +export class QLayout extends Component { + native = new addon.QLayout(); +} diff --git a/src/lib/QtWidgets/QMainWindow/index.ts b/src/lib/QtWidgets/QMainWindow/index.ts new file mode 100644 index 000000000..14fa929fe --- /dev/null +++ b/src/lib/QtWidgets/QMainWindow/index.ts @@ -0,0 +1,32 @@ +import addon from "../../core/addon"; +import { QWidget } from "../../QtGui/QWidget"; +import { Component } from "../../core/Component"; + +export class QMainWindow extends Component { + native: any; + constructor(parent?: QWidget) { + super(); + if (parent) { + this.native = new addon.QMainWindow(parent.native); + this.parent = parent; + } else { + this.native = new addon.QMainWindow(); + } + } + setStyleSheet(style: string) { + this.native.setStyleSheet(style); + } + show() { + this.native.show(); + } + resize(width: number, height: number) { + this.native.resize(width, height); + } + close() { + this.native.close(); + } + setCentralWidget(widget: QWidget) { + this.native.setCentralWidget(widget.native); + this.children.add(widget); + } +} diff --git a/src/lib/core/Component/index.ts b/src/lib/core/Component/index.ts new file mode 100644 index 000000000..3c2ed89f7 --- /dev/null +++ b/src/lib/core/Component/index.ts @@ -0,0 +1,5 @@ +export abstract class Component { + children = new Set(); + parent: Component | null = null; + abstract native: any; +} diff --git a/src/lib/core/addon.ts b/src/lib/core/addon.ts new file mode 100644 index 000000000..ea5beb852 --- /dev/null +++ b/src/lib/core/addon.ts @@ -0,0 +1,2 @@ +const addon = require("../../../build/Release/node_desktop.node"); +export default addon; diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 000000000..ded6f1b57 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,64 @@ +{ + "compilerOptions": { + /* Basic Options */ + "target": "ES2015" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */, + "module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */, + // "lib": [], /* Specify library files to be included in the compilation. */ + // "allowJs": true /* Allow javascript files to be compiled. */, + // "checkJs": true, /* Report errors in .js files. */ + // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */ + "declaration": true /* Generates corresponding '.d.ts' file. */, + // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ + "sourceMap": true /* Generates corresponding '.map' file. */, + // "outFile": "./", /* Concatenate and emit output to single file. */ + "outDir": "./dist" /* Redirect output structure to the directory. */, + // "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ + // "composite": true, /* Enable project compilation */ + // "incremental": true, /* Enable incremental compilation */ + // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */ + // "removeComments": true, /* Do not emit comments to output. */ + // "noEmit": true, /* Do not emit outputs. */ + // "importHelpers": true, /* Import emit helpers from 'tslib'. */ + // "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */ + // "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */ + + /* Strict Type-Checking Options */ + "strict": true /* Enable all strict type-checking options. */, + // "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */ + // "strictNullChecks": true, /* Enable strict null checks. */ + // "strictFunctionTypes": true, /* Enable strict checking of function types. */ + // "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */ + // "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */ + // "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */ + // "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */ + + /* Additional Checks */ + // "noUnusedLocals": true, /* Report errors on unused locals. */ + // "noUnusedParameters": true, /* Report errors on unused parameters. */ + // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */ + // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */ + + /* Module Resolution Options */ + "moduleResolution": "node" /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */, + // "baseUrl": "./" /* Base directory to resolve non-absolute module names. */, + // "paths": {} /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */, + // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */ + // "typeRoots": [], /* List of folders to include type definitions from. */ + // "types": [] /* Type declaration files to be included in compilation. */, + // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */ + "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */, + // "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */ + + /* Source Map Options */ + // "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */ + // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ + // "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */ + // "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */ + + /* Experimental Options */ + // "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */ + // "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */ + "resolveJsonModule": true + }, + "include": ["."] +} diff --git a/yarn.lock b/yarn.lock index 00f70136b..25ead58c6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2,6 +2,16 @@ # yarn lockfile v1 +"@types/lodash@^4.14.130": + version "4.14.130" + resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.130.tgz#04b3a690d5f4fc34579963c99adae067b8c8eb5a" + integrity sha512-H++wk0tbneBsRVfLkgAAd0IIpmpVr2Bj4T0HncoOsQf3/xrJexRYQK2Tqo0Ej3pFslM8GkMgdis9bu6xIb1ycw== + +"@types/node@^12.0.2": + version "12.0.2" + resolved "https://registry.yarnpkg.com/@types/node/-/node-12.0.2.tgz#3452a24edf9fea138b48fad4a0a028a683da1e40" + integrity sha512-5tabW/i+9mhrfEOUcLDu2xBPsHJ+X5Orqy9FKpale3SjDA17j5AEpYq5vfy3oAeAHGcvANRCO3NV3d2D6q3NiA== + abbrev@1: version "1.1.1" resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.1.1.tgz#f8f2c887ad10bf67f634f005b6987fed3179aac8" @@ -657,6 +667,11 @@ tweetnacl@^0.14.3, tweetnacl@~0.14.0: resolved "https://registry.yarnpkg.com/tweetnacl/-/tweetnacl-0.14.5.tgz#5ae68177f192d4456269d108afa93ff8743f4f64" integrity sha1-WuaBd/GS1EViadEIr6k/+HQ/T2Q= +typescript@^3.4.5: + version "3.4.5" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.4.5.tgz#2d2618d10bb566572b8d7aad5180d84257d70a99" + integrity sha512-YycBxUb49UUhdNMU5aJ7z5Ej2XGmaIBL0x34vZ82fn3hGvD+bgrMrVDpatgz2f7YxUMJxMkbWxJZeAvDxVe7Vw== + uri-js@^4.2.2: version "4.2.2" resolved "https://registry.yarnpkg.com/uri-js/-/uri-js-4.2.2.tgz#94c540e1ff772956e2299507c010aea6c8838eb0"