waterui-ffi 0.3.0

FFI bindings for the WaterUI cross-platform UI framework
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
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
//! Type conversion traits for Rust ↔ Java interop.
//!
//! These traits provide bidirectional conversion between Rust types and
//! Java types across the JNI boundary.

extern crate alloc;
extern crate std;

use alloc::vec::Vec;
use alloc::{boxed::Box, string::String};
use jni::objects::{JCharArray, JObject, JString, JValue};
use jni::sys::{jboolean, jdouble, jfloat, jint, jlong};
use jni::{Env as JNIEnv, jni_sig, jni_str};

/// Copies a Java string through its UTF-16 representation without lossy replacement.
pub(crate) fn string_from_java<'local>(env: &mut JNIEnv<'local>, java: &JString<'local>) -> String {
    let chars = env
        .call_method(java, jni_str!("toCharArray"), jni_sig!("()[C"), &[])
        .expect("Java String.toCharArray failed")
        .l()
        .expect("String.toCharArray returned a non-array value");
    let chars = env
        .cast_local::<JCharArray>(chars)
        .expect("String.toCharArray returned a non-char-array value");
    let len = chars.len(env).expect("failed to read Java string length");
    let mut utf16 = alloc::vec![0; len];
    chars
        .get_region(env, 0, &mut utf16)
        .expect("failed to copy Java string");
    String::from_utf16(&utf16).expect("Java string contains an unpaired UTF-16 surrogate")
}

/// Releases an owned FFI array after its elements have been copied or
/// transferred into Java owner objects.
///
/// # Safety
///
/// `array` must be the sole owning FFI array and must not be consumed again.
unsafe fn consume_ffi_array<T>(array: &crate::array::WuiArray<T>) {
    unsafe { core::ptr::read(array).consume() };
}

// ============================================================================
// JniPrimitive trait for macro-based code generation
// ============================================================================

/// Trait for types that can be directly passed across JNI without heap allocation.
///
/// This trait is used by `jni_binding_primitive!` and `jni_computed_primitive!` macros
/// to generate read/set functions for reactive bindings.
///
pub trait JniPrimitive: Sized + 'static {
    /// The corresponding JNI primitive type (jboolean, jint, jfloat, jdouble, jlong)
    type Jni;
    /// Convert from Rust to JNI
    fn to_jni(self) -> Self::Jni;
    /// Convert from JNI to Rust
    fn from_jni(val: Self::Jni) -> Self;
}

impl JniPrimitive for bool {
    type Jni = jboolean;
    fn to_jni(self) -> Self::Jni {
        self
    }
    fn from_jni(val: Self::Jni) -> Self {
        val
    }
}

impl JniPrimitive for i32 {
    type Jni = jint;
    fn to_jni(self) -> Self::Jni {
        self
    }
    fn from_jni(val: Self::Jni) -> Self {
        val
    }
}

impl JniPrimitive for f32 {
    type Jni = jfloat;
    fn to_jni(self) -> Self::Jni {
        self
    }
    fn from_jni(val: Self::Jni) -> Self {
        val
    }
}

impl JniPrimitive for f64 {
    type Jni = jdouble;
    fn to_jni(self) -> Self::Jni {
        self
    }
    fn from_jni(val: Self::Jni) -> Self {
        val
    }
}

// ============================================================================
// Pointer conversions
// ============================================================================

/// Convert jlong back to a raw pointer.
///
/// # Safety
/// The jlong must have been created from a valid pointer of type T.
#[inline]
pub unsafe fn jlong_to_ptr<T>(val: jlong) -> *const T {
    val as *const T
}

/// Convert jlong back to a mutable raw pointer.
///
/// # Safety
/// The jlong must have been created from a valid pointer of type T.
#[inline]
pub unsafe fn jlong_to_ptr_mut<T>(val: jlong) -> *mut T {
    val as *mut T
}

// ============================================================================
// Struct to Java conversion
// ============================================================================

/// Trait for FFI structs that can be converted to Java objects.
pub trait ToJavaStruct {
    /// Convert this FFI struct to a Java object.
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local>;
}

/// Convert an FFI struct to a Java object.
///
/// This uses the `ToJavaStruct` trait to perform type-specific conversions.
pub fn struct_to_java<'local, T: ToJavaStruct>(
    env: &mut JNIEnv<'local>,
    value: &T,
) -> JObject<'local> {
    value.to_java_struct(env)
}

// ============================================================================
// ToJavaStruct implementations for Metadata types
// ============================================================================

use crate::{WuiEnv, WuiMetadata};

/// MetadataEnvStruct(contentPtr: Long, envPtr: Long)
impl ToJavaStruct for WuiMetadata<*mut WuiEnv> {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let class = env
            .find_class(jni_str!("dev/waterui/android/runtime/MetadataEnvStruct"))
            .expect("MetadataEnvStruct class not found");
        env.new_object(
            &class,
            jni_sig!("(JJ)V"),
            &[
                JValue::Long(self.content as jlong),
                JValue::Long(self.value as jlong),
            ],
        )
        .expect("Failed to create MetadataEnvStruct")
    }
}

/// MetadataNavigationTransitionStruct(contentPtr: Long, id: Int)
impl ToJavaStruct for WuiMetadata<crate::id::WuiId> {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let class = env
            .find_class(jni_str!(
                "dev/waterui/android/runtime/MetadataNavigationTransitionStruct"
            ))
            .expect("MetadataNavigationTransitionStruct class not found");
        env.new_object(
            &class,
            jni_sig!("(JI)V"),
            &[
                JValue::Long(self.content as jlong),
                JValue::Int(self.value.inner),
            ],
        )
        .expect("Failed to create MetadataNavigationTransitionStruct")
    }
}

/// MetadataSecureStruct(contentPtr: Long)
/// Note: WuiSecureMarker is just a marker type, no additional data needed
impl ToJavaStruct for crate::WuiMetadataSecure {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let class = env
            .find_class(jni_str!("dev/waterui/android/runtime/MetadataSecureStruct"))
            .expect("MetadataSecureStruct class not found");
        env.new_object(
            &class,
            jni_sig!("(J)V"),
            &[JValue::Long(self.content as jlong)],
        )
        .expect("Failed to create MetadataSecureStruct")
    }
}

/// MetadataLifecycleHookStruct(contentPtr: Long, lifecycleType: Int, handlerPtr: Long)
impl ToJavaStruct for crate::WuiMetadataLifecycleHook {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let class = env
            .find_class(jni_str!(
                "dev/waterui/android/runtime/MetadataLifecycleHookStruct"
            ))
            .expect("MetadataLifecycleHookStruct class not found");
        env.new_object(
            &class,
            jni_sig!("(JIJ)V"),
            &[
                JValue::Long(self.content as jlong),
                JValue::Int(self.value.lifecycle as i32),
                JValue::Long(self.value.handler as jlong),
            ],
        )
        .expect("Failed to create MetadataLifecycleHookStruct")
    }
}

