zng-app 0.22.0

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

use std::{
    collections::HashMap,
    fmt,
    path::PathBuf,
    sync::{self, Arc},
};

pub mod raw_device_events;
pub mod raw_events;

use crate::{
    event::{event, event_args},
    window::{MonitorId, WindowId},
};

use parking_lot::{MappedRwLockReadGuard, MappedRwLockWriteGuard};
use zng_app_context::app_local;
use zng_layout::unit::{DipPoint, DipRect, DipSideOffsets, DipSize, Factor, Px, PxPoint, PxRect};
use zng_task::channel::{self, ChannelError, IpcBytes, IpcReceiver, Receiver};
use zng_txt::Txt;
use zng_var::{ArcEq, ResponderVar, Var, VarHandle, WeakEq};
use zng_view_api::{
    self, DeviceEventsFilter, DragDropId, Event, FocusResult, ViewProcessGen, ViewProcessInfo,
    api_extension::{ApiExtensionId, ApiExtensionName, ApiExtensionPayload, ApiExtensionRecvError},
    audio::{
        AudioDecoded, AudioId, AudioMetadata, AudioMix, AudioOutputConfig, AudioOutputId as ApiAudioOutputId, AudioOutputOpenData,
        AudioOutputRequest, AudioOutputUpdateRequest, AudioPlayId, AudioPlayRequest, AudioRequest,
    },
    dialog::{FileDialog, FileDialogResponse, MsgDialog, MsgDialogResponse, Notification, NotificationResponse},
    drag_drop::{DragDropData, DragDropEffect, DragDropError},
    font::{FontOptions, IpcFontBytes},
    image::{ImageDecoded, ImageEncodeId, ImageEncodeRequest, ImageMaskMode, ImageMetadata, ImageRequest, ImageTextureId},
    window::{
        CursorIcon, FocusIndicator, FrameRequest, FrameUpdateRequest, HeadlessOpenData, HeadlessRequest, RenderMode, ResizeDirection,
        VideoMode, WindowButton, WindowRequest, WindowStateAll,
    },
};

pub(crate) use zng_view_api::{
    Controller, raw_input::InputDeviceId as ApiDeviceId, window::MonitorId as ApiMonitorId, window::WindowId as ApiWindowId,
};
use zng_view_api::{
    clipboard::{ClipboardData, ClipboardError, ClipboardType},
    font::{FontFaceId, FontId, FontVariationName},
    image::ImageId,
};

use self::raw_device_events::InputDeviceId;

use super::{APP, AppId};

/// Connection to the running view-process for the context app.
#[expect(non_camel_case_types)]
pub struct VIEW_PROCESS;
struct ViewProcessService {
    process: zng_view_api::Controller,
    input_device_ids: HashMap<ApiDeviceId, InputDeviceId>,
    monitor_ids: HashMap<ApiMonitorId, MonitorId>,

    data_generation: ViewProcessGen,

    loading_images: Vec<WeakEq<ViewImageHandleData>>,
    encoding_images: Vec<EncodeRequest>,

    loading_audios: Vec<WeakEq<ViewAudioHandleData>>,

    pending_frames: usize,

    message_dialogs: Vec<(zng_view_api::dialog::DialogId, ResponderVar<MsgDialogResponse>)>,
    file_dialogs: Vec<(zng_view_api::dialog::DialogId, ResponderVar<FileDialogResponse>)>,
    notifications: Vec<(zng_view_api::dialog::DialogId, VarHandle, ResponderVar<NotificationResponse>)>,

    ping_count: u16,
}
app_local! {
    static VIEW_PROCESS_SV: Option<ViewProcessService> = None;
    static VIEW_PROCESS_INFO: ViewProcessInfo = const { ViewProcessInfo::new(ViewProcessGen::INVALID, false) };
}
impl VIEW_PROCESS {
    /// If the `VIEW_PROCESS` can be used, this is only true in app threads for apps with render, all other
    /// methods will panic if called when this is not true.
    pub fn is_available(&self) -> bool {
        APP.is_running() && VIEW_PROCESS_SV.read().is_some()
    }

