Scoop API

scoop:scoop.ui.component:0.1.0

src/select/viewport.scoop

Selected production source · src/select/viewport.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.surface.*
5import scoop.ui.*
6import scoop.ui.style.*
8internal class SelectViewportLayout(
9 val scroll: ScrollLayout,
10 val reference: Point,
11 val padding: Double,
12 val arrows: Double
15/** List clipping and scroll reveal use the same viewport that publishes the painted offset. */
16internal class SelectViewport(
17 private val props: Props<SelectPanelProps>,
18 scope: ComponentScope
19): Element<SelectViewportLayout>(scope) {
20 private var owner: ComponentScope? = None
21 private var control: ScrollControl? = None
22 private var up: SelectScrollButton? = None
23 private var down: SelectScrollButton? = None
24 private var reference: Point? = None
25 private var opened = true
26 public override fun onMount() {
27 owner = Some(scope)
28 control =
29 Some(
30 ScrollControl(scope) {
31 val input = props.value
32 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.theme
43 )
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.value
51 if (current && !opened)
52 reference = None
53 opened = current
54 control!!.update();
55 up!!.update();
56 down!!.update()
57 return UiInvalidation.Layout
58 }
59 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
60 SelectViewportLayout
61 > {
62 val input = props.value
63 val density = input.theme.tokens.typography.body.fontSize / 14.0
64 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 * density
69 )
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 -> None
78 }
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 else
93 0.0
94 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 } else
107 None
108 case None -> None
109 }
110 val presented = control!!.layout(viewport, child.size, adjustment)
111 val initial = reference ?: presented.snapshot.offset
112 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.height
121 )
122 )
123 case None -> None
124 }
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.snapshot
132 val initial = layout.reference
133 val padding = layout.padding
134 val arrows = layout.arrows
135 context.onFrameComplete {
136 frame -> if (
137 frame.submission ==
138 FrameSubmission.Accepted &&
139 owner!!.isMounted &&
140 reference ==
141 None
142 )
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 else
154 None,
155 presented
156 )
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 arrows
166 )
167 )
168 else
169 None,
170 presented
171 )
172 }