/// MetadataGestureStruct(contentPtr: Long, gestureType: Int, gestureData: GestureDataStruct, actionPtr: Long)
/// Note: WuiGestureObserver has `gesture: WuiGesture` and `action: *mut WuiAction`
fn gesture_parts(gesture: &crate::gesture::WuiGesture) -> (i32, i32, i32, f32, f32, f32, i64, i64) {
    match gesture {
        crate::gesture::WuiGesture::Tap { count } => (
            0i32,
            *count as i32,
            0i32,
            0.0f32,
            0.0f32,
            0.0f32,
            0i64,
            0i64,
        ),
        crate::gesture::WuiGesture::LongPress { duration } => (
            1i32,
            0i32,
            *duration as i32,
            0.0f32,
            0.0f32,
            0.0f32,
            0i64,
            0i64,
        ),
        crate::gesture::WuiGesture::Drag { min_distance } => {
            (2i32, 0i32, 0i32, *min_distance, 0.0f32, 0.0f32, 0i64, 0i64)
        }
        crate::gesture::WuiGesture::Magnification { initial_scale } => {
            (3i32, 0i32, 0i32, 0.0f32, *initial_scale, 0.0f32, 0i64, 0i64)
        }
        crate::gesture::WuiGesture::Rotation { initial_angle } => {
            (4i32, 0i32, 0i32, 0.0f32, 0.0f32, *initial_angle, 0i64, 0i64)
        }
        crate::gesture::WuiGesture::Then { first, then } => (
            5i32,
            0i32,
            0i32,
            0.0f32,
            0.0f32,
            0.0f32,
            *first as jlong,
            *then as jlong,
        ),
        crate::gesture::WuiGesture::Simultaneous { first, second } => (
            6i32,
            0i32,
            0i32,
            0.0f32,
            0.0f32,
            0.0f32,
            *first as jlong,
            *second as jlong,
        ),
        crate::gesture::WuiGesture::Exclusive { first, second } => (
            7i32,
            0i32,
            0i32,
            0.0f32,
            0.0f32,
            0.0f32,
            *first as jlong,
            *second as jlong,
        ),
    }
}

fn gesture_data_to_java<'local>(
    env: &mut JNIEnv<'local>,
    gesture_data_class: &jni::objects::JClass<'local>,
    gesture: &crate::gesture::WuiGesture,
) -> JObject<'local> {
    let (
        _gesture_type,
        tap_count,
        long_press_duration,
        drag_min_distance,
        magnification_scale,
        rotation_angle,
        first_ptr,
        second_ptr,
    ) = gesture_parts(gesture);

    env.new_object(
        gesture_data_class,
        jni_sig!("(IIFFFJJ)V"),
        &[
            JValue::Int(tap_count),
            JValue::Int(long_press_duration),
            JValue::Float(drag_min_distance),
            JValue::Float(magnification_scale),
            JValue::Float(rotation_angle),
            JValue::Long(first_ptr),
            JValue::Long(second_ptr),
        ],
    )
    .expect("Failed to create GestureDataStruct")
}

impl ToJavaStruct for crate::gesture::WuiGesture {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let gesture_data_class = env
            .find_class(jni_str!("dev/waterui/android/runtime/GestureDataStruct"))
            .expect("GestureDataStruct class not found");
        let class = env
            .find_class(jni_str!("dev/waterui/android/runtime/GestureStruct"))
            .expect("GestureStruct class not found");
        let (gesture_type, _, _, _, _, _, _, _) = gesture_parts(self);
        let gesture_data = gesture_data_to_java(env, &gesture_data_class, self);
        env.new_object(
            &class,
            jni_sig!("(ILdev/waterui/android/runtime/GestureDataStruct;)V"),
            &[JValue::Int(gesture_type), JValue::Object(&gesture_data)],
        )
        .expect("Failed to create GestureStruct")
    }
}

impl ToJavaStruct for crate::WuiMetadataGesture {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        // Create GestureDataStruct first based on gesture type
        let gesture_data_class = env
            .find_class(jni_str!("dev/waterui/android/runtime/GestureDataStruct"))
            .expect("GestureDataStruct class not found");
        let (gesture_type, _, _, _, _, _, _, _) = gesture_parts(&self.value.gesture);
        let gesture_data = gesture_data_to_java(env, &gesture_data_class, &self.value.gesture);

        let class = env
            .find_class(jni_str!(
                "dev/waterui/android/runtime/MetadataGestureStruct"
            ))
            .expect("MetadataGestureStruct class not found");
        env.new_object(
            &class,
            jni_sig!("(JILdev/waterui/android/runtime/GestureDataStruct;J)V"),
            &[
                JValue::Long(self.content as jlong),
                JValue::Int(gesture_type),
                JValue::Object(&gesture_data),
                JValue::Long(self.value.action as jlong),
            ],
        )
        .expect("Failed to create MetadataGestureStruct")
    }
}

/// MetadataOnEventStruct(contentPtr: Long, eventType: Int, handlerPtr: Long)
impl ToJavaStruct for crate::WuiMetadataOnEvent {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let class = env
            .find_class(jni_str!(
                "dev/waterui/android/runtime/MetadataOnEventStruct"
            ))
            .expect("MetadataOnEventStruct class not found");
        env.new_object(
            &class,
            jni_sig!("(JIJ)V"),
            &[
                JValue::Long(self.content as jlong),
                JValue::Int(self.value.event as i32),
                JValue::Long(self.value.handler as jlong),
            ],
        )
        .expect("Failed to create MetadataOnEventStruct")
    }
}

/// MetadataCursorStruct(contentPtr: Long, stylePtr: Long)
impl ToJavaStruct for crate::WuiMetadataCursor {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let class = env
            .find_class(jni_str!("dev/waterui/android/runtime/MetadataCursorStruct"))
            .expect("MetadataCursorStruct class not found");
        env.new_object(
            &class,
            jni_sig!("(JJ)V"),
            &[
                JValue::Long(self.content as jlong),
                JValue::Long(self.value.style as jlong),
            ],
        )
        .expect("Failed to create MetadataCursorStruct")
    }
}

/// MetadataAccessibilityIdentifierStruct(contentPtr: Long, identifier: String)
impl ToJavaStruct for crate::WuiIgnorableMetadataAccessibilityIdentifier {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let identifier: waterui::Str =
            unsafe { crate::IntoRust::into_rust(core::ptr::read(&self.identifier)) };
        let identifier = env
            .new_string(identifier.as_str())
            .expect("Failed to create identifier string");
        let class = env
            .find_class(jni_str!(
                "dev/waterui/android/runtime/MetadataAccessibilityIdentifierStruct"
            ))
            .expect("MetadataAccessibilityIdentifierStruct class not found");
        env.new_object(
            &class,
            jni_sig!("(JLjava/lang/String;)V"),
            &[
                JValue::Long(self.content as jlong),
                JValue::Object(&identifier),
            ],
        )
        .expect("Failed to create MetadataAccessibilityIdentifierStruct")
    }
}

impl ToJavaStruct for crate::WuiIgnorableMetadataAccessibilityLabel {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let class = env
            .find_class(jni_str!(
                "dev/waterui/android/runtime/MetadataAccessibilityLabelStruct"
            ))
            .expect("MetadataAccessibilityLabelStruct class not found");
        env.new_object(
            &class,
            jni_sig!("(JJ)V"),
            &[
                JValue::Long(self.content as jlong),
                JValue::Long(self.label as jlong),
            ],
        )
        .expect("Failed to create MetadataAccessibilityLabelStruct")
    }
}

/// NavigationLinkHintStruct(contentPtr: Long)
///
/// The marker carries no data of its own: it exists so a backend that draws a
/// destination-following affordance around a link's row can recognise one.
/// Android's lists have no such affordance, so nothing registers this type and
/// the marker falls through to its content — but the JNI cast is generated for
/// every ignorable metadata, so the conversion has to exist for the Android
/// build to compile at all.
impl ToJavaStruct for crate::components::navigation::WuiIgnorableMetadataNavigationLinkHint {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let class = env
            .find_class(jni_str!(
                "dev/waterui/android/runtime/NavigationLinkHintStruct"
            ))
            .expect("NavigationLinkHintStruct class not found");
        env.new_object(
            &class,
            jni_sig!("(J)V"),
            &[JValue::Long(self.content as jlong)],
        )
        .expect("Failed to create NavigationLinkHintStruct")
    }
}