    fn read(&self) -> MappedRwLockReadGuard<'_, ViewProcessService> {
        VIEW_PROCESS_SV.read_map(|e| e.as_ref().expect("VIEW_PROCESS not available"))
    }

    fn write(&self) -> MappedRwLockWriteGuard<'_, ViewProcessService> {
        VIEW_PROCESS_SV.write_map(|e| e.as_mut().expect("VIEW_PROCESS not available"))
    }

    fn try_write(&self) -> Result<MappedRwLockWriteGuard<'_, ViewProcessService>> {
        let vp = VIEW_PROCESS_SV.write();
        if let Some(w) = &*vp
            && w.process.is_connected()
        {
            return Ok(MappedRwLockWriteGuard::map(vp, |w| w.as_mut().unwrap()));
        }
        Err(ChannelError::disconnected())
    }

    fn check_app(&self, id: AppId) {
        let actual = APP.id();
        if Some(id) != actual {
            panic!("cannot use view handle from app `{id:?}` in app `{actual:?}`");
        }
    }

    fn handle_write(&self, id: AppId) -> MappedRwLockWriteGuard<'_, ViewProcessService> {
        self.check_app(id);
        self.write()
    }

    /// View-process running, connected and ready.
    pub fn is_connected(&self) -> bool {
        self.read().process.is_connected()
    }

    /// If is running in headless renderer mode.
    pub fn is_headless_with_render(&self) -> bool {
        self.read().process.headless()
    }

    /// If is running both view and app in the same process.
    pub fn is_same_process(&self) -> bool {
        self.read().process.same_process()
    }

    /// Read lock view-process and reference current generation info.
    ///
    /// Strongly recommend to clone/copy the info required, as the lock prevents info update on respawn.
    pub fn info(&self) -> MappedRwLockReadGuard<'static, ViewProcessInfo> {
        VIEW_PROCESS_INFO.read()
    }

    /// Gets the current view-process generation.
    pub fn generation(&self) -> ViewProcessGen {
        self.read().process.generation()
    }

    /// Enable/disable global device events.
    ///
    /// This filter affects device events not targeted at windows, such as mouse move outside windows or
    /// key presses when the app has no focused window.
    pub fn set_device_events_filter(&self, filter: DeviceEventsFilter) -> Result<()> {
        self.write().process.set_device_events_filter(filter)
    }

    /// Sends a request to open a window and associate it with the `window_id`.
    ///
    /// A [`RAW_WINDOW_OPEN_EVENT`] or [`RAW_WINDOW_OR_HEADLESS_OPEN_ERROR_EVENT`] will be received in response to this request.
    ///
    /// [`RAW_WINDOW_OPEN_EVENT`]: crate::view_process::raw_events::RAW_WINDOW_OPEN_EVENT
    /// [`RAW_WINDOW_OR_HEADLESS_OPEN_ERROR_EVENT`]: crate::view_process::raw_events::RAW_WINDOW_OR_HEADLESS_OPEN_ERROR_EVENT
    pub fn open_window(&self, config: WindowRequest) -> Result<()> {
        self.write().process.open_window(config)
    }

    /// Sends a request to open a headless renderer and associate it with the `window_id`.
    ///
    /// Note that no actual window is created, only the renderer, the use of window-ids to identify
    /// this renderer is only for convenience.
    ///
    /// A [`RAW_HEADLESS_OPEN_EVENT`] or [`RAW_WINDOW_OR_HEADLESS_OPEN_ERROR_EVENT`] will be received in response to this request.
    ///
    /// [`RAW_HEADLESS_OPEN_EVENT`]: crate::view_process::raw_events::RAW_HEADLESS_OPEN_EVENT
    /// [`RAW_WINDOW_OR_HEADLESS_OPEN_ERROR_EVENT`]: crate::view_process::raw_events::RAW_WINDOW_OR_HEADLESS_OPEN_ERROR_EVENT
    pub fn open_headless(&self, config: HeadlessRequest) -> Result<()> {
        self.write().process.open_headless(config)
    }

    /// Send a request to open a connection to an audio output device.
    ///
    /// A [`RAW_AUDIO_OUTPUT_OPEN_EVENT`] or [`RAW_AUDIO_OUTPUT_OPEN_ERROR_EVENT`]
    ///
    /// [`RAW_AUDIO_OUTPUT_OPEN_EVENT`]: crate::view_process::raw_events::RAW_AUDIO_OUTPUT_OPEN_EVENT
    /// [`RAW_AUDIO_OUTPUT_OPEN_ERROR_EVENT`]: crate::view_process::raw_events::RAW_AUDIO_OUTPUT_OPEN_ERROR_EVENT
    pub fn open_audio_output(&self, request: AudioOutputRequest) -> Result<()> {
        self.write().process.open_audio_output(request)
    }

    /// Send an image for decoding and caching.
    ///
    /// This function returns immediately, the handle must be held and compared with the [`RAW_IMAGE_METADATA_DECODED_EVENT`],
    /// [`RAW_IMAGE_DECODED_EVENT`] and [`RAW_IMAGE_DECODE_ERROR_EVENT`] events to receive the data.
    ///
    /// [`RAW_IMAGE_METADATA_DECODED_EVENT`]: crate::view_process::raw_events::RAW_IMAGE_METADATA_DECODED_EVENT
    /// [`RAW_IMAGE_DECODED_EVENT`]: crate::view_process::raw_events::RAW_IMAGE_DECODED_EVENT
    /// [`RAW_IMAGE_DECODE_ERROR_EVENT`]: crate::view_process::raw_events::RAW_IMAGE_DECODE_ERROR_EVENT
    pub fn add_image(&self, request: ImageRequest<IpcBytes>) -> Result<ViewImageHandle> {
        let mut app = self.write();

        let id = app.process.add_image(request)?;

        let handle = ArcEq::new((APP.id().unwrap(), app.process.generation(), id));
        app.loading_images.push(ArcEq::downgrade(&handle));

        Ok(ViewImageHandle(Some(handle)))
    }

    /// Starts sending an image for *progressive* decoding and caching.
    ///
    /// This function returns immediately, the handle must be held and compared with the [`RAW_IMAGE_METADATA_DECODED_EVENT`],
    /// [`RAW_IMAGE_DECODED_EVENT`] and [`RAW_IMAGE_DECODE_ERROR_EVENT`] events to receive the data.
    ///
    /// [`RAW_IMAGE_METADATA_DECODED_EVENT`]: crate::view_process::raw_events::RAW_IMAGE_METADATA_DECODED_EVENT
    /// [`RAW_IMAGE_DECODED_EVENT`]: crate::view_process::raw_events::RAW_IMAGE_DECODED_EVENT
    /// [`RAW_IMAGE_DECODE_ERROR_EVENT`]: crate::view_process::raw_events::RAW_IMAGE_DECODE_ERROR_EVENT
    pub fn add_image_pro(&self, request: ImageRequest<IpcReceiver<IpcBytes>>) -> Result<ViewImageHandle> {
        let mut app = self.write();

        let id = app.process.add_image_pro(request)?;

        let handle = ArcEq::new((APP.id().unwrap(), app.process.generation(), id));
        app.loading_images.push(ArcEq::downgrade(&handle));

        Ok(ViewImageHandle(Some(handle)))
    }

    /// Starts encoding an image.
    ///
    /// The returned channel will update once with the result.
    pub fn encode_image(&self, request: ImageEncodeRequest) -> Receiver<std::result::Result<IpcBytes, EncodeError>> {
        let (sender, receiver) = channel::bounded(1);

        if request.id != ImageId::INVALID {
            let mut app = VIEW_PROCESS.write();

            match app.process.encode_image(request) {
                Ok(r) => {
                    app.encoding_images.push(EncodeRequest {
                        task_id: r,
                        listener: sender,
                    });
                }
                Err(_) => {
                    let _ = sender.send_blocking(Err(EncodeError::Disconnected));
                }
            }
        } else {
            let _ = sender.send_blocking(Err(EncodeError::Dummy));
        }

        receiver
    }

    /// Send an audio for decoding and caching.
    ///
    /// Depending on the request the audio may be decoded entirely or it may be decoded on demand.
    ///
    /// This function returns immediately, the handle must be held and compared with the [`RAW_AUDIO_METADATA_DECODED_EVENT`],
    /// [`RAW_AUDIO_DECODED_EVENT`] and [`RAW_AUDIO_DECODE_ERROR_EVENT`] events to receive the metadata and data.
    ///
    /// [`RAW_AUDIO_METADATA_DECODED_EVENT`]: crate::view_process::raw_events::RAW_AUDIO_METADATA_DECODED_EVENT
    /// [`RAW_AUDIO_DECODED_EVENT`]: crate::view_process::raw_events::RAW_AUDIO_DECODED_EVENT
    /// [`RAW_AUDIO_DECODE_ERROR_EVENT`]: crate::view_process::raw_events::RAW_AUDIO_DECODE_ERROR_EVENT
    pub fn add_audio(&self, request: AudioRequest<IpcBytes>) -> Result<ViewAudioHandle> {
        let mut app = self.write();

        let id = app.process.add_audio(request)?;

        let handle = ArcEq::new((APP.id().unwrap(), app.process.generation(), id));
        app.loading_audios.push(ArcEq::downgrade(&handle));

        Ok(ViewAudioHandle(Some(handle)))
    }

    /// Starts sending an audio for decoding and caching.
    ///
    /// Depending on the request the audio may be decoded as it is received or it may be decoded on demand.
    ///
    /// This function returns immediately, the handle must be held and compared with the [`RAW_AUDIO_METADATA_DECODED_EVENT`],
    /// [`RAW_AUDIO_DECODED_EVENT`] and [`RAW_AUDIO_DECODE_ERROR_EVENT`] events to receive the metadata and data.
    ///
    /// [`RAW_AUDIO_METADATA_DECODED_EVENT`]: crate::view_process::raw_events::RAW_AUDIO_METADATA_DECODED_EVENT
    /// [`RAW_AUDIO_DECODED_EVENT`]: crate::view_process::raw_events::RAW_AUDIO_DECODED_EVENT
    /// [`RAW_AUDIO_DECODE_ERROR_EVENT`]: crate::view_process::raw_events::RAW_AUDIO_DECODE_ERROR_EVENT
    pub fn add_audio_pro(&self, request: AudioRequest<IpcReceiver<IpcBytes>>) -> Result<ViewAudioHandle> {
        let mut app = self.write();

        let id = app.process.add_audio_pro(request)?;

        let handle = ArcEq::new((APP.id().unwrap(), app.process.generation(), id));
        app.loading_audios.push(ArcEq::downgrade(&handle));

        Ok(ViewAudioHandle(Some(handle)))
    }

    /// View-process clipboard methods.
    pub fn clipboard(&self) -> Result<&ViewClipboard> {
        if VIEW_PROCESS.is_connected() {
            Ok(&ViewClipboard {})
        } else {
            Err(ChannelError::disconnected())
        }
    }

    /// Register a native notification, either a popup or an entry in the system notifications list.
    ///
    /// If the `notification` var updates the notification content updates or closes.
    ///
    /// If the notification is responded the `responder` variable is set.
    pub fn notification_dialog(&self, notification: Var<Notification>, responder: ResponderVar<NotificationResponse>) -> Result<()> {
        let mut app = self.write();
        let dlg_id = app.process.notification_dialog(notification.get())?;
        let handle = notification.hook(move |n| {
            let mut app = VIEW_PROCESS.write();
            let retain = app.notifications.iter().any(|(id, _, _)| *id == dlg_id);
            if retain {
                app.process.update_notification(dlg_id, n.value().clone()).ok();
            }
            retain
        });
        app.notifications.push((dlg_id, handle, responder));
        Ok(())
    }

    /// Number of frame send that have not finished rendering.
    ///
    /// This is the sum of pending frames for all renderers.
    pub fn pending_frames(&self) -> usize {
        self.write().pending_frames
    }

    /// Reopen the view-process, causing another [`Event::Inited`].
    ///
    /// [`Event::Inited`]: zng_view_api::Event::Inited
    pub fn respawn(&self) {
        self.write().process.respawn()
    }

    /// Gets the ID for the `extension_name` in the current view-process.
    ///
    /// The ID can change for every view-process instance, you must subscribe to the
    /// [`VIEW_PROCESS_INITED_EVENT`] to refresh the ID. The view-process can respawn
    /// at any time in case of error.
    pub fn extension_id(&self, extension_name: impl Into<ApiExtensionName>) -> Result<Option<ApiExtensionId>> {
        let me = self.read();
        if me.process.is_connected() {
            Ok(self.info().extensions.id(&extension_name.into()))
        } else {
            Err(ChannelError::disconnected())
        }
    }

    /// Licenses that may be required to be displayed in the app about screen.
    ///
    /// This is specially important for prebuilt view users, as the tools that scrap licenses
    /// may not find the prebuilt dependencies.
    pub fn third_party_licenses(&self) -> Result<Vec<crate::third_party::LicenseUsed>> {
        self.write().process.third_party_licenses()
    }

    /// Call an extension with custom encoded payload.
    pub fn app_extension_raw(&self, extension_id: ApiExtensionId, extension_request: ApiExtensionPayload) -> Result<ApiExtensionPayload> {
        self.write().process.app_extension(extension_id, extension_request)
    }

    /// Call an extension with payload `request`.
    pub fn app_extension<I, O>(&self, extension_id: ApiExtensionId, request: &I) -> Result<std::result::Result<O, ApiExtensionRecvError>>
    where
        I: serde::Serialize,
        O: serde::de::DeserializeOwned,
    {
        let payload = ApiExtensionPayload::serialize(&request).unwrap();
        let response = self.write().process.app_extension(extension_id, payload)?;
        Ok(response.deserialize::<O>())
    }

    /// Handle an [`Event::Disconnected`].
    ///
    /// The process will exit if the view-process was killed by the user.
    ///
    /// [`Event::Disconnected`]: zng_view_api::Event::Disconnected
    pub fn handle_disconnect(&self, vp_gen: ViewProcessGen) {
        self.write().process.handle_disconnect(vp_gen)
    }

    /// Spawn the View Process.
    pub(super) fn start<F>(&self, view_process_exe: PathBuf, view_process_env: HashMap<Txt, Txt>, headless: bool, on_event: F)
    where
        F: FnMut(Event) + Send + 'static,
    {
        let _s = tracing::debug_span!("VIEW_PROCESS.start", ?view_process_exe, ?view_process_env, ?headless).entered();

        let process = zng_view_api::Controller::start(view_process_exe, view_process_env, headless, on_event);
        *VIEW_PROCESS_SV.write() = Some(ViewProcessService {
            data_generation: process.generation(),
            process,
            input_device_ids: HashMap::default(),
            monitor_ids: HashMap::default(),
            loading_images: vec![],
            encoding_images: vec![],
            loading_audios: vec![],
            pending_frames: 0,
            message_dialogs: vec![],
            file_dialogs: vec![],
            notifications: vec![],
            ping_count: 0,
        });
    }

    pub(crate) fn on_window_opened(&self, window_id: WindowId, data: zng_view_api::window::WindowOpenData) -> (ViewWindow, WindowOpenData) {
        let mut app = self.write();
        let _ = app.check_generation();

        let win = ViewWindow(ArcEq::new(ViewWindowData {
            app_id: APP.id().unwrap(),
            id: ApiWindowId::from_raw(window_id.get()),
            generation: app.data_generation,
        }));
        drop(app);

        let data = WindowOpenData::new(data, |id| self.monitor_id(id));

        (win, data)
    }

    pub(crate) fn on_audio_output_opened(&self, output_id: AudioOutputId, data: AudioOutputOpenData) -> ViewAudioOutput {
        let mut app = self.write();
        let _ = app.check_generation();

        ViewAudioOutput(ArcEq::new(ViewAudioOutputData {
            app_id: APP.id().unwrap(),
            id: ApiAudioOutputId::from_raw(output_id.get()),
            generation: app.data_generation,
            data,
        }))
    }

    /// Translate input device ID, generates a device id if it was unknown.
    pub(super) fn input_device_id(&self, id: ApiDeviceId) -> InputDeviceId {
        *self.write().input_device_ids.entry(id).or_insert_with(InputDeviceId::new_unique)
    }

    /// Translate `MonId` to `MonitorId`, generates a monitor id if it was unknown.
    pub(super) fn monitor_id(&self, id: ApiMonitorId) -> MonitorId {
        *self.write().monitor_ids.entry(id).or_insert_with(MonitorId::new_unique)
    }

    /// Handle an [`Event::Inited`].
    ///
    /// The view-process becomes "connected" only after this call.
    ///
    /// [`Event::Inited`]: zng_view_api::Event::Inited
    pub(super) fn handle_inited(&self, inited: &zng_view_api::ViewProcessInfo) {
        let mut me = self.write();
        *VIEW_PROCESS_INFO.write() = inited.clone();
        me.process.handle_inited(inited.generation);
    }

    pub(super) fn handle_suspended(&self) {
        self.write().process.handle_suspended();
    }

    pub(crate) fn on_headless_opened(
        &self,
        id: WindowId,
        data: zng_view_api::window::HeadlessOpenData,
    ) -> (ViewHeadless, HeadlessOpenData) {
        let mut app = self.write();
        let _ = app.check_generation();

        let surf = ViewHeadless(ArcEq::new(ViewWindowData {
            app_id: APP.id().unwrap(),
            id: ApiWindowId::from_raw(id.get()),
            generation: app.data_generation,
        }));

        (surf, data)
    }

    pub(super) fn on_image_metadata(&self, meta: &ImageMetadata) -> Option<ViewImageHandle> {
        let mut app = self.write();

        let mut found = None;
        app.loading_images.retain(|i| {
            if let Some(h) = i.upgrade() {
                if found.is_none() && h.2 == meta.id {
                    found = Some(h);
                }
                // retain
                true
            } else {
                false
            }
        });

        // Best effort avoid tracking handles already dropped,
        // the VIEW_PROCESS handles all image requests so we
        // can track all primary requests, only entry images are send without
        // knowing so we can skip all not found without parent.
        //
        // This could potentially restart tracking an entry that was dropped, but
        // all that does is generate a no-op event and a second `forget_image` requests for the view-process.

        if found.is_none() && meta.parent.is_some() {
            // start tracking entry image

            let handle = ArcEq::new((APP.id().unwrap(), app.process.generation(), meta.id));
            app.loading_images.push(ArcEq::downgrade(&handle));

            return Some(ViewImageHandle(Some(handle)));
        }

        found.map(|h| ViewImageHandle(Some(h)))
    }

    pub(super) fn on_image_decoded(&self, data: &ImageDecoded) -> Option<ViewImageHandle> {
        let mut app = self.write();

        // retain loading handle only for partial decode, cleanup for full decode.
        //
        // All valid not dropped requests are already in `loading_images` because they are
        // either primary requests or are entries (view-process always sends metadata decoded first for entries).

        let mut found = None;
        app.loading_images.retain(|i| {
            if let Some(h) = i.upgrade() {
                if found.is_none() && h.2 == data.meta.id {
                    found = Some(h);
                    return data.partial.is_some();
                }
                true
            } else {
                false
            }
        });

        found.map(|h| ViewImageHandle(Some(h)))
    }

    pub(super) fn on_image_error(&self, id: ImageId) -> Option<ViewImageHandle> {
        let mut app = self.write();

        let mut found = None;
        app.loading_images.retain(|i| {
            if let Some(h) = i.upgrade() {
                if found.is_none() && h.2 == id {
                    found = Some(h);
                    return false;
                }
                true
            } else {
                false
            }
        });

        // error images should already be removed from view-process, handle will request a removal anyway

        found.map(|h| ViewImageHandle(Some(h)))
    }

    pub(super) fn on_audio_metadata(&self, meta: &AudioMetadata) -> Option<ViewAudioHandle> {
        // this is very similar to `on_image_metadata`

        let mut app = self.write();

        let mut found = None;
        app.loading_audios.retain(|i| {
            if let Some(h) = i.upgrade() {
                if found.is_none() && h.2 == meta.id {
                    found = Some(h);
                }
                // retain
                true
            } else {
                false
            }
        });

        if found.is_none() && meta.parent.is_some() {
            // start tracking entry track

            let handle = ArcEq::new((APP.id().unwrap(), app.process.generation(), meta.id));
            app.loading_audios.push(ArcEq::downgrade(&handle));

            return Some(ViewAudioHandle(Some(handle)));
        }

        found.map(|h| ViewAudioHandle(Some(h)))
    }

    pub(super) fn on_audio_decoded(&self, audio: &AudioDecoded) -> Option<ViewAudioHandle> {
        // this is very similar to `on_image_decoded`, the big difference is that
        // partial decodes represent the latest decoded chunk, not all the previous decoded data,
        // and it may never finish decoding too, the progressive source can never end or the request
        // configured it to always decode on demand and drop the buffer as it is played.

        let mut app = self.write();

        let mut found = None;
        app.loading_audios.retain(|i| {
            if let Some(h) = i.upgrade() {
                if found.is_none() && h.2 == audio.id {
                    found = Some(h);
                    return !audio.is_full;
                }
                true
            } else {
                false
            }
        });

        found.map(|h| ViewAudioHandle(Some(h)))
    }

    pub(super) fn on_audio_error(&self, id: AudioId) -> Option<ViewAudioHandle> {
        let mut app = self.write();

        let mut found = None;
        app.loading_audios.retain(|i| {
            if let Some(h) = i.upgrade() {
                if found.is_none() && h.2 == id {
                    found = Some(h);
                    return false;
                }
                true
            } else {
                false
            }
        });

        // error audios should already be removed from view-process, handle will request a removal anyway

        found.map(|h| ViewAudioHandle(Some(h)))
    }

    pub(crate) fn on_frame_rendered(&self, _id: WindowId) {
        let mut vp = self.write();
        vp.pending_frames = vp.pending_frames.saturating_sub(1);
    }

    pub(crate) fn on_frame_image(&self, data: &ImageDecoded) -> ViewImageHandle {
        ViewImageHandle(Some(ArcEq::new((APP.id().unwrap(), self.generation(), data.meta.id))))
    }

    pub(super) fn on_image_encoded(&self, task_id: ImageEncodeId, data: IpcBytes) {
        self.on_image_encode_result(task_id, Ok(data));
    }
    pub(super) fn on_image_encode_error(&self, task_id: ImageEncodeId, error: Txt) {
        self.on_image_encode_result(task_id, Err(EncodeError::Encode(error)));
    }
    fn on_image_encode_result(&self, task_id: ImageEncodeId, result: std::result::Result<IpcBytes, EncodeError>) {
        let mut app = self.write();
        app.encoding_images.retain(move |r| {
            let done = r.task_id == task_id;
            if done {
                let _ = r.listener.send_blocking(result.clone());
            }
            !done
        })
    }

    pub(crate) fn on_message_dlg_response(&self, id: zng_view_api::dialog::DialogId, response: MsgDialogResponse) {
        let mut app = self.write();
        if let Some(i) = app.message_dialogs.iter().position(|(i, _)| *i == id) {
            let (_, r) = app.message_dialogs.swap_remove(i);
            r.respond(response);
        }
    }

    pub(crate) fn on_file_dlg_response(&self, id: zng_view_api::dialog::DialogId, response: FileDialogResponse) {
        let mut app = self.write();
        if let Some(i) = app.file_dialogs.iter().position(|(i, _)| *i == id) {
            let (_, r) = app.file_dialogs.swap_remove(i);
            r.respond(response);
        }
    }

    pub(crate) fn on_notification_dlg_response(&self, id: zng_view_api::dialog::DialogId, response: NotificationResponse) {
        let mut app = self.write();
        if let Some(i) = app.notifications.iter().position(|(i, _, _)| *i == id) {
            let (_, _, r) = app.notifications.swap_remove(i);
            r.respond(response);
        }
    }

    pub(super) fn on_respawned(&self, _gen: ViewProcessGen) {
        let mut app = self.write();
        app.pending_frames = 0;
        for (_, r) in app.message_dialogs.drain(..) {
            r.respond(MsgDialogResponse::Error(Txt::from_static("respawn")));
        }
        for (_, r) in app.file_dialogs.drain(..) {
            r.respond(FileDialogResponse::Error(Txt::from_static("respawn")));
        }
        for (_, _, r) in app.notifications.drain(..) {
            r.respond(NotificationResponse::Error(Txt::from_static("respawn")));
        }
    }

    pub(crate) fn exit(&self) {
        *VIEW_PROCESS_SV.write() = None;
    }

    pub(crate) fn ping(&self) {
        let mut app = self.write();
        let count = app.ping_count.wrapping_add(1);
        if let Ok(c) = app.process.ping(count)
            && c != count
        {
            tracing::error!("incorrect ping response, expected {count}, was {c}");
        }
        app.ping_count = count;
    }

    pub(crate) fn on_pong(&self, count: u16) {
        let expected = self.read().ping_count;
        if expected != count {
            // this could indicates a severe slowdown in the event pump
            tracing::warn!("unexpected pong event, expected {expected}, was {count}");
        }
    }
}
impl ViewProcessService {
    #[must_use = "if `true` all current WinId, DevId and MonId are invalid"]
    fn check_generation(&mut self) -> bool {
        let vp_gen = self.process.generation();
        let invalid = vp_gen != self.data_generation;
        if invalid {
            self.data_generation = vp_gen;
            self.input_device_ids.clear();
            self.monitor_ids.clear();
        }
        invalid
    }
}

