scoop:scoop.ui.component:0.1.0
src/select/viewport.scoop
Selected production source · src/select/viewport.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.surface.*5
import scoop.ui.*6
import scoop.ui.style.*8
internal class SelectViewportLayout(9
val scroll: ScrollLayout,10
val reference: Point,11
val padding: Double,12
val arrows: Double13
)15
/** List clipping and scroll reveal use the same viewport that publishes the painted offset. */16
internal class SelectViewport(17
private val props: Props<SelectPanelProps>,18
scope: ComponentScope19
): Element<SelectViewportLayout>(scope) {20
private var owner: ComponentScope? = None21
private var control: ScrollControl? = None22
private var up: SelectScrollButton? = None23
private var down: SelectScrollButton? = None24
private var reference: Point? = None25
private var opened = true26
public override fun onMount() {27
owner = Some(scope)28
control =29
Some(30
ScrollControl(scope) {31
val input = props.value32
ScrollControlProps(33
input.popup.scroll,34
ScrollAxes.Vertical,35
28.0 * input.theme.tokens.typography.body.fontSize / 14.0,36
ScrollChain.Contain,37
ScrollbarVisibility.Hidden,38
scrollbarOptions(),39
input.popup.options.enabled,40
FocusOptions(tabStop = false),41
PaintCachePolicy.None,42
input.theme43
)44
}45
)46
up = Some(SelectScrollButton(scope, { props.value }, false))47
down = Some(SelectScrollButton(scope, { props.value }, true))48
}49
public override fun update(): UiInvalidation {50
val current = props.value.popup.open.value51
if (current && !opened)52
reference = None53
opened = current54
control!!.update();55
up!!.update();56
down!!.update()57
return UiInvalidation.Layout58
}59
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<60
SelectViewportLayout61
> {62
val input = props.value63
val density = input.theme.tokens.typography.body.fontSize / 14.064
val padding = upperBound(4.0 * density, (constraints.maxWidth ?: 8.0 * density) / 2.0)65
val height =66
upperBound(67
input.popup.maxHeight * density,68
constraints.maxHeight ?: input.popup.maxHeight * density69
)70
val child =71
context.measureChild(72
0L,73
Constraints(74
lowerBound(0.0, constraints.minWidth - padding * 2.0),75
when (constraints.maxWidth) {76
case Some(val width) -> Some(lowerBound(0.0, width - padding * 2.0));77
case None -> None78
}79
)80
)81
val total =82
constraints.constrain(83
Size(84
child.size.width + padding * 2.0,85
upperBound(height, child.size.height + padding * 2.0)86
)87
)88
val room = lowerBound(0.0, total.height - padding * 2.0)89
val arrows =90
if (input.popup.scrollButtons && child.size.height > room)91
upperBound(24.0 * density, room / 2.0)92
else93
0.094
val viewport =95
Size(lowerBound(0.0, total.width - padding * 2.0), lowerBound(0.0, room - arrows * 2.0))96
val adjustment =97
when (child.overlayAnchor) {98
case Some(val rect) -> if (reference == None) {99
val initial = ScrollSnapshot(Point(0.0, 0.0), viewport, child.size)100
Some(101
ScrollAdjustment(102
input.popup.scroll.requestRevision,103
initial.revealedOffset(rect, vertical = ScrollAlignment.Center)104
)105
)106
} else107
None108
case None -> None109
}110
val presented = control!!.layout(viewport, child.size, adjustment)111
val initial = reference ?: presented.snapshot.offset112
context.placeChild(0L, Point(0.0, 0.0))113
val anchor =114
when (child.overlayAnchor) {115
case Some(val rect) -> Some(116
Rect(117
rect.x + padding,118
rect.y + padding + arrows - initial.y,119
rect.width,120
rect.height121
)122
)123
case None -> None124
}125
return LayoutResult(126
LayoutMetrics(total, overlayAnchor = anchor),127
SelectViewportLayout(presented, initial, padding, arrows)128
)129
}130
public override fun scene(context: SceneContext, layout: SelectViewportLayout) {131
val presented = layout.scroll.snapshot132
val initial = layout.reference133
val padding = layout.padding134
val arrows = layout.arrows135
context.onFrameComplete {136
frame -> if (137
frame.submission ==138
FrameSubmission.Accepted &&139
owner!!.isMounted &&140
reference ==141
None142
)143
reference = Some(initial)144
}145
context.save();146
context.translate(padding, padding + arrows)147
control!!.scene(context, layout.scroll)148
context.restore()149
up!!.scene(150
context,151
if (arrows > 0.0)152
Some(Rect(padding, padding, presented.viewport.width, arrows))153
else154
None,155
presented156
)157
down!!.scene(158
context,159
if (arrows > 0.0)160
Some(161
Rect(162
padding,163
padding + arrows + presented.viewport.height,164
presented.viewport.width,165
arrows166
)167
)168
else169
None,170
presented171
)172
}173
}