impl ToJavaStruct for crate::WuiIgnorableMetadataAccessibilityValue {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let class = env
            .find_class(jni_str!(
                "dev/waterui/android/runtime/MetadataAccessibilityValueStruct"
            ))
            .expect("MetadataAccessibilityValueStruct class not found");
        env.new_object(
            &class,
            jni_sig!("(JI)V"),
            &[JValue::Long(self.content as jlong), JValue::Int(self.value)],
        )
        .expect("Failed to create MetadataAccessibilityValueStruct")
    }
}

impl ToJavaStruct for crate::WuiIgnorableMetadataAccessibilityState {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let class = env
            .find_class(jni_str!(
                "dev/waterui/android/runtime/MetadataAccessibilityStateStruct"
            ))
            .expect("MetadataAccessibilityStateStruct class not found");
        env.new_object(
            &class,
            jni_sig!("(JJJJJJJ)V"),
            &[
                JValue::Long(self.content as jlong),
                JValue::Long(self.state.disabled as jlong),
                JValue::Long(self.state.selected as jlong),
                JValue::Long(self.state.checked as jlong),
                JValue::Long(self.state.expanded as jlong),
                JValue::Long(self.state.busy as jlong),
                JValue::Long(self.state.hidden as jlong),
            ],
        )
        .expect("Failed to create MetadataAccessibilityStateStruct")
    }
}

/// MetadataShadowStruct(contentPtr: Long, colorPtr: Long, offsetX: Float, offsetY: Float, radius: Float)
impl ToJavaStruct for crate::WuiMetadataShadow {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let class = env
            .find_class(jni_str!("dev/waterui/android/runtime/MetadataShadowStruct"))
            .expect("MetadataShadowStruct class not found");
        env.new_object(
            &class,
            jni_sig!("(JJFFF)V"),
            &[
                JValue::Long(self.content as jlong),
                JValue::Long(self.value.color as jlong),
                JValue::Float(self.value.offset_x),
                JValue::Float(self.value.offset_y),
                JValue::Float(self.value.radius),
            ],
        )
        .expect("Failed to create MetadataShadowStruct")
    }
}

/// MetadataBorderStruct(contentPtr: Long, colorPtr: Long, width: Float, cornerRadius: Float, edges: Int)
impl ToJavaStruct for crate::WuiMetadataBorder {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let class = env
            .find_class(jni_str!("dev/waterui/android/runtime/MetadataBorderStruct"))
            .expect("MetadataBorderStruct class not found");
        // Pack edges into a bitmask: top=1, bottom=2, leading=4, trailing=8
        let edges_bits = (if self.value.edges.top { 1i32 } else { 0 })
            | (if self.value.edges.bottom { 2i32 } else { 0 })
            | (if self.value.edges.leading { 4i32 } else { 0 })
            | (if self.value.edges.trailing { 8i32 } else { 0 });
        env.new_object(
            &class,
            jni_sig!("(JJFFI)V"),
            &[
                JValue::Long(self.content as jlong),
                JValue::Long(self.value.color as jlong),
                JValue::Float(self.value.width),
                JValue::Float(self.value.corner_radius),
                JValue::Int(edges_bits),
            ],
        )
        .expect("Failed to create MetadataBorderStruct")
    }
}

/// MetadataScaleStruct(contentPtr: Long, scaleXPtr: Long, scaleYPtr: Long, anchorX: Float, anchorY: Float)
impl ToJavaStruct for crate::WuiMetadataScale {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let class = env
            .find_class(jni_str!("dev/waterui/android/runtime/MetadataScaleStruct"))
            .expect("MetadataScaleStruct class not found");
        env.new_object(
            &class,
            jni_sig!("(JJJFF)V"),
            &[
                JValue::Long(self.content as jlong),
                JValue::Long(self.value.x as jlong),
                JValue::Long(self.value.y as jlong),
                JValue::Float(self.value.anchor.x),
                JValue::Float(self.value.anchor.y),
            ],
        )
        .expect("Failed to create MetadataScaleStruct")
    }
}

/// MetadataRotationStruct(contentPtr: Long, anglePtr: Long, anchorX: Float, anchorY: Float)
impl ToJavaStruct for crate::WuiMetadataRotation {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let class = env
            .find_class(jni_str!(
                "dev/waterui/android/runtime/MetadataRotationStruct"
            ))
            .expect("MetadataRotationStruct class not found");
        env.new_object(
            &class,
            jni_sig!("(JJFF)V"),
            &[
                JValue::Long(self.content as jlong),
                JValue::Long(self.value.angle as jlong),
                JValue::Float(self.value.anchor.x),
                JValue::Float(self.value.anchor.y),
            ],
        )
        .expect("Failed to create MetadataRotationStruct")
    }
}

/// MetadataOffsetStruct(contentPtr: Long, offsetXPtr: Long, offsetYPtr: Long)
impl ToJavaStruct for crate::WuiMetadataOffset {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let class = env
            .find_class(jni_str!("dev/waterui/android/runtime/MetadataOffsetStruct"))
            .expect("MetadataOffsetStruct class not found");
        env.new_object(
            &class,
            jni_sig!("(JJJ)V"),
            &[
                JValue::Long(self.content as jlong),
                JValue::Long(self.value.x as jlong),
                JValue::Long(self.value.y as jlong),
            ],
        )
        .expect("Failed to create MetadataOffsetStruct")
    }
}

/// MetadataOpacityStruct(contentPtr: Long, valuePtr: Long)
impl ToJavaStruct for crate::WuiMetadataOpacity {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let class = env
            .find_class(jni_str!(
                "dev/waterui/android/runtime/MetadataOpacityStruct"
            ))
            .expect("MetadataOpacityStruct class not found");
        env.new_object(
            &class,
            jni_sig!("(JJ)V"),
            &[
                JValue::Long(self.content as jlong),
                JValue::Long(self.value.value as jlong),
            ],
        )
        .expect("Failed to create MetadataOpacityStruct")
    }
}

/// MetadataFocusedStruct(contentPtr: Long, bindingPtr: Long)
impl ToJavaStruct for crate::WuiMetadataFocused {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let class = env
            .find_class(jni_str!(
                "dev/waterui/android/runtime/MetadataFocusedStruct"
            ))
            .expect("MetadataFocusedStruct class not found");
        env.new_object(
            &class,
            jni_sig!("(JJ)V"),
            &[
                JValue::Long(self.content as jlong),
                JValue::Long(self.value.binding as jlong),
            ],
        )
        .expect("Failed to create MetadataFocusedStruct")
    }
}

/// MetadataIgnoreSafeAreaStruct(contentPtr: Long, top: Boolean, bottom: Boolean, leading: Boolean, trailing: Boolean)
impl ToJavaStruct for crate::WuiMetadataIgnoreSafeArea {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let class = env
            .find_class(jni_str!(
                "dev/waterui/android/runtime/MetadataIgnoreSafeAreaStruct"
            ))
            .expect("MetadataIgnoreSafeAreaStruct class not found");
        env.new_object(
            &class,
            jni_sig!("(JZZZZ)V"),
            &[
                JValue::Long(self.content as jlong),
                JValue::Bool(self.value.edges.top),
                JValue::Bool(self.value.edges.bottom),
                JValue::Bool(self.value.edges.leading),
                JValue::Bool(self.value.edges.trailing),
            ],
        )
        .expect("Failed to create MetadataIgnoreSafeAreaStruct")
    }
}

/// MetadataRetainStruct(contentPtr: Long, retainPtr: Long)
impl ToJavaStruct for crate::WuiMetadataRetain {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let class = env
            .find_class(jni_str!("dev/waterui/android/runtime/MetadataRetainStruct"))
            .expect("MetadataRetainStruct class not found");
        let retain_ptr = self.value.opaque_ptr() as jlong;
        env.new_object(
            &class,
            jni_sig!("(JJ)V"),
            &[
                JValue::Long(self.content as jlong),
                JValue::Long(retain_ptr),
            ],
        )
        .expect("Failed to create MetadataRetainStruct")
    }
}