event_args! {
    /// Arguments for the [`VIEW_PROCESS_INITED_EVENT`].
    pub struct ViewProcessInitedArgs {
        /// View-process implementation info.
        pub info: zng_view_api::ViewProcessInfo,

        ..

        /// Broadcast to all.
        fn is_in_target(&self, _id: WidgetId) -> bool {
            true
        }
    }

    /// Arguments for the [`VIEW_PROCESS_SUSPENDED_EVENT`].
    pub struct ViewProcessSuspendedArgs {

        ..

        /// Broadcast to all.
        fn is_in_target(&self, _id: WidgetId) -> bool {
            true
        }
    }
}
impl std::ops::Deref for ViewProcessInitedArgs {
    type Target = zng_view_api::ViewProcessInfo;

    fn deref(&self) -> &Self::Target {
        &self.info
    }
}

event! {
    /// View-Process finished initializing and is now connected and ready.
    pub static VIEW_PROCESS_INITED_EVENT: ViewProcessInitedArgs;
    /// View-Process suspended, all resources dropped.
    ///
    /// The view-process will only be available if the app resumes. On resume [`VIEW_PROCESS_INITED_EVENT`]
    /// notify a view-process respawn.
    pub static VIEW_PROCESS_SUSPENDED_EVENT: ViewProcessSuspendedArgs;
}

