compose-rust 0.0.11

Declarative UI in Rust, rendered by an AOT-compiled Compose Multiplatform engine.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
package dev.darkpyonix.composerust.foundation

import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.key.Key
import androidx.compose.ui.input.key.KeyEventType
import androidx.compose.ui.input.key.KeyInputModifierNode
import androidx.compose.ui.input.key.key
import androidx.compose.ui.input.key.type
import androidx.compose.ui.input.pointer.PointerEvent
import androidx.compose.ui.input.pointer.PointerEventPass
import androidx.compose.ui.input.pointer.PointerEventType
import androidx.compose.ui.input.pointer.PointerInputChange
import androidx.compose.ui.input.pointer.changedToDownIgnoreConsumed
import androidx.compose.ui.input.pointer.isAltPressed
import androidx.compose.ui.input.pointer.isBackPressed
import androidx.compose.ui.input.pointer.isCtrlPressed
import androidx.compose.ui.input.pointer.isForwardPressed
import androidx.compose.ui.input.pointer.isMetaPressed
import androidx.compose.ui.input.pointer.isPrimaryPressed
import androidx.compose.ui.input.pointer.isSecondaryPressed
import androidx.compose.ui.input.pointer.isShiftPressed
import androidx.compose.ui.input.pointer.isTertiaryPressed
import androidx.compose.ui.node.ModifierNodeElement
import androidx.compose.ui.node.PointerInputModifierNode
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalViewConfiguration
import androidx.compose.ui.unit.IntSize
import dev.darkpyonix.composerust.design.HostPlatform
import dev.darkpyonix.composerust.design.detectHostPlatform
import dev.darkpyonix.composerust.foundation.code.CodePosition
import dev.darkpyonix.composerust.protocol.HostEvent
import dev.darkpyonix.composerust.protocol.PointerKind
import dev.darkpyonix.composerust.protocol.PointerType
import dev.darkpyonix.composerust.protocol.PropertyKind
import dev.darkpyonix.composerust.protocol.PropertyValue
import dev.darkpyonix.composerust.protocol.WheelDeltaMode
import dev.darkpyonix.composerust.runtime.EventDispatcher
import dev.darkpyonix.composerust.runtime.LocalZoom
import dev.darkpyonix.composerust.runtime.hostPlatformOverride
import dev.darkpyonix.composerust.runtime.islandDensity
import dev.darkpyonix.composerust.ui.floatProp
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlin.math.abs
import androidx.compose.ui.input.key.KeyEvent as ComposeKeyEvent
import androidx.compose.ui.input.key.isShiftPressed as isShiftHeld
import androidx.compose.ui.input.pointer.PointerType as ComposePointerType
import dev.darkpyonix.composerust.ui.node.Node as HostNode

/**
 * Pointer events for the nodes that subscribed to them.
 *
 * A node subscribes with a handler and a mask of kinds. Only such a node carries the
 * modifier below; a node without a subscription has no pointer input of its own, so a screen
 * of plain containers pays nothing for this.
 *
 * What a subscriber is told, and in which order:
 *
 * - The deepest subscriber under the pointer hears an event first. If its handler does not
 *   consume it, the nearest subscribed ancestor hears it next, each with the position in its
 *   own space. `Enter` and `Leave` do not travel: every node gets its own.
 * - A node that took a `Down` keeps that pointer until it is released or cancelled. Its
 *   `Move` and `Up` start at that node wherever the pointer is, and no node is told about
 *   entering or leaving until the press is over.
 * - `Move` is held and sent once a frame for each node, with the latest position. `Wheel`
 *   is held the same way with its travel added up. Every other kind is sent at once, after
 *   whatever was being held, so the order a handler sees is the order things happened in.
 * - A consumed `Down`, `Up` or `ContextMenu` is consumed for Compose as well, so no click
 *   and no menu follows it. A consumed `Move` only stops at the handler: taking the move
 *   from Compose would cancel the click of every pressable thing around it.
 *
 * Positions are in dp from the node's top left corner. Compose has already taken the
 * position back through every layer the node is drawn through, so a rotated node reports
 * where in its own unrotated box the pointer is.
 */

/**
 * The unit a wheel's travel is reported in.
 *
 * Compose hands over travel in its own scroll units, one for a notch of a wheel, which is
 * what a line is. A platform whose window hands over travel in pixels says so here, and the
 * travel is then sent in dp like a position.
 */
var hostWheelDeltaMode: WheelDeltaMode = WheelDeltaMode.Line

internal const val POINTER_BUTTON_PRIMARY = 1
internal const val POINTER_BUTTON_SECONDARY = 2
internal const val POINTER_BUTTON_TERTIARY = 4
internal const val POINTER_BUTTON_BACK = 8
internal const val POINTER_BUTTON_FORWARD = 16

private const val MODIFIER_SHIFT = 1
private const val MODIFIER_CTRL = 2
private const val MODIFIER_ALT = 4
private const val MODIFIER_META = 8

/**
 * Told when a node of the application consumed a press that asks for a context menu, which is
 * how an application puts up a menu of its own. Filled in by a platform whose window puts up a
 * text menu itself on that same press, so that the window's can stand back for it; left alone
 * everywhere else.
 */
var platformContextMenuTaken: () -> Unit = {}

/** How many pointers can be down at once before the oldest capture is forgotten. */
private const val MAX_POINTERS = 16

