scoop:scoop.ui.component:0.1.0
src/drawer/surface.scoop
Selected production source · src/drawer/surface.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.math.*5
import scoop.surface.*6
import scoop.ui.*7
import scoop.ui.style.*9
internal class DrawerSurface(private val props: Props<DrawerInput>, scope: ComponentScope): Element<10
DrawerGeometry11
>(scope) {12
private var control: DrawerControl? = None13
private var painter: StylePainter? = None14
public override fun onMount() {15
val current = DrawerControl(scope, { props.value })16
control = Some(current);17
props.value.owner.control = Some(current)18
painter = Some(scope.resource { StylePainter() })19
}20
public override fun update(): UiInvalidation {21
control!!.update()22
return UiInvalidation.Layout23
}24
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<25
DrawerGeometry26
> {27
val input = props.value28
val scale = input.density29
val size =30
Size(31
constraints.maxWidth ?: constraints.minWidth,32
constraints.maxHeight ?: constraints.minHeight33
)34
val padding = input.props.options.viewportPadding * scale35
val available =36
Size(max(0.0, size.width - padding * 2.0), max(0.0, size.height - padding * 2.0))37
val side = drawerPhysicalSide(input.props.side, context.layoutDirection)38
val vertical = side == OverlaySide.Top || side == OverlaySide.Bottom39
val width =40
if (vertical)41
available.width42
else43
min(44
available.width,45
(input.props.width ?: min(384.0, available.width * 0.75 / scale)) * scale46
)47
val maximum =48
if (vertical)49
min(50
available.height,51
when (input.props.height) {52
case Some(val value) -> value * scale53
case None -> max(0.0, available.height - input.props.viewportGap * scale)54
}55
)56
else57
available.height58
val fixed = !vertical || input.props.height != None || input.props.snapPoints.size > 0L59
val panel =60
context.measureChild(61
0L,62
Constraints(63
width,64
Some(width),65
if (fixed)66
maximum67
else68
0.0,69
Some(maximum)70
)71
)72
val x =73
when (side) {74
case Left -> min(padding, size.width)75
case Right -> max(0.0, size.width - padding - panel.size.width)76
else -> max(0.0, (size.width - panel.size.width) / 2.0)77
}78
val y =79
when (side) {80
case Top -> min(padding, size.height)81
case Bottom -> max(0.0, size.height - padding - panel.size.height)82
else -> max(0.0, (size.height - panel.size.height) / 2.0)83
}84
val extent =85
if (vertical)86
panel.size.height87
else88
panel.size.width89
val geometry =90
DrawerGeometry(91
size,92
Rect(x, y, panel.size.width, panel.size.height),93
side,94
drawerSnaps(input.props.snapPoints, extent, scale)95
)96
context.placeChild(0L, Point(x, y))97
return LayoutResult(LayoutMetrics(size), geometry)98
}99
public override fun scene(context: SceneContext, shape: DrawerGeometry) {100
val input = props.value101
val current = control!!102
val progress = context.overlayLayout!!.presence.progress103
val visible = current.visualPosition(shape)104
val anchor = current.anchor(shape)105
val dragOpacity =106
if (anchor > 0.0)107
min(1.0, max(0.0, visible / anchor))108
else109
1.0110
if (input.props.mode == OverlayMode.Modal) {111
context.saveLayer(progress * dragOpacity)112
when (input.props.options.backdrop) {113
case Some(val paint) -> context.draw {114
output -> output.canvas.drawRect(115
Rect(0.0, 0.0, shape.viewport.width, shape.viewport.height),116
paint117
)118
}119
case None -> {}120
}121
context.restore()122
}123
painter!!.scene(context, input.props.viewportStyle.resolve(style())) {124
local ->125
local.save()126
val amount = drawerStackAmount(current)127
val nestedVisible = visible + (shape.extent - visible) * min(1.0, amount)128
val distance = (shape.extent - nestedVisible * progress) * shape.outward129
local.translate(130
if (shape.vertical)131
0.0132
else133
distance,134
if (shape.vertical)135
distance136
else137
0.0138
)139
if (amount > 0.0 && shape.extent > 0.0) {140
val zoom = max(0.5, 1.0 - amount * 0.05)141
val target =142
shape.extent +143
(144
(145
if (current.frontExtent > 0.0)146
current.frontExtent147
else148
shape.extent149
) -150
shape.extent151
) *152
min(1.0, amount)153
val mainScale = target / shape.extent * zoom154
val bounds = shape.bounds155
val anchorX =156
when (shape.side) {157
case Left -> bounds.x;158
case Right -> bounds.x + bounds.width;159
else -> bounds.x + bounds.width / 2.0160
}161
val anchorY =162
when (shape.side) {163
case Top -> bounds.y;164
case Bottom -> bounds.y + bounds.height;165
else -> bounds.y + bounds.height / 2.0166
}167
val peek = (16.0 * input.density * amount + target * (1.0 - zoom)) * -shape.outward168
local.translate(169
anchorX +170
if (shape.vertical)171
0.0172
else173
peek,174
anchorY +175
if (shape.vertical)176
peek177
else178
0.0179
)180
local.scale(181
if (shape.vertical)182
zoom183
else184
mainScale,185
if (shape.vertical)186
mainScale187
else188
zoom189
)190
local.translate(-anchorX, -anchorY)191
}192
if (shape.bounds.width > 0.0 && shape.bounds.height > 0.0)193
local.place(current.area, shape.bounds)194
local.children()195
if (amount > 0.0)196
local.draw {197
output -> output.canvas.drawRoundRect(198
shape.bounds,199
12.0 * input.density,200
12.0 * input.density,201
Paint(Color.Black.withAlpha(min(0.3, amount * 0.05)))202
)203
}204
local.restore()205
}206
context.onFrameComplete {207
frame -> if (frame.submission == FrameSubmission.Accepted)208
current.accept(shape, progress)209
}210
}211
public override fun onUnmount() {212
control!!.retire()213
}214
}216
internal class DrawerBodyMarker(217
private val props: Props<DrawerOwner>,218
scope: ComponentScope219
): Element<Unit>(scope) {220
private var area: InputArea? = None221
public override fun onMount() {222
val current = scope.createArea();223
area = Some(current)224
props.value.bodyArea = Some(current);225
scope.routeChildren(current)226
scope.resource {227
current.scope.addDefaultAction {228
context,229
event -> when (event) {230
case Contact(InputContactEvent.Began(_)) -> {231
val control = props.value.control ?: return@addDefaultAction232
if (control.active && control.read().props.dragThreshold == 0.0)233
context.consume()234
}235
else -> {}236
}237
}238
}239
}240
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<241
Unit242
> {243
val child = context.measureChild(0L, constraints);244
context.placeChild(0L, Point(0.0, 0.0));245
return LayoutResult(child, Unit)246
}247
public override fun scene(context: SceneContext, measured: Unit) {248
context.place(area!!, Rect(0.0, 0.0, context.size.width, context.size.height));249
context.children()250
}251
public override fun onUnmount() {252
props.value.bodyArea = None253
}254
}