scoop:scoop.ui.component:0.1.0
src/layout/frame.scoop
Selected production source · src/layout/frame.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.ui.*5
import scoop.ui.style.*7
public class FrameProps public constructor(8
public val content: View,9
public val width: Double? = None,10
public val height: Double? = None,11
public val padding: Insets? = None,12
public val alignment: Alignment = Alignment(),13
public val style: StyleOverride = styleOverride(),14
public val surface: SurfaceKind? = None,15
public val theme: ThemeSource? = None,16
public val logicalPadding: LogicalInsets? = None,17
public val maxWidth: Double? = None,18
public val maxHeight: Double? = None19
) {20
init {21
when (width) {22
case Some(val value) -> nonnegative(value, "invalid frame width");23
case None -> {}24
}25
when (height) {26
case Some(val value) -> nonnegative(value, "invalid frame height");27
case None -> {}28
}29
nonnegative(maxWidth ?: 0.0, "invalid frame maximum width")30
nonnegative(maxHeight ?: 0.0, "invalid frame maximum height")31
padding?.totals()32
logicalPadding?.resolve(LayoutDirection.Ltr)33
}34
}36
public class Frame public constructor(37
private val props: Props<FrameProps>,38
scope: ComponentScope39
): Component(scope) {40
private val themes = ThemeAccess(scope)41
public override fun onMount() {42
when (props.value.surface) {43
case Some(_) -> themes.mount(props.value.theme);44
case None -> {}45
}46
}47
public override fun render(): View {48
val value = props.value49
val theme =50
when (value.surface) {51
case Some(_) -> Some(themes.read(value.theme));52
case None -> None53
}54
return element(55
FrameInput(value, theme),56
{ current, mount -> FrameInk(current, scope = mount) },57
[value.content]58
)59
}60
}62
public fun frame(content: View, configure: FrameBuilder.() -> Unit = {}): View =63
makeFrame(content, configure)65
private class FrameInput(val value: FrameProps, val theme: Theme?)67
private struct FrameLayoutInputs(68
val width: Double?,69
val height: Double?,70
val maxWidth: Double?,71
val maxHeight: Double?,72
val insets: Insets,73
val minimum: Size,74
val suggested: Size,75
val alignment: Alignment76
)78
private fun frameOptions(input: FrameInput, direction: LayoutDirection): SurfaceOptions =79
surfaceOptions {80
this.padding =81
when (input.value.logicalPadding) {82
case Some(val value) -> Some(value.resolve(direction))83
case None -> input.value.padding84
}85
this.style = input.value.style86
}88
private fun frameLayoutInputs(input: FrameInput, direction: LayoutDirection): FrameLayoutInputs {89
val options = frameOptions(input, direction)90
val metrics =91
when (input.theme) {92
case Some(val theme) -> Some(93
theme.surfaceMetrics(94
SurfaceVisual(input.value.surface ?: SurfaceKind.Plain),95
options,96
direction97
)98
)99
case None -> None100
}101
return FrameLayoutInputs(102
input.value.width,103
input.value.height,104
input.value.maxWidth,105
input.value.maxHeight,106
metrics?.contentInsets ?: options.padding ?: Insets(),107
metrics?.minimumSize ?: Size(0.0, 0.0),108
metrics?.suggestedSize ?: Size(0.0, 0.0),109
input.value.alignment110
)111
}113
private class FrameInk(private val props: Props<FrameInput>, scope: ComponentScope): Element<Unit>(114
scope115
) {116
private var painters: ThemePainterCache? = None117
private var style: StylePainter? = None118
private var previous: FrameLayoutInputs? = None119
private var previousTheme: Theme? = None120
public override fun onMount() {121
painters = Some(scope.resource { ThemePainterCache() })122
style = Some(scope.resource { StylePainter() })123
}124
public override fun update(): UiInvalidation {125
val input = props.value126
when (input.theme) {127
case Some(val theme) -> painters!!.get(theme)128
case None -> painters!!.clear()129
}130
val next = frameLayoutInputs(input, scope.layoutDirection)131
val themeChanged =132
when (input.theme) {133
case Some(val theme) -> !sameTheme(previousTheme, theme)134
case None -> panelPresent(previousTheme)135
}136
val changed = previous != Some(next) || themeChanged137
previous = Some(next)138
previousTheme = input.theme139
return if (changed)140
UiInvalidation.Layout141
else142
UiInvalidation.Scene143
}144
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<145
Unit146
> {147
val input = props.value148
val value = input.value149
val metrics = frameLayoutInputs(input, context.layoutDirection)150
val total = metrics.insets.totals()151
val child =152
context.measureChild(153
0L,154
frameChildConstraints(155
constraints,156
value.width,157
value.height,158
total,159
value.maxWidth,160
value.maxHeight161
)162
)163
val natural = Size(child.size.width + total.width, child.size.height + total.height)164
validSize(natural)165
val size =166
constraints.constrain(167
Size(168
frameExtent(169
value.width ?:170
lowerBound(171
natural.width,172
lowerBound(metrics.minimum.width, metrics.suggested.width)173
),174
value.maxWidth175
),176
frameExtent(177
value.height ?:178
lowerBound(179
natural.height,180
lowerBound(metrics.minimum.height, metrics.suggested.height)181
),182
value.maxHeight183
)184
)185
)186
val bounds = Rect(0.0, 0.0, size.width, size.height)187
val content =188
when (input.theme) {189
case Some(val theme) -> theme.surfaceGeometry(190
bounds,191
SurfaceVisual(value.surface ?: SurfaceKind.Plain),192
frameOptions(input, context.layoutDirection),193
context.layoutDirection194
).content195
case None -> Rect(196
metrics.insets.left,197
metrics.insets.top,198
lowerBound(size.width - total.width, 0.0),199
lowerBound(size.height - total.height, 0.0)200
)201
}202
val origin = alignedOrigin(content, child.size, value.alignment, context.layoutDirection)203
context.placeChild(0L, origin)204
return LayoutResult(offsetMetrics(child, size, origin.y), Unit)205
}206
public override fun scene(context: SceneContext, measured: Unit) {207
val input = props.value208
when (input.theme) {209
case None -> style!!.scene(context, input.value.style.resolve(style())) {210
content -> content.children()211
}212
case Some(val theme) -> {213
val visual = SurfaceVisual(input.value.surface ?: SurfaceKind.Plain)214
val geometry =215
theme.surfaceGeometry(216
Rect(0.0, 0.0, context.size.width, context.size.height),217
visual,218
frameOptions(input, context.layoutDirection),219
context.layoutDirection220
)221
val painter = painters!!.get(theme)222
if (geometry.opacity < 1.0)223
context.saveLayer(geometry.opacity)224
context.draw {225
paint -> painter.drawSurface(226
paint.canvas,227
geometry,228
visual,229
SurfaceLayer.Background230
)231
}232
style!!.scene(context, geometry.bounds, geometry.contentStyle) {233
content -> content.children()234
}235
context.draw {236
paint -> painter.drawSurface(237
paint.canvas,238
geometry,239
visual,240
SurfaceLayer.Foreground241
)242
}243
if (geometry.opacity < 1.0)244
context.restore()245
}246
}247
}248
}