UNPKG

@nodegui/svelte-nodegui

Version:
1,617 lines (1,581 loc) 99.9 kB
import { QAction, QWidget, FlexLayout, QBoxLayout, Direction, Component, QGridLayout, QSlider, QMainWindow, QMenuBar, QLabel, QPixmap, QMovie, QPushButton, QCheckBox, QLineEdit, QMenu, QPlainTextEdit, QProgressBar, QRadioButton, QDial, QSpinBox, QScrollArea, QComboBox, QSystemTrayIcon, QTabWidget, QIcon, QSvgWidget, WidgetEventTypes, QObject } from '@nodegui/nodegui'; import phin from 'phin'; function throwUnsupported(instance) { throw new Error(`Unsupported operation performed in ${instance.constructor.name}`); } function isValidUrl(str) { try { new URL(str); return true; } catch (_) { return false; } } const setActionProps = (widget, newProps, oldProps) => { const setter = { set checkable(isCheckable) { widget.setCheckable(isCheckable); }, set checked(isChecked) { widget.setChecked(isChecked); }, set enabled(isEnabled) { widget.setEnabled(isEnabled); }, set font(font) { widget.setFont(font); }, set icon(icon) { widget.setIcon(icon); }, set id(id) { widget.setObjectName(id); }, set on(listenerMap) { const listenerMapLatest = Object.assign({}, listenerMap); const oldListenerMap = Object.assign({}, oldProps.on); Object.entries(oldListenerMap).forEach(([eventType, oldEvtListener]) => { const newEvtListener = listenerMapLatest[eventType]; if (oldEvtListener !== newEvtListener) { widget.removeEventListener(eventType, oldEvtListener); } else { delete listenerMapLatest[eventType]; } }); Object.entries(listenerMapLatest).forEach(([eventType, newEvtListener]) => { widget.addEventListener(eventType, newEvtListener); }); }, set separator(isSeparator) { widget.setSeparator(isSeparator); }, set shortcut(shortcut) { widget.setShortcut(shortcut); }, set shortcutContext(shortcutContext) { widget.setShortcutContext(shortcutContext); }, set text(text) { widget.setText(text); }, }; Object.assign(setter, newProps); }; class RNAction extends QAction { setProps(newProps, oldProps) { setActionProps(this, newProps, oldProps); } appendInitialChild(child) { throwUnsupported(this); } appendChild(child) { throwUnsupported(this); } insertBefore(child, beforeChild) { throwUnsupported(this); } removeChild(child) { throwUnsupported(this); } } RNAction.tagName = "action"; /** * @ignore */ function setViewProps(widget, newProps, oldProps) { const setter = { set visible(shouldShow) { shouldShow ? widget.show() : widget.hide(); }, set styleSheet(styleSheet) { widget.setStyleSheet(styleSheet); }, set style(inlineStyle) { if (newProps.styleSheet) { console.warn("Both styleSheet and inlineStyle can't be used together"); } widget.setInlineStyle(inlineStyle); }, set geometry(geometry) { widget.setGeometry(geometry.x, geometry.y, geometry.width, geometry.height); }, set id(id) { widget.setObjectName(id); }, set mouseTracking(isMouseTracked) { widget.setMouseTracking(isMouseTracked); }, set enabled(enable) { widget.setEnabled(enable); }, set windowOpacity(opacity) { widget.setWindowOpacity(opacity); }, set windowTitle(title) { widget.setWindowTitle(title); }, set windowState(state) { widget.setWindowState(state); }, set cursor(cursor) { widget.setCursor(cursor); }, set windowIcon(icon) { widget.setWindowIcon(icon); }, set minSize(size) { widget.setMinimumSize(size.width, size.height); }, set maxSize(size) { widget.setMaximumSize(size.width, size.height); }, set size(size) { if (size.fixed) { widget.setFixedSize(size.width, size.height); } else { const minSize = newProps.minSize || { width: 0, height: 0 }; const maxSize = newProps.maxSize || { width: 16777215, height: 16777215, }; widget.setMinimumSize(minSize.width, minSize.height); widget.setMaximumSize(maxSize.width, maxSize.height); widget.resize(size.width, size.height); } }, set pos(position) { widget.move(position.x, position.y); }, set on(listenerMap) { const listenerMapLatest = Object.assign({}, listenerMap); const oldListenerMap = Object.assign({}, oldProps.on); Object.entries(oldListenerMap).forEach(([eventType, oldEvtListener]) => { const newEvtListener = listenerMapLatest[eventType]; if (oldEvtListener !== newEvtListener) { widget.removeEventListener(eventType, oldEvtListener); } else { delete listenerMapLatest[eventType]; } }); Object.entries(listenerMapLatest).forEach(([eventType, newEvtListener]) => { widget.addEventListener(eventType, newEvtListener); }); }, set attributes(attributesMap) { Object.entries(attributesMap).forEach(([attribute, value]) => { widget.setAttribute(Number(attribute), value); }); }, set windowFlags(windowFlagsMap) { Object.entries(windowFlagsMap).forEach(([flag, value]) => { widget.setWindowFlag(Number(flag), value); }); }, }; Object.assign(setter, newProps); } /** * @ignore */ class RNView extends QWidget { setProps(newProps, oldProps) { setViewProps(this, newProps, oldProps); } insertBefore(child, beforeChild) { if (!this.layout) { console.warn("parent has no layout to insert child before another child"); return; } this.layout.insertChildBefore(child, beforeChild); } appendInitialChild(child) { this.appendChild(child); } appendChild(child) { if (!child) { return; } if (!this.layout) { const flexLayout = new FlexLayout(); flexLayout.setFlexNode(this.getFlexNode()); this.setLayout(flexLayout); this.layout = flexLayout; } this.layout.addWidget(child); } removeChild(child) { if (!this.layout) { console.warn("parent has no layout to remove child from"); return; } this.layout.removeWidget(child); child.close(); } } RNView.tagName = "view"; const setBoxViewProps = (widget, newProps, oldProps) => { const setter = { set direction(direction) { var _a; (_a = widget.layout) === null || _a === void 0 ? void 0 : _a.setDirection(direction); }, }; Object.assign(setter, newProps); setViewProps(widget, newProps, oldProps); }; /** * @ignore */ class RNBoxView extends QWidget { constructor() { super(...arguments); this.children = []; } get layout() { return super.layout; } set layout(l) { super.layout = l; } setProps(newProps, oldProps) { if (this.layout) { setBoxViewProps(this, newProps, oldProps); } else { this.initialProps = newProps; } } appendInitialChild(child) { this.appendChild(child); } appendChild(child) { const updateChild = () => { var _a; (_a = this.layout) === null || _a === void 0 ? void 0 : _a.addWidget(child); this.children.push(child); }; if (this.layout) { updateChild(); return; } const layout = new QBoxLayout(Direction.LeftToRight); this.setLayout(layout); this.layout = layout; // Newly created layout, so set initial props if (this.initialProps) { setBoxViewProps(this, this.initialProps, {}); } updateChild(); } insertBefore(child, beforeChild) { var _a; const prevIndex = this.children.indexOf(beforeChild); if (prevIndex === -1) { throw new Error("Attempted to insert child Node before nonexistent child"); } this.children.splice(prevIndex, 0, child); (_a = this.layout) === null || _a === void 0 ? void 0 : _a.insertWidget(prevIndex, child); } removeChild(child) { const prevIndex = this.children.indexOf(child); if (prevIndex !== -1) { this.children.splice(prevIndex, 1); } child.close(); } } RNBoxView.tagName = "boxview"; const setGridColumnProps = (widget, parentRow, newProps, oldProps) => { var _a, _b, _c, _d, _e, _f, _g, _h; if (widget.actualWidget) { // TODO: Optimize this (_b = (_a = parentRow.parentGrid) === null || _a === void 0 ? void 0 : _a.layout) === null || _b === void 0 ? void 0 : _b.removeWidget(widget.actualWidget); (_d = (_c = parentRow.parentGrid) === null || _c === void 0 ? void 0 : _c.layout) === null || _d === void 0 ? void 0 : _d.addWidget(widget.actualWidget, (_e = parentRow.rowIndex) !== null && _e !== void 0 ? _e : 0, (_f = widget.columnIndex) !== null && _f !== void 0 ? _f : 0, (_g = parentRow.height) !== null && _g !== void 0 ? _g : 1, (_h = widget.width) !== null && _h !== void 0 ? _h : 1); } const setter = { set width(width) { widget.width = width; }, }; Object.assign(setter, newProps); }; class RNGridColumn extends Component { setParentRowAndUpdateProps(parentRow, index) { var _a, _b; this.parentRow = parentRow; this.columnIndex = index; setGridColumnProps(this, parentRow, (_a = this.latestProps) !== null && _a !== void 0 ? _a : {}, (_b = this.prevProps) !== null && _b !== void 0 ? _b : {}); } remove() { var _a, _b, _c; if (!this.actualWidget) { return; } (_c = (_b = (_a = this.parentRow) === null || _a === void 0 ? void 0 : _a.parentGrid) === null || _b === void 0 ? void 0 : _b.layout) === null || _c === void 0 ? void 0 : _c.removeWidget(this.actualWidget); this.actualWidget.close(); this.actualWidget = undefined; } /* RNComponent */ setProps(newProps, oldProps) { if (this.parentRow) { setGridColumnProps(this, this.parentRow, newProps); } this.latestProps = newProps; this.prevProps = oldProps; } appendInitialChild(child) { if (this.actualWidget) { throw new Error("Grid column can have only one child"); } this.actualWidget = child; } appendChild(child) { this.appendInitialChild(child); } insertBefore(child, beforeChild) { this.appendInitialChild(child); } removeChild(child) { this.remove(); } } RNGridColumn.tagName = "gridcolumn"; const offsetForIndex = (index, items, sizeKey) => { var _a; let offset = 0; if (index > 0) { const previousChild = items[index - 1]; offset = previousChild.offset + ((_a = previousChild.data[sizeKey]) !== null && _a !== void 0 ? _a : 1); } return offset; }; function updateDisplacedChildren(startIndex, items, parent, sizeKey, setParentFuncKey) { var _a, _b, _c; let offset = offsetForIndex(startIndex, items, sizeKey); for (let i = startIndex; i < items.length; i++) { const displacedChild = items[i]; displacedChild.offset = offset; (_b = (_a = displacedChild.data)[setParentFuncKey]) === null || _b === void 0 ? void 0 : _b.call(_a, parent, offset); offset += (_c = displacedChild.data[sizeKey]) !== null && _c !== void 0 ? _c : 1; } } const setGridRowProps = (widget, parentGrid, newProps, oldProps) => { const setter = { set height(height) { widget.height = height; }, }; Object.assign(setter, newProps); }; class RNGridRow extends Component { constructor() { super(...arguments); this.childColumns = []; } setParentGridAndUpdateProps(parentGrid, index) { var _a, _b; this.parentGrid = parentGrid; this.rowIndex = index; setGridRowProps(this, parentGrid, (_a = this.latestProps) !== null && _a !== void 0 ? _a : {}, (_b = this.prevProps) !== null && _b !== void 0 ? _b : {}); this.updateChildren(); } updateChildren(startIndex = 0) { updateDisplacedChildren(startIndex, this.childColumns, this, "width", "setParentRowAndUpdateProps"); } remove() { this.childColumns.forEach(({ data }) => data.remove()); } /* RNComponent */ setProps(newProps, oldProps) { if (this.parentGrid) { setGridRowProps(this, this.parentGrid, newProps); } this.latestProps = newProps; this.prevProps = oldProps; } appendInitialChild(child) { this.appendChild(child); } appendChild(child) { if (!(child instanceof RNGridColumn)) { throw new Error("GridColumn is the only supported child of GridRow"); } const offset = offsetForIndex(this.childColumns.length, this.childColumns, "width"); child.setParentRowAndUpdateProps(this, offset); this.childColumns.push({ offset, data: child, }); } insertBefore(child, beforeChild) { const prevIndex = this.childColumns.findIndex(({ data }) => data === beforeChild); if (prevIndex === -1) { throw new Error("Attempted to insert child GridColumn before nonexistent column"); } const offset = offsetForIndex(prevIndex, this.childColumns, "width"); this.childColumns.splice(prevIndex, 0, { offset, data: child, }); // Update displaced children this.updateChildren(prevIndex); } removeChild(child) { const prevIndex = this.childColumns.findIndex(({ data }) => data === child); if (prevIndex !== -1) { this.childColumns.splice(prevIndex, 1); this.updateChildren(prevIndex); } // Actually remove child from layout child.remove(); child.parentRow = undefined; } } RNGridRow.tagName = "gridrow"; const setGridViewProps = (widget, newProps, oldProps) => { const setter = { set horizontalSpacing(spacing) { var _a; (_a = widget.layout) === null || _a === void 0 ? void 0 : _a.setHorizontalSpacing(spacing); }, set verticalSpacing(spacing) { var _a; (_a = widget.layout) === null || _a === void 0 ? void 0 : _a.setVerticalSpacing(spacing); }, set columnProps(props) { var _a, _b; for (const indexString of Object.keys(props)) { const index = parseInt(indexString, 10); const { stretch, minWidth } = props[index]; (_a = widget.layout) === null || _a === void 0 ? void 0 : _a.setColumnStretch(index, stretch !== null && stretch !== void 0 ? stretch : 0); (_b = widget.layout) === null || _b === void 0 ? void 0 : _b.setColumnMinimumWidth(index, minWidth !== null && minWidth !== void 0 ? minWidth : 0); } }, set rowProps(props) { var _a, _b; for (const indexString of Object.keys(props)) { const index = parseInt(indexString, 10); const { stretch, minHeight } = props[index]; (_a = widget.layout) === null || _a === void 0 ? void 0 : _a.setRowStretch(index, stretch !== null && stretch !== void 0 ? stretch : 0); (_b = widget.layout) === null || _b === void 0 ? void 0 : _b.setRowMinimumHeight(index, minHeight !== null && minHeight !== void 0 ? minHeight : 0); } }, }; Object.assign(setter, newProps); setViewProps(widget, newProps, oldProps); }; /** * @ignore */ class RNGridView extends QWidget { constructor() { super(...arguments); this.childRows = []; } get layout() { return this.layout; } set layout(l) { this._layout = l; } updateChildren(startIndex = 0) { updateDisplacedChildren(startIndex, this.childRows, this, "height", "setParentGridAndUpdateProps"); } /* RNComponent */ setProps(newProps, oldProps) { if (this.layout) { setGridViewProps(this, newProps, oldProps); } else { this.initialProps = newProps; } } appendInitialChild(child) { this.appendChild(child); } appendChild(child) { if (!(child instanceof RNGridRow)) { throw new Error("GridRow is the only supported child of GridView"); } const updateChild = () => { const offset = offsetForIndex(this.childRows.length, this.childRows, "height"); child.setParentGridAndUpdateProps(this, offset); this.childRows.push({ offset, data: child, }); }; if (this.layout) { updateChild(); return; } const layout = new QGridLayout(); this.setLayout(layout); this.layout = layout; // Newly created layout, so set initial props if (this.initialProps) { setGridViewProps(this, this.initialProps, {}); } updateChild(); } insertBefore(child, beforeChild) { const prevIndex = this.childRows.findIndex(({ data }) => data === beforeChild); if (prevIndex === -1) { throw new Error("Attempted to insert child GridRow before nonexistent row"); } const offset = offsetForIndex(prevIndex, this.childRows, "height"); this.childRows.splice(prevIndex, 0, { offset, data: child, }); // Update displaced children this.updateChildren(prevIndex); } removeChild(child) { const prevIndex = this.childRows.findIndex(({ data }) => data === child); if (prevIndex !== -1) { this.childRows.splice(prevIndex, 1); this.updateChildren(prevIndex); } child.remove(); child.parentGrid = undefined; } } RNGridView.tagName = "gridview"; const setSliderProps = (widget, newProps, oldProps) => { const setter = { set tickInterval(tickInterval) { widget.setTickInterval(tickInterval); }, set tickPosition(tickPosition) { widget.setTickPosition(tickPosition); }, set invertedAppearance(inverted) { widget.setInvertedAppearance(inverted); }, set invertedControls(inverted) { widget.setInvertedControls(inverted); }, set maximum(maximum) { widget.setMaximum(maximum); }, set minimum(minimum) { widget.setMinimum(minimum); }, set orientation(orientation) { widget.setOrientation(orientation); }, set pageStep(step) { widget.setPageStep(step); }, set singleStep(step) { widget.setSingleStep(step); }, set isSliderDown(down) { widget.setSliderDown(down); }, set sliderPosition(position) { widget.setSliderPosition(position); }, set hasTracking(enable) { widget.setTracking(enable); }, set value(value) { widget.setValue(value); }, }; Object.assign(setter, newProps); setViewProps(widget, newProps, oldProps); }; /** * @ignore */ class RNSlider extends QSlider { setProps(newProps, oldProps) { setSliderProps(this, newProps, oldProps); } appendInitialChild(child) { throwUnsupported(this); } appendChild(child) { throwUnsupported(this); } insertBefore(child, beforeChild) { throwUnsupported(this); } removeChild(child) { throwUnsupported(this); } } RNSlider.tagName = 'slider'; const setWindowProps = (window, newProps, oldProps) => { const setter = { set menuBar(menubar) { window.setMenuBar(menubar); console.log("menubar was set"); }, }; Object.assign(setter, newProps); setViewProps(window, newProps, oldProps); }; /** * @ignore */ class RNWindow extends QMainWindow { setProps(newProps, oldProps) { setWindowProps(this, newProps, oldProps); } removeChild(child) { const removedChild = this.takeCentralWidget(); if (removedChild) { removedChild.close(); } child.close(); } appendInitialChild(child) { if (child instanceof QMenuBar) { if (!this.menuBar()) { this.setMenuBar(child); } else { console.warn("MainWindow can't have more than one menubar."); } return; } if (!this.centralWidget) { this.setCentralWidget(child); } else { console.warn("MainWindow can't have more than one child node"); } } appendChild(child) { this.appendInitialChild(child); } insertBefore(child, beforeChild) { this.appendInitialChild(child); } } RNWindow.tagName = "mainwindow"; /** * @ignore */ const setTextProps = (widget, newProps, oldProps) => { const setter = { set children(text) { text = Array.isArray(text) ? text.join('') : text; widget.setText(text); }, set wordWrap(shouldWrap) { widget.setWordWrap(shouldWrap); }, set scaledContents(scaled) { widget.setProperty('scaledContents', scaled); }, set openExternalLinks(shouldOpenExternalLinks) { widget.setProperty('openExternalLinks', shouldOpenExternalLinks); }, set textInteractionFlags(interactionFlag) { widget.setProperty('textInteractionFlags', interactionFlag); } }; Object.assign(setter, newProps); setViewProps(widget, newProps, oldProps); }; /** * @ignore */ class RNText extends QLabel { setProps(newProps, oldProps) { setTextProps(this, newProps, oldProps); } appendInitialChild(child) { throwUnsupported(this); } appendChild(child) { throwUnsupported(this); } insertBefore(child, beforeChild) { throwUnsupported(this); } removeChild(child) { throwUnsupported(this); } } RNText.tagName = 'text'; /*! ***************************************************************************** Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. See the Apache Version 2.0 License for specific language governing permissions and limitations under the License. ***************************************************************************** */ function __awaiter(thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); } const setImageProps = (widget, newProps, oldProps) => { const setter = { set src(imageUrlOrPath) { if (!imageUrlOrPath) { return; } getLoadedPixmap(imageUrlOrPath) .then((pixmap) => widget.setPixmap(pixmap)) .catch(console.warn); }, set buffer(imageBuffer) { const pixMap = new QPixmap(); pixMap.loadFromData(imageBuffer); widget.setPixmap(pixMap); }, set aspectRatioMode(mode) { widget.setAspectRatioMode(mode); }, set transformationMode(mode) { widget.setTransformationMode(mode); }, }; Object.assign(setter, newProps); setTextProps(widget, newProps, oldProps); }; /** * @ignore */ class RNImage extends QLabel { constructor() { super(...arguments); this.setPixmap = (pixmap) => { // react:✓ super.setPixmap(pixmap); this.originalPixmap = pixmap; }; } setProps(newProps, oldProps) { setImageProps(this, newProps, oldProps); } appendInitialChild(child) { throwUnsupported(this); } appendChild(child) { throwUnsupported(this); } insertBefore(child, beforeChild) { throwUnsupported(this); } removeChild(child) { throwUnsupported(this); } setAspectRatioMode(mode) { // react:✓ TODO://getter this.aspectRatioMode = mode; this.scalePixmap(this.size()); } setTransformationMode(mode) { // react:✓ TODO://getter this.transformationMode = mode; this.scalePixmap(this.size()); } scalePixmap(size) { if (this.originalPixmap) { return super.setPixmap(this.originalPixmap.scaled(size.width(), size.height(), this.aspectRatioMode, this.transformationMode)); } } } RNImage.tagName = "image"; function getLoadedPixmap(imageUrlOrPath) { return __awaiter(this, void 0, void 0, function* () { const pixMap = new QPixmap(); if (isValidUrl(imageUrlOrPath)) { const res = yield phin(imageUrlOrPath); const imageBuffer = Buffer.from(res.body); pixMap.loadFromData(imageBuffer); } else { pixMap.load(imageUrlOrPath); } return pixMap; }); } const setAnimatedImageProps = (widget, newProps, oldProps) => { const setter = { set src(imageUrlOrPath) { if (!imageUrlOrPath) { return; } getLoadedQMovie(imageUrlOrPath) .then(movie => { var _a; widget.setMovie(movie); (_a = widget.movie()) === null || _a === void 0 ? void 0 : _a.start(); }) .catch(console.warn); }, set buffer(imageBuffer) { var _a; const movie = new QMovie(); movie.loadFromData(imageBuffer); widget.setMovie(movie); (_a = widget.movie()) === null || _a === void 0 ? void 0 : _a.start(); } }; Object.assign(setter, newProps); setTextProps(widget, newProps, oldProps); }; /** * @ignore */ class RNAnimatedImage extends QLabel { setProps(newProps, oldProps) { setAnimatedImageProps(this, newProps, oldProps); } appendInitialChild(child) { throwUnsupported(this); } appendChild(child) { throwUnsupported(this); } insertBefore(child, beforeChild) { throwUnsupported(this); } removeChild(child) { throwUnsupported(this); } scaleMovie(size) { const movie = this.movie(); movie === null || movie === void 0 ? void 0 : movie.setScaledSize(size); } } RNAnimatedImage.tagName = "animatedimage"; function getLoadedQMovie(imageUrlOrPath) { return __awaiter(this, void 0, void 0, function* () { const movie = new QMovie(); if (isValidUrl(imageUrlOrPath)) { const res = yield phin(imageUrlOrPath); const imageBuffer = Buffer.from(res.body); movie.loadFromData(imageBuffer); } else { movie.setFileName(imageUrlOrPath); } return movie; }); } function setAbstractButtonProps(widget, newProps, oldProps) { const setter = { set children(childrenText) { widget.setText(childrenText); }, set text(buttonText) { widget.setText(buttonText); }, set icon(icon) { widget.setIcon(icon); }, set iconSize(iconSize) { widget.setIconSize(iconSize); }, }; Object.assign(setter, newProps); setViewProps(widget, newProps, oldProps); } const setButtonProps = (widget, newProps, oldProps) => { const setter = { set flat(isFlat) { widget.setFlat(isFlat); } }; Object.assign(setter, newProps); setAbstractButtonProps(widget, newProps, oldProps); }; /** * @ignore */ class RNButton extends QPushButton { appendInitialChild(child) { throwUnsupported(this); } appendChild(child) { throwUnsupported(this); } insertBefore(child, beforeChild) { throwUnsupported(this); } removeChild(child) { throwUnsupported(this); } setProps(newProps, oldProps) { setButtonProps(this, newProps, oldProps); } } RNButton.tagName = "button"; const setCheckBoxProps = (widget, newProps, oldProps) => { const setter = { set checked(isChecked) { widget.setChecked(isChecked); } }; Object.assign(setter, newProps); setAbstractButtonProps(widget, newProps, oldProps); }; /** * @ignore */ class RNCheckBox extends QCheckBox { setProps(newProps, oldProps) { setCheckBoxProps(this, newProps, oldProps); } appendInitialChild(child) { throwUnsupported(this); } appendChild(child) { throwUnsupported(this); } insertBefore(child, beforeChild) { throwUnsupported(this); } removeChild(child) { throwUnsupported(this); } } RNCheckBox.tagName = "checkbox"; const setLineEditProps = (widget, newProps, oldProps) => { const setter = { set text(text) { text ? widget.setText(text) : widget.clear(); }, set placeholderText(text) { widget.setPlaceholderText(text); }, set readOnly(isReadOnly) { widget.setReadOnly(isReadOnly); }, set echoMode(mode) { widget.setEchoMode(mode); } }; Object.assign(setter, newProps); setViewProps(widget, newProps, oldProps); }; /** * @ignore */ class RNLineEdit extends QLineEdit { setProps(newProps, oldProps) { setLineEditProps(this, newProps, oldProps); } appendInitialChild(child) { throwUnsupported(this); } appendChild(child) { throwUnsupported(this); } insertBefore(child, beforeChild) { throwUnsupported(this); } removeChild(child) { throwUnsupported(this); } } RNLineEdit.tagName = "linedit"; const setMenuProps = (widget, newProps, oldProps) => { const setter = { set title(title) { widget.setTitle(title); }, }; Object.assign(setter, newProps); setViewProps(widget, newProps, oldProps); }; class RNMenu extends QMenu { setProps(newProps, oldProps) { setMenuProps(this, newProps, oldProps); } appendInitialChild(child) { this.appendChild(child); } appendChild(child) { if (!(child instanceof RNAction)) { console.warn("Menu only supports Action as its children"); return; } this.addAction(child); } insertBefore(child, beforeChild) { throwUnsupported(this); } removeChild(child) { if (child instanceof RNAction) { this.removeAction(child); } } } RNMenu.tagName = "menu"; const setMenuBarProps = (widget, newProps, oldProps) => { const setter = { set nativeMenuBar(shouldBeNative) { widget.setNativeMenuBar(shouldBeNative); }, }; Object.assign(setter, newProps); setViewProps(widget, newProps, oldProps); }; class RNMenuBar extends QMenuBar { setProps(newProps, oldProps) { setMenuBarProps(this, newProps, oldProps); } appendInitialChild(child) { if (child instanceof QMenu) { this.addMenu(child); } else { console.warn("MenuBar only supports Menu as its children"); } } appendChild(child) { this.appendInitialChild(child); } insertBefore(child, beforeChild) { console.warn("Updating menubar is not yet supported. Please help by raising a PR"); throwUnsupported(this); } removeChild(child) { console.warn("Updating menubar is not yet supported. Please help by raising a PR"); throwUnsupported(this); } } RNMenuBar.tagName = "menubar"; const setPlainTextEditProps = (widget, newProps, oldProps) => { const setter = { set text(text) { text ? widget.setPlainText(text) : widget.clear(); }, set readOnly(isReadOnly) { widget.setReadOnly(isReadOnly); }, set placeholderText(text) { widget.setPlaceholderText(text); } }; Object.assign(setter, newProps); setViewProps(widget, newProps, oldProps); }; /** * @ignore */ class RNPlainTextEdit extends QPlainTextEdit { setProps(newProps, oldProps) { setPlainTextEditProps(this, newProps, oldProps); } appendInitialChild(child) { throwUnsupported(this); } appendChild(child) { throwUnsupported(this); } insertBefore(child, beforeChild) { throwUnsupported(this); } removeChild(child) { throwUnsupported(this); } } RNPlainTextEdit.tagName = "plaintextedit"; const setProgressBarProps = (widget, newProps, oldProps) => { const setter = { set value(val) { widget.setValue(val); }, set minimum(min) { widget.setMinimum(min); }, set maximum(max) { widget.setMaximum(max); }, set orientation(orientation) { widget.setOrientation(orientation); } }; Object.assign(setter, newProps); setViewProps(widget, newProps, oldProps); }; /** * @ignore */ class RNProgressBar extends QProgressBar { setProps(newProps, oldProps) { setProgressBarProps(this, newProps, oldProps); } appendInitialChild(child) { throwUnsupported(this); } appendChild(child) { throwUnsupported(this); } insertBefore(child, beforeChild) { throwUnsupported(this); } removeChild(child) { throwUnsupported(this); } } RNProgressBar.tagName = "progressbar"; const setRadioButtonProps = (widget, newProps, oldProps) => { const setter = { // more setters to be added }; Object.assign(setter, newProps); setAbstractButtonProps(widget, newProps, oldProps); }; /** * @ignore */ class RNRadioButton extends QRadioButton { setProps(newProps, oldProps) { setRadioButtonProps(this, newProps, oldProps); } appendInitialChild(child) { throwUnsupported(this); } appendChild(child) { throwUnsupported(this); } insertBefore(child, beforeChild) { throwUnsupported(this); } removeChild(child) { throwUnsupported(this); } } RNRadioButton.tagName = "radiobutton"; const setDialProps = (widget, newProps, oldProps) => { const setter = { set notchesVisible(notchesVisible) { widget.setNotchesVisible(notchesVisible); }, set wrapping(wrapping) { widget.setWrapping(wrapping); }, set notchTarget(notchTarget) { widget.setNotchTarget(notchTarget); }, }; Object.assign(setter, newProps); setViewProps(widget, newProps, oldProps); }; /** * @ignore */ class RNDial extends QDial { setProps(newProps, oldProps) { setDialProps(this, newProps, oldProps); } appendInitialChild(child) { throwUnsupported(this); } appendChild(child) { throwUnsupported(this); } insertBefore(child, beforeChild) { throwUnsupported(this); } removeChild(child) { throwUnsupported(this); } } RNDial.tagName = "dial"; const setSpinBoxProps = (widget, newProps, oldProps) => { const setter = { set prefix(prefix) { widget.setPrefix(prefix); }, set suffix(suffix) { widget.setSuffix(suffix); }, set singleStep(step) { widget.setSingleStep(step); }, set range(range) { widget.setRange(range.minimum, range.maximum); }, set value(value) { widget.setValue(value); } }; Object.assign(setter, newProps); setViewProps(widget, newProps, oldProps); }; /** * @ignore */ class RNSpinBox extends QSpinBox { setProps(newProps, oldProps) { setSpinBoxProps(this, newProps, oldProps); } appendInitialChild(child) { throwUnsupported(this); } appendChild(child) { throwUnsupported(this); } insertBefore(child, beforeChild) { throwUnsupported(this); } removeChild(child) { throwUnsupported(this); } } RNSpinBox.tagName = "spinbox"; const setScrollAreaProps = (widget, newProps, oldProps) => { const setter = { set widgetResizable(resizable) { widget.setWidgetResizable(resizable); } }; Object.assign(setter, newProps); setViewProps(widget, newProps, oldProps); }; /** * @ignore */ class RNScrollArea extends QScrollArea { setProps(newProps, oldProps) { setScrollAreaProps(this, newProps, oldProps); } removeChild(child) { const removedChild = this.takeWidget(); if (removedChild) { removedChild.close(); } child.close(); } appendInitialChild(child) { if (this.contentWidget) { console.warn("ScrollView can't have more than one child node"); return; } this.setWidget(child); } appendChild(child) { this.appendInitialChild(child); } insertBefore(child, beforeChild) { this.appendInitialChild(child); } } RNScrollArea.tagName = "scrollarea"; const setComboBoxProps = (widget, newProps, oldProps) => { const setter = { set items(items) { widget.clear(); items.forEach(item => { widget.addItem(item.icon, item.text, item.userData); }); }, set count(count) { widget.setProperty("count", count); }, set iconSize(iconSize) { widget.setProperty("iconSize", iconSize.native); }, set frame(frame) { widget.setProperty("frame", frame); }, set currentIndex(currentIndex) { widget.setProperty("currentIndex", currentIndex); }, set currentData(value) { widget.setProperty("currentData", value.native); }, set currentText(text) { widget.setProperty("currentText", text); }, set duplicatesEnabled(enabled) { widget.setProperty("duplicatesEnabled", enabled); }, set editable(enabled) { widget.setProperty("editable", enabled); }, set insertPolicy(policy) { widget.setProperty("insertPolicy", policy); }, set maxCount(count) { widget.setProperty("maxCount", count); }, set maxVisibleItems(count) { widget.setProperty("maxVisibleItems", count); }, set minimumContentsLength(count) { widget.setProperty("minimumContentsLength", count); }, set modelColumn(column) { widget.setProperty("modelColumn", column); }, set sizeAdjustPolicy(policy) { widget.setProperty("sizeAdjustPolicy", policy); } }; Object.assign(setter, newProps); setViewProps(widget, newProps, oldProps); }; /** * @ignore */ class RNComboBox extends QComboBox { setProps(newProps, oldProps) { setComboBoxProps(this, newProps, oldProps); } appendInitialChild(child) { throwUnsupported(this); } appendChild(child) { throwUnsupported(this); } insertBefore(child, beforeChild) { throwUnsupported(this); } removeChild(child) { throwUnsupported(this); } } RNComboBox.tagName = "combobox"; const setSystemTrayIconProps = (widget, newProps, oldProps) => { const setter = { set icon(icon) { widget.setIcon(icon); }, set id(id) { widget.setObjectName(id); }, set on(listenerMap) { const listenerMapLatest = Object.assign({}, listenerMap); const oldListenerMap = Object.assign({}, oldProps.on); Object.entries(oldListenerMap).forEach(([eventType, oldEvtListener]) => { const newEvtListener = listenerMapLatest[eventType]; if (oldEvtListener !== newEvtListener) { widget.removeEventListener(eventType, oldEvtListener); } else { delete listenerMapLatest[eventType]; } }); Object.entries(listenerMapLatest).forEach(([eventType, newEvtListener]) => { widget.addEventListener(eventType, newEvtListener); }); }, set tooltip(tooltip) { widget.setToolTip(tooltip); }, set visible(shouldShow) { shouldShow ? widget.show() : widget.hide(); }, }; Object.assign(setter, newProps); }; /** * @ignore */ class RNSystemTrayIcon extends QSystemTrayIcon { setProps(newProps, oldProps) { setSystemTrayIconProps(this, newProps, oldProps); } appendInitialChild(child) { if (child instanceof QMenu) { if (!this.contextMenu) { this.setContextMenu(child); } else { console.warn("SystemTrayIcon can't have more than one Menu."); } } else { console.warn("SystemTrayIcon only supports Menu as its children"); } } appendChild(child) { this.appendInitialChild(child); } insertBefore(child, beforeChild) { throwUnsupported(this); } removeChild(child) { throwUnsupported(this); } } RNSystemTrayIcon.tagName = "systemtrayicon"; /** * @ignore */ const setTabItemProps = (tabItem, parentTab, newProps, oldProps) => { if (!tabItem.actualTabWidget) { return; } const tabIndex = parentTab.indexOf(tabItem.actualTabWidget); if (tabIndex < 0) { console.error("TabItem is not part of the parent tab it references to"); return; } const setter = { set title(text) { parentTab.setTabText(tabIndex, text); }, set icon(qicon) { parentTab.setTabIcon(tabIndex, qicon); } }; Object.assign(setter, newProps); }; /** * @ignore */ class RNTabItem extends Component { constructor() { super(...arguments); this.initialProps = {}; } setProps(newProps, oldProps) { if (this.parentTab) { setTabItemProps(this, this.parentTab, newProps); } else { this.initialProps = newProps; } } appendInitialChild(child) { if (this.actualTabWidget) { throw new Error("Tab Item can have only one child"); } this.actualTabWidget = child; } appendChild(child) { this.appendInitialChild(child); } insertBefore(child, beforeChild) { this.appendInitialChild(child); } removeChild(child) { child.close(); delete this.actualTabWidget; } } RNTabItem.tagName = "tabitem"; /** * @ignore */ const setTabProps = (widget, newProps, oldProps) => { const setter = { set tabPosition(value) { widget.setTabPosition(value); } }; Object.assign(setter, newProps); setViewProps(widget, newProps, oldProps); }; /** * @ignore */ class RNTab extends QTabWidget { setProps(newProps, oldProps) { setTabProps(this, newProps, oldProps); } appendInitialChild(tabItem) { if (!(tabItem instanceof RNTabItem)) { throw new Error("Children of tab should be of type TabItem"); } if (tabItem.actualTabWidget) { this.addTab(tabItem.actualTabWidget, new QIcon(), ""); tabItem.parentTab = this; setTabItemProps(tabItem, this, tabItem.initialProps); } } appendChild(child) { this.appendInitialChild(child); } insertBefore(child, beforeChild) { if (!(child instanceof RNTabItem)) { throw new Error("Children of tab should be of type TabItem"); } const index = this.indexOf(beforeChild.actualTabWidget); this.insertTab(index, child.actualTabWidget, new QIcon(), ""); child.parentTab = this; setTabItemProps(child, this, child.initialProps); } removeChild(child) { const childIndex = this.indexOf(child.actualTabWidget); this.removeTab(childIndex); } } RNTab.tagName = "tabwidget"; const setSvgProps = (widget, newProps, oldProps) => { const setter = { set graphicsEffect(effect) { widget.setGraphicsEffect(effect); }, set src(file) { widget.load(file); }, }; Object.assign(setter, newProps); setViewProps(widget, newProps, oldProps); }; /** * @ignore */ class RNSvg extends QSvgWidget { setProps(newProps, oldProps) { setSvgProps(this, newProps, oldProps); } appendInitialChild(child) { throwUnsupported(this); } appendChild(child) { throwUnsupported(this); } insertBefore(child, beforeChild) { throwUnsupported(this); } removeChild(child) { throwUnsupported(this); } } RNSvg.tagName = "svg"; let defaultViewMeta = { viewFlags: 0 /* NONE */, }; let elementMap = {}; function getViewMeta(elementName) { // console.log(`->getViewMeta(${elementName})`) const normalizedName = normalizeElementName(elementName); const entry = elementMap[normalizedName]; if (!entry) { throw new Error(`No known component for element ${elementName}.`); } return entry.meta; } /** * @param elementName The name of the element registered into the elementMap. * @returns The nativeView associated with this element name. May be undefined * (e.g. for virtual elements like head and style). */ function getViewClass(elementName) { // console.log(`->getViewClass(${elementName})`) const normalizedName = normalizeElementName(elementName); const entry = elementMap[normalizedName]; if (!entry) { throw new Error(`No known component for element ${elementName}.`); } try { return entry.resolver();