Scoop API

scoop:scoop.ui.component:0.1.0

src/drawer/surface.scoop

Selected production source · src/drawer/surface.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.math.*
5import scoop.surface.*
6import scoop.ui.*
7import scoop.ui.style.*
9internal class DrawerSurface(private val props: Props<DrawerInput>, scope: ComponentScope): Element<
10 DrawerGeometry
11>(scope) {
12 private var control: DrawerControl? = None
13 private var painter: StylePainter? = None
14 public override fun onMount() {
15 val current = DrawerControl(scope, { props.value })
16 control = Some(current);
17 props.value.owner.control = Some(current)
18 painter = Some(scope.resource { StylePainter() })
19 }
20 public override fun update(): UiInvalidation {
21 control!!.update()
22 return UiInvalidation.Layout
23 }
24 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
25 DrawerGeometry
26 > {
27 val input = props.value
28 val scale = input.density
29 val size =
30 Size(
31 constraints.maxWidth ?: constraints.minWidth,
32 constraints.maxHeight ?: constraints.minHeight
33 )
34 val padding = input.props.options.viewportPadding * scale
35 val available =
36 Size(max(0.0, size.width - padding * 2.0), max(0.0, size.height - padding * 2.0))
37 val side = drawerPhysicalSide(input.props.side, context.layoutDirection)
38 val vertical = side == OverlaySide.Top || side == OverlaySide.Bottom
39 val width =
40 if (vertical)
41 available.width
42 else
43 min(
44 available.width,
45 (input.props.width ?: min(384.0, available.width * 0.75 / scale)) * scale
46 )
47 val maximum =
48 if (vertical)
49 min(
50 available.height,
51 when (input.props.height) {
52 case Some(val value) -> value * scale
53 case None -> max(0.0, available.height - input.props.viewportGap * scale)
54 }
55 )
56 else
57 available.height
58 val fixed = !vertical || input.props.height != None || input.props.snapPoints.size > 0L
59 val panel =
60 context.measureChild(
61 0L,
62 Constraints(
63 width,
64 Some(width),
65 if (fixed)
66 maximum
67 else
68 0.0,
69 Some(maximum)
70 )
71 )
72 val x =
73 when (side) {
74 case Left -> min(padding, size.width)
75 case Right -> max(0.0, size.width - padding - panel.size.width)
76 else -> max(0.0, (size.width - panel.size.width) / 2.0)
77 }
78 val y =
79 when (side) {
80 case Top -> min(padding, size.height)
81 case Bottom -> max(0.0, size.height - padding - panel.size.height)
82 else -> max(0.0, (size.height - panel.size.height) / 2.0)
83 }
84 val extent =
85 if (vertical)
86 panel.size.height
87 else
88 panel.size.width
89 val geometry =
90 DrawerGeometry(
91 size,
92 Rect(x, y, panel.size.width, panel.size.height),
93 side,
94 drawerSnaps(input.props.snapPoints, extent, scale)
95 )
96 context.placeChild(0L, Point(x, y))
97 return LayoutResult(LayoutMetrics(size), geometry)
98 }
99 public override fun scene(context: SceneContext, shape: DrawerGeometry) {
100 val input = props.value
101 val current = control!!
102 val progress = context.overlayLayout!!.presence.progress
103 val visible = current.visualPosition(shape)
104 val anchor = current.anchor(shape)
105 val dragOpacity =
106 if (anchor > 0.0)
107 min(1.0, max(0.0, visible / anchor))
108 else
109 1.0
110 if (input.props.mode == OverlayMode.Modal) {
111 context.saveLayer(progress * dragOpacity)
112 when (input.props.options.backdrop) {
113 case Some(val paint) -> context.draw {
114 output -> output.canvas.drawRect(
115 Rect(0.0, 0.0, shape.viewport.width, shape.viewport.height),
116 paint
117 )
118 }
119 case None -> {}
120 }
121 context.restore()
122 }
123 painter!!.scene(context, input.props.viewportStyle.resolve(style())) {
124 local ->
125 local.save()
126 val amount = drawerStackAmount(current)
127 val nestedVisible = visible + (shape.extent - visible) * min(1.0, amount)
128 val distance = (shape.extent - nestedVisible * progress) * shape.outward
129 local.translate(
130 if (shape.vertical)
131 0.0
132 else
133 distance,
134 if (shape.vertical)
135 distance
136 else
137 0.0
138 )
139 if (amount > 0.0 && shape.extent > 0.0) {
140 val zoom = max(0.5, 1.0 - amount * 0.05)
141 val target =
142 shape.extent +
143 (
144 (
145 if (current.frontExtent > 0.0)
146 current.frontExtent
147 else
148 shape.extent
149 ) -
150 shape.extent
151 ) *
152 min(1.0, amount)
153 val mainScale = target / shape.extent * zoom
154 val bounds = shape.bounds
155 val anchorX =
156 when (shape.side) {
157 case Left -> bounds.x;
158 case Right -> bounds.x + bounds.width;
159 else -> bounds.x + bounds.width / 2.0
160 }
161 val anchorY =
162 when (shape.side) {
163 case Top -> bounds.y;
164 case Bottom -> bounds.y + bounds.height;
165 else -> bounds.y + bounds.height / 2.0
166 }
167 val peek = (16.0 * input.density * amount + target * (1.0 - zoom)) * -shape.outward
168 local.translate(
169 anchorX +
170 if (shape.vertical)
171 0.0
172 else
173 peek,
174 anchorY +
175 if (shape.vertical)
176 peek
177 else
178 0.0
179 )
180 local.scale(
181 if (shape.vertical)
182 zoom
183 else
184 mainScale,
185 if (shape.vertical)
186 mainScale
187 else
188 zoom
189 )
190 local.translate(-anchorX, -anchorY)
191 }
192 if (shape.bounds.width > 0.0 && shape.bounds.height > 0.0)
193 local.place(current.area, shape.bounds)
194 local.children()
195 if (amount > 0.0)
196 local.draw {
197 output -> output.canvas.drawRoundRect(
198 shape.bounds,
199 12.0 * input.density,
200 12.0 * input.density,
201 Paint(Color.Black.withAlpha(min(0.3, amount * 0.05)))
202 )
203 }
204 local.restore()
205 }
206 context.onFrameComplete {
207 frame -> if (frame.submission == FrameSubmission.Accepted)
208 current.accept(shape, progress)
209 }
210 }
211 public override fun onUnmount() {
212 control!!.retire()
213 }
216internal class DrawerBodyMarker(
217 private val props: Props<DrawerOwner>,
218 scope: ComponentScope
219): Element<Unit>(scope) {
220 private var area: InputArea? = None
221 public override fun onMount() {
222 val current = scope.createArea();
223 area = Some(current)
224 props.value.bodyArea = Some(current);
225 scope.routeChildren(current)
226 scope.resource {
227 current.scope.addDefaultAction {
228 context,
229 event -> when (event) {
230 case Contact(InputContactEvent.Began(_)) -> {
231 val control = props.value.control ?: return@addDefaultAction
232 if (control.active && control.read().props.dragThreshold == 0.0)
233 context.consume()
234 }
235 else -> {}
236 }
237 }
238 }
239 }
240 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
241 Unit
242 > {
243 val child = context.measureChild(0L, constraints);
244 context.placeChild(0L, Point(0.0, 0.0));
245 return LayoutResult(child, Unit)
246 }
247 public override fun scene(context: SceneContext, measured: Unit) {
248 context.place(area!!, Rect(0.0, 0.0, context.size.width, context.size.height));
249 context.children()
250 }
251 public override fun onUnmount() {
252 props.value.bodyArea = None
253 }