/** The bit of one kind in a subscription mask: the kind's wire tag, less one, as a shift. */
internal fun pointerKindBit(kind: PointerKind): Int = when (kind) {
    PointerKind.Move -> 1
    PointerKind.Enter -> 1 shl 1
    PointerKind.Leave -> 1 shl 2
    PointerKind.Down -> 1 shl 3
    PointerKind.Up -> 1 shl 4
    PointerKind.Cancel -> 1 shl 5
    PointerKind.Wheel -> 1 shl 6
    PointerKind.ContextMenu -> 1 shl 7
}

/** The kinds a node subscribed to, or 0 for a node that subscribed to none. */
internal fun HostNode.pointerKinds(): Int =
    (property(PropertyKind.PointerKinds) as? PropertyValue.Integer)?.value?.toInt() ?: 0

/** Whether a node has both halves of a subscription: a handler and at least one kind. */
internal fun HostNode.subscribesToPointer(): Boolean =
    handler(PropertyKind.OnPointer) != null && pointerKinds() != 0

/**
 * Whether a code editor has both halves of its own subscription: the handler that hears its
 * pointer with the text under it, and at least one kind in the same mask a node's has.
 */
internal fun HostNode.subscribesToCodePointer(): Boolean =
    handler(PropertyKind.OnCodePointer) != null && pointerKinds() != 0

/**
 * A code editor's surface, as the pointer events over it see it: what is under a point.
 *
 * The editor's pointer node routes in the first pass rather than the main one. The text
 * the editor draws handles a press in the main pass, so a press the Host consumed has to be
 * consumed before then, or the caret would move under a Cmd-click that was the Host's.
 */
interface CodePointerTarget {
    /**
     * The character under a point of the editor's view, in its pixels, or null where there
     * is none: the gutter, past the end of a line, below the last line, or a row the editor
     * shows that is not the document's.
     */
    fun positionAt(x: Float, y: Float): CodePosition?
}

/**
 * The modifier of a subscribed node. Call it only for a node that subscribes.
 *
 * The scale positions are divided by is the node's own: inside an HTML island that is the
 * island's, which carries its zoom, so the Host is handed the island's own pixels and can
 * test them against its layout without knowing the zoom. The root of an island is sized
 * among native widgets but reports in the island's pixels too, because the Host tests the
 * same layout against it.
 */
@Composable
internal fun hostPointerEvents(
    node: HostNode,
    router: PointerRouter,
    dispatcher: EventDispatcher,
): Modifier {
    val outer = LocalDensity.current
    val zoom = LocalZoom.current
    val islandZoom = node.floatProp(PropertyKind.IslandZoom)
    val pxPerUnit = if (islandZoom != null) islandDensity(outer, zoom, islandZoom).density else outer.density
    val view = LocalViewConfiguration.current
    val controlClickIsMenu = remember {
        (hostPlatformOverride ?: detectHostPlatform()) == HostPlatform.MacOs
    }
    return HostPointerElement(
        node,
        router,
        dispatcher,
        pxPerUnit,
        view.doubleTapTimeoutMillis,
        view.longPressTimeoutMillis,
        view.touchSlop,
        controlClickIsMenu,
        hostWheelDeltaMode,
    )
}

/**
 * The modifier of a code editor's view whose node subscribes to [PropertyKind.OnCodePointer].
 * The records are the ones [hostPointerEvents] sends with the character under the pointer
 * after them, and every rule is the same: the kinds, the capture, a move a frame, the click
 * count, the menu, and a consumed press consumed for Compose too.
 */
@Composable
internal fun codePointerEvents(
    node: HostNode,
    router: PointerRouter,
    dispatcher: EventDispatcher,
    target: CodePointerTarget,
): Modifier {
    val outer = LocalDensity.current
    val view = LocalViewConfiguration.current
    val controlClickIsMenu = remember {
        (hostPlatformOverride ?: detectHostPlatform()) == HostPlatform.MacOs
    }
    return HostPointerElement(
        node,
        router,
        dispatcher,
        outer.density,
        view.doubleTapTimeoutMillis,
        view.longPressTimeoutMillis,
        view.touchSlop,
        controlClickIsMenu,
        hostWheelDeltaMode,
        target,
    )
}

private data class HostPointerElement(
    val node: HostNode,
    val router: PointerRouter,
    val dispatcher: EventDispatcher,
    val pxPerUnit: Float,
    val repeatMillis: Long,
    val longPressMillis: Long,
    val slop: Float,
    val controlClickIsMenu: Boolean,
    val wheelMode: WheelDeltaMode,
    /** A code editor's view, for the editor's own events. Null for a node's [PropertyKind.OnPointer]. */
    val code: CodePointerTarget? = null,
) : ModifierNodeElement<HostPointerNode>() {
    override fun create(): HostPointerNode = HostPointerNode(node, router, dispatcher).also(::configure)

    override fun update(node: HostPointerNode) {
        if (node.host !== this.node || node.router !== router) {
            // A different node or table behind the same place in the tree: nothing held
            // for the old one belongs to the new one.
            node.router.forget(node)
            if (node.router !== router) {
                node.router.detached(node)
                router.attached(node)
            }
            node.host = this.node
            node.router = router
            node.clearHeld()
        }
        node.dispatcher = dispatcher
        configure(node)
    }

    private fun configure(node: HostPointerNode) {
        node.pxPerUnit = if (pxPerUnit > 0f) pxPerUnit else 1f
        node.repeatMillis = repeatMillis
        node.longPressMillis = longPressMillis
        node.slop = slop
        node.controlClickIsMenu = controlClickIsMenu
        node.wheelMode = wheelMode
        node.code = code
    }
}

