|
| 1 | +import { FlexLayout, FocusReason, NodeWidget, QDialog, QDialogSignals, QFont } from "@nodegui/nodegui"; |
| 2 | +import { NodeDialog } from "@nodegui/nodegui/dist/lib/QtWidgets/QDialog"; |
| 3 | +import { RNWidget } from "../config"; |
| 4 | +import { setViewProps, ViewProps } from "../View/RNView"; |
| 5 | + |
| 6 | +export interface DialogProps<T = QDialogSignals> extends ViewProps<T> { |
| 7 | + open?: boolean; |
| 8 | + font?: QFont; |
| 9 | + focus?: FocusReason; |
| 10 | + modal?: boolean; |
| 11 | + result?: number; |
| 12 | + reject?: boolean; |
| 13 | + enableSizeGrip?: boolean; |
| 14 | +} |
| 15 | + |
| 16 | +export function setDialogProps(widget: RNDialog, newProps: DialogProps, oldProps: DialogProps) { |
| 17 | + const setter: DialogProps = { |
| 18 | + set open(open: boolean) { |
| 19 | + open ? widget.open() : widget.close(); |
| 20 | + }, |
| 21 | + set font(font: QFont) { |
| 22 | + widget.setFont(font); |
| 23 | + }, |
| 24 | + set focus(focus: FocusReason) { |
| 25 | + widget.setFocus(focus); |
| 26 | + }, |
| 27 | + set modal(modal: boolean) { |
| 28 | + widget.setModal(modal); |
| 29 | + }, |
| 30 | + set reject(reject: boolean) { |
| 31 | + reject && widget.reject(); |
| 32 | + }, |
| 33 | + set result(result: number) { |
| 34 | + widget.setResult(result); |
| 35 | + }, |
| 36 | + set enableSizeGrip(sizeGrip: boolean) { |
| 37 | + widget.setSizeGripEnabled(sizeGrip); |
| 38 | + }, |
| 39 | + }; |
| 40 | + Object.assign(setter, newProps); |
| 41 | + setViewProps(widget, newProps, oldProps); |
| 42 | +} |
| 43 | + |
| 44 | +export class RNDialog extends QDialog implements RNWidget { |
| 45 | + setProps(newProps: DialogProps, oldProps: DialogProps): void { |
| 46 | + setDialogProps(this, newProps, oldProps); |
| 47 | + } |
| 48 | + appendInitialChild(child: NodeWidget<any>): void { |
| 49 | + this.appendChild(child); |
| 50 | + } |
| 51 | + appendChild(child: NodeWidget<any>): void { |
| 52 | + if (!child || child instanceof NodeDialog) { |
| 53 | + return; |
| 54 | + } |
| 55 | + if (!this.layout) { |
| 56 | + const flexLayout = new FlexLayout(); |
| 57 | + flexLayout.setFlexNode(this.getFlexNode()); |
| 58 | + this.setLayout(flexLayout); |
| 59 | + this.layout = flexLayout; |
| 60 | + } |
| 61 | + this.layout.addWidget(child); |
| 62 | + } |
| 63 | + insertBefore(child: NodeWidget<any>, beforeChild: NodeWidget<any>): void { |
| 64 | + if (child! instanceof NodeDialog) { |
| 65 | + this.appendChild(child); |
| 66 | + } |
| 67 | + } |
| 68 | + removeChild(child: NodeWidget<any>): void { |
| 69 | + if (!this.layout) { |
| 70 | + console.warn("parent has no layout to remove child from"); |
| 71 | + return; |
| 72 | + } |
| 73 | + this.layout.removeWidget(child); |
| 74 | + child.close(); |
| 75 | + } |
| 76 | + static tagName = "dialog"; |
| 77 | +} |
0 commit comments