Abstracted away the logic for event listeners from JS side
This commit is contained in:
parent
d0ef8472a3
commit
6ddc6167e2
@ -2,8 +2,8 @@ import addon from "../../core/addon";
|
||||
import { YogaWidget } from "../../core/YogaWidget";
|
||||
import { NodeLayout } from "../../QtWidgets/QLayout";
|
||||
|
||||
// Implement all native QWidget methods here so that all widgets get access to those aswell
|
||||
// All Widgets should extend from NodeWidget
|
||||
// Implement all native QWidget methods here so that all widgets get access to those aswell
|
||||
export abstract class NodeWidget extends YogaWidget {
|
||||
type = "widget";
|
||||
layout?: NodeLayout;
|
||||
|
||||
@ -2,7 +2,7 @@ import { Component } from "../../core/Component";
|
||||
|
||||
// All Layouts should extend this abstract class.
|
||||
export abstract class NodeLayout extends Component {
|
||||
type: string = "Layout";
|
||||
type: string = "layout";
|
||||
}
|
||||
|
||||
// export class QLayout extends NodeLayout { //Dont need QLayout for now
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import addon from "../../core/addon";
|
||||
import { NodeWidget } from "../../QtGui/QWidget";
|
||||
import { EventEmitter } from "events";
|
||||
import { EventNodeWidget } from "../../core/EventNodeWidget";
|
||||
|
||||
export enum QPushButtonEvents {
|
||||
clicked = "clicked",
|
||||
@ -9,26 +9,21 @@ export enum QPushButtonEvents {
|
||||
toggled = "toggled"
|
||||
}
|
||||
|
||||
type EventCallback = (payload?: any) => void;
|
||||
export class QPushButton extends NodeWidget {
|
||||
export class QPushButton extends EventNodeWidget {
|
||||
native: any;
|
||||
private emitter: EventEmitter;
|
||||
constructor(parent?: NodeWidget) {
|
||||
super();
|
||||
let native;
|
||||
if (parent) {
|
||||
this.native = new addon.QPushButton(parent.native);
|
||||
this.parent = parent;
|
||||
native = new addon.QPushButton(parent.native);
|
||||
} else {
|
||||
this.native = new addon.QPushButton();
|
||||
native = new addon.QPushButton();
|
||||
}
|
||||
this.emitter = new EventEmitter();
|
||||
this.native.setupEventListeners(this.emitter.emit.bind(this.emitter));
|
||||
super(native);
|
||||
this.parent = parent;
|
||||
this.native = native;
|
||||
}
|
||||
|
||||
setText(text: string) {
|
||||
this.native.setText(text);
|
||||
}
|
||||
setEventListener(eventType: QPushButtonEvents, callback: EventCallback) {
|
||||
this.emitter.on(eventType, callback);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
export abstract class Component {
|
||||
protected children = new Set<Component>(); //TODO if react stub these as react will manage the instances from beings gc'ed better.
|
||||
protected parent: Component | null = null;
|
||||
protected parent?: Component;
|
||||
abstract native: any;
|
||||
}
|
||||
|
||||
19
src/lib/core/EventNodeWidget/index.ts
Normal file
19
src/lib/core/EventNodeWidget/index.ts
Normal file
@ -0,0 +1,19 @@
|
||||
import { NodeWidget } from "../../QtGui/QWidget";
|
||||
import { EventEmitter } from "events";
|
||||
|
||||
export abstract class EventNodeWidget extends NodeWidget {
|
||||
private emitter: EventEmitter;
|
||||
constructor(native: any) {
|
||||
super();
|
||||
if (native.setupEventListeners) {
|
||||
this.emitter = new EventEmitter();
|
||||
native.setupEventListeners(this.emitter.emit.bind(this.emitter));
|
||||
} else {
|
||||
throw new Error("Event Listeners not implemented on native side");
|
||||
}
|
||||
}
|
||||
|
||||
setEventListener(eventType: string, callback: (payload?: any) => void) {
|
||||
this.emitter.on(eventType, callback);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user