@dark-engine/platform-desktop
Version:
Dark renderer to desktop platforms like Windows, Linux, macOS via Nodegui and Qt
197 lines (196 loc) • 6.25 kB
JavaScript
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
exports.createAttrSetter =
exports.CommentNativeElement =
exports.TextNativeElement =
exports.TagNativeElement =
exports.NativeElement =
exports.INITIAL_ATTR_VALUE =
void 0;
const nodegui_1 = require('@nodegui/nodegui');
const core_1 = require('@dark-engine/core');
const utils_1 = require('../utils');
const events_1 = require('../events');
const registry_1 = require('../registry');
const constants_1 = require('../constants');
class NativeElement {
type;
parentElement = null;
constructor(type) {
this.type = type;
}
getText() {
return this.type;
}
}
exports.NativeElement = NativeElement;
class TagNativeElement extends NativeElement {
name = null;
attrs = {};
children = [];
nativeView;
eventListeners = new Map();
constructor(name) {
super(core_1.NodeType.TAG);
this.name = name;
this.nativeView = (0, registry_1.getElementFactory)(name).create();
}
getNativeView() {
if (this.name === core_1.ROOT) {
const tag = this.children[0];
return tag.getNativeView();
}
return this.nativeView;
}
appendChild(element) {
element.parentElement = this;
this.children.push(element);
if (element.type === core_1.NodeType.TAG) {
const parent = this.nativeView;
const child = element.getNativeView();
(0, utils_1.detectIsContainer)(parent) && parent.appendChild(child);
} else if (element.type === core_1.NodeType.TEXT) {
this.updateText();
}
}
insertBefore(element, siblingElement) {
if (!siblingElement) {
return this.appendChild(element);
}
const idx = this.children.findIndex(node => node === siblingElement);
if (idx === -1) {
return this.appendChild(element);
}
if (element.parentElement) {
element.parentElement.removeChild(element);
}
this.children.splice(idx, 0, element);
element.parentElement = this;
if (element.type === core_1.NodeType.TAG) {
const parent = this.nativeView;
const child = element.getNativeView();
const sibling = siblingElement.getNativeView();
const idx = this.children.filter(node => node.type === core_1.NodeType.TAG).findIndex(node => node === element);
(0, utils_1.detectIsContainer)(parent) && parent.insertBefore(child, sibling, idx);
} else if (element.type === core_1.NodeType.TEXT) {
this.updateText();
}
}
removeChild(element) {
const idx = this.children.findIndex(node => node === element);
if (idx !== -1) {
this.children.splice(idx, 1);
element.parentElement = null;
if (element.type === core_1.NodeType.TAG) {
const parent = this.nativeView;
const child = element.getNativeView();
(0, utils_1.detectIsContainer)(parent) && parent.removeChild(child);
} else if (element.type === core_1.NodeType.TEXT) {
this.updateText();
}
}
}
getAttribute(name) {
return this.attrs[name];
}
setAttribute(name, value) {
const setterName = (0, utils_1.createSetterName)(name);
defaultAttrSetter(this, name, value);
if (!(0, core_1.detectIsFunction)(this.nativeView[setterName])) return;
if (!this.nativeView[exports.INITIAL_ATTR_VALUE]) {
this.nativeView[exports.INITIAL_ATTR_VALUE] = {};
}
this.nativeView[exports.INITIAL_ATTR_VALUE][name] = this.nativeView[name];
this.nativeView[setterName](value);
this.attrs[name] = value;
}
removeAttribute(name) {
const setterName = (0, utils_1.createSetterName)(name);
if (!(0, core_1.detectIsFunction)(this.nativeView[setterName])) return;
const initialValue = this.nativeView[exports.INITIAL_ATTR_VALUE][name];
this.nativeView[setterName](initialValue);
delete this.nativeView[exports.INITIAL_ATTR_VALUE][name];
delete this.attrs[name];
}
updateText() {
let text = '';
for (const child of this.children) {
if (child.type === core_1.NodeType.TEXT) {
text += child.value;
}
}
this.setAttribute(constants_1.TEXT_ATTR, text);
}
getText() {
return this.getAttribute(constants_1.TEXT_ATTR);
}
addEventListener(eventName, handler) {
const syntheticHandler = (0, events_1.createSyntheticEventHandler)(eventName, handler);
this.removeEventListener(eventName);
this.eventListeners.set(eventName, syntheticHandler);
this.nativeView.addEventListener(eventName, syntheticHandler);
}
removeEventListener(eventName) {
const handler = this.eventListeners.get(eventName);
this.eventListeners.delete(eventName);
this.nativeView.removeEventListener(eventName, handler);
}
}
exports.TagNativeElement = TagNativeElement;
class TextNativeElement extends NativeElement {
_value = '';
constructor(text) {
super(core_1.NodeType.TEXT);
this._value = text;
}
get value() {
return this._value;
}
set value(value) {
this._value = value;
if (this.parentElement?.type === core_1.NodeType.TAG) {
this.parentElement.updateText();
}
}
getText() {
return this._value;
}
}
exports.TextNativeElement = TextNativeElement;
class CommentNativeElement extends NativeElement {
_value = '';
constructor(text) {
super(core_1.NodeType.COMMENT);
this._value = `<!--${text}-->`;
}
get value() {
return this._value;
}
set value(value) {
this._value = value;
}
getText() {
return this._value;
}
}
exports.CommentNativeElement = CommentNativeElement;
function createAttrSetter(setter) {
const map = {
id: (w, n) => w.setObjectName(n),
posX: (w, n) => w.move(n, w.y()),
posY: (w, n) => w.move(w.x(), n),
width: (w, n) => w.resize(n, w.height()),
height: (w, n) => w.resize(w.width(), n),
style: (w, n) => w.setInlineStyle(n),
...setter,
};
return (element, name, value) => {
const widget = element.getNativeView();
if (!nodegui_1.QWidget.isPrototypeOf(widget) && !(widget instanceof nodegui_1.QWidget)) return;
map[name] && map[name](widget, value);
};
}
exports.createAttrSetter = createAttrSetter;
const defaultAttrSetter = createAttrSetter({});
exports.INITIAL_ATTR_VALUE = '_INITIAL_ATTR_VALUE';
//# sourceMappingURL=native-element.js.map