Scoop API

scoop:scoop.ui.component:0.1.0

src/table/resize.scoop

Selected production source · src/table/resize.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.surface.*
5import scoop.ui.*
6import scoop.ui.style.*
7import scoop.ui.style.Theme
8import scoop.window.*
9import scoop.window.accessibility.*
11internal enum TableResizeOrigin {
12 Programmatic, Pointer, Keyboard, Accessibility
15internal class TableResizeInput(
16 val width: Binding<Double?>,
17 val size: TableColumnSize,
18 val theme: Theme,
19 val enabled: Boolean,
20 val semantics: ComponentSemantics,
21 val focus: FocusOptions = FocusOptions(),
22 val onResizeStart: () -> Boolean = { true },
23 val onResizeEnd: ((TableColumnResizeEnd) -> Unit)? = None,
24 val onWidthChange: ((Double?, TableResizeOrigin) -> Unit)? = None
25) {
26 val density = theme.tokens.typography.body.fontSize / 14.0
27 fun write(value: Double?, origin: TableResizeOrigin) {
28 when (onWidthChange) {
29 case Some(val change) -> change(value, origin)
30 case None -> width.value = value
31 }
32 }
35private class TableResizeGesture(
36 val width: Double,
37 val initial: Double?,
38 val density: Double,
39 val direction: LayoutDirection,
40 val binding: Binding<Double?>,
41 val write: (Double?, TableResizeOrigin) -> Unit,
42 val onEnd: ((TableColumnResizeEnd) -> Unit)?
43) {
44 var expected = initial
47internal class TableResizeHeader(
48 private val props: Props<TableResizeInput>,
49 scope: ComponentScope
50) : Element<Unit>(scope) {
51 private val window = scope.nativeWindow
52 private var accepted: Double? = None
53 private var gesture: TableResizeGesture? = None
54 private var cursor = false
55 private val area = scope
56 .createArea(focusable = true) { context, event ->
57 if (context.phase == RoutedPhase.Notification) {
58 when (event) {
59 case FocusChanged(false) -> cancel();
60 else -> {}
61 }
62 if (scope.isMounted) {
63 updateCursor();
64 scope.requestFrame()
65 }
66 }
67 }
68 private val focus = scope.focusTarget(area, props.value.focus)
69 private val semantics = createComponentSemantics(scope, SemanticRole.Separator)
70 private val drag = scope
71 .drag(
72 area,
73 options(),
74 { _ -> props.value.enabled && accepted != None },
75 { event -> receive(event) }
76 )
77 init {
78 semantics?.bindFocus(Some(focus))
79 area.scope.defaultAction = { context, event ->
80 when (event) {
81 case Keyboard(val key) -> {
82 if (key.state ==
83 ElementState.Pressed &&
84 !key.synthetic &&
85 area.isFocusActive &&
86 props.value.enabled
87 ) {
88 when (key.logicalKey) {
89 case Named(NamedKey.Escape) ->
90 if (panelPresent(gesture)) {
91 cancel(TableResizeOrigin.Keyboard);
92 context.consume()
93 }
95 case Named(NamedKey.Home) -> {
96 cancel();
97 props.value.write(None, TableResizeOrigin.Keyboard);
98 context.consume()
99 }
101 case Named(NamedKey.ArrowLeft) -> {
102 adjust(false);
103 context.consume()
104 }
106 case Named(NamedKey.ArrowRight) -> {
107 adjust(true);
108 context.consume()
109 }
111 else -> {}
112 }
113 }
114 }
116 case Pointer(
117 InputPointerEvent.Button(_, MouseButton.Left, ElementState.Pressed, _)
118 ) -> {
119 if (props.value.enabled) focus.handle.requestFocus(FocusOrigin.Pointer)
120 }
122 else -> {}
123 }
124 }
125 }
126 private fun options(): DragOptions = DragOptions(
127 DragAxis.Horizontal,
128 enabled = props.value.enabled
129 )
130 private fun adjust(forward: Boolean) {
131 val width = accepted ?: return
132 val sign = if (forward != (scope.layoutDirection == LayoutDirection.Rtl)) 1.0 else -1.0
133 props
134 .value
135 .write(
136 Some(props.value.size.constrain(lowerBound(width + sign * 10.0, 0.0))),
137 TableResizeOrigin.Keyboard
138 )
139 }
140 private fun setNumber(value: Double): SemanticActionResult {
141 if (!scope.isMounted) return SemanticActionResult.Unavailable
142 if (!props.value.enabled) return SemanticActionResult.Rejected
143 if (!value.isFinite() || value < 0.0) return SemanticActionResult.InvalidArgument
144 if (accepted == None) return SemanticActionResult.Unavailable
145 cancel()
146 if (!scope.isMounted) return SemanticActionResult.Unavailable
147 val next = props.value.size.constrain(value)
148 props.value.write(Some(next), TableResizeOrigin.Accessibility)
149 if (!scope.isMounted) return SemanticActionResult.Unavailable
150 return if (props.value.width.value == Some(next))
151 SemanticActionResult.Completed
152 else
153 SemanticActionResult.Rejected
154 }
155 private fun stepNumber(amount: Double): SemanticActionResult {
156 val current = props.value.width.value ?:
157 (accepted ?: return SemanticActionResult.Unavailable)
158 return setNumber(lowerBound(current + amount, 0.0))
159 }
160 private fun updateSemantics() {
161 val input = props.value
162 val current = when (accepted) {
163 case Some(val width) -> Some(input.size.constrain(input.width.value ?: width))
164 case None -> None
165 }
166 semantics?.bindReference(input.semantics.reference)
167 semantics
168 ?.update(
169 input
170 .semantics
171 .properties(
172 enabled = input.enabled,
173 range = when (current) {
174 case Some(val width) ->
175 Some(
176 SemanticValueRange(
177 input.size.minimum,
178 input.size.maximum ?: Double.MAX_VALUE,
179 width,
180 10.0
181 )
182 )
183 case None -> None
184 }
185 )
186 .copy { orientation = Some(SemanticOrientation.Vertical) },
187 if (current != None)
188 SemanticActions(
189 setNumber = Some { value -> setNumber(value) },
190 increment = Some { stepNumber(10.0) },
191 decrement = Some { stepNumber(-10.0) }
192 )
193 else
194 SemanticActions(),
195 input.semantics.relations
196 )
197 }
198 private fun receive(event: DragEvent) {
199 when (event) {
200 case Started(val sample) -> {
201 val width = accepted ?: return
202 val start = props.value
203 if (!start.onResizeStart() || !scope.isMounted || !props.value.enabled) {
204 drag.cancel()
205 return
206 }
207 gesture = Some(
208 TableResizeGesture(
209 width,
210 props.value.width.value,
211 props.value.density,
212 scope.layoutDirection,
213 props.value.width,
214 { value, origin -> start.write(value, origin) },
215 start.onResizeEnd
216 )
217 )
218 move(sample.displacement)
219 }
221 case Moved(val sample) -> move(sample.displacement)
223 case Ended(val sample) -> {
224 move(sample.displacement)
225 val previous = gesture
226 gesture = None
227 when (previous) {
228 case Some(val active) -> active.onEnd?.invoke(TableColumnResizeEnd.Completed)
229 case None -> {}
230 }
231 }
233 case Cancelled(_, _) -> cancel(TableResizeOrigin.Pointer)
234 }
235 if (scope.isMounted) {
236 updateCursor();
237 scope.requestFrame()
238 }
239 }
240 private fun move(delta: Double) {
241 val active = gesture ?: return
242 if (props.value.width.value != active.expected) {
243 cancel();
244 return
245 }
246 val logical = if (active.direction == LayoutDirection.Rtl) -delta else delta
247 val next = props
248 .value
249 .size
250 .constrain(lowerBound(active.width + logical / active.density, 0.0))
251 active.expected = Some(next)
252 props.value.write(Some(next), TableResizeOrigin.Pointer)
253 if (scope.isMounted && props.value.width.value != active.expected) cancel()
254 }
255 private fun cancel(origin: TableResizeOrigin = TableResizeOrigin.Programmatic) {
256 val active = gesture ?: return
257 gesture = None
258 drag.cancel()
259 when (active.onEnd) {
260 case Some(val finish) -> finish(TableColumnResizeEnd.Cancelled(active.initial))
261 case None ->
262 if (active.binding.value == active.expected) active.write(active.initial, origin)
263 }
264 }
265 private fun updateCursor() {
266 val next = props.value.enabled && (area.scope.isHovered || drag.isDragging)
267 if (next || cursor)
268 resizeCursor?.invoke(window, if (next) Some(Orientation.Horizontal) else None)
269 cursor = next
270 }
271 public override fun update(): UiInvalidation {
272 when (gesture) {
273 case Some(val value) ->
274 if (props.value.width.value !=
275 value.expected ||
276 value.density !=
277 props.value.density ||
278 value.direction !=
279 scope.layoutDirection
280 )
281 cancel()
282 case None -> {}
283 }
284 drag
285 .update(
286 options(),
287 { _ -> props.value.enabled && accepted != None },
288 { event -> receive(event) }
289 )
290 area.scope.enabled = props.value.enabled
291 focus.options = props.value.focus
292 updateSemantics()
293 updateCursor()
294 return UiInvalidation.Layout
295 }
296 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
297 Unit
298 > {
299 val child = context.measureChild(0L, constraints)
300 context.placeChild(0L, Point(0.0, 0.0))
301 return LayoutResult(child, Unit)
302 }
303 public override fun scene(context: SceneContext, value: Unit) {
304 val input = props.value
305 val density = input.density
306 val size = context.size
307 val width = size.width / density
308 context
309 .onFrameComplete { frame ->
310 if (frame.submission == FrameSubmission.Accepted && scope.isMounted) {
311 accepted = if (size.width > 0.0 && size.height > 0.0) Some(width) else None
312 if (accepted == None) cancel()
313 updateSemantics()
314 }
315 }
316 context.children()
317 val x = if (context.layoutDirection == LayoutDirection.Rtl)
318 0.0
319 else
320 lowerBound(size.width - 8.0 * density, 0.0)
321 val bounds = Rect(
322 x,
323 4.0 * density,
324 lowerBound(upperBound(size.width, 8.0 * density), 0.0),
325 lowerBound(size.height - 8.0 * density, 0.0)
326 )
327 context.place(area, bounds)
328 when (semantics) {
329 case Some(val handle) -> context.semantics(handle, bounds)
330 case None -> {}
331 }
332 if (area.scope.isHovered || area.isFocusVisible || drag.isDragging)
333 context
334 .draw { paint ->
335 val color = if (area.isFocusVisible)
336 input.theme.tokens.palette.ring
337 else
338 input.theme.tokens.palette.border
339 paint
340 .canvas
341 .drawRect(
342 Rect(
343 bounds.x + bounds.width / 2.0 - density,
344 bounds.y,
345 2.0 * density,
346 bounds.height
347 ),
348 Paint(color)
349 )
350 }
351 }
352 public override fun onUnmount() {
353 cancel();
354 if (cursor &&
355 (window.state == WindowState.Open || window.state == WindowState.PendingConfigure)
356 )
357 resizeCursor?.invoke(window, None)
358 }