/// MetadataClipShapeStruct(contentPtr: Long, kind: ShapeKindStruct, commands: Array<PathCommandStruct>)
impl ToJavaStruct for crate::WuiMetadataClipShape {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        // Create PathCommandStruct array
        let commands = self.value.commands.as_slice();
        let path_command_class = env
            .find_class(jni_str!("dev/waterui/android/runtime/PathCommandStruct"))
            .expect("PathCommandStruct class not found");

        let java_array = env
            .new_object_array(
                super::array_len(commands.len()),
                &path_command_class,
                JObject::null(),
            )
            .expect("Failed to create PathCommandStruct array");

        for (i, cmd) in commands.iter().enumerate() {
            let path_cmd = create_path_command_struct(env, cmd);
            java_array
                .set_element(env, i, &path_cmd)
                .expect("Failed to set path command element");
        }

        let kind = self.value.kind.to_java_struct(env);
        let class = env
            .find_class(jni_str!(
                "dev/waterui/android/runtime/MetadataClipShapeStruct"
            ))
            .expect("MetadataClipShapeStruct class not found");
        let object = env
            .new_object(
                &class,
                jni_sig!(
                    "(JLdev/waterui/android/runtime/ShapeKindStruct;[Ldev/waterui/android/runtime/PathCommandStruct;)V"
                ),
                &[
                    JValue::Long(self.content as jlong),
                    JValue::Object(&kind),
                    JValue::Object(&java_array),
                ],
            )
            .expect("Failed to create MetadataClipShapeStruct");
        unsafe { consume_ffi_array(&self.value.commands) };
        object
    }
}

/// MetadataContextMenuStruct(contentPtr: Long, itemsPtr: Long)
impl ToJavaStruct for crate::WuiMetadataContextMenu {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let class = env
            .find_class(jni_str!(
                "dev/waterui/android/runtime/MetadataContextMenuStruct"
            ))
            .expect("MetadataContextMenuStruct class not found");
        env.new_object(
            &class,
            jni_sig!("(JJ)V"),
            &[
                JValue::Long(self.content as jlong),
                JValue::Long(self.value.items as jlong),
            ],
        )
        .expect("Failed to create MetadataContextMenuStruct")
    }
}

/// MetadataHittableStruct(contentPtr: Long, enabledPtr: Long)
impl ToJavaStruct for crate::WuiMetadataHittable {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let class = env
            .find_class(jni_str!(
                "dev/waterui/android/runtime/MetadataHittableStruct"
            ))
            .expect("MetadataHittableStruct class not found");
        env.new_object(
            &class,
            jni_sig!("(JJ)V"),
            &[
                JValue::Long(self.content as jlong),
                JValue::Long(self.value.enabled as jlong),
            ],
        )
        .expect("Failed to create MetadataHittableStruct")
    }
}

/// Both dynamic-range markers carry the same content-only JNI projection. The
/// registered renderer derives SDR versus HDR from the view's distinct type ID.
impl ToJavaStruct for crate::WuiMetadata<crate::WuiDynamicRangeMarker> {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let class = env
            .find_class(jni_str!(
                "dev/waterui/android/runtime/MetadataDynamicRangeStruct"
            ))
            .expect("MetadataDynamicRangeStruct class not found");
        env.new_object(
            &class,
            jni_sig!("(J)V"),
            &[JValue::Long(self.content as jlong)],
        )
        .expect("Failed to create MetadataDynamicRangeStruct")
    }
}

// The host struct itself only exists with the GPU stack compiled in, so its
// Java projection has to carry the same gate.
#[cfg(all(target_os = "android", feature = "gpu"))]
impl ToJavaStruct for crate::components::media::video::WuiAndroidVideoSurfaceHost {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let class = env
            .find_class(jni_str!(
                "dev/waterui/android/runtime/AndroidVideoSurfaceHostStruct"
            ))
            .expect("AndroidVideoSurfaceHostStruct class not found");
        env.new_object(
            &class,
            jni_sig!("(JJ)V"),
            &[
                JValue::Long(self.content as jlong),
                JValue::Long(self.bridge as jlong),
            ],
        )
        .expect("Failed to create AndroidVideoSurfaceHostStruct")
    }
}

/// Helper to create a PathCommandStruct Java object from a WuiPathCommand
fn create_path_command_struct<'local>(
    env: &mut JNIEnv<'local>,
    cmd: &crate::WuiPathCommand,
) -> JObject<'local> {
    let class = env
        .find_class(jni_str!("dev/waterui/android/runtime/PathCommandStruct"))
        .expect("PathCommandStruct class not found");

    // Extract values based on the command type
    // PathCommandStruct(tag, x, y, cx, cy, c1x, c1y, c2x, c2y, rx, ry, start, sweep)
    let (tag, x, y, cx, cy, c1x, c1y, c2x, c2y, rx, ry, start, sweep) = match cmd {
        crate::WuiPathCommand::MoveTo { x, y } => {
            (0, *x, *y, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
        }
        crate::WuiPathCommand::LineTo { x, y } => {
            (1, *x, *y, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
        }
        crate::WuiPathCommand::QuadTo { cx, cy, x, y } => {
            (2, *x, *y, *cx, *cy, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
        }
        crate::WuiPathCommand::CubicTo {
            c1x,
            c1y,
            c2x,
            c2y,
            x,
            y,
        } => (
            3, *x, *y, 0.0, 0.0, *c1x, *c1y, *c2x, *c2y, 0.0, 0.0, 0.0, 0.0,
        ),
        crate::WuiPathCommand::Arc {
            cx,
            cy,
            rx,
            ry,
            start,
            sweep,
        } => (
            4, 0.0, 0.0, *cx, *cy, 0.0, 0.0, 0.0, 0.0, *rx, *ry, *start, *sweep,
        ),
        crate::WuiPathCommand::Close => (
            5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
        ),
    };

    env.new_object(
        &class,
        jni_sig!("(IFFFFFFFFFFFF)V"),
        &[
            JValue::Int(tag),
            JValue::Float(x),
            JValue::Float(y),
            JValue::Float(cx),
            JValue::Float(cy),
            JValue::Float(c1x),
            JValue::Float(c1y),
            JValue::Float(c2x),
            JValue::Float(c2y),
            JValue::Float(rx),
            JValue::Float(ry),
            JValue::Float(start),
            JValue::Float(sweep),
        ],
    )
    .expect("Failed to create PathCommandStruct")
}

// ============================================================================
// ToJavaStruct implementations for View types
// ============================================================================

/// WuiStr -> PlainStruct(text: String)
impl ToJavaStruct for crate::WuiStr {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let value: waterui::Str = unsafe { crate::IntoRust::into_rust(core::ptr::read(self)) };
        let text = env
            .new_string(value.as_str())
            .expect("Failed to create plain text string");

        let class = env
            .find_class(jni_str!("dev/waterui/android/runtime/PlainStruct"))
            .expect("PlainStruct class not found");
        env.new_object(
            &class,
            jni_sig!("(Ljava/lang/String;)V"),
            &[JValue::Object(&text)],
        )
        .expect("Failed to create PlainStruct")
    }
}

/// WuiResolvedColor -> ResolvedColorStruct(red, green, blue, opacity, headroom)
impl ToJavaStruct for crate::color::WuiResolvedColor {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let class = env
            .find_class(jni_str!("dev/waterui/android/runtime/ResolvedColorStruct"))
            .expect("ResolvedColorStruct class not found");
        env.new_object(
            &class,
            jni_sig!("(FFFFF)V"),
            &[
                JValue::Float(self.red),
                JValue::Float(self.green),
                JValue::Float(self.blue),
                JValue::Float(self.opacity),
                JValue::Float(self.headroom),
            ],
        )
        .expect("Failed to create ResolvedColorStruct")
    }
}