/**
 * One subscribed node's place in the pointer path, and everything held for it between an
 * event and the frame it is sent in. All of it is fields of a fixed size, so holding a move
 * or adding up a wheel allocates nothing.
 */
internal class HostPointerNode(
    var host: HostNode,
    var router: PointerRouter,
    var dispatcher: EventDispatcher,
) : Modifier.Node(), PointerInputModifierNode, KeyInputModifierNode {
    var pxPerUnit = 1f
    var repeatMillis = 300L
    var longPressMillis = 500L
    var slop = 0f
    var controlClickIsMenu = false
    var wheelMode = WheelDeltaMode.Line

    /** The code editor this node is the view of, which makes its records `CodePointer` ones. */
    var code: CodePointerTarget? = null

    /** The event being dispatched, as this node sees it: its positions are this node's own. */
    var current: PointerEvent? = null

    // The latest the node was told by Compose, which is what the next record carries.
    var lastX = 0f
    var lastY = 0f
    var lastPointerId = 1
    var lastPointerType = PointerType.Mouse
    var lastButtons = 0
    var lastModifiers = 0
    var lastUptimeMillis = 0L

    /** Whether the Host has been told the pointer is inside. */
    var inside = false

    /** Whether the pointer is inside, which the Host is told once no press holds it back. */
    var wantInside = false

    /** Inside only because a finger is down: there is no hover to say it left. */
    var insideWhilePressed = false

    /** Pointers down in this node or under it. */
    var pressed = 0

    var movePending = false
    var wheelPending = false
    var wheelX = 0f
    var wheelY = 0f
    var wheelPendingMode = WheelDeltaMode.Line

    /**
     * What the handler answered to the last wheel. A wheel is added up and sent at the next
     * frame, and by then Compose has finished with the event, so whether Compose may scroll
     * with it is decided by the last answer.
     */
    var wheelTaken = false

    private var longPress: Job? = null

    fun mask(): Int = host.pointerKinds()

    fun wants(kind: PointerKind): Boolean = mask() and pointerKindBit(kind) != 0

    override fun onAttach() {
        router.attached(this)
    }

    override fun onDetach() {
        router.forget(this)
        router.detached(this)
        clearHeld()
    }

    fun clearHeld() {
        current = null
        inside = false
        wantInside = false
        insideWhilePressed = false
        pressed = 0
        movePending = false
        wheelPending = false
        wheelX = 0f
        wheelY = 0f
        wheelTaken = false
        cancelLongPress()
    }

    override fun onPointerEvent(pointerEvent: PointerEvent, pass: PointerEventPass, bounds: IntSize) {
        if (code != null) {
            // Routed on the way down, before the editor's text sees the event: the nodes of
            // the chain further out are already on the stack, and the editor is the
            // innermost subscriber there is.
            if (pass != PointerEventPass.Initial) return
            current = pointerEvent
            router.begin(this)
            if (pointerEvent.type == PointerEventType.Scroll && wheelTaken && wants(PointerKind.Wheel)) {
                val changes = pointerEvent.changes
                for (index in changes.indices) changes[index].consume()
            }
            router.finish(this, pointerEvent)
            return
        }
        when (pass) {
            // On the way down every subscriber in the path says it is there. The deepest
            // one is the first to see the main pass, and by then the whole path is known.
            PointerEventPass.Initial -> {
                current = pointerEvent
                router.begin(this)
                if (pointerEvent.type == PointerEventType.Scroll && wheelTaken && wants(PointerKind.Wheel)) {
                    val changes = pointerEvent.changes
                    for (index in changes.indices) changes[index].consume()
                }
            }
            PointerEventPass.Main -> router.finish(this, pointerEvent)
            else -> Unit
        }
    }

    override fun onCancelPointerInput() {
        router.cancel(this)
    }

    /** The menu key and Shift with F10 ask for the menu of whatever holds the focus. */
    override fun onKeyEvent(event: ComposeKeyEvent): Boolean {
        if (event.type != KeyEventType.KeyDown) return false
        val menuKey = event.key == Key.Menu || (event.key == Key.F10 && event.isShiftHeld)
        if (!menuKey || !wants(PointerKind.ContextMenu)) return false
        router.flush()
        lastButtons = 0
        return send(PointerKind.ContextMenu)
    }

    override fun onPreKeyEvent(event: ComposeKeyEvent): Boolean = false

    /** Keeps what Compose said about one pointer, in this node's space. */
    fun remember(change: PointerInputChange, event: PointerEvent) {
        lastX = change.position.x
        lastY = change.position.y
        val type = change.type
        lastPointerType = when (type) {
            ComposePointerType.Touch -> PointerType.Touch
            ComposePointerType.Stylus, ComposePointerType.Eraser -> PointerType.Pen
            else -> PointerType.Mouse
        }
        lastPointerId = if (lastPointerType == PointerType.Mouse) 1 else change.id.value.toInt()
        lastButtons = buttonsOf(event, change)
        lastModifiers = modifiersOf(event)
        lastUptimeMillis = change.uptimeMillis
    }

    fun changeFor(pointer: Long): PointerInputChange? {
        val changes = current?.changes ?: return null
        for (index in changes.indices) {
            val change = changes[index]
            if (change.id.value == pointer) return change
        }
        return null
    }

    /**
     * Sends one record built from what the node last heard, and answers whether the handler
     * consumed it. A node the Host has already removed is sent nothing: the Host has no
     * handler left to hear it.
     */
    fun send(
        kind: PointerKind,
        button: Int = 0,
        clickCount: Int = 0,
        deltaX: Float = 0f,
        deltaY: Float = 0f,
        deltaMode: WheelDeltaMode = WheelDeltaMode.Pixel,
    ): Boolean {
        val editor = code
        if (editor != null) return sendCode(editor, kind, button, clickCount, deltaX, deltaY, deltaMode)
        val handler = host.handler(PropertyKind.OnPointer) ?: return false
        if (!router.alive(host.id)) return false
        return dispatcher.dispatch(
            HostEvent.Pointer(
                nodeId = host.id,
                handlerId = handler,
                kind = kind,
                pointerId = lastPointerId,
                pointerType = lastPointerType,
                x = lastX / pxPerUnit,
                y = lastY / pxPerUnit,
                buttons = lastButtons,
                button = button,
                modifiers = lastModifiers,
                clickCount = clickCount,
                deltaX = deltaX,
                deltaY = deltaY,
                deltaMode = deltaMode,
                timestampNanos = lastUptimeMillis * 1_000_000L,
            ),
        )
    }

    /** [send] for a code editor's view: the same record, and the character under the pointer. */
    private fun sendCode(
        editor: CodePointerTarget,
        kind: PointerKind,
        button: Int,
        clickCount: Int,
        deltaX: Float,
        deltaY: Float,
        deltaMode: WheelDeltaMode,
    ): Boolean {
        val handler = host.handler(PropertyKind.OnCodePointer) ?: return false
        if (!router.alive(host.id)) return false
        val position = editor.positionAt(lastX, lastY)
        return dispatcher.dispatch(
            HostEvent.CodePointer(
                nodeId = host.id,
                handlerId = handler,
                kind = kind,
                pointerId = lastPointerId,
                pointerType = lastPointerType,
                x = lastX / pxPerUnit,
                y = lastY / pxPerUnit,
                buttons = lastButtons,
                button = button,
                modifiers = lastModifiers,
                clickCount = clickCount,
                deltaX = deltaX,
                deltaY = deltaY,
                deltaMode = deltaMode,
                timestampNanos = lastUptimeMillis * 1_000_000L,
                line = position?.line ?: -1,
                column = position?.column ?: -1,
            ),
        )
    }

    fun armLongPress() {
        cancelLongPress()
        if (!isAttached) return
        val wait = longPressMillis
        longPress = coroutineScope.launch {
            delay(wait)
            longPress = null
            router.longPressed(this@HostPointerNode)
        }
    }

    fun cancelLongPress() {
        longPress?.cancel()
        longPress = null
    }
}

