Scoop API

scoop:scoop.ui.component:0.1.0

src/drawer/panel.scoop

Selected production source · src/drawer/panel.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.math.*
5import scoop.surface.*
6import scoop.ui.*
7import scoop.ui.style.*
8import scoop.window.*
10internal fun drawerPanel(input: DrawerInput, content: View): View =
11 element(
12 input,
13 { current, mount -> DrawerPanel(current, scope = mount) },
14 [
15 content,
16 element(
17 input,
18 { current, mount -> DrawerSwipeHandle(current, scope = mount) },
19 [input.props.swipeHandle.content ?: empty()]
20 )
21 ]
22 )
24internal class DrawerPanel(private val props: Props<DrawerInput>, scope: ComponentScope): Element<
25 Unit
26>(scope) {
27 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
28 Unit
29 > {
30 val input = props.value
31 val side = drawerPhysicalSide(input.props.side, context.layoutDirection)
32 val vertical = side == OverlaySide.Top || side == OverlaySide.Bottom
33 if (!input.props.swipeHandle.visible) {
34 context.skipChild(1L)
35 val panel = context.measureChild(0L, constraints)
36 context.placeChild(0L, Point(0.0, 0.0));
37 return LayoutResult(panel, Unit)
38 }
39 val main =
40 if (vertical)
41 constraints.maxHeight
42 else
43 constraints.maxWidth
44 val thickness = min(12.0 * input.density, main ?: 12.0 * input.density)
45 val cross =
46 if (vertical)
47 constraints.maxWidth ?: constraints.minWidth
48 else
49 constraints.maxHeight ?: constraints.minHeight
50 context.measureChild(
51 1L,
52 Constraints.tight(
53 if (vertical)
54 Size(cross, thickness)
55 else
56 Size(thickness, cross)
57 )
58 )
59 val panel =
60 context.measureChild(
61 0L,
62 childConstraints(
63 constraints,
64 if (vertical)
65 Size(0.0, thickness)
66 else
67 Size(thickness, 0.0)
68 )
69 )
70 val size =
71 constraints.constrain(
72 Size(
73 panel.size.width +
74 if (vertical)
75 0.0
76 else
77 thickness,
78 panel.size.height +
79 if (vertical)
80 thickness
81 else
82 0.0
83 )
84 )
85 val leading = side == OverlaySide.Bottom || side == OverlaySide.Right
86 context.placeChild(
87 0L,
88 Point(
89 if (!vertical && leading)
90 thickness
91 else
92 0.0,
93 if (vertical && leading)
94 thickness
95 else
96 0.0
97 )
98 )
99 context.placeChild(
100 1L,
101 Point(
102 if (!vertical && !leading)
103 size.width - thickness
104 else
105 0.0,
106 if (vertical && !leading)
107 size.height - thickness
108 else
109 0.0
110 )
111 )
112 return LayoutResult(LayoutMetrics(size), Unit)
113 }
114 public override fun scene(context: SceneContext, measured: Unit) {
115 context.children()
116 }
119internal class DrawerSwipeHandle(
120 private val props: Props<DrawerInput>,
121 scope: ComponentScope
122): Element<Unit>(scope) {
123 private var area: InputArea? = None
124 private var focus: FocusTarget? = None
125 private var painter: ThemePainterCache? = None
126 public override fun onMount() {
127 val current =
128 scope.createArea(focusable = true) {
129 context,
130 _ -> if (context.phase == RoutedPhase.Notification && scope.isMounted)
131 scope.requestFrame()
132 }
133 area = Some(current);
134 props.value.owner.handleArea = Some(current)
135 scope.routeChildren(current)
136 focus = Some(scope.focusTarget(current, props.value.props.swipeHandle.focus))
137 painter = Some(scope.resource { ThemePainterCache() })
138 scope.resource {
139 current.scope.addDefaultAction {
140 context,
141 event -> when (event) {
142 case Keyboard(val key) -> if (current.isFocusActive) {
143 when (props.value.owner.control) {
144 case Some(val control) -> drawerKeyboard(control, context, key, true)
145 case None -> {}
146 }
147 }
148 case Pointer(
149 InputPointerEvent.Button(_, MouseButton.Left, ElementState.Pressed, _)
150 ) -> {
151 if (props.value.props.options.enabled)
152 focus!!.handle.requestFocus(FocusOrigin.Pointer)
153 }
154 else -> {}
155 }
156 }
157 }
158 }
159 public override fun update(): UiInvalidation {
160 val input = props.value
161 area!!.scope.enabled = input.props.options.enabled && input.props.swipeHandle.visible
162 if (!scope.isMounted)
163 return UiInvalidation.Layout
164 focus!!.options = input.props.swipeHandle.focus
165 painter!!.get(input.theme)
166 return UiInvalidation.Layout
167 }
168 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
169 Unit
170 > {
171 val size = constraints.constrain(Size(0.0, 0.0))
172 val child =
173 context.measureChild(
174 0L,
175 Constraints(maxWidth = Some(size.width), maxHeight = Some(size.height))
176 )
177 context.placeChild(
178 0L,
179 Point((size.width - child.size.width) / 2.0, (size.height - child.size.height) / 2.0)
180 )
181 return LayoutResult(LayoutMetrics(size), Unit)
182 }
183 public override fun scene(context: SceneContext, measured: Unit) {
184 val input = props.value
185 val density = input.density
186 val vertical = input.props.side == SheetSide.Top || input.props.side == SheetSide.Bottom
187 val width =
188 if (vertical)
189 min(context.size.width, 96.0 * density)
190 else
191 min(context.size.width, 4.0 * density)
192 val height =
193 if (vertical)
194 min(context.size.height, 4.0 * density)
195 else
196 min(context.size.height, 96.0 * density)
197 val bounds =
198 Rect(
199 (context.size.width - width) / 2.0,
200 (context.size.height - height) / 2.0,
201 width,
202 height
203 )
204 val visual =
205 SurfaceVisual(
206 SurfaceKind.Plain,
207 InteractionVisual(
208 enabled = input.props.options.enabled,
209 hovered = area!!.scope.isHovered,
210 focusVisible = area!!.isFocusVisible
211 )
212 )
213 val local =
214 styleOverride {
215 background(
216 if (panelPresent(input.props.swipeHandle.content))
217 None
218 else
219 Some(Paint(input.theme.tokens.palette.muted.background))
220 )
221 border(None);
222 corners(Corners.all(4.0 * density))
223 }.then(input.props.swipeHandle.style)
224 val shape =
225 input.theme.surfaceGeometry(
226 bounds,
227 visual,
228 surfaceOptions {
229 style = local;
230 padding = Some(Insets());
231 focus = Some(OutlineStyle(input.theme.tokens.palette.ring, density, density))
232 },
233 context.layoutDirection
234 )
235 context.draw {
236 output -> painter!!.get(input.theme).drawSurface(output.canvas, shape, visual)
237 }
238 if (context.size.width > 0.0 && context.size.height > 0.0)
239 context.place(
240 area!!,
241 if (vertical)
242 Rect(
243 0.0,
244 (context.size.height - 20.0 * density) / 2.0,
245 context.size.width,
246 20.0 * density
247 )
248 else
249 Rect(
250 (context.size.width - 20.0 * density) / 2.0,
251 0.0,
252 20.0 * density,
253 context.size.height
254 )
255 )
256 context.children()
257 }
258 public override fun onUnmount() {
259 props.value.owner.handleArea = None
260 }