/// Information about a successfully opened window.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct WindowOpenData {
    /// Window complete state.
    pub state: WindowStateAll,

    /// Monitor that contains the window.
    pub monitor: Option<MonitorId>,

    /// Final top-left offset of the window (excluding outer chrome).
    ///
    /// The values are the global position and the position in the monitor.
    pub position: (PxPoint, DipPoint),
    /// Final dimensions of the client area of the window (excluding outer chrome).
    pub size: DipSize,

    /// Final scale factor.
    pub scale_factor: Factor,

    /// Actual render mode, can be different from the requested mode if it is not available.
    pub render_mode: RenderMode,

    /// Padding that must be applied to the window content so that it stays clear of screen obstructions
    /// such as a camera notch cutout.
    ///
    /// Note that the *unsafe* area must still be rendered as it may be partially visible, just don't place nay
    /// interactive or important content outside of this padding.
    pub safe_padding: DipSideOffsets,
}
impl WindowOpenData {
    pub(crate) fn new(data: zng_view_api::window::WindowOpenData, map_monitor: impl FnOnce(ApiMonitorId) -> MonitorId) -> Self {
        WindowOpenData {
            state: data.state,
            monitor: data.monitor.map(map_monitor),
            position: data.position,
            size: data.size,
            scale_factor: data.scale_factor,
            render_mode: data.render_mode,
            safe_padding: data.safe_padding,
        }
    }
}

/// Handle to a window open in the view-process.
///
/// The window is closed when all clones of the handle are dropped.
#[derive(Debug, Clone, PartialEq, Eq)]
#[must_use = "the window is closed when all clones of the handle are dropped"]
pub struct ViewWindow(ArcEq<ViewWindowData>);
impl ViewWindow {
    /// Returns the view-process generation on which the window was open.
    pub fn generation(&self) -> ViewProcessGen {
        self.0.generation
    }

    /// Set the window title.
    pub fn set_title(&self, title: Txt) -> Result<()> {
        self.0.call(|id, p| p.set_title(id, title))
    }

    /// Set the window visibility.
    pub fn set_visible(&self, visible: bool) -> Result<()> {
        self.0.call(|id, p| p.set_visible(id, visible))
    }

    /// Set if the window is "top-most".
    pub fn set_always_on_top(&self, always_on_top: bool) -> Result<()> {
        self.0.call(|id, p| p.set_always_on_top(id, always_on_top))
    }

    /// Set if the user can drag-move the window.
    pub fn set_movable(&self, movable: bool) -> Result<()> {
        self.0.call(|id, p| p.set_movable(id, movable))
    }

    /// Set if the user can resize the window.
    pub fn set_resizable(&self, resizable: bool) -> Result<()> {
        self.0.call(|id, p| p.set_resizable(id, resizable))
    }

    /// Set the window icon.
    pub fn set_icon(&self, icon: Option<&ViewImageHandle>) -> Result<()> {
        self.0.call(|id, p| {
            if let Some(icon) = icon.and_then(|i| i.0.as_ref()) {
                if p.generation() == icon.1 {
                    p.set_icon(id, Some(icon.2))
                } else {
                    Err(ChannelError::disconnected())
                }
            } else {
                p.set_icon(id, None)
            }
        })
    }

    /// Set the window cursor icon and visibility.
    pub fn set_cursor(&self, cursor: Option<CursorIcon>) -> Result<()> {
        self.0.call(|id, p| p.set_cursor(id, cursor))
    }

    /// Set the window cursor to a custom image.
    ///
    /// Falls back to cursor icon if set to `None`.
    ///
    /// The `hotspot` value is an exact point in the image that is the mouse position. This value is only used if
    /// the image format does not contain a hotspot.
    pub fn set_cursor_image(&self, cursor: Option<&ViewImageHandle>, hotspot: PxPoint) -> Result<()> {
        self.0.call(|id, p| {
            if let Some(cur) = cursor.and_then(|i| i.0.as_ref()) {
                if p.generation() == cur.1 {
                    p.set_cursor_image(id, Some(zng_view_api::window::CursorImage::new(cur.2, hotspot)))
                } else {
                    Err(ChannelError::disconnected())
                }
            } else {
                p.set_cursor_image(id, None)
            }
        })
    }

    /// Set the window icon visibility in the taskbar.
    pub fn set_taskbar_visible(&self, visible: bool) -> Result<()> {
        self.0.call(|id, p| p.set_taskbar_visible(id, visible))
    }

    /// Bring the window the z top.
    pub fn bring_to_top(&self) -> Result<()> {
        self.0.call(|id, p| p.bring_to_top(id))
    }

