Scoop API

scoop:scoop.ui.component:0.1.0

src/overlays/modal_layout.scoop

Selected production source · src/overlays/modal_layout.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.math.min
5import scoop.math.max
6import scoop.surface.InputArea
7import scoop.ui.*
9internal class ModalTriggerLayout(scope: ComponentScope): Element<Unit>(scope) {
10 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
11 Unit
12 > {
13 val trigger = context.measureChild(0L, constraints)
14 context.placeChild(0L, Point(0.0, 0.0))
15 context.measureChild(1L, Constraints())
16 context.placeChild(1L, Point(0.0, 0.0))
17 return LayoutResult(trigger, Unit)
18 }
19 public override fun scene(context: SceneContext, measured: Unit) {
20 context.children()
21 }
24internal class ModalSurfaceProps(val input: ModalInput, val density: Double)
26/** The viewport paints the backdrop, while only the transformed panel participates in hit tests. */
27internal class ModalSurface(
28 private val props: Props<ModalSurfaceProps>,
29 scope: ComponentScope
30): Element<Rect>(scope) {
31 private var area: InputArea? = None
32 public override fun onMount() {
33 val target = scope.createArea()
34 area = Some(target)
35 scope.routeChildren(target)
36 }
37 public override fun update(): UiInvalidation {
38 area!!.scope.enabled = props.value.input.options.enabled
39 return UiInvalidation.Layout
40 }
41 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
42 Rect
43 > {
44 val input = props.value.input
45 val scale = props.value.density
46 val size =
47 Size(
48 constraints.maxWidth ?: constraints.minWidth,
49 constraints.maxHeight ?: constraints.minHeight
50 )
51 val padding = input.options.viewportPadding * scale
52 val available =
53 Size(max(0.0, size.width - padding * 2.0), max(0.0, size.height - padding * 2.0))
54 val side = modalPhysicalSide(input.kind, context.layoutDirection)
55 val vertical = side == Some(OverlaySide.Left) || side == Some(OverlaySide.Right)
56 val sheet = panelPresent(side)
57 val widthHint =
58 when (input.width) {
59 case Some(val value) -> Some(value * scale);
60 case None -> None
61 }
62 val heightHint =
63 when (input.height) {
64 case Some(val value) -> Some(value * scale);
65 case None -> None
66 }
67 val desiredWidth =
68 if (sheet && !vertical)
69 available.width
70 else
71 if (vertical)
72 widthHint ?: min(384.0 * scale, available.width * 0.75)
73 else
74 (input.width ?: 384.0) * scale
75 val width = min(available.width, desiredWidth)
76 val fixedHeight =
77 if (vertical)
78 Some(available.height)
79 else
80 if (sheet)
81 heightHint
82 else
83 None
84 val height = min(available.height, fixedHeight ?: available.height)
85 val panel =
86 context.measureChild(
87 0L,
88 Constraints(
89 width,
90 Some(width),
91 if (panelPresent(fixedHeight))
92 height
93 else
94 0.0,
95 Some(height)
96 )
97 )
98 val x =
99 when (side) {
100 case Some(OverlaySide.Left) -> min(padding, size.width)
101 case Some(OverlaySide.Right) -> max(0.0, size.width - padding - panel.size.width)
102 else -> max(0.0, (size.width - panel.size.width) / 2.0)
103 }
104 val y =
105 when (side) {
106 case Some(OverlaySide.Top) -> min(padding, size.height)
107 case Some(OverlaySide.Bottom) -> max(0.0, size.height - padding - panel.size.height)
108 else -> max(0.0, (size.height - panel.size.height) / 2.0)
109 }
110 val bounds = Rect(x, y, panel.size.width, panel.size.height)
111 context.placeChild(0L, Point(x, y))
112 return LayoutResult(LayoutMetrics(size), bounds)
113 }
114 public override fun scene(context: SceneContext, bounds: Rect) {
115 val input = props.value.input
116 val progress = context.overlayLayout!!.presence.progress
117 val viewport = Rect(0.0, 0.0, context.size.width, context.size.height)
118 context.saveLayer(progress)
119 when (input.options.backdrop) {
120 case Some(val value) -> context.draw { paint -> paint.canvas.drawRect(viewport, value) }
121 case None -> {}
122 }
123 context.restore()
124 context.saveLayer(progress)
125 val side = modalPhysicalSide(input.kind, context.layoutDirection)
126 val distance = 40.0 * props.value.density * (1.0 - progress)
127 when (side) {
128 case Some(OverlaySide.Top) -> context.translate(0.0, -distance)
129 case Some(OverlaySide.Bottom) -> context.translate(0.0, distance)
130 case Some(OverlaySide.Left) -> context.translate(-distance, 0.0)
131 case Some(OverlaySide.Right) -> context.translate(distance, 0.0)
132 else -> {
133 val center = Point(bounds.x + bounds.width / 2.0, bounds.y + bounds.height / 2.0)
134 context.translate(center.x, center.y)
135 val zoom = 0.95 + 0.05 * progress
136 context.scale(zoom, zoom)
137 context.translate(-center.x, -center.y)
138 }
139 }
140 context.place(area!!, bounds)
141 context.children()
142 context.restore()
143 }
146internal fun modalPhysicalSide(kind: ModalKind, direction: LayoutDirection): OverlaySide? =
147 when (kind) {
148 case Drawer(val props, _) -> Some(drawerPhysicalSide(props.side, direction))
149 case Sheet(val side) -> Some(
150 when (side) {
151 case Top -> OverlaySide.Top
152 case Bottom -> OverlaySide.Bottom
153 case Start -> if (direction == LayoutDirection.Rtl)
154 OverlaySide.Right
155 else
156 OverlaySide.Left
157 case End -> if (direction == LayoutDirection.Rtl)
158 OverlaySide.Left
159 else
160 OverlaySide.Right
161 }
162 )
163 else -> None
164 }
166internal class ModalPanelProps(val density: Double, val header: Boolean, val contentPadding: Double)
168/** Header, body, footer and close keep stable slots across every size and visibility change. */
169internal class ModalPanel(
170 private val props: Props<ModalPanelProps>,
171 scope: ComponentScope
172): Element<Unit>(scope) {
173 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
174 Unit
175 > {
176 val input = props.value
177 val padding = input.contentPadding * input.density
178 val width = constraints.maxWidth ?: constraints.minWidth
179 val maximum = constraints.maxHeight ?: constraints.minHeight
180 val inner = max(0.0, width - padding * 2.0)
181 val close =
182 context.measureChild(3L, Constraints(maxWidth = Some(width), maxHeight = Some(maximum)))
183 val footer =
184 context.measureChild(2L, Constraints(width, Some(width), maxHeight = Some(maximum)))
185 val headerHeight = max(0.0, maximum - footer.size.height - padding * 2.0)
186 val headerWidth = max(0.0, inner - close.size.width)
187 val header =
188 context.measureChild(
189 0L,
190 Constraints(maxWidth = Some(headerWidth), maxHeight = Some(headerHeight))
191 )
192 val headerExtent =
193 if (input.header)
194 padding + header.size.height
195 else
196 0.0
197 val bodyHeight = max(0.0, maximum - headerExtent - footer.size.height)
198 val body =
199 context.measureChild(1L, Constraints(width, Some(width), maxHeight = Some(bodyHeight)))
200 val naturalHeight = headerExtent + body.size.height + footer.size.height
201 val size =
202 constraints.constrain(Size(width, max(naturalHeight, close.size.height + padding)))
203 val rtl = context.layoutDirection == LayoutDirection.Rtl
204 context.placeChild(
205 0L,
206 Point(
207 if (rtl)
208 width - padding - header.size.width
209 else
210 padding,
211 min(padding, size.height)
212 )
213 )
214 context.placeChild(1L, Point(0.0, min(size.height, headerExtent)))
215 context.placeChild(2L, Point(0.0, max(0.0, size.height - footer.size.height)))
216 val inset = 8.0 * input.density
217 context.placeChild(
218 3L,
219 Point(
220 if (rtl)
221 min(inset, width)
222 else
223 max(0.0, width - inset - close.size.width),
224 min(inset, size.height)
225 )
226 )
227 return LayoutResult(LayoutMetrics(size), Unit)
228 }
229 public override fun scene(context: SceneContext, measured: Unit) {
230 containerScene(context, true)
231 }