Scoop API

scoop:scoop.ui.component:0.1.0

src/toast/surface.scoop

Selected production source · src/toast/surface.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.math.*
5import scoop.surface.*
6import scoop.ui.*
8/** Each card shares its transform between pixels, hit testing and focus geometry. */
9internal class ToastSurface(
10 private val props: Props<ToastItemInput>,
11 scope: ComponentScope
12): Element<ToastGeometry>(scope) {
13 private val control = ToastCardControl(scope, { props.value })
14 public override fun update(): UiInvalidation {
15 control.update()
16 return UiInvalidation.Layout
17 }
18 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
19 ToastGeometry
20 > {
21 val input = props.value;
22 val d = input.density
23 val viewport =
24 Size(
25 constraints.maxWidth ?: constraints.minWidth,
26 constraints.maxHeight ?: constraints.minHeight
27 )
28 val pad = input.props.offset * d
29 val width = min(input.props.width * d, max(0.0, viewport.width - pad * 2.0))
30 val maxHeight = max(0.0, viewport.height - pad * 2.0)
31 val natural =
32 context.measureChild(0L, Constraints(width, Some(width), maxHeight = Some(maxHeight)))
33 val entries = input.host.group(input.position)
34 var depth = 0L
35 var before = 0.0
36 for (entry in entries) {
37 if (entry === input.entry)
38 break
39 depth += 1L
40 before += entry.height + input.props.gap * d
41 }
42 val expanded = input.expanded
43 val frontHeight =
44 if (entries.size > 0L && entries[0L].height > 0.0)
45 entries[0L].height
46 else
47 natural.size.height
48 val height =
49 if (expanded || depth == 0L)
50 natural.size.height
51 else
52 min(maxHeight, frontHeight)
53 if (height != natural.size.height)
54 context.measureChild(0L, Constraints(width, Some(width), height, Some(height)))
55 val top = toastTop(input.position)
56 val position = input.position
57 val side = toastSide(position, context.layoutDirection)
58 val center = position == ToastPosition.TopCenter || position == ToastPosition.BottomCenter
59 val x =
60 if (center)
61 (viewport.width - width) / 2.0
62 else
63 if (side == OverlaySide.Left)
64 min(pad, viewport.width)
65 else
66 max(0.0, viewport.width - pad - width)
67 val distance =
68 if (expanded)
69 before
70 else
71 depth.toDouble() * 12.0 * d
72 val y =
73 if (top)
74 min(pad, viewport.height) + distance
75 else
76 viewport.height - pad - height - distance
77 val zoom =
78 if (expanded)
79 1.0
80 else
81 max(0.1, 1.0 - depth.toDouble() * 0.1)
82 val geometry =
83 ToastGeometry(viewport, Rect(x, y, width, height), natural.size.height, zoom, side)
84 if (width > 0.0 && height > 0.0)
85 context.placeChild(0L, Point(0.0, 0.0))
86 return LayoutResult(LayoutMetrics(viewport), geometry)
87 }
88 public override fun scene(context: SceneContext, layout: ToastGeometry) {
89 val input = props.value;
90 val shape = layout;
91 val card = control
92 val presence = context.overlayLayout!!.presence
93 val sampled = panelPresent(card.geometry)
94 val origin =
95 if (sampled)
96 card.origin.value
97 else
98 Point(shape.bounds.x, shape.bounds.y)
99 val zoom =
100 if (sampled)
101 card.zoom.value
102 else
103 shape.zoom
104 val shift = card.offset.value
105 val swiping = input.entry.closing.value == Some(ToastCloseReason.Swipe)
106 val exitVertical = !swiping || shape.vertical
107 val outward =
108 if (swiping)
109 shape.outward
110 else
111 if (toastTop(input.position))
112 -1.0
113 else
114 1.0
115 val extent =
116 if (exitVertical)
117 shape.bounds.height
118 else
119 shape.bounds.width
120 val travel =
121 (extent + input.props.offset * input.density) *
122 1.5 *
123 (1.0 - presence.progress) *
124 outward
125 val x =
126 origin.x +
127 (
128 if (!shape.vertical)
129 shift
130 else
131 0.0
132 ) +
133 (
134 if (!exitVertical)
135 travel
136 else
137 0.0
138 )
139 val y =
140 origin.y +
141 (
142 if (shape.vertical)
143 shift
144 else
145 0.0
146 ) +
147 (
148 if (exitVertical)
149 travel
150 else
151 0.0
152 )
153 context.saveLayer(presence.progress)
154 context.translate(x, y)
155 val pivot =
156 Point(
157 shape.bounds.width / 2.0,
158 if (toastTop(input.position))
159 0.0
160 else
161 shape.bounds.height
162 )
163 context.translate(pivot.x, pivot.y);
164 context.scale(zoom, zoom);
165 context.translate(-pivot.x, -pivot.y)
166 if (shape.bounds.width > 0.0 && shape.bounds.height > 0.0)
167 context.place(card.area, Rect(0.0, 0.0, shape.bounds.width, shape.bounds.height))
168 context.children()
169 context.restore()
170 context.onFrameComplete {
171 frame -> if (frame.submission == FrameSubmission.Accepted)
172 card.accept(shape, presence.phase)
173 }
174 }
175 public override fun onUnmount() {
176 control.retire()
177 }