/** The buttons held, as the wire's bits. A finger or a pen tip that is down is the primary one. */
private fun buttonsOf(event: PointerEvent, change: PointerInputChange): Int {
    val buttons = event.buttons
    var bits = 0
    if (buttons.isPrimaryPressed) bits = bits or POINTER_BUTTON_PRIMARY
    if (buttons.isSecondaryPressed) bits = bits or POINTER_BUTTON_SECONDARY
    if (buttons.isTertiaryPressed) bits = bits or POINTER_BUTTON_TERTIARY
    if (buttons.isBackPressed) bits = bits or POINTER_BUTTON_BACK
    if (buttons.isForwardPressed) bits = bits or POINTER_BUTTON_FORWARD
    if (bits == 0 && change.type != ComposePointerType.Mouse && change.pressed) bits = POINTER_BUTTON_PRIMARY
    return bits
}

private fun modifiersOf(event: PointerEvent): Int {
    val held = event.keyboardModifiers
    var bits = 0
    if (held.isShiftPressed) bits = bits or MODIFIER_SHIFT
    if (held.isCtrlPressed) bits = bits or MODIFIER_CTRL
    if (held.isAltPressed) bits = bits or MODIFIER_ALT
    if (held.isMetaPressed) bits = bits or MODIFIER_META
    return bits
}

/**
 * Routes what Compose dispatches to the subscribed nodes of one tree.
 *
 * Compose walks the nodes under a pointer twice: outermost to innermost, then back out.
 * Every subscriber joins [stack] on the way in, so when the innermost one is reached on the
 * way out the whole chain of subscribers is known and the event is routed along it once,
 * innermost first. The subscribers further out then find the pointer already routed and do
 * nothing.
 *
 * [alive] answers whether the Host still has a node. A node the Host removed stays composed
 * until the next frame, and nothing is sent for it in between.
 */
internal class PointerRouter(val alive: (Int) -> Boolean) {
    /** Counts up when something starts waiting for a frame, which is what the frame loop watches. */
    val pendingGeneration = mutableIntStateOf(0)

    /** How many nodes carry the pointer modifier, which is how many subscribed. */
    var subscribedNodes = 0
        private set