    /// Set the window state.
    pub fn set_state(&self, state: WindowStateAll) -> Result<()> {
        self.0.call(|id, p| p.set_state(id, state))
    }

    /// Set video mode used in exclusive fullscreen.
    pub fn set_video_mode(&self, mode: VideoMode) -> Result<()> {
        self.0.call(|id, p| p.set_video_mode(id, mode))
    }

    /// Set enabled window chrome buttons.
    pub fn set_enabled_buttons(&self, buttons: WindowButton) -> Result<()> {
        self.0.call(|id, p| p.set_enabled_buttons(id, buttons))
    }

    /// Reference the window renderer.
    pub fn renderer(&self) -> ViewRenderer {
        ViewRenderer(ArcEq::downgrade(&self.0))
    }

    /// Sets if the headed window is in *capture-mode*. If `true` the resources used to capture
    /// a screenshot may be kept in memory to be reused in the next screenshot capture.
    ///
    /// Note that capture must still be requested in each frame request.
    pub fn set_capture_mode(&self, enabled: bool) -> Result<()> {
        self.0.call(|id, p| p.set_capture_mode(id, enabled))
    }

    /// Brings the window to the front and sets input focus.
    ///
    /// This request can steal focus from other apps disrupting the user, be careful with it.
    pub fn focus(&self) -> Result<FocusResult> {
        self.0.call(|id, p| p.focus(id))
    }

    /// Sets the user attention request indicator, the indicator is cleared when the window is focused or
    /// if canceled by setting to `None`.
    pub fn set_focus_indicator(&self, indicator: Option<FocusIndicator>) -> Result<()> {
        self.0.call(|id, p| p.set_focus_indicator(id, indicator))
    }

    /// Moves the window with the left mouse button until the button is released.
    ///
    /// There's no guarantee that this will work unless the left mouse button was pressed immediately before this function is called.
    pub fn drag_move(&self) -> Result<()> {
        self.0.call(|id, p| p.drag_move(id))
    }

    /// Resizes the window with the left mouse button until the button is released.
    ///
    /// There's no guarantee that this will work unless the left mouse button was pressed immediately before this function is called.
    pub fn drag_resize(&self, direction: ResizeDirection) -> Result<()> {
        self.0.call(|id, p| p.drag_resize(id, direction))
    }

    /// Start a drag and drop operation, if the window is pressed.
    ///
    /// A [`RAW_APP_DRAG_ENDED_EVENT`] will be received when the operation finishes.
    ///
    /// [`RAW_APP_DRAG_ENDED_EVENT`]: raw_events::RAW_APP_DRAG_ENDED_EVENT
    pub fn start_drag_drop(
        &self,
        data: Vec<DragDropData>,
        allowed_effects: DragDropEffect,
    ) -> Result<std::result::Result<DragDropId, DragDropError>> {
        self.0.call(|id, p| p.start_drag_drop(id, data, allowed_effects))
    }

    /// Notify the drag source of what effect was applied for a received drag&drop.
    pub fn drag_dropped(&self, drop_id: DragDropId, applied: DragDropEffect) -> Result<()> {
        self.0.call(|id, p| p.drag_dropped(id, drop_id, applied))
    }

    /// Open system title bar context menu.
    pub fn open_title_bar_context_menu(&self, position: DipPoint) -> Result<()> {
        self.0.call(|id, p| p.open_title_bar_context_menu(id, position))
    }

    /// Shows a native message dialog for the window.
    ///
    /// The window is not interactive while the dialog is visible and the dialog may be modal in the view-process.
    /// In the app-process this is always async, and the response var will update once when the user responds.
    pub fn message_dialog(&self, dlg: MsgDialog, responder: ResponderVar<MsgDialogResponse>) -> Result<()> {
        let dlg_id = self.0.call(|id, p| p.message_dialog(id, dlg))?;
        VIEW_PROCESS.handle_write(self.0.app_id).message_dialogs.push((dlg_id, responder));
        Ok(())
    }

    /// Shows a native file/folder dialog for the window.
    ///
    /// The window is not interactive while the dialog is visible and the dialog may be modal in the view-process.
    /// In the app-process this is always async, and the response var will update once when the user responds.
    pub fn file_dialog(&self, dlg: FileDialog, responder: ResponderVar<FileDialogResponse>) -> Result<()> {
        let dlg_id = self.0.call(|id, p| p.file_dialog(id, dlg))?;
        VIEW_PROCESS.handle_write(self.0.app_id).file_dialogs.push((dlg_id, responder));
        Ok(())
    }

    /// Update the window's accessibility info tree.
    pub fn access_update(&self, update: zng_view_api::access::AccessTreeUpdate) -> Result<()> {
        self.0.call(|id, p| p.access_update(id, update))
    }

    /// Enable or disable IME by setting a cursor area.
    ///
    /// In mobile platforms also shows the software keyboard for `Some(_)` and hides it for `None`.
    pub fn set_ime_area(&self, area: Option<DipRect>) -> Result<()> {
        self.0.call(|id, p| p.set_ime_area(id, area))
    }

    /// Attempt to set a system wide shutdown warning associated with the window.
    ///
    /// Operating systems that support this show the `reason` in a warning for the user, it must be a short text
    /// that identifies the critical operation that cannot be cancelled.
    ///
    /// Note that there is no guarantee that the view-process or operating system will actually set a block, there
    /// is no error result because operating systems can silently ignore block requests at any moment, even after
    /// an initial successful block.
    ///
    /// Set to an empty text to remove the warning.
    pub fn set_system_shutdown_warn(&self, reason: Txt) -> Result<()> {
        self.0.call(move |id, p| p.set_system_shutdown_warn(id, reason))
    }

    /// Drop `self`.
    pub fn close(self) {
        drop(self)
    }

    /// Call a window extension with custom encoded payload.
    pub fn window_extension_raw(&self, extension_id: ApiExtensionId, request: ApiExtensionPayload) -> Result<ApiExtensionPayload> {
        self.0.call(|id, p| p.window_extension(id, extension_id, request))
    }

    /// Call a window extension with serialized payload.
    pub fn window_extension<I, O>(&self, extension_id: ApiExtensionId, request: &I) -> Result<std::result::Result<O, ApiExtensionRecvError>>
    where
        I: serde::Serialize,
        O: serde::de::DeserializeOwned,
    {
        let r = self.window_extension_raw(extension_id, ApiExtensionPayload::serialize(&request).unwrap())?;
        Ok(r.deserialize())
    }

    /// Create a weak reference to this handle.
    pub fn downgrade(&self) -> WeakViewWindow {
        WeakViewWindow(ArcEq::downgrade(&self.0))
    }
}
/// Weak reference to a [`ViewWindow`].
#[derive(Debug, Clone)]
pub struct WeakViewWindow(WeakEq<ViewWindowData>);
impl PartialEq for WeakViewWindow {
    fn eq(&self, other: &Self) -> bool {
        sync::Weak::ptr_eq(&self.0, &other.0)
    }
}
impl Eq for WeakViewWindow {}
impl WeakViewWindow {
    /// Create a strong reference to the view window, if it is still open.
    pub fn upgrade(&self) -> Option<ViewWindow> {
        let d = self.0.upgrade()?;
        if d.generation == VIEW_PROCESS.generation() {
            Some(ViewWindow(d))
        } else {
            None
        }
    }
}

/// View window or headless surface.
#[derive(Clone, Debug)]
pub enum ViewWindowOrHeadless {
    /// Headed window view.
    Window(ViewWindow),
    /// Headless surface view.
    Headless(ViewHeadless),
}
impl ViewWindowOrHeadless {
    /// Reference the window or surface renderer.
    pub fn renderer(&self) -> ViewRenderer {
        match self {
            ViewWindowOrHeadless::Window(w) => w.renderer(),
            ViewWindowOrHeadless::Headless(h) => h.renderer(),
        }
    }

    /// Call a window extension with custom encoded payload.
    pub fn window_extension_raw(&self, extension_id: ApiExtensionId, request: ApiExtensionPayload) -> Result<ApiExtensionPayload> {
        match self {
            ViewWindowOrHeadless::Window(w) => w.window_extension_raw(extension_id, request),
            ViewWindowOrHeadless::Headless(h) => h.window_extension_raw(extension_id, request),
        }
    }

    /// Call a window extension with serialized payload.
    pub fn window_extension<I, O>(&self, extension_id: ApiExtensionId, request: &I) -> Result<std::result::Result<O, ApiExtensionRecvError>>
    where
        I: serde::Serialize,
        O: serde::de::DeserializeOwned,
    {
        match self {
            ViewWindowOrHeadless::Window(w) => w.window_extension(extension_id, request),
            ViewWindowOrHeadless::Headless(h) => h.window_extension(extension_id, request),
        }
    }
}
impl From<ViewWindow> for ViewWindowOrHeadless {
    fn from(w: ViewWindow) -> Self {
        ViewWindowOrHeadless::Window(w)
    }
}
impl From<ViewHeadless> for ViewWindowOrHeadless {
    fn from(w: ViewHeadless) -> Self {
        ViewWindowOrHeadless::Headless(w)
    }
}

