Scoop API

scoop:scoop.ui:0.1.0

src/overlay/placement.scoop

Selected production source · src/overlay/placement.scoop

1package scoop.ui
3import scoop.graphics.Rect
4import scoop.graphics.Size
6/** One bounded measurement followed by a pure placement decision. */
7internal class OverlayMeasurePlan(
8 val constraints: Constraints,
9 private val available: Rect,
10 private val placement: OverlayPlanPlacement
11) {
12 fun layout(
13 size: Size,
14 presence: PresenceSnapshot = PresenceSnapshot(PresencePhase.Present, 1.0),
15 contentAnchor: Rect? = None
16 ): OverlayLayout =
17 when (placement) {
18 case Viewport(val horizontal, val vertical) -> OverlayLayout(
19 Rect(
20 aligned(available.x, available.width, size.width, horizontal),
21 aligned(available.y, available.height, size.height, vertical),
22 size.width,
23 size.height
24 ),
25 available,
26 None,
27 None,
28 presence
29 )
30 case Anchored(val anchor, val side, val alignment, val preferred, val opposite, val contentAligned) -> {
31 if (contentAligned && contentAnchor != None) {
32 val local = contentAnchor!!
33 OverlayLayout(
34 Rect(
35 clampPosition(
36 aligned(anchor.x, anchor.width, local.width, alignment) - local.x,
37 available.x,
38 available.width,
39 size.width
40 ),
41 clampPosition(
42 anchor.y + anchor.height / 2.0 - local.y - local.height / 2.0,
43 available.y,
44 available.height,
45 size.height
46 ),
47 size.width,
48 size.height
49 ),
50 available,
51 Some(anchor),
52 None,
53 presence
54 )
55 } else {
56 val vertical = verticalSide(side)
57 val extent =
58 if (vertical)
59 size.height
60 else
61 size.width
62 val first =
63 if (vertical)
64 preferred.height
65 else
66 preferred.width
67 val second =
68 if (vertical)
69 opposite.height
70 else
71 opposite.width
72 val keep = extent <= first || (extent > second && first >= second)
73 val actual =
74 if (keep)
75 side
76 else
77 reverseSide(side)
78 val space =
79 if (keep)
80 preferred
81 else
82 opposite
83 val x =
84 when (actual) {
85 case Left -> space.x + space.width - size.width
86 case Right -> space.x
87 else -> clampPosition(
88 aligned(anchor.x, anchor.width, size.width, alignment),
89 available.x,
90 available.width,
91 size.width
92 )
93 }
94 val y =
95 when (actual) {
96 case Top -> space.y + space.height - size.height
97 case Bottom -> space.y
98 else -> clampPosition(
99 aligned(anchor.y, anchor.height, size.height, alignment),
100 available.y,
101 available.height,
102 size.height
103 )
104 }
105 OverlayLayout(
106 Rect(x, y, size.width, size.height),
107 space,
108 Some(anchor),
109 Some(actual.publicSide()),
110 presence
111 )
112 }
113 }
114 }
117internal enum OverlayPlanPlacement {
118 Viewport(OverlayAlignment, OverlayAlignment),
119 Anchored(Rect, PhysicalOverlaySide, OverlayAlignment, Rect, Rect, Boolean)
122internal fun planOverlay(
123 placement: OverlayPlacement,
124 viewport: Rect,
125 anchor: Rect?,
126 direction: LayoutDirection = LayoutDirection.Ltr
127): OverlayMeasurePlan {
128 checkLayoutSize(Size(viewport.width, viewport.height))
129 return when (placement) {
130 case Viewport(val horizontal, val vertical, val padding) -> {
131 val available = insetViewport(viewport, padding)
132 OverlayMeasurePlan(
133 Constraints(maxWidth = Some(available.width), maxHeight = Some(available.height)),
134 available,
135 OverlayPlanPlacement.Viewport(overlayHorizontal(horizontal, direction), vertical)
136 )
137 }
138 case Anchored(_, val side, val alignment, val gap, val padding, val matchWidth) -> pointOverlayPlan(
139 anchor ?: throw IllegalStateException(Some("anchor has no current geometry")),
140 side,
141 alignment,
142 gap,
143 padding,
144 matchWidth,
145 viewport,
146 direction
147 )
148 case ContentAligned(_, val side, val alignment, val gap, val padding, val matchWidth) -> pointOverlayPlan(
149 anchor ?: throw IllegalStateException(Some("anchor has no current geometry")),
150 side,
151 alignment,
152 gap,
153 padding,
154 matchWidth,
155 viewport,
156 direction,
157 true
158 )
159 case AtPoint(val point, val side, val alignment, val gap, val padding) -> pointOverlayPlan(
160 Rect(point.x, point.y, 0.0, 0.0),
161 side,
162 alignment,
163 gap,
164 padding,
165 false,
166 viewport,
167 direction
168 )
169 }
172private fun pointOverlayPlan(
173 bounds: Rect,
174 side: OverlaySide,
175 alignment: OverlayAlignment,
176 gap: Double,
177 padding: Double,
178 matchWidth: Boolean,
179 viewport: Rect,
180 direction: LayoutDirection,
181 contentAligned: Boolean = false
182): OverlayMeasurePlan {
183 val physical = physicalOverlaySide(side, direction)
184 requireUi(
185 (bounds.x + bounds.width).isFinite() && (bounds.y + bounds.height).isFinite(),
186 "anchor bounds overflow"
187 )
188 val available = insetViewport(viewport, padding)
189 val preferred = sideSpace(available, bounds, physical, gap)
190 val opposite = sideSpace(available, bounds, reverseSide(physical), gap)
191 val width = maximum(preferred.width, opposite.width)
192 val height = maximum(preferred.height, opposite.height)
193 val matched = minimum(bounds.width, width)
194 return OverlayMeasurePlan(
195 Constraints(
196 minWidth =
197 if (matchWidth)
198 matched
199 else
200 0.0,
201 maxWidth =
202 Some(
203 if (matchWidth)
204 matched
205 else
206 width
207 ),
208 maxHeight = Some(height)
209 ),
210 available,
211 OverlayPlanPlacement.Anchored(
212 bounds,
213 physical,
214 if (contentAligned || verticalSide(physical))
215 overlayHorizontal(alignment, direction)
216 else
217 alignment,
218 preferred,
219 opposite,
220 contentAligned
221 )
222 )
225private fun minimum(a: Double, b: Double): Double =
226 if (a < b)
227 a
228 else
229 b
231private fun maximum(a: Double, b: Double): Double =
232 if (a > b)
233 a
234 else
235 b
237private fun clamp(value: Double, lower: Double, upper: Double): Double =
238 minimum(maximum(value, lower), upper)
240private fun insetViewport(size: Rect, padding: Double): Rect {
241 val x = minimum(padding, size.width / 2.0)
242 val y = minimum(padding, size.height / 2.0)
243 return Rect(size.x + x, size.y + y, size.width - x * 2.0, size.height - y * 2.0)
246private fun aligned(
247 origin: Double,
248 available: Double,
249 extent: Double,
250 alignment: OverlayAlignment
251): Double =
252 when (alignment) {
253 case Start -> origin
254 case Center -> origin + available / 2.0 - extent / 2.0
255 case End -> origin + available - extent
256 }
258private fun clampPosition(
259 value: Double,
260 origin: Double,
261 available: Double,
262 extent: Double
263): Double = clamp(value, origin, origin + available - extent)
265private fun verticalSide(side: PhysicalOverlaySide): Boolean =
266 side == PhysicalOverlaySide.Top || side == PhysicalOverlaySide.Bottom
268private fun reverseSide(side: PhysicalOverlaySide): PhysicalOverlaySide =
269 when (side) {
270 case Top -> PhysicalOverlaySide.Bottom
271 case Right -> PhysicalOverlaySide.Left
272 case Bottom -> PhysicalOverlaySide.Top
273 case Left -> PhysicalOverlaySide.Right
274 }
276private fun sideSpace(viewport: Rect, anchor: Rect, side: PhysicalOverlaySide, gap: Double): Rect {
277 val right = viewport.x + viewport.width
278 val bottom = viewport.y + viewport.height
279 return when (side) {
280 case Top -> Rect(
281 viewport.x,
282 viewport.y,
283 viewport.width,
284 clamp(anchor.y - gap, viewport.y, bottom) - viewport.y
285 )
286 case Bottom -> {
287 val y = clamp(anchor.y + anchor.height + gap, viewport.y, bottom)
288 Rect(viewport.x, y, viewport.width, bottom - y)
289 }
290 case Left -> Rect(
291 viewport.x,
292 viewport.y,
293 clamp(anchor.x - gap, viewport.x, right) - viewport.x,
294 viewport.height
295 )
296 case Right -> {
297 val x = clamp(anchor.x + anchor.width + gap, viewport.x, right)
298 Rect(x, viewport.y, right - x, viewport.height)
299 }
300 }