    private var stack = arrayOfNulls<HostPointerNode>(16)
    private var depth = 0

    /** The pointers already routed in the dispatch that is under way. */
    private val routed = LongArray(MAX_POINTERS)
    private var routedCount = 0

    private val capturePointers = LongArray(MAX_POINTERS)
    private val captureNodes = arrayOfNulls<HostPointerNode>(MAX_POINTERS)

    /** Each held pointer as the Host knows it: the `pointer_id` its records carry. */
    private val captureWireIds = IntArray(MAX_POINTERS)
    private var captures = 0

    /**
     * Pointers whose press a torn-off window took over ([cancelPointer]). The node that held
     * one has had its Cancel, so nothing more of that press is sent.
     */
    private val takenPointers = LongArray(MAX_POINTERS)
    private var takenCount = 0

    /** Nodes with a move or a wheel waiting for the frame, innermost first. */
    private val pending = ArrayList<HostPointerNode>()
    private var scheduled = false
    private var flushing = false

    /** Nodes the pointer entered or left during a press, told once the press is over. */
    private val unsettled = ArrayList<HostPointerNode>()

    // The run of presses in one place, which is what a click count is.
    private var runNode: HostPointerNode? = null
    private var runButton = 0
    private var runCount = 0
    private var runUptimeMillis = 0L
    private var runX = 0f
    private var runY = 0f

    /** The buttons held after the last event seen, which the next one is compared with. */
    private var heldButtons = 0
    private var seenButtons = 0

    // The touch that may turn into a long press, and the chain it would be routed along.
    private var longPressChain = arrayOfNulls<HostPointerNode>(16)
    private var longPressDepth = 0
    private var longPressPointer = 0L
    private var longPressX = 0f
    private var longPressY = 0f

    val hasPending: Boolean get() = pending.isNotEmpty()

    fun attached(node: HostPointerNode) {
        subscribedNodes++
    }

    fun detached(node: HostPointerNode) {
        subscribedNodes--
    }

    /** Drops everything held for a node that is going away. Nothing is sent. */
    fun forget(node: HostPointerNode) {
        pending.remove(node)
        unsettled.remove(node)
        releaseCapturesOf(node)
        if (runNode === node) runNode = null
        for (index in 0 until longPressDepth) {
            if (longPressChain[index] === node) {
                disarmLongPress()
                break
            }
        }
        val at = stackIndexOf(node)
        if (at >= 0) {
            for (index in at until depth - 1) stack[index] = stack[index + 1]
            stack[--depth] = null
        }
        node.current = null
    }

    fun begin(node: HostPointerNode) {
        // A node still on the stack was left there by a dispatch that did not finish.
        val stale = stackIndexOf(node)
        if (stale >= 0) truncate(stale)
        if (depth == stack.size) stack = stack.copyOf(depth * 2)
        stack[depth++] = node
    }

    fun finish(node: HostPointerNode, event: PointerEvent) {
        val top = stackIndexOf(node)
        if (top < 0) return
        truncate(top + 1)
        val changes = event.changes
        for (index in changes.indices) {
            val change = changes[index]
            if (markRouted(change.id.value)) route(top, event, change)
        }
        node.current = null
        stack[top] = null
        depth = top
        if (depth == 0) {
            routedCount = 0
            heldButtons = seenButtons
        }
    }

    private fun stackIndexOf(node: HostPointerNode): Int {
        for (index in depth - 1 downTo 0) if (stack[index] === node) return index
        return -1
    }

    private fun truncate(to: Int) {
        while (depth > to) {
            depth--
            stack[depth]?.current = null
            stack[depth] = null
        }
        if (depth == 0) routedCount = 0
    }

    private fun markRouted(pointer: Long): Boolean {
        for (index in 0 until routedCount) if (routed[index] == pointer) return false
        if (routedCount < routed.size) routed[routedCount++] = pointer
        return true
    }

    private fun route(top: Int, event: PointerEvent, change: PointerInputChange) {
        if (takenCount > 0 && isTaken(change.id.value) && swallowTaken(top, event, change)) return
        when (event.type) {
            PointerEventType.Press -> press(top, event, change)
            PointerEventType.Release -> release(top, event, change)
            PointerEventType.Scroll -> wheel(top, change)
            PointerEventType.Move, PointerEventType.Enter, PointerEventType.Exit -> motion(top, change)
            else -> Unit
        }
    }