#[derive(Debug)]
struct ViewAudioOutputData {
    app_id: AppId,
    id: ApiAudioOutputId,
    generation: ViewProcessGen,
    data: AudioOutputOpenData,
}
impl ViewAudioOutputData {
    fn call<R>(&self, f: impl FnOnce(ApiAudioOutputId, &mut Controller) -> Result<R>) -> Result<R> {
        let mut app = VIEW_PROCESS.handle_write(self.app_id);
        if app.check_generation() {
            Err(ChannelError::disconnected())
        } else {
            f(self.id, &mut app.process)
        }
    }
}
impl Drop for ViewAudioOutputData {
    fn drop(&mut self) {
        if VIEW_PROCESS.is_available() {
            let mut app = VIEW_PROCESS.handle_write(self.app_id);
            if self.generation == app.process.generation() {
                let _ = app.process.close_audio_output(self.id);
            }
        }
    }
}

/// Handle to an audio output stream in the View Process.
///
/// The stream is disposed when all clones of the handle are dropped.
#[derive(Clone, Debug, PartialEq, Eq)]
#[must_use = "the audio output is disposed when all clones of the handle are dropped"]
pub struct ViewAudioOutput(ArcEq<ViewAudioOutputData>);
impl ViewAudioOutput {
    /// Play or enqueue audio.
    pub fn cue(&self, mix: AudioMix) -> Result<AudioPlayId> {
        self.0.call(|id, p| p.cue_audio(AudioPlayRequest::new(id, mix)))
    }

    /// Update state, volume, speed.
    pub fn update(&self, cfg: AudioOutputConfig) -> Result<()> {
        self.0.call(|id, p| p.update_audio_output(AudioOutputUpdateRequest::new(id, cfg)))
    }

    /// Audio output stream data.
    pub fn data(&self) -> &AudioOutputOpenData {
        &self.0.data
    }

    /// Create a weak reference to this audio output.
    pub fn downgrade(&self) -> WeakViewAudioOutput {
        WeakViewAudioOutput(ArcEq::downgrade(&self.0))
    }
}
/// Weak reference to a [`ViewAudioOutput`].
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct WeakViewAudioOutput(WeakEq<ViewAudioOutputData>);
impl WeakViewAudioOutput {
    /// Create a strong reference to the audio output, if it is still open.
    pub fn upgrade(&self) -> Option<ViewAudioOutput> {
        let d = self.0.upgrade()?;
        if d.generation == VIEW_PROCESS.generation() {
            Some(ViewAudioOutput(d))
        } else {
            None
        }
    }
}

#[derive(Debug)]
struct ViewWindowData {
    app_id: AppId,
    id: ApiWindowId,
    generation: ViewProcessGen,
}
impl ViewWindowData {
    fn call<R>(&self, f: impl FnOnce(ApiWindowId, &mut Controller) -> Result<R>) -> Result<R> {
        let mut app = VIEW_PROCESS.handle_write(self.app_id);
        if app.check_generation() {
            Err(ChannelError::disconnected())
        } else {
            f(self.id, &mut app.process)
        }
    }
}
impl Drop for ViewWindowData {
    fn drop(&mut self) {
        if VIEW_PROCESS.is_available() {
            let mut app = VIEW_PROCESS.handle_write(self.app_id);
            if self.generation == app.process.generation() {
                let _ = app.process.close(self.id);
            }
        }
    }
}

type Result<T> = std::result::Result<T, ChannelError>;

/// Handle to a headless surface/document open in the View Process.
///
/// The view is disposed when all clones of the handle are dropped.
#[derive(Clone, Debug, PartialEq, Eq)]
#[must_use = "the view is disposed when all clones of the handle are dropped"]
pub struct ViewHeadless(ArcEq<ViewWindowData>);
impl ViewHeadless {
    /// Resize the headless surface.
    pub fn set_size(&self, size: DipSize, scale_factor: Factor) -> Result<()> {
        self.0.call(|id, p| p.set_headless_size(id, size, scale_factor))
    }

    /// Reference the window renderer.
    pub fn renderer(&self) -> ViewRenderer {
        ViewRenderer(ArcEq::downgrade(&self.0))
    }

    /// Call a window extension with custom encoded payload.
    pub fn window_extension_raw(&self, extension_id: ApiExtensionId, request: ApiExtensionPayload) -> Result<ApiExtensionPayload> {
        self.0.call(|id, p| p.window_extension(id, extension_id, request))
    }

    /// Call a window extension with serialized payload.
    pub fn window_extension<I, O>(&self, extension_id: ApiExtensionId, request: &I) -> Result<std::result::Result<O, ApiExtensionRecvError>>
    where
        I: serde::Serialize,
        O: serde::de::DeserializeOwned,
    {
        let r = self.window_extension_raw(extension_id, ApiExtensionPayload::serialize(&request).unwrap())?;
        Ok(r.deserialize())
    }

    /// Create a weak reference to this handle.
    pub fn downgrade(&self) -> WeakViewHeadless {
        WeakViewHeadless(ArcEq::downgrade(&self.0))
    }
}

/// Weak reference to a [`ViewHeadless`] handle.
#[derive(Clone, Debug)]
pub struct WeakViewHeadless(WeakEq<ViewWindowData>);
impl PartialEq for WeakViewHeadless {
    fn eq(&self, other: &Self) -> bool {
        sync::Weak::ptr_eq(&self.0, &other.0)
    }
}
impl WeakViewHeadless {
    /// Create a strong reference to the headless surface, if it is still alive.
    pub fn upgrade(&self) -> Option<ViewHeadless> {
        let d = self.0.upgrade()?;
        if d.generation == VIEW_PROCESS.generation() {
            Some(ViewHeadless(d))
        } else {
            None
        }
    }
}

/// Weak handle to a window or view.
///
/// This is only a weak reference, every method returns [`ChannelError::disconnected`] if the
/// window is closed or view is disposed.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ViewRenderer(WeakEq<ViewWindowData>);
impl ViewRenderer {
    fn call<R>(&self, f: impl FnOnce(ApiWindowId, &mut Controller) -> Result<R>) -> Result<R> {
        if let Some(c) = self.0.upgrade() {
            c.call(f)
        } else {
            Err(ChannelError::disconnected())
        }
    }

    /// Returns the view-process generation on which the renderer was created.
    pub fn generation(&self) -> Result<ViewProcessGen> {
        self.0.upgrade().map(|c| c.generation).ok_or(ChannelError::disconnected())
    }

    /// Use an image resource in the window renderer.
    ///
    /// Returns the image texture ID.
    pub fn use_image(&self, image: &ViewImageHandle) -> Result<ImageTextureId> {
        self.call(|id, p| {
            if let Some(img) = &image.0 {
                if p.generation() == img.1 {
                    p.use_image(id, img.2)
                } else {
                    Err(ChannelError::disconnected())
                }
            } else {
                Ok(ImageTextureId::INVALID)
            }
        })
    }

    /// Replace the image resource in the window renderer.
    ///
    /// The new `image` handle must represent an image with same dimensions and format as the previous. If the
    /// image cannot be updated an error is logged and `false` is returned.
    ///
    /// The `dirty_rect` can be set to optimize texture upload to the GPU, if not set the entire image region updates.
    pub fn update_image_use(&mut self, tex_id: ImageTextureId, image: &ViewImageHandle, dirty_rect: Option<PxRect>) -> Result<bool> {
        self.call(|id, p| {
            if let Some(img) = &image.0 {
                if p.generation() == img.1 {
                    p.update_image_use(id, tex_id, img.2, dirty_rect)
                } else {
                    Err(ChannelError::disconnected())
                }
            } else {
                Ok(false)
            }
        })
    }

    /// Delete the image resource in the window renderer.
    pub fn delete_image_use(&mut self, tex_id: ImageTextureId) -> Result<()> {
        self.call(|id, p| p.delete_image_use(id, tex_id))
    }

    /// Add a raw font resource to the window renderer.
    ///
    /// Returns the new font face ID, unique for this renderer.
    pub fn add_font_face(&self, bytes: IpcFontBytes, index: u32) -> Result<FontFaceId> {
        self.call(|id, p| p.add_font_face(id, bytes, index))
    }

    /// Delete the font resource in the window renderer.
    pub fn delete_font_face(&self, font_face_id: FontFaceId) -> Result<()> {
        self.call(|id, p| p.delete_font_face(id, font_face_id))
    }

    /// Add a sized font to the window renderer.
    ///
    /// Returns the new font ID, unique for this renderer.
    pub fn add_font(
        &self,
        font_face_id: FontFaceId,
        glyph_size: Px,
        options: FontOptions,
        variations: Vec<(FontVariationName, f32)>,
    ) -> Result<FontId> {
        self.call(|id, p| p.add_font(id, font_face_id, glyph_size, options, variations))
    }

