• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15import { int32 } from "@koalaui/common"
16import { bitsToPtr, ptrToBits, NativeString } from "@koalaui/interop"
17
18// Must match ArkUIAPIEventKind in api.h.
19export enum PeerEventKind {
20    Invalid = 0,
21    SinglePointerInput = 1,
22    MultiPointerInput = 2,
23    Callback = 3,
24    ComponentAsyncEvent = 4,
25    TextInput = 5,
26    GestureAsyncEvent = 6,
27    KeyInput = 7,
28    MouseInput = 8,
29}
30
31
32const enum ArkUINodeType {
33    ArkuiText = 1,
34    ArkuiSpan,
35    ArkuiImageSpan,
36    ArkuiImage,
37    ArkuiToggle,
38    ArkuiLoadingProgress,
39    ArkuiTextInput,
40    ArkuiStack,
41    ArkuiScroll,
42    ArkuiList,
43    ArkuiSwiper,
44    ArkuiTextarea,
45    ArkuiButton,
46    ArkuiProgress,
47    ArkuiCheckbox,
48    ArkuiColumn,
49    ArkuiRow,
50    ArkuiFlex,
51    ArkuiListItem,
52    ArkuiTabs,
53    ArkuiNavigator,
54    ArkuiWeb,
55    ArkuiSlider,
56    ArkuiCanvas,
57    ArkuiRadio,
58    ArkuiGrid,
59    ArkuiXcomponent,
60    ArkuiSidebar,
61    ArkuiRefresh,
62    ArkuiRoot,
63    ArkuiComponentRoot,
64    ArkuiCustom,
65    ArkuiNavigation,
66    ArkuiCustomContainerBox,
67    ArkuiListItemGroup,
68    ArkuiDatePicker,
69    ArkuiTimePicker,
70    ArkuiTextPicker,
71    ArkuiGridItem,
72}
73
74const ArkuiMaxEventNum = 1000
75
76enum EventOffsets {
77    Kind = 0,
78    SubKind = 1,
79    NodeId = 4
80}
81
82export enum PeerEnterKeyType {
83    Go,
84    Search,
85    Send,
86    Next,
87    Done,
88    PREVIOUS = 7,
89    NEW_LINE = 8,
90}
91
92export interface PeerSubmitEvent {
93    keepEditableState(): void;
94    text: string;
95}
96
97export enum ComponentAsyncEventSubKind {
98    //CommonEvents
99    OnAppear = 0,
100    OnDisappear = 1,
101    OnTouch = 2,
102    OnClick = 3,
103    OnHover = 4,
104    OnBlur = 5,
105    OnKeyEvent = 6,
106    OnMouse = 7,
107    OnAreaChange = 8,
108    OnVisibleAreaChange = 9,
109    OnGesture = 10,
110    OnFocus = 11,
111    OnImageComplete = ArkuiMaxEventNum * ArkUINodeType.ArkuiImage,
112    OnImageError = OnImageComplete + 1,
113    //ComponentsEvents
114
115    // List
116    OnListScroll = ArkuiMaxEventNum * ArkUINodeType.ArkuiList,
117    OnListScrollIndex = OnListScroll + 1,
118    OnListScrollStart = OnListScroll + 2,
119    OnListScrollStop = OnListScroll + 3,
120
121    // Toggle
122    OnToggleChange = ArkuiMaxEventNum * ArkUINodeType.ArkuiToggle,
123
124    // CheckBox
125    OnCheckboxChange = ArkuiMaxEventNum * ArkUINodeType.ArkuiCheckbox,
126
127    // TextInput
128    OnTextInputEditChange = ArkuiMaxEventNum * ArkUINodeType.ArkuiTextInput,
129    OnTextInputSubmit = OnTextInputEditChange + 1,
130    OnTextInputChange = OnTextInputEditChange + 2,
131
132    // TextArea
133    OnTextareaEditChange = ArkuiMaxEventNum * ArkUINodeType.ArkuiTextarea,
134    OnTextareaSubmit = OnTextareaEditChange + 1,
135    OnTextareaChange = OnTextareaEditChange + 2,
136
137    // Swiper
138    OnSwiperChange = ArkuiMaxEventNum * ArkUINodeType.ArkuiSwiper,
139    OnSwiperAnimationStart = OnSwiperChange + 1,
140    OnSwiperAnimationEnd = OnSwiperChange + 2,
141    OnSwiperGestureSwipe = OnSwiperChange + 3,
142
143    // Scroll
144    OnScroll = ArkuiMaxEventNum * ArkUINodeType.ArkuiScroll,
145    OnScrollFrameBegin = OnScroll + 1,
146    OnScrollStart = OnScroll + 2,
147    OnScrollStop = OnScroll + 3,
148    OnScrollEdge = OnScroll + 4,
149
150    // Navigation
151    OnNavBarStateChange = ArkuiMaxEventNum * ArkUINodeType.ArkuiNavigation,
152    NavDestination = OnNavBarStateChange + 1,
153
154    // Tabs
155    OnTabsChange = ArkuiMaxEventNum * ArkUINodeType.ArkuiTabs,
156
157    // Navigator
158    OnNavigatorClick = ArkuiMaxEventNum * ArkUINodeType.ArkuiNavigator,
159
160    // Web
161    OnWebIntercept = ArkuiMaxEventNum * ArkUINodeType.ArkuiWeb,
162
163    // Slider
164    OnSliderChange = ArkuiMaxEventNum * ArkUINodeType.ArkuiSlider,
165
166    // Canvas
167    OnCanvasReady = ArkuiMaxEventNum * ArkUINodeType.ArkuiCanvas,
168
169    // Radio
170    OnRadioChange = ArkuiMaxEventNum * ArkUINodeType.ArkuiRadio,
171
172    // Grid
173    OnGridScroll = ArkuiMaxEventNum * ArkUINodeType.ArkuiGrid,
174    OnGridStart = OnGridScroll + 1,
175    OnGridStop = OnGridScroll + 2,
176
177    // Sidebar
178    OnSidebarChange = ArkuiMaxEventNum * ArkUINodeType.ArkuiSidebar,
179
180    // XComponent
181    OnXComponentLoad = ArkuiMaxEventNum * ArkUINodeType.ArkuiXcomponent,
182    OnXComponentDestroy = OnXComponentLoad + 1,
183
184    // Refresh
185    OnRefreshStateChange = ArkuiMaxEventNum * ArkUINodeType.ArkuiRefresh,
186    OnRefreshRefreshing = OnRefreshStateChange + 1,
187
188    // DatePicker
189    OnDatePickerDateChange = ArkuiMaxEventNum * ArkUINodeType.ArkuiDatePicker,
190
191    // TimePicker
192    OnTimePickerChange = ArkuiMaxEventNum * ArkUINodeType.ArkuiTimePicker
193}
194
195export enum GestureAsyncEventSubKind {
196    OnAction = 0,
197    OnActionStart = 1,
198    OnActionUpdate = 2,
199    OnActionEnd = 3,
200    OnActionCancel = 4
201}
202
203export class PeerEvent {
204    public kind: number
205    public nodeId: number
206
207    static Kind = 0
208    static NodeId = 1
209
210    constructor(kind: number, nodeId: number) {
211        this.kind = kind
212        this.nodeId = nodeId
213    }
214    fromInt32Array(array: Int32Array): PeerEvent {
215        return new PeerEvent(array[PeerEvent.Kind], array[PeerEvent.NodeId])
216    }
217    toInt32Array(): Int32Array {
218        let result = new Int32Array(5)
219        result[PeerEvent.Kind] = this.kind
220        result[PeerEvent.NodeId] = this.nodeId
221        return result
222    }
223}
224
225export class AreaPosition {
226    public width: number
227    public height: number
228    public x: number
229    public y: number
230    public globalX: number
231    public globalY: number
232
233    constructor(width: number, height: number, x: number, y: number, globalX: number, globalY: number) {
234        this.width = width
235        this.height = height
236        this.x = x
237        this.y = y
238        this.globalX = globalX
239        this.globalY = globalY
240    }
241}
242
243export class AsyncComponentPeerEvent extends PeerEvent {
244    public subKind: ComponentAsyncEventSubKind
245    static SubKind = 16
246    constructor(nodeId: number, subKind: ComponentAsyncEventSubKind) {
247        super(PeerEventKind.ComponentAsyncEvent, nodeId)
248        this.subKind = subKind
249    }
250    static fromInt32Array(array: Int32Array): AsyncComponentPeerEvent {
251        return new AsyncComponentPeerEvent(
252            array[AsyncComponentPeerEvent.NodeId],
253            array[AsyncComponentPeerEvent.SubKind]
254        )
255    }
256    override toInt32Array(): Int32Array {
257        let result = new Int32Array(17)
258        result[AsyncComponentPeerEvent.Kind] = this.kind
259        result[AsyncComponentPeerEvent.NodeId] = this.nodeId
260        result[AsyncComponentPeerEvent.SubKind] = this.subKind
261        return result
262    }
263}
264
265export class RefreshStateChangePeerEvent extends AsyncComponentPeerEvent {
266    public state: number
267    static State = 6
268    constructor(nodeId: number, state: number) {
269        super(nodeId, ComponentAsyncEventSubKind.OnRefreshStateChange)
270        this.state = state
271    }
272    static fromInt32Array(array: Int32Array): RefreshStateChangePeerEvent {
273        return new RefreshStateChangePeerEvent(
274            array[RefreshStateChangePeerEvent.NodeId],
275            array[RefreshStateChangePeerEvent.State]
276        )
277    }
278    override toInt32Array(): Int32Array {
279        let result = new Int32Array(5)
280        result[RefreshStateChangePeerEvent.Kind] = this.kind
281        result[RefreshStateChangePeerEvent.SubKind] = this.subKind
282        result[RefreshStateChangePeerEvent.NodeId] = this.nodeId
283        result[RefreshStateChangePeerEvent.State] = this.state
284        return result
285    }
286}
287
288export class OnRefreshingPeerEvent extends AsyncComponentPeerEvent {
289    constructor(nodeId: number) {
290        super(nodeId, ComponentAsyncEventSubKind.OnRefreshRefreshing)
291    }
292}
293
294export class OnRadioChangePeerEvent extends AsyncComponentPeerEvent {
295    public isChecked: boolean
296    static Checked = 6
297    constructor(nodeId: number, isChecked: boolean) {
298        super(nodeId, ComponentAsyncEventSubKind.OnRadioChange)
299        this.isChecked = isChecked
300    }
301    override toInt32Array(): Int32Array {
302        let result = new Int32Array(6)
303        result[OnRadioChangePeerEvent.Kind] = this.kind
304        result[OnRadioChangePeerEvent.NodeId] = this.nodeId
305        result[OnRadioChangePeerEvent.SubKind] = this.subKind
306        result[OnRadioChangePeerEvent.Checked] = this.isChecked ? 1 : 0
307        return result
308    }
309}
310
311export class ImageCompletePeerEvent extends AsyncComponentPeerEvent {
312    public width: number
313    public height: number
314    public componentWidth: number
315    public componentHeight: number
316    public loadingStatus: number
317    public contentWidth: number
318    public contentHeight: number
319    public contentOffsetX: number
320    public contentOffsetY: number
321
322    static Width = 6
323    static Height = 7
324    static ComponentWidth = 8
325    static ComponentHeight = 9
326    static LoadingStatus = 10
327    static ContentWidth = 11
328    static ContentHeight = 12
329    static ContentOffsetX = 12
330    static ContentOffsetY = 13
331
332    constructor(nodeId: number, width: number, height: number, componentWidth: number, componentHeight: number,
333        loadingStatus: number, contentWidth: number, contentHeight: number, contentOffsetX: number, contentOffsetY: number) {
334        super(nodeId, ComponentAsyncEventSubKind.OnImageComplete)
335        this.width = width
336        this.height = height
337        this.componentWidth = componentWidth
338        this.componentHeight = componentHeight
339        this.loadingStatus = loadingStatus
340        this.contentWidth = contentWidth
341        this.contentHeight = contentHeight
342        this.contentOffsetX = contentOffsetX
343        this.contentOffsetY = contentOffsetY
344    }
345    static fromInt32Array(array: Int32Array): ImageCompletePeerEvent {
346        return new ImageCompletePeerEvent(
347            array[ImageCompletePeerEvent.NodeId],
348            array[ImageCompletePeerEvent.Width],
349            array[ImageCompletePeerEvent.Height],
350            array[ImageCompletePeerEvent.ComponentWidth],
351            array[ImageCompletePeerEvent.ComponentHeight],
352            array[ImageCompletePeerEvent.LoadingStatus],
353            array[ImageCompletePeerEvent.ContentWidth],
354            array[ImageCompletePeerEvent.ContentHeight],
355            array[ImageCompletePeerEvent.ContentOffsetX],
356            array[ImageCompletePeerEvent.ContentOffsetY],
357        )
358    }
359    override toInt32Array(): Int32Array {
360        let result = new Int32Array(14)
361        result[ImageCompletePeerEvent.Kind] = this.kind
362        result[ImageCompletePeerEvent.NodeId] = this.nodeId
363        result[ImageCompletePeerEvent.SubKind] = this.subKind
364        result[ImageCompletePeerEvent.Width] = this.width
365        result[ImageCompletePeerEvent.Height] = this.height
366        result[ImageCompletePeerEvent.ComponentWidth] = this.componentWidth
367        result[ImageCompletePeerEvent.ComponentHeight] = this.componentHeight
368        result[ImageCompletePeerEvent.LoadingStatus] = this.loadingStatus
369        result[ImageCompletePeerEvent.ContentWidth] = this.contentWidth
370        result[ImageCompletePeerEvent.ContentHeight] = this.contentHeight
371        result[ImageCompletePeerEvent.ContentOffsetX] = this.contentOffsetX
372        result[ImageCompletePeerEvent.ContentOffsetY] = this.contentOffsetY
373        return result
374    }
375}
376export class ImageErrorPeerEvent extends AsyncComponentPeerEvent {
377    public componentWidth: number
378    public componentHeight: number
379    public message: string
380
381    static StringLow = 6
382    static StringHigh = 7
383    static ComponentWidth = 8
384    static ComponentHeight = 9
385
386    constructor(nodeId: number, componentWidth: number, componentHeight: number, message: string) {
387        super(nodeId, ComponentAsyncEventSubKind.OnImageError)
388        this.componentWidth = componentWidth
389        this.componentHeight = componentHeight
390        this.message = message
391    }
392    static fromInt32Array(array: Int32Array): ImageErrorPeerEvent {
393        let errorMessage = new NativeString(bitsToPtr(array, ImageErrorPeerEvent.StringLow))
394        return new ImageErrorPeerEvent(
395            array[ImageErrorPeerEvent.NodeId],
396            array[ImageErrorPeerEvent.ComponentWidth],
397            array[ImageErrorPeerEvent.ComponentHeight],
398            errorMessage.toString())
399    }
400    override toInt32Array(): Int32Array {
401        let nativeStringPtr = NativeString.Make(this.message).release()
402        let result = new Int32Array(5)
403        result[ImageErrorPeerEvent.Kind] = this.kind
404        result[ImageErrorPeerEvent.NodeId] = this.nodeId
405        result[ImageErrorPeerEvent.SubKind] = this.subKind
406        let bits = ptrToBits(nativeStringPtr)!
407        result[ImageErrorPeerEvent.StringLow] = bits[0]
408        result[ImageErrorPeerEvent.StringHigh] = bits[1]
409        return result
410    }
411}
412
413export class BlurPeerEvent extends AsyncComponentPeerEvent {
414    constructor(nodeId: number) {
415        super(nodeId, ComponentAsyncEventSubKind.OnBlur)
416    }
417}
418
419export class HoverPeerEvent extends AsyncComponentPeerEvent {
420    public isHover: boolean
421    static IsHover = 6
422    constructor(nodeId: number, isHover: boolean) {
423        super(nodeId, ComponentAsyncEventSubKind.OnHover)
424        this.isHover = isHover
425    }
426    override toInt32Array(): Int32Array {
427        let result = new Int32Array(4)
428        result[HoverPeerEvent.Kind] = this.kind
429        result[HoverPeerEvent.NodeId] = this.nodeId
430        result[HoverPeerEvent.SubKind] = this.subKind
431        result[HoverPeerEvent.IsHover] = this.isHover ? 1 : 0
432        return result
433    }
434}
435
436export class CanvasReadyPeerEvent extends AsyncComponentPeerEvent {
437    constructor(nodeId: number) {
438        super(nodeId, ComponentAsyncEventSubKind.OnCanvasReady)
439    }
440    static fromInt32Array(array: Int32Array): CanvasReadyPeerEvent {
441        return new CanvasReadyPeerEvent(
442            array[CanvasReadyPeerEvent.NodeId]
443        )
444    }
445}
446export class ListScrollStartPeerEvent extends AsyncComponentPeerEvent {
447    constructor(nodeId: number) {
448        super(nodeId, ComponentAsyncEventSubKind.OnListScrollStart)
449    }
450}
451
452export class ListScrollStopPeerEvent extends AsyncComponentPeerEvent {
453    constructor(nodeId: number) {
454        super(nodeId, ComponentAsyncEventSubKind.OnListScrollStop)
455    }
456}
457
458export class KeyPeerEvent extends PeerEvent {
459    public type: number
460    public keyCode: number
461    public keyText: string
462    public keySource: number
463    public deviceId: number
464    public metaKey: number
465    public timestamp: number
466    public intentionCode: number
467    public stopPropagation: () => void
468    public getModifierKeyState: (keys: string[]) => boolean
469
470    constructor(
471        nodeId: number,
472        type: number,
473        keyCode: number,
474        keyText: string,
475        keySource: number,
476        deviceId: number,
477        metaKey: number,
478        timestamp: number,
479        intentionCode: number,
480        stopPropagation?: () => void,
481        getModifierKeyState?: (keys: string[]) => boolean
482    ) {
483        super(PeerEventKind.KeyInput, nodeId)
484        this.type = type
485        this.keyCode = keyCode
486        this.keyText = keyText
487        this.keySource = keySource
488        this.deviceId = deviceId
489        this.metaKey = metaKey
490        this.timestamp = timestamp
491        this.intentionCode = intentionCode
492        this.stopPropagation = stopPropagation ?? (() => {})
493        this.getModifierKeyState = getModifierKeyState ?? ((keys: string[]) => { return false })
494    }
495    override toInt32Array(): Int32Array {
496        let result = new Int32Array(9)
497        result[0] = this.kind
498        result[1] = this.nodeId
499        result[2] = this.type
500        result[3] = this.keyCode
501        result[4] = this.keySource
502        result[5] = this.deviceId
503        result[6] = this.metaKey
504        result[7] = this.timestamp
505        result[8] = this.intentionCode
506        return result
507    }
508}
509
510export class MousePeerEvent extends PeerEvent {
511    public button: number
512    public action: number
513    public displayX: number
514    public displayY: number
515    public windowX: number
516    public windowY: number
517    public screenX: number
518    public screenY: number
519    public x: number
520    public y: number
521    public stopPropagation: () => void
522
523    static Button = 6
524    static Action = 7
525    static DisplayX = 8
526    static DisplayY = 9
527    static WindowX = 10
528    static WindowY = 11
529    static ScreenX = 12
530    static ScreenY = 13
531    static X = 14
532    static Y = 15
533
534    constructor(
535        nodeId: number,
536        button: number,
537        action: number,
538        displayX: number,
539        displayY: number,
540        windowX: number,
541        windowY: number,
542        screenX: number,
543        screenY: number,
544        x: number,
545        y: number,
546        stopPropagation?: () => void
547    ) {
548        super(PeerEventKind.SinglePointerInput, nodeId)
549        this.button = button
550        this.action = action
551        this.displayX = displayX
552        this.displayY = displayY
553        this.windowX = windowX
554        this.windowY = windowY
555        this.screenX = screenX
556        this.screenY = screenY
557        this.x = x
558        this.y = y
559        this.stopPropagation = stopPropagation ?? (() => {})
560    }
561    static fromInt32Array(array: Int32Array): MousePeerEvent {
562        return new MousePeerEvent(
563            array[MousePeerEvent.NodeId],
564            array[MousePeerEvent.Button],
565            array[MousePeerEvent.Action],
566            array[MousePeerEvent.DisplayX],
567            array[MousePeerEvent.DisplayY],
568            array[MousePeerEvent.WindowX],
569            array[MousePeerEvent.WindowY],
570            array[MousePeerEvent.ScreenX],
571            array[MousePeerEvent.ScreenY],
572            array[MousePeerEvent.X],
573            array[MousePeerEvent.Y]
574        )
575    }
576    override toInt32Array(): Int32Array {
577        let result = new Int32Array(16)
578        result[MousePeerEvent.Kind] = this.kind
579        result[MousePeerEvent.NodeId] = this.nodeId
580        result[MousePeerEvent.Button] = this.button
581        result[MousePeerEvent.Action] = this.action
582        result[MousePeerEvent.DisplayX] = this.displayX
583        result[MousePeerEvent.DisplayY] = this.displayY
584        result[MousePeerEvent.WindowX] = this.windowX
585        result[MousePeerEvent.WindowY] = this.windowY
586        result[MousePeerEvent.ScreenX] = this.screenX
587        result[MousePeerEvent.ScreenY] = this.screenY
588        result[MousePeerEvent.X] = this.x
589        result[MousePeerEvent.Y] = this.y
590        return result
591    }
592}
593
594export class SinglePointerPeerEvent extends PeerEvent {
595    static X = 4
596    static Y = 5
597    static State = 6
598
599    public x: number
600    public y: number
601    public state: number
602
603    constructor(nodeId: number, x: number, y: number, state: number) {
604        super(PeerEventKind.SinglePointerInput, nodeId)
605        this.state = state
606        this.y = y
607        this.x = x
608    }
609    static fromInt32Array(array: Int32Array): SinglePointerPeerEvent {
610        return new SinglePointerPeerEvent(
611            array[SinglePointerPeerEvent.NodeId],
612            array[SinglePointerPeerEvent.X],
613            array[SinglePointerPeerEvent.Y],
614            array[SinglePointerPeerEvent.State]
615        )
616    }
617    override toInt32Array(): Int32Array {
618        let result = new Int32Array(10)
619        result[SinglePointerPeerEvent.Kind] = this.kind
620        result[SinglePointerPeerEvent.NodeId] = this.nodeId
621        result[SinglePointerPeerEvent.X] = this.x
622        result[SinglePointerPeerEvent.Y] = this.y
623        result[SinglePointerPeerEvent.State] = this.state
624        return result
625    }
626}
627
628export class MultiPointerPeerEvent extends PeerEvent {
629    public xs: Int32Array
630    public ys: Int32Array
631    public status: Int32Array
632
633    constructor(nodeId: number, xs: Int32Array, ys: Int32Array, status: Int32Array) {
634        super(PeerEventKind.MultiPointerInput, nodeId)
635        this.status = status
636        this.ys = ys
637        this.xs = xs
638    }
639    static fromInt32Array(array: Int32Array): MultiPointerPeerEvent {
640        throw new Error()
641    }
642    override toInt32Array(): Int32Array {
643        let result = new Int32Array(34)
644        result[0] = this.kind
645        result[1] = this.nodeId
646        let count = this.xs.length
647        result[2] = count
648        for (let i = 0; i < count; i++) {
649            result[3 + i] = this.xs[i]
650        }
651        for (let i = 0; i < count; i++) {
652            result[13 + i] = this.ys[i]
653        }
654        for (let i = 0; i < count; i++) {
655            result[23 + i] = this.status[i]
656        }
657        return result
658    }
659}
660
661export class TextInputEvent extends PeerEvent {
662    public text: string
663
664    static TextLow = 6
665    static TextHigh = 7
666
667    constructor(nodeId: number, text: string) {
668        super(PeerEventKind.TextInput, nodeId)
669        this.text = text
670    }
671    static fromInt32Array(array: Int32Array): TextInputEvent {
672        return new TextInputEvent(
673            array[TextInputEvent.NodeId],
674            new NativeString(bitsToPtr(array, TextInputEvent.TextLow)).toString()
675        )
676    }
677    override toInt32Array(): Int32Array {
678        let nativeStringPtr = NativeString.Make(this.text).release()
679        let result = new Int32Array(8)
680        result[TextInputEvent.Kind] = this.kind
681        result[TextInputEvent.NodeId] = this.nodeId
682        let bits = ptrToBits(nativeStringPtr)!
683        result[TextInputEvent.TextLow] = bits[0]
684        result[TextInputEvent.TextHigh] = bits[1]
685        return result
686    }
687}
688
689export class XComponentLoadPeerEvent extends AsyncComponentPeerEvent {
690    public id: string
691    constructor(nodeId: number, id: string) {
692        super(nodeId, ComponentAsyncEventSubKind.OnXComponentLoad)
693        this.id = id
694    }
695
696    override toInt32Array(): Int32Array {
697        let nativeStringPtr = NativeString.Make(this.id).release()
698        let result = new Int32Array(4)
699        result[0] = this.kind
700        result[1] = this.nodeId
701        let bits = ptrToBits(nativeStringPtr)!
702        result[2] = bits[0]
703        result[3] = bits[1]
704        return result
705    }
706}
707
708export class XComponentDestroyPeerEvent extends AsyncComponentPeerEvent {
709    constructor(nodeId: number) {
710        super(nodeId, ComponentAsyncEventSubKind.OnXComponentDestroy)
711    }
712}
713
714export class CallbackPeerEvent extends PeerEvent {
715    static currentId = 1
716    static callbackById = new Map<number, CallbackPeerEvent>()
717
718    public id: number
719    public callback: (args: Int32Array) => void
720    public autoDisposable = true
721
722    static Id = 4
723    static NumArgs = 5
724    static ContinuationId = 6
725
726    constructor(callback: (args: Int32Array) => void, autoDisposable: boolean = true) {
727        super(PeerEventKind.Callback, 0)
728        this.autoDisposable = autoDisposable
729        this.callback = callback
730        this.id = CallbackPeerEvent.currentId++
731        this.callback = callback
732        this.autoDisposable = autoDisposable
733        CallbackPeerEvent.callbackById.set(this.id, this)
734    }
735
736    static byId(id: number): CallbackPeerEvent | undefined {
737        return CallbackPeerEvent.callbackById.get(id)
738    }
739
740    static wrap(callback: (args: Int32Array) => void, autoDisposable: boolean = true): number {
741        return new CallbackPeerEvent(callback, autoDisposable).id
742    }
743
744    dispose() {
745        CallbackPeerEvent.callbackById.delete(this.id)
746    }
747
748    invoke(continuationId: number, args: Int32Array) {
749        this.callback(args)
750    }
751}
752
753export class AsyncGesturePeerEvent extends PeerEvent {
754    public subKind: GestureAsyncEventSubKind
755    constructor(nodeId: number, subKind: GestureAsyncEventSubKind) {
756        super(PeerEventKind.GestureAsyncEvent, nodeId)
757        this.subKind = subKind
758    }
759}
760
761export class AppearPeerEvent extends AsyncComponentPeerEvent {
762    constructor(nodeId: number) {
763        super(nodeId, ComponentAsyncEventSubKind.OnAppear)
764    }
765}
766
767export class DisappearPeerEvent extends AsyncComponentPeerEvent {
768    constructor(nodeId: number) {
769        super(nodeId, ComponentAsyncEventSubKind.OnDisappear)
770    }
771}
772
773export class SwiperChangePeerEvent extends AsyncComponentPeerEvent {
774    public index: int32
775    constructor(nodeId: number, index: int32) {
776        super(nodeId, ComponentAsyncEventSubKind.OnSwiperChange)
777        this.index = index
778    }
779    override toInt32Array(): Int32Array {
780        let result = new Int32Array(4)
781        result[0] = this.kind
782        result[1] = this.nodeId
783        result[2] = this.subKind
784        result[3] = this.index
785        return result
786    }
787}
788
789export class TabsChangePeerEvent extends AsyncComponentPeerEvent {
790    public index: int32
791    constructor(nodeId: number, index: int32) {
792        super(nodeId, ComponentAsyncEventSubKind.OnTabsChange)
793        this.index = index
794    }
795    override toInt32Array(): Int32Array {
796        let result = new Int32Array(4)
797        result[0] = this.kind
798        result[1] = this.nodeId
799        result[2] = this.subKind
800        result[3] = this.index
801        return result
802    }
803}
804
805export class VisibleAreaChangePeerEvent extends AsyncComponentPeerEvent {
806    public isVisible: boolean
807    public currentRatio: number
808
809    constructor(nodeId: number, isVisible: boolean, currentRatio: number) {
810        super(nodeId, ComponentAsyncEventSubKind.OnVisibleAreaChange)
811        this.currentRatio = currentRatio
812        this.isVisible = isVisible
813    }
814    override toInt32Array(): Int32Array {
815        let result = new Int32Array(5)
816        result[0] = this.kind
817        result[1] = this.nodeId
818        result[2] = this.subKind
819        result[3] = this.isVisible ? 1 : 0
820        result[4] = this.currentRatio
821        return result
822    }
823}
824
825export class ScrollIndexPeerEvent extends AsyncComponentPeerEvent {
826    public first: number
827    public last: number
828
829
830    constructor(nodeId: number, first: number, last: number) {
831        super(nodeId, ComponentAsyncEventSubKind.OnListScrollIndex)
832        this.first = first
833        this.last = last
834    }
835
836    override toInt32Array(): Int32Array {
837        let result = new Int32Array(5)
838        result[0] = this.kind
839        result[1] = this.nodeId
840        result[2] = this.subKind
841        result[3] = this.first
842        result[4] = this.last
843        return result
844    }
845}
846
847export class GesturePeerEvent extends AsyncGesturePeerEvent {
848    public gestureEvent: Int32Array
849    constructor(nodeId: number, subKind: GestureAsyncEventSubKind, gestureEvent: Int32Array) {
850        super(nodeId,  subKind)
851        this.gestureEvent = gestureEvent;
852    }
853    override toInt32Array(): Int32Array {
854        let result = new Int32Array(3);
855        result[0] = this.kind
856        result[1] = this.nodeId
857        result[2] = this.subKind
858        return result
859    }
860}
861
862export class NavigatorClickPeerEvent extends AsyncComponentPeerEvent {
863    constructor(nodeId: number) {
864        super(nodeId, ComponentAsyncEventSubKind.OnNavigatorClick)
865    }
866}
867
868export class SwiperAnimationPeerEvent extends AsyncComponentPeerEvent {
869    public index: number
870    public targetIndex: number
871    public currentOffset: number
872    public targetOffset: number
873    public velocity: number
874
875    static Index = 6
876    static TargetIndex = 7
877    static CurrentOffset = 8
878    static TargetOffset = 9
879    static Velocity = 10
880
881    constructor(
882        nodeId: number,
883        index: number,
884        targetIndex: number,
885        currentOffset: number,
886        targetOffset: number,
887        velocity: number
888    ) {
889        super(nodeId, ComponentAsyncEventSubKind.OnListScrollIndex)
890        this.index = index
891        this.targetIndex = targetIndex
892        this.currentOffset = currentOffset
893        this.targetOffset = targetOffset
894        this.velocity = velocity
895    }
896    static fromInt32Array(array: Int32Array): SwiperAnimationPeerEvent {
897        return new SwiperAnimationPeerEvent(
898            array[SwiperAnimationPeerEvent.NodeId],
899            array[SwiperAnimationPeerEvent.Index],
900            array[SwiperAnimationPeerEvent.TargetIndex],
901            array[SwiperAnimationPeerEvent.CurrentOffset],
902            array[SwiperAnimationPeerEvent.TargetOffset],
903            array[SwiperAnimationPeerEvent.Velocity]
904        )
905    }
906    override toInt32Array(): Int32Array {
907        let result = new Int32Array(8)
908        result[SwiperAnimationPeerEvent.Kind] = this.kind
909        result[SwiperAnimationPeerEvent.SubKind] = this.subKind
910        result[SwiperAnimationPeerEvent.NodeId] = this.nodeId
911        result[SwiperAnimationPeerEvent.Index] = this.index
912        result[SwiperAnimationPeerEvent.TargetIndex] = this.targetIndex
913        result[SwiperAnimationPeerEvent.CurrentOffset] = this.currentOffset
914        result[SwiperAnimationPeerEvent.TargetOffset] = this.targetOffset
915        result[SwiperAnimationPeerEvent.Velocity] = this.velocity
916        return result
917    }
918}
919
920export class SliderChangePeerEvent extends AsyncComponentPeerEvent {
921    public value: number
922    public mode: number
923    constructor(nodeId: number, value: number, mode: number) {
924        super(nodeId, ComponentAsyncEventSubKind.OnSwiperChange)
925        this.value = value
926        this.mode = mode
927    }
928    override toInt32Array(): Int32Array {
929        let result = new Int32Array(5)
930        result[0] = this.kind
931        result[1] = this.nodeId
932        result[2] = this.subKind
933        result[3] = this.value
934        result[4] = this.mode
935        return result
936    }
937}
938export class ListScrollPeerEvent extends AsyncComponentPeerEvent {
939    public scrollOffset: number
940    public scrollState: number
941
942    static ScrollOffset = 6
943    static ScrollState = 7
944
945    constructor(nodeId: number, scrollOffset: number, scrollState: number) {
946        super(nodeId, ComponentAsyncEventSubKind.OnListScroll)
947        this.scrollOffset = scrollOffset
948        this.scrollState = scrollState
949    }
950    static fromInt32Array(array: Int32Array): ListScrollPeerEvent {
951        return new ListScrollPeerEvent(
952            array[ListScrollPeerEvent.NodeId],
953            array[ListScrollPeerEvent.ScrollOffset],
954            array[ListScrollPeerEvent.ScrollState]
955        )
956    }
957    override toInt32Array(): Int32Array {
958        let result = new Int32Array(5)
959        result[ListScrollPeerEvent.Kind] = this.kind
960        result[ListScrollPeerEvent.NodeId] = this.nodeId
961        result[ListScrollPeerEvent.SubKind] = this.subKind
962        result[ListScrollPeerEvent.ScrollOffset] = this.scrollOffset
963        result[ListScrollPeerEvent.ScrollState] = this.scrollState
964        return result
965    }
966}
967
968export class AreaChangePeerEvent extends AsyncComponentPeerEvent {
969
970    public oldValue: AreaPosition
971    public newValue: AreaPosition
972
973    constructor(nodeId: number, oldValue: AreaPosition, newValue: AreaPosition) {
974        super(nodeId, ComponentAsyncEventSubKind.OnSwiperChange)
975        this.oldValue = oldValue
976        this.newValue = newValue
977    }
978    static fromInt32Array(array: Int32Array): AreaChangePeerEvent {
979        throw new Error()
980    }
981    override toInt32Array(): Int32Array {
982        let result = new Int32Array(3)
983        result[0] = this.kind
984        result[1] = this.nodeId
985        result[2] = this.subKind
986        return result
987    }
988}
989export class ListScrollIndexPeerEvent extends AsyncComponentPeerEvent {
990    public start: number
991    public end: number
992    public center: number
993
994    constructor(nodeId: number, start: number, end: number, center: number) {
995        super(nodeId, ComponentAsyncEventSubKind.OnListScrollIndex)
996        this.start = start
997        this.end = end
998        this.center = center
999    }
1000
1001    override toInt32Array(): Int32Array {
1002        let result = new Int32Array(6)
1003        result[0] = this.kind
1004        result[1] = this.nodeId
1005        result[2] = this.subKind
1006        result[3] = this.start
1007        result[4] = this.end
1008        result[5] = this.center
1009        return result
1010    }
1011}
1012
1013export class GridScrollPeerEvent extends AsyncComponentPeerEvent {
1014    public scrollOffset: number
1015    public scrollState: number
1016
1017    constructor(nodeId: number, scrollOffset: number, scrollState: number) {
1018        super(nodeId, ComponentAsyncEventSubKind.OnGridScroll)
1019        this.scrollOffset = scrollOffset
1020        this.scrollState = scrollState
1021    }
1022
1023    override toInt32Array(): Int32Array {
1024        let result = new Int32Array(5)
1025        result[0] = this.kind
1026        result[1] = this.nodeId
1027        result[2] = this.subKind
1028        result[3] = this.scrollOffset
1029        result[4] = this.scrollState
1030        return result
1031    }
1032}
1033
1034export class GridScrollStartPeerEvent extends AsyncComponentPeerEvent {
1035    constructor(nodeId: number) {
1036        super(nodeId, ComponentAsyncEventSubKind.OnGridStart)
1037    }
1038}
1039
1040export class GridScrollStopPeerEvent extends AsyncComponentPeerEvent {
1041    constructor(nodeId: number) {
1042        super(nodeId, ComponentAsyncEventSubKind.OnGridStop)
1043    }
1044}
1045
1046export class SideBarChangePeerEvent extends AsyncComponentPeerEvent {
1047    public isChanged: boolean
1048    constructor(nodeId: number,changed: boolean) {
1049        super(nodeId, ComponentAsyncEventSubKind.OnSidebarChange)
1050        this.isChanged = changed
1051    }
1052
1053    override toInt32Array(): Int32Array {
1054        let result = new Int32Array(4)
1055        result[0] = this.kind
1056        result[1] = this.nodeId
1057        result[2] = this.subKind
1058        result[3] = this.isChanged ? 1 : 0
1059        return result
1060    }
1061}
1062
1063export class NavBarStateChangePeerEvent extends AsyncComponentPeerEvent {
1064    public onVisible: boolean
1065    constructor(nodeId: number, onVisible: boolean) {
1066        super(nodeId, ComponentAsyncEventSubKind.OnNavBarStateChange)
1067        this.onVisible = onVisible
1068    }
1069
1070    override toInt32Array(): Int32Array {
1071        let result = new Int32Array(4)
1072        result[0] = this.kind
1073        result[1] = this.nodeId
1074        result[2] = this.subKind
1075        result[3] = this.onVisible ? 1 : 0
1076        return result
1077    }
1078}
1079
1080export class NavDestinationPeerEvent extends AsyncComponentPeerEvent {
1081    public name: string
1082    public param: unknown
1083    constructor(nodeId: number, name: string, param: unknown) {
1084        super(nodeId, ComponentAsyncEventSubKind.NavDestination)
1085        this.name = name
1086        this.param = param
1087    }
1088
1089    override toInt32Array(): Int32Array {
1090        let nativeStringPtr = NativeString.Make(this.name).release()
1091        let result = new Int32Array(4)
1092        result[0] = this.kind
1093        result[1] = this.nodeId
1094        let bits = ptrToBits(nativeStringPtr)!
1095        result[2] = bits[0]
1096        result[3] = bits[1]
1097        return result
1098    }
1099}
1100