scoop:scoop.ui.component:0.1.0
src/toast/surface.scoop
Selected production source · src/toast/surface.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.math.*5
import scoop.surface.*6
import scoop.ui.*8
/** Each card shares its transform between pixels, hit testing and focus geometry. */9
internal class ToastSurface(10
private val props: Props<ToastItemInput>,11
scope: ComponentScope12
): Element<ToastGeometry>(scope) {13
private val control = ToastCardControl(scope, { props.value })14
public override fun update(): UiInvalidation {15
control.update()16
return UiInvalidation.Layout17
}18
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<19
ToastGeometry20
> {21
val input = props.value;22
val d = input.density23
val viewport =24
Size(25
constraints.maxWidth ?: constraints.minWidth,26
constraints.maxHeight ?: constraints.minHeight27
)28
val pad = input.props.offset * d29
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 = 0L35
var before = 0.036
for (entry in entries) {37
if (entry === input.entry)38
break39
depth += 1L40
before += entry.height + input.props.gap * d41
}42
val expanded = input.expanded43
val frontHeight =44
if (entries.size > 0L && entries[0L].height > 0.0)45
entries[0L].height46
else47
natural.size.height48
val height =49
if (expanded || depth == 0L)50
natural.size.height51
else52
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.position57
val side = toastSide(position, context.layoutDirection)58
val center = position == ToastPosition.TopCenter || position == ToastPosition.BottomCenter59
val x =60
if (center)61
(viewport.width - width) / 2.062
else63
if (side == OverlaySide.Left)64
min(pad, viewport.width)65
else66
max(0.0, viewport.width - pad - width)67
val distance =68
if (expanded)69
before70
else71
depth.toDouble() * 12.0 * d72
val y =73
if (top)74
min(pad, viewport.height) + distance75
else76
viewport.height - pad - height - distance77
val zoom =78
if (expanded)79
1.080
else81
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 = control92
val presence = context.overlayLayout!!.presence93
val sampled = panelPresent(card.geometry)94
val origin =95
if (sampled)96
card.origin.value97
else98
Point(shape.bounds.x, shape.bounds.y)99
val zoom =100
if (sampled)101
card.zoom.value102
else103
shape.zoom104
val shift = card.offset.value105
val swiping = input.entry.closing.value == Some(ToastCloseReason.Swipe)106
val exitVertical = !swiping || shape.vertical107
val outward =108
if (swiping)109
shape.outward110
else111
if (toastTop(input.position))112
-1.0113
else114
1.0115
val extent =116
if (exitVertical)117
shape.bounds.height118
else119
shape.bounds.width120
val travel =121
(extent + input.props.offset * input.density) *122
1.5 *123
(1.0 - presence.progress) *124
outward125
val x =126
origin.x +127
(128
if (!shape.vertical)129
shift130
else131
0.0132
) +133
(134
if (!exitVertical)135
travel136
else137
0.0138
)139
val y =140
origin.y +141
(142
if (shape.vertical)143
shift144
else145
0.0146
) +147
(148
if (exitVertical)149
travel150
else151
0.0152
)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.0160
else161
shape.bounds.height162
)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
}178
}