    private fun press(top: Int, event: PointerEvent, change: PointerInputChange) {
        val deepest = stack[top] ?: return
        val pointer = change.id.value
        val mouse = change.type == ComposePointerType.Mouse
        val buttons = buttonsOf(event, change)
        seenButtons = buttons
        var button = (buttons and heldButtons.inv()).takeLowestOneBit()
        if (button == 0) button = buttons.takeLowestOneBit()
        if (button == 0) button = POINTER_BUTTON_PRIMARY
        val modifiers = modifiersOf(event)
        // The platform's way of asking for a menu is a menu and not a press: the secondary
        // button anywhere, and Control with the primary one where that is the convention.
        val menu = mouse && (
            button == POINTER_BUTTON_SECONDARY ||
                (button == POINTER_BUTTON_PRIMARY && deepest.controlClickIsMenu && modifiers and MODIFIER_CTRL != 0)
            )
        val kind = if (menu) PointerKind.ContextMenu else PointerKind.Down
        val count = if (menu) 0 else countPress(deepest, change, button)
        val newPointer = change.changedToDownIgnoreConsumed()
        val start = captureStart(pointer, top)
        flush()
        // A press lands inside every node of the chain. A finger has no hover to have said
        // so already, so the node is entered here and left when the finger lifts.
        for (index in top downTo 0) {
            val entry = stack[index] ?: continue
            val own = entry.changeFor(pointer) ?: continue
            entry.remember(own, entry.current ?: event)
            if (newPointer) entry.pressed++
            if (!entry.inside && captures == 0) {
                entry.inside = true
                entry.wantInside = true
                if (!mouse) entry.insideWhilePressed = true
                if (entry.wants(PointerKind.Enter)) entry.send(PointerKind.Enter)
            }
        }
        var consumed = false
        for (index in start downTo 0) {
            val entry = stack[index] ?: continue
            if (entry.changeFor(pointer) == null || !entry.wants(kind)) continue
            if (kind == PointerKind.Down && captureOf(pointer) == null) capture(pointer, entry)
            if (entry.send(kind, button = if (menu) 0 else button, clickCount = count)) {
                consumed = true
                break
            }
        }
        if (consumed) {
            change.consume()
            if (menu) platformContextMenuTaken()
        }
        if (!mouse && newPointer) armLongPress(top, pointer, change)
    }

    private fun release(top: Int, event: PointerEvent, change: PointerInputChange) {
        val pointer = change.id.value
        val buttons = buttonsOf(event, change)
        seenButtons = buttons
        var button = (heldButtons and buttons.inv()).takeLowestOneBit()
        if (button == 0) button = POINTER_BUTTON_PRIMARY
        val count = if (runNode != null && runButton == button) runCount else 1
        val start = captureStart(pointer, top)
        flush()
        var consumed = false
        for (index in top downTo 0) {
            val entry = stack[index] ?: continue
            val own = entry.changeFor(pointer) ?: continue
            entry.remember(own, entry.current ?: event)
            if (index > start || consumed || !entry.wants(PointerKind.Up)) continue
            if (entry.send(PointerKind.Up, button = button, clickCount = count)) consumed = true
        }
        if (consumed) change.consume()
        if (change.pressed) return
        // Nothing is held any more: the press is over, and so is everything that hung on it.
        releaseCapture(pointer)
        if (longPressPointer == pointer) disarmLongPress()
        for (index in top downTo 0) {
            val entry = stack[index] ?: continue
            if (entry.changeFor(pointer) == null) continue
            if (entry.pressed > 0) entry.pressed--
            if (entry.insideWhilePressed && entry.pressed == 0) {
                entry.insideWhilePressed = false
                entry.inside = false
                entry.wantInside = false
                if (entry.wants(PointerKind.Leave)) entry.send(PointerKind.Leave)
            }
        }
        settle()
    }

    private fun motion(top: Int, change: PointerInputChange) {
        val pointer = change.id.value
        val start = captureStart(pointer, top)
        if (longPressDepth > 0 && longPressPointer == pointer) {
            val armed = longPressChain[longPressDepth - 1]
            val moved = armed == null ||
                armed !== stack[top] ||
                abs(change.position.x - longPressX) > armed.slop ||
                abs(change.position.y - longPressY) > armed.slop
            if (moved) disarmLongPress()
        }
        for (index in top downTo 0) {
            val entry = stack[index] ?: continue
            val event = entry.current ?: continue
            val own = entry.changeFor(pointer) ?: continue
            when (event.type) {
                PointerEventType.Enter -> hover(entry, own, event, inside = true)
                PointerEventType.Exit -> hover(entry, own, event, inside = false)
                else -> {
                    entry.remember(own, event)
                    if (index <= start && entry.wants(PointerKind.Move)) {
                        entry.movePending = true
                        enqueue(entry)
                    }
                }
            }
        }
        stack[top]?.let { seenButtons = it.lastButtons }
    }

    private fun hover(entry: HostPointerNode, change: PointerInputChange, event: PointerEvent, inside: Boolean) {
        entry.wantInside = inside
        if (captures > 0 || takenCount > 0) {
            // A press holds the pointer. Who it is over is told when the press ends.
            entry.remember(change, event)
            if (!unsettled.contains(entry)) unsettled.add(entry)
            return
        }
        if (entry.inside == inside) {
            entry.remember(change, event)
            return
        }
        val kind = if (inside) PointerKind.Enter else PointerKind.Leave
        // What was held goes first, with the position it was held with.
        if (entry.wants(kind)) flush()
        entry.remember(change, event)
        entry.inside = inside
        if (!inside) entry.insideWhilePressed = false
        if (entry.wants(kind)) entry.send(kind)
    }

    /** Tells the nodes the pointer entered or left during a press, now that it is over. */
    private fun settle() {
        if (captures > 0 || takenCount > 0 || unsettled.isEmpty()) return
        var index = 0
        while (index < unsettled.size) {
            val entry = unsettled[index++]
            if (entry.inside == entry.wantInside) continue
            entry.inside = entry.wantInside
            val kind = if (entry.inside) PointerKind.Enter else PointerKind.Leave
            if (entry.wants(kind)) {
                flush()
                entry.send(kind)
            }
        }
        unsettled.clear()
    }