/// WuiResolvedGradientStop -> ResolvedGradientStopStruct(position, color)
impl ToJavaStruct for crate::gradient::WuiResolvedGradientStop {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let class = env
            .find_class(jni_str!(
                "dev/waterui/android/runtime/ResolvedGradientStopStruct"
            ))
            .expect("ResolvedGradientStopStruct class not found");
        let color = crate::color::WuiResolvedColor {
            red: self.color.red,
            green: self.color.green,
            blue: self.color.blue,
            opacity: self.color.opacity,
            headroom: self.color.headroom,
        };
        let java_color = color.to_java_struct(env);
        env.new_object(
            &class,
            jni_sig!("(FLdev/waterui/android/runtime/ResolvedColorStruct;)V"),
            &[JValue::Float(self.position), JValue::Object(&java_color)],
        )
        .expect("Failed to create ResolvedGradientStopStruct")
    }
}

/// WuiResolvedGradient -> ResolvedGradientStruct(...)
impl ToJavaStruct for crate::gradient::WuiResolvedGradient {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let stop_class = env
            .find_class(jni_str!(
                "dev/waterui/android/runtime/ResolvedGradientStopStruct"
            ))
            .expect("ResolvedGradientStopStruct class not found");
        let stop_array = env
            .new_object_array(
                super::array_len(self.stops.len()),
                &stop_class,
                JObject::null(),
            )
            .expect("Failed to create ResolvedGradientStopStruct array");

        for (index, stop) in self.stops.as_slice().iter().enumerate() {
            let java_stop = stop.to_java_struct(env);
            stop_array
                .set_element(env, index, &java_stop)
                .expect("Failed to set ResolvedGradientStopStruct array element");
        }

        let class = env
            .find_class(jni_str!(
                "dev/waterui/android/runtime/ResolvedGradientStruct"
            ))
            .expect("ResolvedGradientStruct class not found");
        let object = env
            .new_object(
                &class,
                jni_sig!("(I[Ldev/waterui/android/runtime/ResolvedGradientStopStruct;FFFFFF)V"),
                &[
                    JValue::Int(self.gradient_type as i32),
                    JValue::Object(&stop_array),
                    JValue::Float(self.start_x),
                    JValue::Float(self.start_y),
                    JValue::Float(self.end_x),
                    JValue::Float(self.end_y),
                    JValue::Float(self.start_value),
                    JValue::Float(self.end_value),
                ],
            )
            .expect("Failed to create ResolvedGradientStruct");
        unsafe { consume_ffi_array(&self.stops) };
        object
    }
}

/// WuiShapeKind -> ShapeKindStruct(tag, topLeft, topRight, bottomRight, bottomLeft)
impl ToJavaStruct for crate::shape::WuiShapeKind {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let class = env
            .find_class(jni_str!("dev/waterui/android/runtime/ShapeKindStruct"))
            .expect("ShapeKindStruct class not found");
        env.new_object(
            &class,
            jni_sig!("(IFFFF)V"),
            &[
                JValue::Int(self.tag),
                JValue::Float(self.top_left),
                JValue::Float(self.top_right),
                JValue::Float(self.bottom_right),
                JValue::Float(self.bottom_left),
            ],
        )
        .expect("Failed to create ShapeKindStruct")
    }
}

/// WuiResolvedShape -> ResolvedShapeStruct(kind, commands, fill)
impl ToJavaStruct for crate::shape::WuiResolvedShape {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let command_class = env
            .find_class(jni_str!("dev/waterui/android/runtime/PathCommandStruct"))
            .expect("PathCommandStruct class not found");
        let command_array = env
            .new_object_array(
                super::array_len(self.commands.len()),
                &command_class,
                JObject::null(),
            )
            .expect("Failed to create PathCommandStruct array");

        for (index, cmd) in self.commands.as_slice().iter().enumerate() {
            let java_cmd = create_path_command_struct(env, cmd);
            command_array
                .set_element(env, index, &java_cmd)
                .expect("Failed to set PathCommandStruct array element");
        }

        let java_kind = self.kind.to_java_struct(env);
        let class = env
            .find_class(jni_str!("dev/waterui/android/runtime/ResolvedShapeStruct"))
            .expect("ResolvedShapeStruct class not found");
        let object = env.new_object(&class, jni_sig!("(Ldev/waterui/android/runtime/ShapeKindStruct;[Ldev/waterui/android/runtime/PathCommandStruct;J)V"), &[
            JValue::Object(&java_kind),
            JValue::Object(&command_array),
            JValue::Long(self.fill as jlong),
        ],)
        .expect("Failed to create ResolvedShapeStruct");
        unsafe { consume_ffi_array(&self.commands) };
        object
    }
}

/// WuiText -> TextStruct(contentPtr: Long, paragraphAlignmentPtr: Long, lineLimit: Int)
impl ToJavaStruct for crate::components::text::WuiText {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let class = env
            .find_class(jni_str!("dev/waterui/android/runtime/TextStruct"))
            .expect("TextStruct class not found");
        env.new_object(
            &class,
            jni_sig!("(JJI)V"),
            &[
                JValue::Long(self.content as jlong),
                JValue::Long(self.paragraph_alignment as jlong),
                JValue::Int(i32::try_from(self.line_limit).unwrap_or(i32::MAX)),
            ],
        )
        .expect("Failed to create TextStruct")
    }
}

/// WuiButton -> ButtonStruct(labelPtr: Long, actionPtr: Long, style: Int, accessibilityLabelPtr: Long)
///
/// `label` is now a `WuiLabel` struct; we project its `view` pointer as the
/// labelPtr and its `accessibility_label` pointer separately. Display mode is
/// not surfaced to Android yet — visual hide already happens in the Rust
/// `Label::body` rendering path, which produces an empty view for Hidden mode.
impl ToJavaStruct for crate::components::button::WuiButton {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let class = env
            .find_class(jni_str!("dev/waterui/android/runtime/ButtonStruct"))
            .expect("ButtonStruct class not found");
        env.new_object(
            &class,
            jni_sig!("(JJIJ)V"),
            &[
                JValue::Long(self.label.view as jlong),
                JValue::Long(self.action as jlong),
                JValue::Int(self.style as i32),
                JValue::Long(self.label.accessibility_label as jlong),
            ],
        )
        .expect("Failed to create ButtonStruct")
    }
}

/// WuiTextField -> TextFieldStruct(labelPtr, accessibilityLabelPtr, valuePtr,
/// promptPtr, promptAlignmentPtr, keyboardType, selectionMenuItemsPtr, lineLimit)
impl ToJavaStruct for crate::components::form::WuiTextField {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let class = env
            .find_class(jni_str!("dev/waterui/android/runtime/TextFieldStruct"))
            .expect("TextFieldStruct class not found");
        env.new_object(
            &class,
            jni_sig!("(JJJJJIJI)V"),
            &[
                JValue::Long(self.label.view as jlong),
                JValue::Long(self.label.accessibility_label as jlong),
                JValue::Long(self.value as jlong),
                JValue::Long(self.prompt.content as jlong),
                JValue::Long(self.prompt.paragraph_alignment as jlong),
                JValue::Int(self.keyboard as i32),
                JValue::Long(self.selection_menu as jlong),
                JValue::Int(i32::try_from(self.line_limit).unwrap_or(i32::MAX)),
            ],
        )
        .expect("Failed to create TextFieldStruct")
    }
}

/// WuiSecureField -> SecureFieldStruct(labelPtr, accessibilityLabelPtr, valuePtr)
impl ToJavaStruct for crate::components::form::WuiSecureField {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let class = env
            .find_class(jni_str!("dev/waterui/android/runtime/SecureFieldStruct"))
            .expect("SecureFieldStruct class not found");
        env.new_object(
            &class,
            jni_sig!("(JJJ)V"),
            &[
                JValue::Long(self.label.view as jlong),
                JValue::Long(self.label.accessibility_label as jlong),
                JValue::Long(self.value as jlong),
            ],
        )
        .expect("Failed to create SecureFieldStruct")
    }
}

