scoop:scoop.ui.component:0.1.0
src/toast/region.scoop
Selected production source · src/toast/region.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.math.*5
import scoop.surface.*6
import scoop.ui.*8
internal class ToastRegionInput(val host: ToasterControl, val position: ToastPosition)10
internal class ToastRegion(val area: InputArea, val position: ToastPosition)12
/** A transparent region below the cards keeps hover continuous across expanded gaps. */13
internal class ToastRegionSurface(14
private val props: Props<ToastRegionInput>,15
scope: ComponentScope16
): Element<Unit>(scope) {17
private val host = props.value.host18
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
Unit31
> =32
LayoutResult(33
LayoutMetrics(34
Size(35
constraints.maxWidth ?: constraints.minWidth,36
constraints.maxHeight ?: constraints.minHeight37
)38
),39
Unit40
)41
public override fun scene(context: SceneContext, layout: Unit) {42
val input = props.value43
var bounds: Rect? = None44
for (entry in input.host.group(input.position)) {45
val card = entry.card ?: continue46
val box = card.area.visibleBounds ?: continue47
bounds =48
Some(49
when (bounds) {50
case None -> box51
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) - y59
)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
break74
}75
host.refresh()76
}77
}79
internal 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
}