    private fun wheel(top: Int, change: PointerInputChange) {
        val pointer = change.id.value
        val deltaX = change.scrollDelta.x
        val deltaY = change.scrollDelta.y
        for (index in top downTo 0) {
            val entry = stack[index] ?: continue
            val event = entry.current ?: continue
            val own = entry.changeFor(pointer) ?: continue
            entry.remember(own, event)
            if (!entry.wants(PointerKind.Wheel)) continue
            val mode = entry.wheelMode
            // Travel that is not added to what is held is sent first, not mixed into it.
            if (entry.wheelPending && entry.wheelPendingMode != mode) flush()
            // Pixels are a length like a position is, so they are sent in the same unit.
            val scale = if (mode == WheelDeltaMode.Pixel) 1f / entry.pxPerUnit else 1f
            entry.wheelX += deltaX * scale
            entry.wheelY += deltaY * scale
            entry.wheelPendingMode = mode
            entry.wheelPending = true
            enqueue(entry)
        }
    }

    /**
     * Compose took the pointer back: the window lost it, a system gesture began, a touch
     * was cancelled. A node that had a press is told once, and the press and its capture end.
     */
    fun cancel(node: HostPointerNode) {
        val at = stackIndexOf(node)
        if (at >= 0) truncate(at)
        val held = node.pressed > 0 || holdsCapture(node)
        if (held) flush()
        node.pressed = 0
        node.movePending = false
        node.wheelPending = false
        node.wheelX = 0f
        node.wheelY = 0f
        // A Host that answers a held Move by tearing a window off cancels from inside the
        // flush that sent it. The flush is walking the list and empties it when it is done.
        if (!flushing) pending.remove(node)
        unsettled.remove(node)
        releaseCapturesOf(node)
        for (index in 0 until longPressDepth) {
            if (longPressChain[index] === node) {
                disarmLongPress()
                break
            }
        }
        heldButtons = 0
        seenButtons = 0
        node.lastButtons = 0
        if (held && node.wants(PointerKind.Cancel)) node.send(PointerKind.Cancel)
        // A finger that was down is gone with its press. A mouse is still where it was, and
        // Compose says so itself when it leaves.
        if (node.insideWhilePressed) {
            node.inside = false
            node.wantInside = false
            node.insideWhilePressed = false
            if (node.wants(PointerKind.Leave)) node.send(PointerKind.Leave)
        }
        settle()
    }

    /**
     * A window was torn off under a held pointer and follows it from here on: the node that
     * holds [pointerId] (the id its records carry, 1 for the mouse) is told once with a
     * Cancel, and its press and capture end. Answers whether a node held it.
     *
     * The window the press began in goes on hearing that press from the system, its drags and
     * its release. None of it is sent: after the Cancel there is no press for a Move or an Up
     * to belong to.
     */
    fun cancelPointer(pointerId: Int): Boolean {
        for (index in 0 until captures) {
            if (captureWireIds[index] != pointerId) continue
            val node = captureNodes[index] ?: continue
            if (takenCount == takenPointers.size) removeTakenAt(0)
            takenPointers[takenCount++] = capturePointers[index]
            cancel(node)
            return true
        }
        return false
    }

    /**
     * What is left of a press a torn-off window took over. Answers true when the event was
     * part of that press and has been dropped, and false when the press turns out to be over
     * already, so the event is routed as any other.
     *
     * Where the pointer is goes on being kept, and is told as an Enter or a Leave once the
     * button is let go, as after any press.
     */
    private fun swallowTaken(top: Int, event: PointerEvent, change: PointerInputChange): Boolean {
        val pointer = change.id.value
        val released = event.type == PointerEventType.Release && !change.pressed
        // A button that is up, or a press that begins, with no release heard in between: the
        // release went somewhere this tree did not hear it. The press is over all the same.
        if (!released && (!change.pressed || change.changedToDownIgnoreConsumed())) {
            releaseTaken(pointer)
            settle()
            return false
        }
        for (index in top downTo 0) {
            val entry = stack[index] ?: continue
            val own = entry.changeFor(pointer) ?: continue
            val seen = entry.current ?: event
            when (seen.type) {
                PointerEventType.Enter -> hover(entry, own, seen, inside = true)
                PointerEventType.Exit -> hover(entry, own, seen, inside = false)
                else -> entry.remember(own, seen)
            }
        }
        seenButtons = buttonsOf(event, change)
        if (!released) return true
        releaseTaken(pointer)
        if (longPressPointer == pointer) disarmLongPress()
        for (index in top downTo 0) {
            val entry = stack[index] ?: continue
            if (entry.changeFor(pointer) == null) continue
            if (entry.pressed > 0) entry.pressed--
            if (entry.insideWhilePressed && entry.pressed == 0) {
                entry.insideWhilePressed = false
                entry.inside = false
                entry.wantInside = false
                if (entry.wants(PointerKind.Leave)) entry.send(PointerKind.Leave)
            }
        }
        settle()
        return true
    }

    private fun isTaken(pointer: Long): Boolean {
        for (index in 0 until takenCount) if (takenPointers[index] == pointer) return true
        return false
    }

    private fun releaseTaken(pointer: Long) {
        for (index in 0 until takenCount) {
            if (takenPointers[index] == pointer) {
                removeTakenAt(index)
                return
            }
        }
    }