/// WuiToggle -> ToggleStruct(labelPtr, accessibilityLabelPtr, bindingPtr, style)
impl ToJavaStruct for crate::components::form::WuiToggle {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let class = env
            .find_class(jni_str!("dev/waterui/android/runtime/ToggleStruct"))
            .expect("ToggleStruct class not found");
        env.new_object(
            &class,
            jni_sig!("(JJJI)V"),
            &[
                JValue::Long(self.label.view as jlong),
                JValue::Long(self.label.accessibility_label as jlong),
                JValue::Long(self.toggle as jlong),
                JValue::Int(self.style as i32),
            ],
        )
        .expect("Failed to create ToggleStruct")
    }
}

/// WuiSlider -> SliderStruct
impl ToJavaStruct for crate::components::form::WuiSlider {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let class = env
            .find_class(jni_str!("dev/waterui/android/runtime/SliderStruct"))
            .expect("SliderStruct class not found");
        env.new_object(
            &class,
            jni_sig!("(JJJJDDJ)V"),
            &[
                JValue::Long(self.label.view as jlong),
                JValue::Long(self.label.accessibility_label as jlong),
                JValue::Long(self.min_value_label as jlong),
                JValue::Long(self.max_value_label as jlong),
                JValue::Double(self.range.start),
                JValue::Double(self.range.end),
                JValue::Long(self.value as jlong),
            ],
        )
        .expect("Failed to create SliderStruct")
    }
}

/// WuiStepper -> StepperStruct
impl ToJavaStruct for crate::components::form::WuiStepper {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let class = env
            .find_class(jni_str!("dev/waterui/android/runtime/StepperStruct"))
            .expect("StepperStruct class not found");
        env.new_object(
            &class,
            jni_sig!("(JJJJJII)V"),
            &[
                JValue::Long(self.value as jlong),
                JValue::Long(self.step as jlong),
                JValue::Long(self.label.view as jlong),
                JValue::Long(self.label.accessibility_label as jlong),
                JValue::Long(self.value_formatter as jlong),
                JValue::Int(self.range.start),
                JValue::Int(self.range.end),
            ],
        )
        .expect("Failed to create StepperStruct")
    }
}

/// WuiColorPicker -> ColorPickerStruct
impl ToJavaStruct for crate::components::form::WuiColorPicker {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let class = env
            .find_class(jni_str!("dev/waterui/android/runtime/ColorPickerStruct"))
            .expect("ColorPickerStruct class not found");
        env.new_object(
            &class,
            jni_sig!("(JJJZZ)V"),
            &[
                JValue::Long(self.label.view as jlong),
                JValue::Long(self.label.accessibility_label as jlong),
                JValue::Long(self.value as jlong),
                JValue::Bool(self.support_alpha),
                JValue::Bool(self.support_hdr),
            ],
        )
        .expect("Failed to create ColorPickerStruct")
    }
}

/// WuiPicker -> PickerStruct(itemsPtr, selectionPtr, style)
impl ToJavaStruct for crate::components::form::WuiPicker {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let class = env
            .find_class(jni_str!("dev/waterui/android/runtime/PickerStruct"))
            .expect("PickerStruct class not found");
        env.new_object(
            &class,
            jni_sig!("(JJI)V"),
            &[
                JValue::Long(self.items as jlong),
                JValue::Long(self.selection as jlong),
                JValue::Int(self.style as i32),
            ],
        )
        .expect("Failed to create PickerStruct")
    }
}

impl ToJavaStruct for crate::components::form::WuiPickerItem {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let class = env
            .find_class(jni_str!("dev/waterui/android/runtime/PickerItemStruct"))
            .expect("PickerItemStruct class not found");
        env.new_object(
            &class,
            jni_sig!("(IJ)V"),
            &[
                JValue::Int(self.tag.inner),
                JValue::Long(self.label as jlong),
            ],
        )
        .expect("Failed to create PickerItemStruct")
    }
}

/// WuiDatePicker -> DatePickerStruct
impl ToJavaStruct for crate::components::form::WuiDatePicker {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let class = env
            .find_class(jni_str!("dev/waterui/android/runtime/DatePickerStruct"))
            .expect("DatePickerStruct class not found");
        let date_time_class = env
            .find_class(jni_str!("dev/waterui/android/runtime/DateTimeStruct"))
            .expect("DateTimeStruct class not found");
        let start = env
            .new_object(
                &date_time_class,
                jni_sig!("(IIIIII)V"),
                &[
                    JValue::Int(self.range.start.year),
                    JValue::Int(self.range.start.month as i32),
                    JValue::Int(self.range.start.day as i32),
                    JValue::Int(self.range.start.hour as i32),
                    JValue::Int(self.range.start.minute as i32),
                    JValue::Int(self.range.start.second as i32),
                ],
            )
            .expect("Failed to create DateTimeStruct for range start");
        let end = env
            .new_object(
                &date_time_class,
                jni_sig!("(IIIIII)V"),
                &[
                    JValue::Int(self.range.end.year),
                    JValue::Int(self.range.end.month as i32),
                    JValue::Int(self.range.end.day as i32),
                    JValue::Int(self.range.end.hour as i32),
                    JValue::Int(self.range.end.minute as i32),
                    JValue::Int(self.range.end.second as i32),
                ],
            )
            .expect("Failed to create DateTimeStruct for range end");
        env.new_object(&class, jni_sig!("(JJJLdev/waterui/android/runtime/DateTimeStruct;Ldev/waterui/android/runtime/DateTimeStruct;I)V"), &[
            JValue::Long(self.label.view as jlong),
            JValue::Long(self.label.accessibility_label as jlong),
            JValue::Long(self.value as jlong),
            JValue::Object(&start),
            JValue::Object(&end),
            JValue::Int(self.ty as i32),
        ],)
        .expect("Failed to create DatePickerStruct")
    }
}

impl ToJavaStruct for crate::components::form::WuiMultiDatePicker {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let class = env
            .find_class(jni_str!(
                "dev/waterui/android/runtime/MultiDatePickerStruct"
            ))
            .expect("MultiDatePickerStruct class not found");
        let date_class = env
            .find_class(jni_str!("dev/waterui/android/runtime/DateStruct"))
            .expect("DateStruct class not found");
        let start = env
            .new_object(
                &date_class,
                jni_sig!("(III)V"),
                &[
                    JValue::Int(self.range.start.year),
                    JValue::Int(self.range.start.month as i32),
                    JValue::Int(self.range.start.day as i32),
                ],
            )
            .expect("Failed to create DateStruct for range start");
        let end = env
            .new_object(
                &date_class,
                jni_sig!("(III)V"),
                &[
                    JValue::Int(self.range.end.year),
                    JValue::Int(self.range.end.month as i32),
                    JValue::Int(self.range.end.day as i32),
                ],
            )
            .expect("Failed to create DateStruct for range end");
        env.new_object(&class, jni_sig!("(JJJLdev/waterui/android/runtime/DateStruct;Ldev/waterui/android/runtime/DateStruct;J)V"), &[
            JValue::Long(self.label.view as jlong),
            JValue::Long(self.label.accessibility_label as jlong),
            JValue::Long(self.value as jlong),
            JValue::Object(&start),
            JValue::Object(&end),
            JValue::Long(self.decorated as jlong),
        ],)
        .expect("Failed to create MultiDatePickerStruct")
    }
}