    /// Delete the sized font.
    pub fn delete_font(&self, font_id: FontId) -> Result<()> {
        self.call(|id, p| p.delete_font(id, font_id))
    }

    /// Create a new image resource from the current rendered frame.
    pub fn frame_image(&self, mask: Option<ImageMaskMode>) -> Result<ViewImageHandle> {
        if let Some(c) = self.0.upgrade() {
            let id = c.call(|id, p| p.frame_image(id, mask))?;
            Ok(Self::add_frame_image(c.app_id, id))
        } else {
            Err(ChannelError::disconnected())
        }
    }

    /// Create a new image resource from a selection of the current rendered frame.
    pub fn frame_image_rect(&self, rect: PxRect, mask: Option<ImageMaskMode>) -> Result<ViewImageHandle> {
        if let Some(c) = self.0.upgrade() {
            let id = c.call(|id, p| p.frame_image_rect(id, rect, mask))?;
            Ok(Self::add_frame_image(c.app_id, id))
        } else {
            Err(ChannelError::disconnected())
        }
    }

    fn add_frame_image(app_id: AppId, id: ImageId) -> ViewImageHandle {
        if id == ImageId::INVALID {
            ViewImageHandle::dummy()
        } else {
            let mut app = VIEW_PROCESS.handle_write(app_id);
            let handle = ArcEq::new((APP.id().unwrap(), app.process.generation(), id));
            app.loading_images.push(ArcEq::downgrade(&handle));

            ViewImageHandle(Some(handle))
        }
    }

    /// Render a new frame.
    pub fn render(&self, frame: FrameRequest) -> Result<()> {
        if let Some(w) = self.0.upgrade() {
            w.call(|id, p| p.render(id, frame))?;
            VIEW_PROCESS.handle_write(w.app_id).pending_frames += 1;
            Ok(())
        } else {
            Err(ChannelError::disconnected())
        }
    }

    /// Update the current frame and re-render it.
    pub fn render_update(&self, frame: FrameUpdateRequest) -> Result<()> {
        if let Some(w) = self.0.upgrade() {
            w.call(|id, p| p.render_update(id, frame))?;
            VIEW_PROCESS.handle_write(w.app_id).pending_frames += 1;
            Ok(())
        } else {
            Err(ChannelError::disconnected())
        }
    }

    /// Call a render extension with custom encoded payload.
    pub fn render_extension_raw(&self, extension_id: ApiExtensionId, request: ApiExtensionPayload) -> Result<ApiExtensionPayload> {
        if let Some(w) = self.0.upgrade() {
            w.call(|id, p| p.render_extension(id, extension_id, request))
        } else {
            Err(ChannelError::disconnected())
        }
    }

    /// Call a render extension with serialized payload.
    pub fn render_extension<I, O>(&self, extension_id: ApiExtensionId, request: &I) -> Result<std::result::Result<O, ApiExtensionRecvError>>
    where
        I: serde::Serialize,
        O: serde::de::DeserializeOwned,
    {
        let r = self.render_extension_raw(extension_id, ApiExtensionPayload::serialize(&request).unwrap())?;
        Ok(r.deserialize())
    }
}

type ViewImageHandleData = (AppId, ViewProcessGen, ImageId);

/// Handle to an image loading or loaded in the View Process.
///
/// The image is disposed when all clones of the handle are dropped.
#[must_use = "the image is disposed when all clones of the handle are dropped"]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ViewImageHandle(Option<ArcEq<ViewImageHandleData>>);
impl ViewImageHandle {
    /// New handle to nothing.
    pub fn dummy() -> Self {
        ViewImageHandle(None)
    }

    /// Is handle to nothing.
    pub fn is_dummy(&self) -> bool {
        self.0.is_none()
    }

    /// Image ID.
    ///
    /// Is [`ImageId::INVALID`] for dummy.
    pub fn image_id(&self) -> ImageId {
        self.0.as_ref().map(|h| h.2).unwrap_or(ImageId::INVALID)
    }

    /// Application that requested this image.
    ///
    /// Images can only be used in the same app.
    ///
    /// Is `None` for dummy.
    pub fn app_id(&self) -> Option<AppId> {
        self.0.as_ref().map(|h| h.0.0)
    }

    /// View-process generation that provided this image.
    ///
    /// Images can only be used in the same view-process instance.
    ///
    /// Is [`ViewProcessGen::INVALID`] for dummy.
    pub fn view_process_gen(&self) -> ViewProcessGen {
        self.0.as_ref().map(|h| h.1).unwrap_or(ViewProcessGen::INVALID)
    }

    /// Create a weak reference to this handle.
    pub fn downgrade(&self) -> WeakViewImageHandle {
        match &self.0 {
            Some(h) => WeakViewImageHandle(ArcEq::downgrade(h)),
            None => WeakViewImageHandle(WeakEq::new()),
        }
    }
}
impl Drop for ViewImageHandle {
    fn drop(&mut self) {
        if let Some(h) = self.0.take()
            && Arc::strong_count(&h) == 1
            && let Some(app) = APP.id()
        {
            if h.0.0 != app {
                tracing::error!("image from app `{:?}` dropped in app `{:?}`", h.0, app);
                return;
            }

            if VIEW_PROCESS.is_available() && VIEW_PROCESS.generation() == h.1 {
                let _ = VIEW_PROCESS.write().process.forget_image(h.2);
            }
        }
    }
}
/// Connection to an image loading or loaded in the View Process.
///
/// The image is removed from the View Process cache when all clones of [`ViewImageHandle`] drops, but
/// if there is another image pointer holding the image, this weak pointer can be upgraded back
/// to a strong connection to the image.
///
/// Dummy handles never upgrade back.
#[derive(Clone, Debug)]
pub struct WeakViewImageHandle(WeakEq<ViewImageHandleData>);
impl PartialEq for WeakViewImageHandle {
    fn eq(&self, other: &Self) -> bool {
        sync::Weak::ptr_eq(&self.0, &other.0)
    }
}
impl WeakViewImageHandle {
    /// Attempt to upgrade the weak pointer to the image to a full image.
    ///
    /// Returns `Some` if the is at least another [`ViewImageHandle`] holding the image alive.
    pub fn upgrade(&self) -> Option<ViewImageHandle> {
        let d = self.0.upgrade()?;
        if d.1 == VIEW_PROCESS.generation() {
            Some(ViewImageHandle(Some(d)))
        } else {
            None
        }
    }
}

/// Error returned by [`VIEW_PROCESS::encode_image`].
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum EncodeError {
    /// Encode error.
    Encode(Txt),
    /// Attempted to encode dummy image.
    ///
    /// In a headless-app without renderer all images are dummy because there is no
    /// view-process backend running.
    Dummy,
    /// Image is still loading, await it first.
    Loading,
    /// The View-Process disconnected or has not finished initializing yet, try again after [`VIEW_PROCESS_INITED_EVENT`].
    Disconnected,
}
impl From<Txt> for EncodeError {
    fn from(e: Txt) -> Self {
        EncodeError::Encode(e)
    }
}
impl From<ChannelError> for EncodeError {
    fn from(_: ChannelError) -> Self {
        EncodeError::Disconnected
    }
}
impl fmt::Display for EncodeError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            EncodeError::Encode(e) => write!(f, "{e}"),
            EncodeError::Dummy => write!(f, "cannot encode dummy image"),
            EncodeError::Loading => write!(f, "cannot encode, image is still loading"),
            EncodeError::Disconnected => write!(f, "{}", ChannelError::disconnected()),
        }
    }
}
impl std::error::Error for EncodeError {}

struct EncodeRequest {
    task_id: ImageEncodeId,
    listener: channel::Sender<std::result::Result<IpcBytes, EncodeError>>,
}

type ClipboardResult<T> = std::result::Result<T, ClipboardError>;

/// View-process clipboard methods.
#[non_exhaustive]
pub struct ViewClipboard {}
impl ViewClipboard {
    /// Read [`ClipboardType::Text`].
    ///
    /// [`ClipboardType::Text`]: zng_view_api::clipboard::ClipboardType::Text
    pub fn read_text(&self) -> Result<ClipboardResult<Txt>> {
        match VIEW_PROCESS
            .try_write()?
            .process
            .read_clipboard(vec![ClipboardType::Text], true)?
            .map(|mut r| r.pop())
        {
            Ok(Some(ClipboardData::Text(t))) => Ok(Ok(t)),
            Err(e) => Ok(Err(e)),
            _ => Ok(Err(ClipboardError::Other(Txt::from_static("view-process returned incorrect type")))),
        }
    }

    /// Write [`ClipboardType::Text`].
    ///
    /// [`ClipboardType::Text`]: zng_view_api::clipboard::ClipboardType::Text
    pub fn write_text(&self, txt: Txt) -> Result<ClipboardResult<()>> {
        VIEW_PROCESS
            .try_write()?
            .process
            .write_clipboard(vec![ClipboardData::Text(txt)])
            .map(|r| r.map(|_| ()))
    }

