-
-
Notifications
You must be signed in to change notification settings - Fork 173
/
Copy pathRNBoxView.ts
107 lines (92 loc) · 2.44 KB
/
RNBoxView.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import {
QWidget,
QBoxLayoutSignals,
QBoxLayout,
Direction,
QLayout,
QObjectSignals,
} from "@nodegui/nodegui";
import { ViewProps, setViewProps } from "../View/RNView";
import { RNComponent } from "../config";
import { QDialog } from "@nodegui/nodegui/dist/lib/QtWidgets/QDialog";
export interface BoxViewProps extends ViewProps<QBoxLayoutSignals> {
direction?: Direction;
}
const setBoxViewProps = (
widget: RNBoxView,
newProps: BoxViewProps,
oldProps: BoxViewProps
) => {
const setter: BoxViewProps = {
set direction(direction: Direction) {
widget.layout()?.setDirection(direction);
},
};
Object.assign(setter, newProps);
setViewProps(widget, newProps, oldProps);
};
/**
* @ignore
*/
export class RNBoxView extends QWidget implements RNComponent {
native: any;
initialProps?: BoxViewProps;
_children: Array<QWidget<any>> = [];
layout(): QBoxLayout | null {
return super.layout() as any;
}
setLayout(layout: QBoxLayout): void {
super.setLayout(layout);
}
setProps(newProps: BoxViewProps, oldProps: BoxViewProps): void {
if (this.layout()) {
setBoxViewProps(this, newProps, oldProps);
} else {
this.initialProps = newProps;
}
}
appendInitialChild(child: QWidget<any>): void {
this.appendChild(child);
}
appendChild(child: QWidget<any>): void {
if (child instanceof QDialog) {
return;
}
const updateChild = () => {
this.layout()?.addWidget(child);
this._children.push(child);
};
if (this.layout()) {
updateChild();
return;
}
const layout = new QBoxLayout(Direction.LeftToRight);
this.setLayout(layout);
// Newly created layout, so set initial props
if (this.initialProps) {
setBoxViewProps(this, this.initialProps, {});
}
updateChild();
}
insertBefore(child: QWidget<any>, beforeChild: QWidget<any>): void {
if (child instanceof QDialog) {
return;
}
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);
this.layout()?.insertWidget(prevIndex, child);
}
removeChild(child: QWidget<any>): void {
const prevIndex = this._children.indexOf(child);
if (prevIndex !== -1) {
this._children.splice(prevIndex, 1);
}
child.close();
}
static tagName: string = "boxview";
}