/// WuiScrollView -> ScrollStruct(axis, contentPtr, targetXPtr, targetYPtr, generationPtr)
impl ToJavaStruct for crate::components::layout::WuiScrollView {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let class = env
            .find_class(jni_str!("dev/waterui/android/runtime/ScrollStruct"))
            .expect("ScrollStruct class not found");
        env.new_object(
            &class,
            jni_sig!("(IJJJJ)V"),
            &[
                JValue::Int(self.axis as i32),
                JValue::Long(self.content as jlong),
                JValue::Long(self.target_x as jlong),
                JValue::Long(self.target_y as jlong),
                JValue::Long(self.scroll_generation as jlong),
            ],
        )
        .expect("Failed to create ScrollStruct")
    }
}

/// WuiFixedContainer -> FixedContainerStruct(layoutPtr, childPointers: LongArray)
impl ToJavaStruct for crate::components::layout::WuiFixedContainer {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        // Create the long array for child pointers
        let children = self.contents.as_slice();
        let java_array = env
            .new_long_array(children.len())
            .expect("Failed to create long array");
        let child_ptrs: Vec<i64> = children.iter().map(|p| *p as jlong).collect();
        java_array
            .set_region(env, 0, &child_ptrs)
            .expect("Failed to set long array region");

        let class = env
            .find_class(jni_str!("dev/waterui/android/runtime/FixedContainerStruct"))
            .expect("FixedContainerStruct class not found");
        let object = env
            .new_object(
                &class,
                jni_sig!("(J[J)V"),
                &[
                    JValue::Long(self.layout as jlong),
                    JValue::Object(&java_array),
                ],
            )
            .expect("Failed to create FixedContainerStruct");
        unsafe { consume_ffi_array(&self.contents) };
        object
    }
}

/// WuiContainer -> LayoutContainerStruct(layoutPtr, contentsPtr)
impl ToJavaStruct for crate::components::layout::WuiContainer {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let class = env
            .find_class(jni_str!(
                "dev/waterui/android/runtime/LayoutContainerStruct"
            ))
            .expect("LayoutContainerStruct class not found");
        env.new_object(
            &class,
            jni_sig!("(JJ)V"),
            &[
                JValue::Long(self.layout as jlong),
                JValue::Long(self.contents as jlong),
            ],
        )
        .expect("Failed to create LayoutContainerStruct")
    }
}

/// WuiNavigationView -> NavigationViewStruct
impl ToJavaStruct for crate::components::navigation::WuiNavigationView {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let search = if self.bar.search.has_value {
            self.bar.search.value.to_java_struct(env)
        } else {
            JObject::null()
        };
        let toolbar_items = self.bar.toolbar.as_slice();
        let toolbar_item_class = env
            .find_class(jni_str!(
                "dev/waterui/android/runtime/NavigationToolbarItemStruct"
            ))
            .expect("NavigationToolbarItemStruct class not found");
        let toolbar = env
            .new_object_array(
                super::array_len(toolbar_items.len()),
                &toolbar_item_class,
                JObject::null(),
            )
            .expect("Failed to create NavigationToolbarItemStruct array");
        for (index, item) in toolbar_items.iter().enumerate() {
            let item = env
                .new_object(
                    &toolbar_item_class,
                    jni_sig!("(IJ)V"),
                    &[
                        JValue::Int(item.placement as i32),
                        JValue::Long(item.content as jlong),
                    ],
                )
                .expect("Failed to create NavigationToolbarItemStruct");
            toolbar
                .set_element(env, index, item)
                .expect("Failed to set navigation toolbar item");
        }
        let bar_class = env
            .find_class(jni_str!("dev/waterui/android/runtime/BarStruct"))
            .expect("BarStruct class not found");
        let bar = env.new_object(&bar_class, jni_sig!("(JJ[Ldev/waterui/android/runtime/NavigationToolbarItemStruct;Ldev/waterui/android/runtime/NavigationSearchStruct;JJI)V"), &[
            JValue::Long(self.bar.title as jlong),
            JValue::Long(self.bar.subtitle as jlong),
            JValue::Object(&toolbar),
            JValue::Object(&search),
            JValue::Long(self.bar.color as jlong),
            JValue::Long(self.bar.hidden as jlong),
            JValue::Int(self.bar.display_mode as i32),
        ],)
            .expect("Failed to create BarStruct");
        unsafe { consume_ffi_array(&self.bar.toolbar) };

        let class = env
            .find_class(jni_str!("dev/waterui/android/runtime/NavigationViewStruct"))
            .expect("NavigationViewStruct class not found");
        env.new_object(
            &class,
            jni_sig!("(Ldev/waterui/android/runtime/BarStruct;JJJJJJII)V"),
            &[
                JValue::Object(&bar),
                JValue::Long(self.content as jlong),
                JValue::Long(self.state.pop_enabled as jlong),
                JValue::Long(self.state.pop_attempted as jlong),
                JValue::Long(self.state.appear as jlong),
                JValue::Long(self.state.disappear as jlong),
                JValue::Long(self.state.pop as jlong),
                JValue::Int(self.transition.kind as i32),
                JValue::Int(self.transition.source_id),
            ],
        )
        .expect("Failed to create NavigationViewStruct")
    }
}

/// WuiNavigationSearch -> NavigationSearchStruct
impl ToJavaStruct for crate::components::navigation::WuiNavigationSearch {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let class = env
            .find_class(jni_str!(
                "dev/waterui/android/runtime/NavigationSearchStruct"
            ))
            .expect("NavigationSearchStruct class not found");
        env.new_object(
            &class,
            jni_sig!("(JJ)V"),
            &[
                JValue::Long(self.text as jlong),
                JValue::Long(self.prompt as jlong),
            ],
        )
        .expect("Failed to create NavigationSearchStruct")
    }
}

/// WuiNavigationStack -> NavigationStackStruct(rootPtr, transition, transitionSourceId)
impl ToJavaStruct for crate::components::navigation::WuiNavigationStack {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let class = env
            .find_class(jni_str!(
                "dev/waterui/android/runtime/NavigationStackStruct"
            ))
            .expect("NavigationStackStruct class not found");
        env.new_object(
            &class,
            jni_sig!("(JII)V"),
            &[
                JValue::Long(self.root as jlong),
                JValue::Int(self.transition.kind as i32),
                JValue::Int(self.transition.source_id),
            ],
        )
        .expect("Failed to create NavigationStackStruct")
    }
}

/// WuiNavigationSplitLayout -> SplitNavigationContainerStruct
impl ToJavaStruct for crate::components::navigation::WuiNavigationSplitLayout {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let class = env
            .find_class(jni_str!(
                "dev/waterui/android/runtime/SplitNavigationContainerStruct"
            ))
            .expect("SplitNavigationContainerStruct class not found");
        env.new_object(
            &class,
            jni_sig!("(JJJJJJJFFFI)V"),
            &[
                JValue::Long(self.sidebar as jlong),
                JValue::Long(self.placeholder as jlong),
                JValue::Long(self.primary_selection as jlong),
                JValue::Long(self.content as jlong),
                JValue::Long(self.secondary_selection as jlong),
                JValue::Long(self.detail as jlong),
                JValue::Long(self.column_visibility as jlong),
                JValue::Float(self.sidebar_width.min),
                JValue::Float(self.sidebar_width.ideal),
                JValue::Float(self.sidebar_width.max),
                JValue::Int(self.style as i32),
            ],
        )
        .expect("Failed to create SplitNavigationContainerStruct")
    }
}

/// WuiTabs -> TabsStruct
impl ToJavaStruct for crate::components::navigation::WuiTabs {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        // Create array of TabStruct
        let tabs = self.tabs.as_slice();
        let tab_class = env
            .find_class(jni_str!("dev/waterui/android/runtime/TabStruct"))
            .expect("TabStruct class not found");

        let java_array = env
            .new_object_array(super::array_len(tabs.len()), &tab_class, JObject::null())
            .expect("Failed to create TabStruct array");