    /// Read [`ClipboardType::Image`].
    ///
    /// [`ClipboardType::Image`]: zng_view_api::clipboard::ClipboardType::Image
    pub fn read_image(&self) -> Result<ClipboardResult<ViewImageHandle>> {
        let mut app = VIEW_PROCESS.try_write()?;
        match app.process.read_clipboard(vec![ClipboardType::Image], true)?.map(|mut r| r.pop()) {
            Ok(Some(ClipboardData::Image(id))) => {
                if id == ImageId::INVALID {
                    Ok(Err(ClipboardError::Other(Txt::from_static("view-process returned invalid image"))))
                } else {
                    let handle = ArcEq::new((APP.id().unwrap(), app.process.generation(), id));
                    app.loading_images.push(ArcEq::downgrade(&handle));
                    Ok(Ok(ViewImageHandle(Some(handle))))
                }
            }
            Err(e) => Ok(Err(e)),
            _ => Ok(Err(ClipboardError::Other(Txt::from_static("view-process returned incorrect type")))),
        }
    }

    /// Write [`ClipboardType::Image`].
    ///
    /// [`ClipboardType::Image`]: zng_view_api::clipboard::ClipboardType::Image
    pub fn write_image(&self, img: &ViewImageHandle) -> Result<ClipboardResult<()>> {
        return VIEW_PROCESS
            .try_write()?
            .process
            .write_clipboard(vec![ClipboardData::Image(img.image_id())])
            .map(|r| r.map(|_| ()));
    }

    /// Read [`ClipboardType::Paths`].
    ///
    /// [`ClipboardType::Paths`]: zng_view_api::clipboard::ClipboardType::Paths
    pub fn read_paths(&self) -> Result<ClipboardResult<Vec<PathBuf>>> {
        match VIEW_PROCESS
            .try_write()?
            .process
            .read_clipboard(vec![ClipboardType::Paths], true)?
            .map(|mut r| r.pop())
        {
            Ok(Some(ClipboardData::Paths(f))) => Ok(Ok(f)),
            Err(e) => Ok(Err(e)),
            _ => Ok(Err(ClipboardError::Other(Txt::from_static("view-process returned incorrect type")))),
        }
    }

    /// Write [`ClipboardType::Paths`].
    ///
    /// [`ClipboardType::Paths`]: zng_view_api::clipboard::ClipboardType::Paths
    pub fn write_paths(&self, list: Vec<PathBuf>) -> Result<ClipboardResult<()>> {
        VIEW_PROCESS
            .try_write()?
            .process
            .write_clipboard(vec![ClipboardData::Paths(list)])
            .map(|r| r.map(|_| ()))
    }

    /// Read [`ClipboardType::Extension`].
    ///
    /// [`ClipboardType::Extension`]: zng_view_api::clipboard::ClipboardType::Extension
    pub fn read_extension(&self, data_type: Txt) -> Result<ClipboardResult<IpcBytes>> {
        match VIEW_PROCESS
            .try_write()?
            .process
            .read_clipboard(vec![ClipboardType::Extension(data_type.clone())], true)?
            .map(|mut r| r.pop())
        {
            Ok(Some(ClipboardData::Extension { data_type: rt, data })) if rt == data_type => Ok(Ok(data)),
            Err(e) => Ok(Err(e)),
            _ => Ok(Err(ClipboardError::Other(Txt::from_static("view-process returned incorrect type")))),
        }
    }

    /// Write [`ClipboardType::Extension`].
    ///
    /// [`ClipboardType::Extension`]: zng_view_api::clipboard::ClipboardType::Extension
    pub fn write_extension(&self, data_type: Txt, data: IpcBytes) -> Result<ClipboardResult<()>> {
        VIEW_PROCESS
            .try_write()?
            .process
            .write_clipboard(vec![ClipboardData::Extension { data_type, data }])
            .map(|r| r.map(|_| ()))
    }
}

type ViewAudioHandleData = (AppId, ViewProcessGen, AudioId);

/// Handle to an audio loading or loaded in the View Process.
///
/// The audio is disposed when all clones of the handle are dropped.
#[must_use = "the audio is disposed when all clones of the handle are dropped"]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ViewAudioHandle(Option<ArcEq<ViewAudioHandleData>>);
impl ViewAudioHandle {
    /// New handle to nothing.
    pub fn dummy() -> Self {
        ViewAudioHandle(None)
    }

    /// Is handle to nothing.
    pub fn is_dummy(&self) -> bool {
        self.0.is_none()
    }

    /// Audio ID.
    ///
    /// Is [`AudioId::INVALID`] for dummy.
    pub fn audio_id(&self) -> AudioId {
        self.0.as_ref().map(|h| h.2).unwrap_or(AudioId::INVALID)
    }

    /// Application that requested this image.
    ///
    /// Audios can only be used in the same app.
    ///
    /// Is `None` for dummy.
    pub fn app_id(&self) -> Option<AppId> {
        self.0.as_ref().map(|h| h.0.0)
    }

    /// View-process generation that provided this image.
    ///
    /// Audios can only be used in the same view-process instance.
    ///
    /// Is [`ViewProcessGen::INVALID`] for dummy.
    pub fn view_process_gen(&self) -> ViewProcessGen {
        self.0.as_ref().map(|h| h.1).unwrap_or(ViewProcessGen::INVALID)
    }

    /// Create a weak reference to this audio.
    pub fn downgrade(&self) -> WeakViewAudioHandle {
        match &self.0 {
            Some(h) => WeakViewAudioHandle(ArcEq::downgrade(h)),
            None => WeakViewAudioHandle(WeakEq::new()),
        }
    }
}
impl Drop for ViewAudioHandle {
    fn drop(&mut self) {
        if let Some(h) = self.0.take()
            && Arc::strong_count(&h) == 1
            && let Some(app) = APP.id()
        {
            if h.0.0 != app {
                tracing::error!("audio from app `{:?}` dropped in app `{:?}`", h.0, app);
                return;
            }

            if VIEW_PROCESS.is_available() && VIEW_PROCESS.generation() == h.1 {
                let _ = VIEW_PROCESS.write().process.forget_audio(h.2);
            }
        }
    }
}
/// Connection to an audio loading or loaded in the View Process.
///
/// The audio is removed from the View Process cache when all clones of [`ViewAudioHandle`] drops, but
/// if there is another audio pointer holding it, this weak pointer can be upgraded back
/// to a strong connection to the audio.
///
/// Dummy handles never upgrade back.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct WeakViewAudioHandle(WeakEq<ViewAudioHandleData>);
impl WeakViewAudioHandle {
    /// Attempt to upgrade the weak pointer to the audio to a full audio.
    ///
    /// Returns `Some` if the is at least another [`ViewAudioHandle`] holding the audio alive.
    pub fn upgrade(&self) -> Option<ViewAudioHandle> {
        let h = self.0.upgrade()?;
        if h.1 == VIEW_PROCESS.generation() {
            Some(ViewAudioHandle(Some(h)))
        } else {
            None
        }
    }
}

zng_unique_id::unique_id_32! {
    /// Unique identifier of an open audio output.
    ///
    /// # Name
    ///
    /// IDs are only unique for the same process.
    /// You can associate a [`name`] with an ID to give it a persistent identifier.
    ///
    /// [`name`]: AudioOutputId::name
    pub struct AudioOutputId;
}
zng_unique_id::impl_unique_id_name!(AudioOutputId);
zng_unique_id::impl_unique_id_fmt!(AudioOutputId);
zng_unique_id::impl_unique_id_bytemuck!(AudioOutputId);
zng_var::impl_from_and_into_var! {
    /// Calls [`AudioOutputId::named`].
    fn from(name: &'static str) -> AudioOutputId {
        AudioOutputId::named(name)
    }
    /// Calls [`AudioOutputId::named`].
    fn from(name: String) -> AudioOutputId {
        AudioOutputId::named(name)
    }
    /// Calls [`AudioOutputId::named`].
    fn from(name: std::borrow::Cow<'static, str>) -> AudioOutputId {
        AudioOutputId::named(name)
    }
    /// Calls [`AudioOutputId::named`].
    fn from(name: char) -> AudioOutputId {
        AudioOutputId::named(name)
    }
    /// Calls [`AudioOutputId::named`].
    fn from(name: Txt) -> AudioOutputId {
        AudioOutputId::named(name)
    }

    fn from(some: AudioOutputId) -> Option<AudioOutputId>;
}
impl serde::Serialize for AudioOutputId {
    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let name = self.name();
        if name.is_empty() {
            use serde::ser::Error;
            return Err(S::Error::custom("cannot serialize unnamed `AudioOutputId`"));
        }
        name.serialize(serializer)
    }
}
impl<'de> serde::Deserialize<'de> for AudioOutputId {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let name = Txt::deserialize(deserializer)?;
        Ok(AudioOutputId::named(name))
    }
}