Scoop API

scoop:scoop.ui.component:0.1.0

src/resizable/component.scoop

Selected production source · src/resizable/component.scoop

1package scoop.ui.component
3import scoop.collections.HashMap
4import scoop.graphics.*
5import scoop.surface.*
6import scoop.ui.*
7import scoop.ui.style.*
9internal class ResizableOwner {
10 var control: ResizableControl? = None
13internal class ResizableInput(
14 val props: ResizableProps,
15 val values: Array<ResizableSize>,
16 val model: HashMap<ViewKey, ResizableSize>,
17 val theme: Theme,
18 val owner: ResizableOwner
21internal class ResizableGeometry(
22 val size: Size,
23 val target: Double,
24 val lengths: Array<Double>,
25 val input: ResizableProps,
26 val direction: LayoutDirection
29public class Resizable public constructor(
30 private val props: Props<ResizableProps>,
31 scope: ComponentScope
32): Component(scope) {
33 private val themes = ThemeAccess(scope)
34 private val owner = ResizableOwner()
35 private var checked: Array<ResizableSize>? = None
36 private var model = HashMap<ViewKey, ResizableSize>()
37 public override fun onMount() {
38 themes.mount(props.value.theme)
39 }
40 public override fun render(): View {
41 val input = props.value
42 val values = input.sizes.value
43 val same =
44 when (checked) {
45 case Some(val previous) -> previous === values;
46 case None -> false
47 }
48 if (!same) {
49 model = resizableValues(values)
50 checked = Some(values)
51 }
52 val config = ResizableInput(input, values, model, themes.read(input.theme), owner)
53 val children =
54 Array<View>(input.panels.size) {
55 index ->
56 val panel = input.panels[index]
57 val part = ResizablePanelInput(config, index)
58 element(
59 part,
60 { current, mount -> ResizablePanelLayout(current, scope = mount) },
61 [
62 element(
63 panel.style,
64 { current, mount -> ResizableContent(current, scope = mount) },
65 [panel.content],
66 key = ViewKey.Text("content")
67 ),
68 if (part.hasHandle)
69 element(
70 part,
71 { current, mount -> ResizableHandle(current, scope = mount) },
72 key = ViewKey.Text("handle")
73 )
74 else
75 empty()
76 ],
77 key = panel.key
78 )
79 }
80 return element(
81 config,
82 { current, mount -> ResizableLayout(current, scope = mount) },
83 children
84 )
85 }
88private class ResizableLayout(
89 private val props: Props<ResizableInput>,
90 scope: ComponentScope
91): Element<ResizableGeometry>(scope) {
92 private var painter: StylePainter? = None
93 public override fun onMount() {
94 props.value.owner.control = Some(ResizableControl(scope, { props.value }))
95 painter = Some(scope.resource { StylePainter() })
96 }
97 public override fun update(): UiInvalidation {
98 props.value.owner.control!!.update()
99 return UiInvalidation.Layout
100 }
101 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
102 ResizableGeometry
103 > {
104 val input = props.value
105 val horizontal = input.props.orientation == Orientation.Horizontal
106 val thickness = input.theme.tokens.typography.body.fontSize / 14.0
107 val length =
108 input.props.length ?:
109 (
110 if (horizontal)
111 constraints.maxWidth
112 else
113 constraints.maxHeight
114 ) ?:
115 320.0
116 val base = constraints.constrain(resizableSize(length, 0.0, horizontal))
117 val main =
118 if (horizontal)
119 base.width
120 else
121 base.height
122 val gap = thickness * lowerBound(0.0, (context.childCount - 1L).toDouble())
123 nonnegative(gap, "resizable separator total overflow")
124 val target = lowerBound(0.0, main - gap)
125 val sizes = resizableAllocation(input.props.panels, input.model, target)
126 val maxCross =
127 if (horizontal)
128 constraints.maxHeight
129 else
130 constraints.maxWidth
131 var cross = maxCross ?: 0.0
132 if (maxCross == None) {
133 for (index in 0L ..< context.childCount) {
134 val extent =
135 sizes[index] +
136 (
137 if (index + 1L < context.childCount)
138 thickness
139 else
140 0.0
141 )
142 val measured =
143 context.measureChild(
144 index,
145 if (horizontal)
146 Constraints(minWidth = extent, maxWidth = Some(extent))
147 else
148 Constraints(minHeight = extent, maxHeight = Some(extent))
149 )
150 cross =
151 lowerBound(
152 cross,
153 if (horizontal)
154 measured.size.height
155 else
156 measured.size.width
157 )
158 }
159 }
160 val size = constraints.constrain(resizableSize(main, cross, horizontal))
161 val actualCross =
162 if (horizontal)
163 size.height
164 else
165 size.width
166 var offset = 0.0
167 for (index in 0L ..< context.childCount) {
168 val extent =
169 sizes[index] +
170 (
171 if (index + 1L < context.childCount)
172 thickness
173 else
174 0.0
175 )
176 context.measureChild(
177 index,
178 Constraints.tight(resizableSize(extent, actualCross, horizontal))
179 )
180 context.placeChild(
181 index,
182 if (horizontal)
183 Point(logicalX(offset, extent, size.width, context.layoutDirection), 0.0)
184 else
185 Point(0.0, offset)
186 )
187 offset += extent
188 }
189 val shape = ResizableGeometry(size, target, sizes, input.props, context.layoutDirection)
190 return LayoutResult(LayoutMetrics(size), shape)
191 }
192 public override fun scene(context: SceneContext, shape: ResizableGeometry) {
193 val order = Array<Long>(shape.lengths.size) { index -> shape.lengths.size - 1L - index }
194 painter!!.scene(context, props.value.props.style.resolve(style())) {
195 local -> containerScene(local, props.value.props.clip, Some(order))
196 }
197 context.onFrameComplete {
198 result -> if (result.submission == FrameSubmission.Accepted)
199 props.value.owner.control!!.accept(shape)
200 }
201 }
202 public override fun onUnmount() {
203 props.value.owner.control!!.retire()
204 }
207internal fun resizableSize(main: Double, cross: Double, horizontal: Boolean): Size =
208 if (horizontal)
209 Size(main, cross)
210 else
211 Size(cross, main)