UNPKG

@nodegui/svelte-nodegui

Version:
1,570 lines (1,527 loc) 72.1 kB
import { QAction, QWidget, FlexLayout, QBoxLayout, Direction, Component, QGridLayout, QSlider, QSvgWidget, QMainWindow, QMenuBar, QLabel, QPixmap, WidgetEventTypes, QMovie, QPushButton, QCheckBox, QLineEdit, QMenu, QPlainTextEdit, QProgressBar, QRadioButton, QDial, QSpinBox, QScrollArea, QComboBox, QSystemTrayIcon, QTabWidget, QIcon } from '@nodegui/nodegui'; import phin from 'phin'; class ComponentConfig { getContext(parentContext, rootInstance) { return {}; } finalizeInitialChildren(instance, newProps, rootContainerInstance, context) { return false; } commitMount(instance, newProps, internalInstanceHandle) { return; } // Update methods: prepareUpdate(instance, oldProps, newProps, rootContainerInstance, hostContext) { return true; } } const components = new Map(); function registerComponent(config) { if (components.has(config.tagName)) { throw `A component with tagName: ${config.tagName} already exists. This base component will be ignored`; } components.set(config.tagName, config); return config.tagName; } 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"; class ActionConfig extends ComponentConfig { constructor() { super(...arguments); this.tagName = RNAction.tagName; } shouldSetTextContent(nextProps) { return false; } createInstance(newProps, rootInstance, context, workInProgress) { const widget = new RNAction(); widget.setProps(newProps, {}); return widget; } commitMount(instance, newProps, internalInstanceHandle) { } commitUpdate(instance, updatePayload, oldProps, newProps, finishedWork) { instance.setProps(newProps, oldProps); } } const Action = registerComponent(new ActionConfig()); /** * @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"; class BoxViewConfig extends ComponentConfig { constructor() { super(...arguments); this.tagName = RNBoxView.tagName; } shouldSetTextContent(nextProps) { return false; } createInstance(newProps, rootInstance, context, workInProgress) { const widget = new RNBoxView(); widget.setProps(newProps, {}); return widget; } finalizeInitialChildren(instance, newProps, rootContainerInstance, context) { return true; } commitMount(instance, newProps, internalInstanceHandle) { if (newProps.visible !== false) { instance.show(); } } commitUpdate(instance, updatePayload, oldProps, newProps, finishedWork) { instance.setProps(newProps, oldProps); } } const BoxView = registerComponent(new BoxViewConfig()); 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"; class GridViewConfig extends ComponentConfig { constructor() { super(...arguments); this.tagName = RNGridView.tagName; } shouldSetTextContent(nextProps) { return false; } createInstance(newProps, rootInstance, context, workInProgress) { const widget = new RNGridView(); widget.setProps(newProps, { children: [], }); return widget; } finalizeInitialChildren(instance, newProps, rootContainerInstance, context) { return true; } commitMount(instance, newProps, internalInstanceHandle) { if (newProps.visible !== false) { instance.show(); } } commitUpdate(instance, updatePayload, oldProps, newProps, finishedWork) { instance.setProps(newProps, oldProps); } } const GridView = registerComponent(new GridViewConfig()); class GridRowConfig extends ComponentConfig { constructor() { super(...arguments); this.tagName = RNGridRow.tagName; } shouldSetTextContent(nextProps) { return false; } createInstance(newProps, rootInstance, context, workInProgress) { const widget = new RNGridRow(); widget.setProps(newProps, newProps); return widget; } finalizeInitialChildren(instance, newProps, rootContainerInstance, context) { return true; } commitMount(instance, newProps, internalInstanceHandle) { return; } commitUpdate(instance, updatePayload, oldProps, newProps, finishedWork) { instance.setProps(newProps, oldProps); } } const GridRow = registerComponent(new GridRowConfig()); class GridColumnConfig extends ComponentConfig { constructor() { super(...arguments); this.tagName = RNGridColumn.tagName; } shouldSetTextContent(nextProps) { return false; } createInstance(newProps, rootInstance, context, workInProgress) { const widget = new RNGridColumn(); widget.setProps(newProps, newProps); return widget; } finalizeInitialChildren(instance, newProps, rootContainerInstance, context) { return true; } commitMount(instance, newProps, internalInstanceHandle) { return; } commitUpdate(instance, updatePayload, oldProps, newProps, finishedWork) { instance.setProps(newProps, oldProps); } } const GridColumn = registerComponent(new GridColumnConfig()); 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'; class SliderConfig extends ComponentConfig { constructor() { super(...arguments); this.tagName = RNSlider.tagName; } shouldSetTextContent(nextProps) { return true; } createInstance(newProps, rootInstance, context, workInProgress) { const widget = new RNSlider(); widget.setProps(newProps, {}); return widget; } commitMount(instance, newProps, internalInstanceHandle) { if (newProps.visible !== false) { instance.show(); } return; } commitUpdate(instance, updatePayload, oldProps, newProps, finishedWork) { instance.setProps(newProps, oldProps); } } const Slider = registerComponent(new SliderConfig()); 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"; class SvgConfig extends ComponentConfig { constructor() { super(...arguments); this.tagName = RNSvg.tagName; } shouldSetTextContent(nextProps) { return false; } createInstance(newProps, rootInstance, context, workInProgress) { const widget = new RNSvg(); widget.setProps(newProps, {}); return widget; } commitMount(instance, newProps, internalInstanceHandle) { if (newProps.visible !== false) { instance.show(); } } commitUpdate(instance, updatePayload, oldProps, newProps, finishedWork) { instance.setProps(newProps, oldProps); } } const Svg = registerComponent(new SvgConfig()); class ViewConfig extends ComponentConfig { constructor() { super(...arguments); this.tagName = RNView.tagName; } shouldSetTextContent() { return false; } createInstance(newProps, rootInstance, context, workInProgress) { const widget = new RNView(); widget.setProps(newProps, {}); return widget; } commitMount(instance, newProps, internalInstanceHandle) { if (newProps.visible !== false) { instance.show(); } return; } commitUpdate(instance, updatePayload, oldProps, newProps, finishedWork) { instance.setProps(newProps, oldProps); } } const View = registerComponent(new ViewConfig()); 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"; class WindowConfig extends ComponentConfig { constructor() { super(...arguments); this.tagName = RNWindow.tagName; } shouldSetTextContent(nextProps) { return false; } createInstance(newProps, rootInstance, context, workInProgress) { const window = new RNWindow(); window.setProps(newProps, {}); return window; } finalizeInitialChildren(instance, newProps, rootContainerInstance, context) { return true; } commitMount(instance, newProps, internalInstanceHandle) { if (newProps.visible !== false) { instance.show(); } return; } commitUpdate(instance, updatePayload, oldProps, newProps, finishedWork) { instance.setProps(newProps, oldProps); } } const Window = registerComponent(new WindowConfig()); /** * @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'; class TextConfig extends ComponentConfig { constructor() { super(...arguments); this.tagName = RNText.tagName; } shouldSetTextContent(nextProps) { return true; } createInstance(newProps, rootInstance, context, workInProgress) { const widget = new RNText(); widget.setProps(newProps, {}); return widget; } commitMount(instance, newProps, internalInstanceHandle) { if (newProps.visible !== false) { instance.show(); } return; } finalizeInitialChildren(instance, newProps, rootContainerInstance, context) { return true; } commitUpdate(instance, updatePayload, oldProps, newProps, finishedWork) { instance.setProps(newProps, oldProps); } } const Text = registerComponent(new TextConfig()); /*! ***************************************************************************** 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; }); } class ImageConfig extends ComponentConfig { constructor() { super(...arguments); this.tagName = RNImage.tagName; } shouldSetTextContent(nextProps) { return true; } createInstance(newProps, rootInstance, context, workInProgress) { const widget = new RNImage(); widget.setProperty("scaledContents", true); widget.setProps(newProps, {}); widget.addEventListener(WidgetEventTypes.Resize, () => { widget.scalePixmap(widget.size()); }); return widget; } commitMount(instance, newProps, internalInstanceHandle) { if (newProps.visible !== false) { instance.show(); } return; } commitUpdate(instance, updatePayload, oldProps, newProps, finishedWork) { instance.setProps(newProps, oldProps); } } const Image = registerComponent(new ImageConfig()); 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; }); } class AnimatedImageConfig extends ComponentConfig { constructor() { super(...arguments); this.tagName = RNAnimatedImage.tagName; } shouldSetTextContent(nextProps) { return true; } createInstance(newProps, rootInstance, context, workInProgress) { const widget = new RNAnimatedImage(); widget.setProperty("scaledContents", true); widget.setProps(newProps, {}); return widget; } commitMount(instance, newProps, internalInstanceHandle) { if (newProps.visible !== false) { instance.show(); } return; } commitUpdate(instance, updatePayload, oldProps, newProps, finishedWork) { instance.setProps(newProps, oldProps); } } const AnimatedImage = registerComponent(new AnimatedImageConfig()); 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"; class ButtonConfig extends ComponentConfig { constructor() { super(...arguments); this.tagName = RNButton.tagName; } shouldSetTextContent(nextProps) { return true; } createInstance(newProps, rootInstance, context, workInProgress) { const widget = new RNButton(); widget.setProps(newProps, {}); return widget; } commitMount(instance, newProps, internalInstanceHandle) { if (newProps.visible !== false) { instance.show(); } return; } finalizeInitialChildren(instance, newProps, rootContainerInstance, context) { return true; } commitUpdate(instance, updatePayload, oldProps, newProps, finishedWork) { instance.setProps(newProps, oldProps); } } const Button = registerComponent(new ButtonConfig()); 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"; class CheckBoxConfig extends ComponentConfig { constructor() { super(...arguments); this.tagName = RNCheckBox.tagName; } shouldSetTextContent(nextProps) { return true; } createInstance(newProps, rootInstance, context, workInProgress) { const widget = new RNCheckBox(); widget.setProps(newProps, {}); return widget; } finalizeInitialChildren(instance, newProps, rootContainerInstance, context) { return true; } commitMount(instance, newProps, internalInstanceHandle) { if (newProps.visible !== false) { instance.show(); } return; } commitUpdate(instance, updatePayload, oldProps, newProps, finishedWork) { instance.setProps(newProps, oldProps); } } const CheckBox = registerComponent(new CheckBoxConfig()); 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"; class LineEditConfig extends ComponentConfig { constructor() { super(...arguments); this.tagName = RNLineEdit.tagName; } shouldSetTextContent(nextProps) { return true; } createInstance(newProps, rootInstance, context, workInProgress) { const widget = new RNLineEdit(); widget.setProps(newProps, {}); return widget; } commitMount(instance, newProps, internalInstanceHandle) { if (newProps.visible !== false) { instance.show(); } return; } commitUpdate(instance, updatePayload, oldProps, newProps, finishedWork) { instance.setProps(newProps, oldProps); } } const LineEdit = registerComponent(new LineEditConfig()); 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"; class MenuConfig extends ComponentConfig { constructor() { super(...arguments); this.tagName = RNMenu.tagName; } shouldSetTextContent(nextProps) { return false; } createInstance(newProps, rootInstance, context, workInProgress) { const widget = new RNMenu(); widget.setProps(newProps, {}); return widget; } commitMount(instance, newProps, internalInstanceHandle) { if (newProps.visible !== false) { instance.show(); } return; } commitUpdate(instance, updatePayload, oldProps, newProps, finishedWork) { instance.setProps(newProps, oldProps); } } const Menu = registerComponent(new MenuConfig()); 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"; class MenuBarConfig extends ComponentConfig { constructor() { super(...arguments); this.tagName = RNMenuBar.tagName; } shouldSetTextContent(nextProps) { return false; } createInstance(newProps, rootInstance, context, workInProgress) { const widget = new RNMenuBar();