Scoop API

scoop:scoop.ui.component:0.1.0

src/toast/region.scoop

Selected production source · src/toast/region.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.math.*
5import scoop.surface.*
6import scoop.ui.*
8internal class ToastRegionInput(val host: ToasterControl, val position: ToastPosition)
10internal class ToastRegion(val area: InputArea, val position: ToastPosition)
12/** A transparent region below the cards keeps hover continuous across expanded gaps. */
13internal class ToastRegionSurface(
14 private val props: Props<ToastRegionInput>,
15 scope: ComponentScope
16): Element<Unit>(scope) {
17 private val host = props.value.host
18 private val area =
19 scope.createArea {
20 context,
21 _ -> if (context.phase == RoutedPhase.Notification && host.active)
22 host.refresh()
23 }
24 private val region = ToastRegion(area, props.value.position)
25 init {
26 scope.onDispose { remove() }
27 host.regions.add(region)
28 }
29 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
30 Unit
31 > =
32 LayoutResult(
33 LayoutMetrics(
34 Size(
35 constraints.maxWidth ?: constraints.minWidth,
36 constraints.maxHeight ?: constraints.minHeight
37 )
38 ),
39 Unit
40 )
41 public override fun scene(context: SceneContext, layout: Unit) {
42 val input = props.value
43 var bounds: Rect? = None
44 for (entry in input.host.group(input.position)) {
45 val card = entry.card ?: continue
46 val box = card.area.visibleBounds ?: continue
47 bounds =
48 Some(
49 when (bounds) {
50 case None -> box
51 case Some(val old) -> {
52 val x = min(old.x, box.x);
53 val y = min(old.y, box.y)
54 Rect(
55 x,
56 y,
57 max(old.x + old.width, box.x + box.width) - x,
58 max(old.y + old.height, box.y + box.height) - y
59 )
60 }
61 }
62 )
63 }
64 when (bounds) {
65 case Some(val box) -> context.place(region.area, box);
66 case None -> {}
67 }
68 }
69 private fun remove() {
70 for (at in 0L ..< host.regions.size)
71 if (host.regions[at] === region) {
72 host.regions.removeAt(at);
73 break
74 }
75 host.refresh()
76 }
79internal fun toastRegion(host: ToasterControl, position: ToastPosition, key: Long): View =
80 portal(
81 binding({ host.group(position).size > 0L }, { _ -> Unit }),
82 element(
83 ToastRegionInput(host, position),
84 { value, mount -> ToastRegionSurface(value, mount) }
85 )
86 ) {
87 this.key = ViewKey.Number(key)
88 placement = OverlayPlacement.Viewport(viewportPadding = 0.0)
89 }