    private fun removeTakenAt(at: Int) {
        for (index in at until takenCount - 1) takenPointers[index] = takenPointers[index + 1]
        takenCount--
    }

    private fun countPress(deepest: HostPointerNode, change: PointerInputChange, button: Int): Int {
        val x = change.position.x
        val y = change.position.y
        val again = runNode === deepest &&
            runButton == button &&
            change.uptimeMillis - runUptimeMillis <= deepest.repeatMillis &&
            abs(x - runX) <= deepest.slop &&
            abs(y - runY) <= deepest.slop
        runCount = if (again) runCount + 1 else 1
        runNode = deepest
        runButton = button
        runUptimeMillis = change.uptimeMillis
        runX = x
        runY = y
        return runCount
    }

    private fun enqueue(node: HostPointerNode) {
        // Taken out and put back, so the list stays innermost first whichever node was
        // already waiting.
        pending.remove(node)
        pending.add(node)
        if (!scheduled) {
            scheduled = true
            pendingGeneration.intValue++
        }
    }

    /**
     * Sends every held move and wheel, innermost node first. Called at the start of a frame,
     * and before anything that is sent at once so nothing overtakes a move that came first.
     */
    fun flush() {
        if (flushing) return
        scheduled = false
        if (pending.isEmpty()) return
        flushing = true
        try {
            var moveTaken = false
            var moveTakenPointer = 0
            var wheelTaken = false
            var index = 0
            while (index < pending.size) {
                val node = pending[index++]
                if (node.movePending) {
                    node.movePending = false
                    if (!(moveTaken && moveTakenPointer == node.lastPointerId) && node.send(PointerKind.Move)) {
                        moveTaken = true
                        moveTakenPointer = node.lastPointerId
                    }
                }
                if (node.wheelPending) {
                    node.wheelPending = false
                    val deltaX = node.wheelX
                    val deltaY = node.wheelY
                    node.wheelX = 0f
                    node.wheelY = 0f
                    if (!wheelTaken) {
                        val taken = node.send(
                            PointerKind.Wheel,
                            deltaX = deltaX,
                            deltaY = deltaY,
                            deltaMode = node.wheelPendingMode,
                        )
                        node.wheelTaken = taken
                        wheelTaken = taken
                    }
                }
            }
        } finally {
            pending.clear()
            flushing = false
        }
    }

    private fun captureOf(pointer: Long): HostPointerNode? {
        for (index in 0 until captures) if (capturePointers[index] == pointer) return captureNodes[index]
        return null
    }

    /** Where in the stack routing starts: at the node holding the pointer, or at the innermost. */
    private fun captureStart(pointer: Long, top: Int): Int {
        val holder = captureOf(pointer) ?: return top
        for (index in top downTo 0) if (stack[index] === holder) return index
        return top
    }

    private fun capture(pointer: Long, node: HostPointerNode) {
        if (captures == MAX_POINTERS) removeCaptureAt(0)
        capturePointers[captures] = pointer
        captureNodes[captures] = node
        captureWireIds[captures] = node.lastPointerId
        captures++
    }

    private fun holdsCapture(node: HostPointerNode): Boolean {
        for (index in 0 until captures) if (captureNodes[index] === node) return true
        return false
    }

    private fun releaseCapture(pointer: Long) {
        for (index in 0 until captures) {
            if (capturePointers[index] == pointer) {
                removeCaptureAt(index)
                return
            }
        }
    }

    private fun releaseCapturesOf(node: HostPointerNode) {
        var index = 0
        while (index < captures) {
            if (captureNodes[index] === node) removeCaptureAt(index) else index++
        }
    }

    private fun removeCaptureAt(at: Int) {
        for (index in at until captures - 1) {
            capturePointers[index] = capturePointers[index + 1]
            captureNodes[index] = captureNodes[index + 1]
            captureWireIds[index] = captureWireIds[index + 1]
        }
        captures--
        captureNodes[captures] = null
    }

    /** A finger held still long enough asks for the menu. Only armed where a node wants one. */
    private fun armLongPress(top: Int, pointer: Long, change: PointerInputChange) {
        disarmLongPress()
        var wanted = false
        for (index in top downTo 0) if (stack[index]?.wants(PointerKind.ContextMenu) == true) wanted = true
        if (!wanted) return
        if (longPressChain.size <= top) longPressChain = arrayOfNulls(stack.size)
        for (index in 0..top) longPressChain[index] = stack[index]
        longPressDepth = top + 1
        longPressPointer = pointer
        longPressX = change.position.x
        longPressY = change.position.y
        stack[top]?.armLongPress()
    }

    private fun disarmLongPress() {
        if (longPressDepth == 0) return
        longPressChain[longPressDepth - 1]?.cancelLongPress()
        for (index in 0 until longPressDepth) longPressChain[index] = null
        longPressDepth = 0
    }

    fun longPressed(owner: HostPointerNode) {
        if (longPressDepth == 0 || longPressChain[longPressDepth - 1] !== owner) return
        flush()
        for (index in longPressDepth - 1 downTo 0) {
            val entry = longPressChain[index] ?: continue
            if (entry.wants(PointerKind.ContextMenu) && entry.send(PointerKind.ContextMenu)) break
        }
        for (index in 0 until longPressDepth) longPressChain[index] = null
        longPressDepth = 0
    }
}