        for (i, tab) in tabs.iter().enumerate() {
            let tab_obj = env
                .new_object(
                    &tab_class,
                    jni_sig!("(JJJJJJJ)V"),
                    &[
                        JValue::Long(tab.id as jlong),
                        JValue::Long(tab.label as jlong),
                        JValue::Long(tab.content as jlong),
                        JValue::Long(tab.badge as jlong),
                        JValue::Long(tab.enabled as jlong),
                        JValue::Long(tab.system_icon as jlong),
                        JValue::Long(tab.icon as jlong),
                    ],
                )
                .expect("Failed to create TabStruct");
            java_array
                .set_element(env, i, &tab_obj)
                .expect("Failed to set tab element");
        }

        let class = env
            .find_class(jni_str!("dev/waterui/android/runtime/TabsStruct"))
            .expect("TabsStruct class not found");
        let object = env
            .new_object(
                &class,
                jni_sig!("(J[Ldev/waterui/android/runtime/TabStruct;I)V"),
                &[
                    JValue::Long(self.selection as jlong),
                    JValue::Object(&java_array),
                    JValue::Int(self.style as i32),
                ],
            )
            .expect("Failed to create TabsStruct");
        unsafe { consume_ffi_array(&self.tabs) };
        object
    }
}

/// WuiList -> ListStruct
impl ToJavaStruct for crate::components::list::WuiList {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let class = env
            .find_class(jni_str!("dev/waterui/android/runtime/ListStruct"))
            .expect("ListStruct class not found");
        env.new_object(
            &class,
            jni_sig!("(JJJJJJZ)V"),
            &[
                JValue::Long(self.contents as jlong),
                JValue::Long(self.editing as jlong),
                JValue::Long(self.on_delete as jlong),
                JValue::Long(self.on_move as jlong),
                JValue::Long(self.target_index as jlong),
                JValue::Long(self.scroll_generation as jlong),
                JValue::Bool(self.uses_sections.into()),
            ],
        )
        .expect("Failed to create ListStruct")
    }
}

/// WuiListItem -> ListItemStruct
impl ToJavaStruct for crate::components::list::WuiListItem {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let class = env
            .find_class(jni_str!("dev/waterui/android/runtime/ListItemStruct"))
            .expect("ListItemStruct class not found");
        env.new_object(
            &class,
            jni_sig!("(JJJZJJ)V"),
            &[
                JValue::Long(self.content as jlong),
                JValue::Long(self.deletable as jlong),
                JValue::Long(self.selected as jlong),
                JValue::Bool(self.section.has_value.into()),
                JValue::Long(self.section.label as jlong),
                JValue::Long(self.section.footer as jlong),
            ],
        )
        .expect("Failed to create ListItemStruct")
    }
}

/// WuiProgress -> ProgressStruct(labelPtr, valueLabelPtr, valuePtr, style, fourColor)
impl ToJavaStruct for crate::components::progress::WuiProgress {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let class = env
            .find_class(jni_str!("dev/waterui/android/runtime/ProgressStruct"))
            .expect("ProgressStruct class not found");
        env.new_object(
            &class,
            jni_sig!("(JJJIZ)V"),
            &[
                JValue::Long(self.label as jlong),
                JValue::Long(self.value_label as jlong),
                JValue::Long(self.value as jlong),
                JValue::Int(self.style as i32),
                JValue::Bool(self.four_color.into()),
            ],
        )
        .expect("Failed to create ProgressStruct")
    }
}

/// WuiGpuSurface -> GpuSurfaceStruct(rendererPtr, HDR preference, PiP host)
#[cfg(feature = "gpu")]
impl ToJavaStruct for crate::components::gpu_surface::WuiGpuSurface {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let preference =
            unsafe { crate::components::gpu_surface::waterui_gpu_surface_hdr_preference(self) };
        let class = env
            .find_class(jni_str!("dev/waterui/android/runtime/GpuSurfaceStruct"))
            .expect("GpuSurfaceStruct class not found");
        env.new_object(
            &class,
            jni_sig!("(JZZZJ)V"),
            &[
                JValue::Long(self.surface as jlong),
                JValue::Bool(preference.has_preference),
                JValue::Bool(preference.prefers_hdr),
                JValue::Bool(self.has_picture_in_picture_host_id),
                JValue::Long(self.picture_in_picture_host_id as jlong),
            ],
        )
        .expect("Failed to create GpuSurfaceStruct")
    }
}

/// *mut WuiWebView -> WebViewStruct(webviewPtr)
impl ToJavaStruct for *mut crate::components::webview::WuiWebView {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let class = env
            .find_class(jni_str!("dev/waterui/android/runtime/WebViewStruct"))
            .expect("WebViewStruct class not found");
        env.new_object(&class, jni_sig!("(J)V"), &[JValue::Long(*self as jlong)])
            .expect("Failed to create WebViewStruct")
    }
}

/// *mut WuiDynamic -> DynamicStruct(dynamicPtr)
impl ToJavaStruct for *mut crate::components::dynamic::WuiDynamic {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let class = env
            .find_class(jni_str!("dev/waterui/android/runtime/DynamicStruct"))
            .expect("DynamicStruct class not found");
        env.new_object(&class, jni_sig!("(J)V"), &[JValue::Long(*self as jlong)])
            .expect("Failed to create DynamicStruct")
    }
}

/// WuiMenu -> MenuStruct(labelPtr, itemsPtr, accessibilityLabelPtr)
impl ToJavaStruct for crate::WuiMenu {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let class = env
            .find_class(jni_str!("dev/waterui/android/runtime/MenuStruct"))
            .expect("MenuStruct class not found");
        env.new_object(
            &class,
            jni_sig!("(JJJ)V"),
            &[
                JValue::Long(self.label as jlong),
                JValue::Long(self.items as jlong),
                JValue::Long(self.accessibility_label as jlong),
            ],
        )
        .expect("Failed to create MenuStruct")
    }
}

impl ToJavaStruct for crate::WuiMenuItem {
    fn to_java_struct<'local>(&self, env: &mut JNIEnv<'local>) -> JObject<'local> {
        let label_ptr = if self.label.is_null() {
            0
        } else {
            let label = unsafe { Box::from_raw(self.label) };
            if !label.paragraph_alignment.is_null() {
                unsafe { drop(Box::from_raw(label.paragraph_alignment)) };
            }
            label.content as jlong
        };

        assert!(
            self.icon.is_null(),
            "SystemIcon menu items are unsupported on Android; use a portable icon-set view outside native menus"
        );

        let (key_equivalent, command, shift, option, control) = if self.shortcut.is_null() {
            (JObject::null(), false, false, false, false)
        } else {
            let shortcut = unsafe { *Box::from_raw(self.shortcut) };
            let modifiers = shortcut.modifiers;
            let key: waterui::Str = unsafe { crate::IntoRust::into_rust(shortcut.key) };
            let key = env
                .new_string(key.as_str())
                .expect("Failed to create shortcut key string");
            (
                JObject::from(key),
                modifiers.command,
                modifiers.shift,
                modifiers.option,
                modifiers.control,
            )
        };

        let class = env
            .find_class(jni_str!("dev/waterui/android/runtime/MenuItemStruct"))
            .expect("MenuItemStruct class not found");
        env.new_object(
            &class,
            jni_sig!("(IJJJJLjava/lang/String;ZZZZJ)V"),
            &[
                JValue::Int(self.tag as jint),
                JValue::Long(label_ptr),
                JValue::Long(self.action as jlong),
                JValue::Long(self.disabled as jlong),
                JValue::Long(self.selected as jlong),
                JValue::Object(&key_equivalent),
                JValue::Bool(command),
                JValue::Bool(shift),
                JValue::Bool(option),
                JValue::Bool(control),
                JValue::Long(self.items as jlong),
            ],
        )
        .expect("Failed to create MenuItemStruct")
    }
}