scoop:scoop.ui.component:0.1.0
src/overlays/popover_layout.scoop
Selected production source · src/overlays/popover_layout.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.surface.InputArea5
import scoop.ui.*7
/** The portal is a zero-sized sibling of the persistent trigger. */8
internal class PopoverLayout(private val anchor: LayoutAnchor, scope: ComponentScope): Element<9
Unit10
>(scope) {11
public override fun onMount() {12
scope.layoutAnchor(anchor)13
}14
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<15
Unit16
> {17
val trigger = context.measureChild(0L, constraints)18
context.placeChild(0L, Point(0.0, 0.0))19
context.measureChild(1L, Constraints())20
context.placeChild(1L, Point(0.0, 0.0))21
return LayoutResult(trigger, Unit)22
}23
public override fun scene(context: SceneContext, measured: Unit) {24
context.anchor(anchor, Rect(0.0, 0.0, context.size.width, context.size.height))25
context.children()26
}27
}29
internal class PopupMotionProps(30
val enabled: Boolean,31
val distance: Double,32
val arrow: PopupArrowInk? = None,33
val minimumScale: Double = 0.9534
)36
/** One published transform supplies drawing, hit testing and editor caret geometry. */37
internal class PopupMotion(38
private val props: Props<PopupMotionProps>,39
scope: ComponentScope40
): Element<Unit>(scope) {41
private var area: InputArea? = None42
private var arrow: Path? = None43
public override fun onMount() {44
val value = scope.createArea()45
area = Some(value)46
scope.routeChildren(value)47
arrow = Some(scope.resource { popupArrowPath() })48
}49
public override fun update(): UiInvalidation {50
area!!.scope.enabled = props.value.enabled51
return UiInvalidation.Layout52
}53
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<54
Unit55
> {56
val result = context.measureChild(0L, constraints)57
context.placeChild(0L, Point(0.0, 0.0))58
return LayoutResult(result, Unit)59
}60
public override fun scene(context: SceneContext, measured: Unit) {61
val layout = context.overlayLayout!!62
val progress = layout.presence.progress63
val size = context.size64
val offset = props.value.distance * (1.0 - progress)65
val side = layout.side ?: OverlaySide.Bottom66
val pivot =67
when (side) {68
case Top -> Point(size.width / 2.0, size.height)69
case Bottom -> Point(size.width / 2.0, 0.0)70
case Left -> Point(size.width, size.height / 2.0)71
else -> Point(0.0, size.height / 2.0)72
}73
context.saveLayer(progress)74
when (side) {75
case Top -> context.translate(0.0, offset)76
case Bottom -> context.translate(0.0, -offset)77
case Left -> context.translate(offset, 0.0)78
else -> context.translate(-offset, 0.0)79
}80
context.translate(pivot.x, pivot.y)81
val zoom = props.value.minimumScale + (1.0 - props.value.minimumScale) * progress82
context.scale(zoom, zoom)83
context.translate(-pivot.x, -pivot.y)84
context.place(area!!, Rect(0.0, 0.0, size.width, size.height))85
context.children()86
when (props.value.arrow) {87
case Some(val ink) -> drawPopupArrow(context, arrow!!, ink)88
case None -> {}89
}90
context.restore()91
}92
}