scoop:scoop.ui.component:0.1.0
src/overlays/modal_layout.scoop
Selected production source · src/overlays/modal_layout.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.math.min5
import scoop.math.max6
import scoop.surface.InputArea7
import scoop.ui.*9
internal class ModalTriggerLayout(scope: ComponentScope): Element<Unit>(scope) {10
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<11
Unit12
> {13
val trigger = context.measureChild(0L, constraints)14
context.placeChild(0L, Point(0.0, 0.0))15
context.measureChild(1L, Constraints())16
context.placeChild(1L, Point(0.0, 0.0))17
return LayoutResult(trigger, Unit)18
}19
public override fun scene(context: SceneContext, measured: Unit) {20
context.children()21
}22
}24
internal class ModalSurfaceProps(val input: ModalInput, val density: Double)26
/** The viewport paints the backdrop, while only the transformed panel participates in hit tests. */27
internal class ModalSurface(28
private val props: Props<ModalSurfaceProps>,29
scope: ComponentScope30
): Element<Rect>(scope) {31
private var area: InputArea? = None32
public override fun onMount() {33
val target = scope.createArea()34
area = Some(target)35
scope.routeChildren(target)36
}37
public override fun update(): UiInvalidation {38
area!!.scope.enabled = props.value.input.options.enabled39
return UiInvalidation.Layout40
}41
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<42
Rect43
> {44
val input = props.value.input45
val scale = props.value.density46
val size =47
Size(48
constraints.maxWidth ?: constraints.minWidth,49
constraints.maxHeight ?: constraints.minHeight50
)51
val padding = input.options.viewportPadding * scale52
val available =53
Size(max(0.0, size.width - padding * 2.0), max(0.0, size.height - padding * 2.0))54
val side = modalPhysicalSide(input.kind, context.layoutDirection)55
val vertical = side == Some(OverlaySide.Left) || side == Some(OverlaySide.Right)56
val sheet = panelPresent(side)57
val widthHint =58
when (input.width) {59
case Some(val value) -> Some(value * scale);60
case None -> None61
}62
val heightHint =63
when (input.height) {64
case Some(val value) -> Some(value * scale);65
case None -> None66
}67
val desiredWidth =68
if (sheet && !vertical)69
available.width70
else71
if (vertical)72
widthHint ?: min(384.0 * scale, available.width * 0.75)73
else74
(input.width ?: 384.0) * scale75
val width = min(available.width, desiredWidth)76
val fixedHeight =77
if (vertical)78
Some(available.height)79
else80
if (sheet)81
heightHint82
else83
None84
val height = min(available.height, fixedHeight ?: available.height)85
val panel =86
context.measureChild(87
0L,88
Constraints(89
width,90
Some(width),91
if (panelPresent(fixedHeight))92
height93
else94
0.0,95
Some(height)96
)97
)98
val x =99
when (side) {100
case Some(OverlaySide.Left) -> min(padding, size.width)101
case Some(OverlaySide.Right) -> max(0.0, size.width - padding - panel.size.width)102
else -> max(0.0, (size.width - panel.size.width) / 2.0)103
}104
val y =105
when (side) {106
case Some(OverlaySide.Top) -> min(padding, size.height)107
case Some(OverlaySide.Bottom) -> max(0.0, size.height - padding - panel.size.height)108
else -> max(0.0, (size.height - panel.size.height) / 2.0)109
}110
val bounds = Rect(x, y, panel.size.width, panel.size.height)111
context.placeChild(0L, Point(x, y))112
return LayoutResult(LayoutMetrics(size), bounds)113
}114
public override fun scene(context: SceneContext, bounds: Rect) {115
val input = props.value.input116
val progress = context.overlayLayout!!.presence.progress117
val viewport = Rect(0.0, 0.0, context.size.width, context.size.height)118
context.saveLayer(progress)119
when (input.options.backdrop) {120
case Some(val value) -> context.draw { paint -> paint.canvas.drawRect(viewport, value) }121
case None -> {}122
}123
context.restore()124
context.saveLayer(progress)125
val side = modalPhysicalSide(input.kind, context.layoutDirection)126
val distance = 40.0 * props.value.density * (1.0 - progress)127
when (side) {128
case Some(OverlaySide.Top) -> context.translate(0.0, -distance)129
case Some(OverlaySide.Bottom) -> context.translate(0.0, distance)130
case Some(OverlaySide.Left) -> context.translate(-distance, 0.0)131
case Some(OverlaySide.Right) -> context.translate(distance, 0.0)132
else -> {133
val center = Point(bounds.x + bounds.width / 2.0, bounds.y + bounds.height / 2.0)134
context.translate(center.x, center.y)135
val zoom = 0.95 + 0.05 * progress136
context.scale(zoom, zoom)137
context.translate(-center.x, -center.y)138
}139
}140
context.place(area!!, bounds)141
context.children()142
context.restore()143
}144
}146
internal fun modalPhysicalSide(kind: ModalKind, direction: LayoutDirection): OverlaySide? =147
when (kind) {148
case Drawer(val props, _) -> Some(drawerPhysicalSide(props.side, direction))149
case Sheet(val side) -> Some(150
when (side) {151
case Top -> OverlaySide.Top152
case Bottom -> OverlaySide.Bottom153
case Start -> if (direction == LayoutDirection.Rtl)154
OverlaySide.Right155
else156
OverlaySide.Left157
case End -> if (direction == LayoutDirection.Rtl)158
OverlaySide.Left159
else160
OverlaySide.Right161
}162
)163
else -> None164
}166
internal class ModalPanelProps(val density: Double, val header: Boolean, val contentPadding: Double)168
/** Header, body, footer and close keep stable slots across every size and visibility change. */169
internal class ModalPanel(170
private val props: Props<ModalPanelProps>,171
scope: ComponentScope172
): Element<Unit>(scope) {173
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<174
Unit175
> {176
val input = props.value177
val padding = input.contentPadding * input.density178
val width = constraints.maxWidth ?: constraints.minWidth179
val maximum = constraints.maxHeight ?: constraints.minHeight180
val inner = max(0.0, width - padding * 2.0)181
val close =182
context.measureChild(3L, Constraints(maxWidth = Some(width), maxHeight = Some(maximum)))183
val footer =184
context.measureChild(2L, Constraints(width, Some(width), maxHeight = Some(maximum)))185
val headerHeight = max(0.0, maximum - footer.size.height - padding * 2.0)186
val headerWidth = max(0.0, inner - close.size.width)187
val header =188
context.measureChild(189
0L,190
Constraints(maxWidth = Some(headerWidth), maxHeight = Some(headerHeight))191
)192
val headerExtent =193
if (input.header)194
padding + header.size.height195
else196
0.0197
val bodyHeight = max(0.0, maximum - headerExtent - footer.size.height)198
val body =199
context.measureChild(1L, Constraints(width, Some(width), maxHeight = Some(bodyHeight)))200
val naturalHeight = headerExtent + body.size.height + footer.size.height201
val size =202
constraints.constrain(Size(width, max(naturalHeight, close.size.height + padding)))203
val rtl = context.layoutDirection == LayoutDirection.Rtl204
context.placeChild(205
0L,206
Point(207
if (rtl)208
width - padding - header.size.width209
else210
padding,211
min(padding, size.height)212
)213
)214
context.placeChild(1L, Point(0.0, min(size.height, headerExtent)))215
context.placeChild(2L, Point(0.0, max(0.0, size.height - footer.size.height)))216
val inset = 8.0 * input.density217
context.placeChild(218
3L,219
Point(220
if (rtl)221
min(inset, width)222
else223
max(0.0, width - inset - close.size.width),224
min(inset, size.height)225
)226
)227
return LayoutResult(LayoutMetrics(size), Unit)228
}229
public override fun scene(context: SceneContext, measured: Unit) {230
containerScene(context, true)231
}232
}