Scoop API

scoop:scoop.ui.component:0.1.0

src/sidebar/layout.scoop

Selected production source · src/sidebar/layout.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.math.*
5import scoop.surface.*
6import scoop.ui.*
7import scoop.ui.style.*
9internal class SidebarHostInput(
10 val props: SidebarProps,
11 val theme: Theme,
12 val shown: SidebarPresentation,
13 val setMobile: (Boolean) -> Unit,
14 val toggle: () -> Unit
17internal fun sidebarOutside(input: SidebarProps, density: Double): Double =
18 if (input.variant == SidebarVariant.Sidebar)
19 0.0
20 else
21 8.0 * density
23internal fun sidebarOnLeft(side: SidebarSide, direction: LayoutDirection): Boolean =
24 (side == SidebarSide.Start) != (direction == LayoutDirection.Rtl)
26internal class SidebarHost(
27 private val props: Props<SidebarHostInput>,
28 scope: ComponentScope
29): Element<Boolean>(scope) {
30 private var width: Animatable<Double>? = None
31 private var keys: ShortcutScope? = None
32 private var wasMobile = false
33 private fun target(): Double {
34 val input = props.value
35 val density = input.theme.tokens.typography.body.fontSize / 14.0
36 val value = sidebarWidth(input.props, input.shown) * density
37 return value +
38 if (value > 0.0)
39 sidebarOutside(input.props, density) * 2.0
40 else
41 0.0
42 }
43 public override fun onMount() {
44 width = Some(scope.animation(target()))
45 wasMobile = props.value.shown.mobile
46 keys = Some(scope.resource { ShortcutScope(scope.input) })
47 }
48 public override fun update(): UiInvalidation {
49 val input = props.value
50 sidebarKeyBindings(keys!!, input.props, input.toggle)
51 val animation = width!!
52 val next = target()
53 val motion = sidebarTransition(input.props, input.theme)
54 if (wasMobile != input.shown.mobile || !panelPresent(motion)) {
55 if (animation.value != next || animation.state == AnimationState.Running)
56 animation.snapTo(next)
57 } else
58 if (animation.target != next)
59 animation.animateTo(next, motion!!)
60 wasMobile = input.shown.mobile
61 return UiInvalidation.Layout
62 }
63 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
64 Boolean
65 > {
66 val input = props.value
67 val density = input.theme.tokens.typography.body.fontSize / 14.0
68 val nextMobile = sidebarIsMobile(input.props, constraints.maxWidth, density)
69 val occupied =
70 if (input.shown.mobile || nextMobile)
71 0.0
72 else
73 min(max(0.0, width!!.value), constraints.maxWidth ?: width!!.value)
74 val pane =
75 context.measureChild(
76 0L,
77 Constraints(occupied, Some(occupied), maxHeight = constraints.maxHeight)
78 )
79 val inset = panelPresent(input.props.inset)
80 val margins =
81 if (!input.shown.mobile && input.props.variant == SidebarVariant.Inset)
82 8.0 * density
83 else
84 0.0
85 val near =
86 if (input.shown.expanded && occupied > 0.0)
87 0.0
88 else
89 margins
90 val mainMaximum =
91 when (constraints.maxWidth) {
92 case Some(val value) -> Some(max(0.0, value - occupied - near - margins))
93 case None -> None
94 }
95 val mainHeight =
96 when (constraints.maxHeight) {
97 case Some(val value) -> Some(max(0.0, value - margins * 2.0))
98 case None -> None
99 }
100 val main =
101 if (inset)
102 context.measureChild(
103 1L,
104 Constraints(mainMaximum ?: 0.0, mainMaximum, mainHeight ?: 0.0, mainHeight)
105 )
106 else {
107 context.skipChild(1L);
108 LayoutMetrics(Size(0.0, 0.0))
109 }
110 val total =
111 occupied +
112 if (inset)
113 main.size.width + near + margins
114 else
115 0.0
116 val size =
117 constraints.constrain(
118 Size(
119 total,
120 max(
121 pane.size.height,
122 main.size.height +
123 if (inset)
124 margins * 2.0
125 else
126 0.0
127 )
128 )
129 )
130 val left = sidebarOnLeft(input.props.side, context.layoutDirection)
131 context.placeChild(
132 0L,
133 Point(
134 if (left)
135 0.0
136 else
137 max(0.0, size.width - occupied),
138 0.0
139 )
140 )
141 if (inset)
142 context.placeChild(
143 1L,
144 Point(
145 if (left)
146 occupied + near
147 else
148 margins,
149 margins
150 )
151 )
152 if (
153 input.props.rail &&
154 !input.shown.mobile &&
155 !nextMobile &&
156 input.props.collapsible !=
157 SidebarCollapse.None &&
158 size.height >
159 0.0
160 ) {
161 val rail =
162 context.measureChild(
163 2L,
164 Constraints(
165 16.0 * density,
166 Some(16.0 * density),
167 size.height,
168 Some(size.height)
169 )
170 )
171 context.placeChild(
172 2L,
173 Point(
174 (
175 if (left)
176 occupied
177 else
178 size.width - occupied
179 ) -
180 rail.size.width /
181 2.0,
182 0.0
183 )
184 )
185 } else
186 context.skipChild(2L)
187 return LayoutResult(LayoutMetrics(size), nextMobile)
188 }
189 public override fun scene(context: SceneContext, nextMobile: Boolean) {
190 context.children([1L, 0L, 2L])
191 val desired = nextMobile
192 context.onFrameComplete {
193 frame -> if (frame.submission == FrameSubmission.Accepted && scope.isMounted)
194 props.value.setMobile(desired)
195 }
196 }
199internal class SidebarPaneInput(
200 val props: SidebarProps,
201 val shown: SidebarPresentation,
202 val density: Double
205internal class SidebarPane(
206 private val props: Props<SidebarPaneInput>,
207 scope: ComponentScope
208): Element<Unit>(scope) {
209 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
210 Unit
211 > {
212 val input = props.value
213 val outside = sidebarOutside(input.props, input.density)
214 val width = constraints.maxWidth ?: constraints.minWidth
215 if (width <= 0.0) {
216 context.skipChild(0L)
217 return LayoutResult(
218 LayoutMetrics(constraints.constrain(Size(0.0, constraints.maxHeight ?: 0.0))),
219 Unit
220 )
221 }
222 val available = max(0.0, width - outside * 2.0)
223 val full =
224 if (input.props.collapsible == SidebarCollapse.Offcanvas)
225 input.props.width * input.density
226 else
227 available
228 val height =
229 when (constraints.maxHeight) {
230 case Some(val value) -> Some(max(0.0, value - outside * 2.0))
231 case None -> None
232 }
233 val child = context.measureChild(0L, Constraints(full, Some(full), height ?: 0.0, height))
234 val offset =
235 if (sidebarOnLeft(input.props.side, context.layoutDirection))
236 available - full
237 else
238 0.0
239 context.placeChild(0L, Point(outside + offset, outside))
240 return LayoutResult(
241 LayoutMetrics(constraints.constrain(Size(width, child.size.height + outside * 2.0))),
242 Unit
243 )
244 }
245 public override fun scene(context: SceneContext, measured: Unit) {
246 context.clipRect(Rect(0.0, 0.0, context.size.width, context.size.height))
247 context.children()
248 }
251internal class SidebarGateInput(val enabled: Boolean, val shown: Boolean)
253internal fun sidebarGate(content: View, enabled: Boolean, shown: Boolean): View =
254 element(
255 SidebarGateInput(enabled, shown),
256 { current, mount -> SidebarGate(current, scope = mount) },
257 [content]
258 )
260private class SidebarGate(
261 private val props: Props<SidebarGateInput>,
262 scope: ComponentScope
263): Element<Boolean>(scope) {
264 public override fun onMount() {
265 scope.input.enabled = false
266 }
267 public override fun update(): UiInvalidation {
268 val input = props.value
269 if (!input.enabled || !input.shown)
270 scope.input.enabled = false
271 scope.isFrameEnabled = input.enabled && input.shown
272 return UiInvalidation.Layout
273 }
274 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
275 Boolean
276 > {
277 if (!props.value.shown) {
278 context.skipChild(0L);
279 return LayoutResult(LayoutMetrics(constraints.constrain(Size(0.0, 0.0))), false)
280 }
281 val child = context.measureChild(0L, constraints)
282 val placed = child.size.width > 0.0 && child.size.height > 0.0
283 if (placed)
284 context.placeChild(0L, Point(0.0, 0.0))
285 return LayoutResult(child, placed)
286 }
287 public override fun scene(context: SceneContext, placed: Boolean) {
288 context.children()
289 val visible = placed
290 context.onFrameComplete {
291 frame -> if (frame.submission == FrameSubmission.Accepted && scope.isMounted)
292 scope.input.enabled = visible && props.value.enabled && props.value.shown
293 }
294 }
297internal fun sidebarKeyBindings(scope: ShortcutScope, input: SidebarProps, toggle: () -> Unit) {
298 scope.bindings =
299 when (input.shortcut) {
300 case Some(val chord) -> [
301 shortcutBinding(chord, toggle) {
302 enabled = input.enabled && input.collapsible != SidebarCollapse.None
303 }
304 ]
305 case None -> []
306 }
309private class SidebarKeyInput(val props: SidebarProps, val toggle: () -> Unit)
311internal fun sidebarKeys(content: View, input: SidebarProps, toggle: () -> Unit): View =
312 element(
313 SidebarKeyInput(input, toggle),
314 { current, mount -> SidebarKeys(current, scope = mount) },
315 [content]
316 )
318private class SidebarKeys(
319 private val props: Props<SidebarKeyInput>,
320 scope: ComponentScope
321): Element<Unit>(scope) {
322 private var keys: ShortcutScope? = None
323 public override fun onMount() {
324 keys = Some(scope.resource { ShortcutScope(scope.input) })
325 }
326 public override fun update(): UiInvalidation {
327 sidebarKeyBindings(keys!!, props.value.props, props.value.toggle)
328 return UiInvalidation.Layout
329 }
330 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
331 Unit
332 > {
333 val child = context.measureChild(0L, constraints);
334 context.placeChild(0L, Point(0.0, 0.0));
335 return LayoutResult(child, Unit)
336 }
337 public override fun scene(context: SceneContext, measured: Unit) {
338 context.children()
339 }