nsh-core 0.7.2

Platform-independent core: shadow tree, styles, layout and mount instructions.
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
//! The props of host elements as JS sets them: how each one is decoded
//! from the JS value and what its TypeScript type is, from one definition.
//! `#[derive(HostProps)]` on [`crate::Prop`] (and the enums it nests) maps
//! JS names to variants, decodes their payloads with [`HostValue`] and
//! writes the [`HostPropSpec`]s the element types are generated from. The
//! decoders read JS values through [`HostInput`], which `nsh-js` implements
//! for QuickJS values: no JSON in between.
//!
//! A few props are not [`HostProps`]: the state styles and a page's header
//! style, whose values each renderer decodes with its own style decoder, and
//! the class lists of an element's parts. [`style_patch_prop`] and
//! [`part_class_prop`] name them for both renderers (QuickJS' and the
//! browser runtime's).

use std::sync::Arc;

pub use nsh_macros::HostProps;

use crate::{EdgeInsets, EventKind, element::TsTypes, geometry::Points, style::Color};

/// A JS value as host props read it.
pub trait HostInput: Sized {
    /// `null` or `undefined`.
    fn is_nullish(&self) -> bool;
    /// An object (arrays and functions are objects too).
    fn is_object(&self) -> bool;
    /// A boolean.
    fn as_bool(&self) -> Option<bool>;
    /// A number.
    fn as_number(&self) -> Option<f64>;
    /// A string.
    fn as_string(&self) -> Option<String>;
    /// Field `key` of an object (`None`: not an object, or no such field).
    fn field(&self, key: &str) -> Option<Self>;
    /// The items of an array (empty for anything else).
    fn items(&self) -> Vec<Self> {
        match self.as_list() {
            Some(list) => (0..list.len()).filter_map(|i| list.item(i)).collect(),
            None => Vec::new(),
        }
    }
    /// An array, to read by index (checked once, not per item).
    fn as_list(&self) -> Option<Self::List>;
    /// What [`HostInput::as_list`] gives.
    type List: HostList<Item = Self>;
    /// The fields of an object (empty for anything else).
    fn entries(&self) -> Vec<(String, Self)>;
    /// Truthy, as JS sees it.
    fn truthy(&self) -> bool;
    /// The host element this value is (the node object a `ref` holds).
    fn node(&self) -> Option<crate::NodeId> {
        None
    }
    /// The native animation node this value is (`AnimatedNode.__av`).
    fn animated_node(&self) -> Option<crate::animation::NodeId> {
        self.field("__av")
            .and_then(|v| v.as_number())
            .map(|n| n as crate::animation::NodeId)
    }
}

/// A JS array, read by index.
pub trait HostList {
    /// Its items.
    type Item;
    /// Its length.
    fn len(&self) -> usize;
    /// Whether it is empty.
    fn is_empty(&self) -> bool {
        self.len() == 0
    }
    /// Item `index`.
    fn item(&self, index: usize) -> Option<Self::Item>;
}

/// Where decoding reports what it could not take, and the element it
/// decodes for.
#[derive(Debug, Default)]
pub struct HostCx {
    /// Warnings for the app's console (`unknown role`).
    pub warnings: Vec<String>,
    /// The [`on`] bits of the element the props are set on
    /// ([`element_bits`]); 0 when unknown, which decodes a name several
    /// elements share as the first prop of that name.
    pub element: u32,
}

impl HostCx {
    /// The context of props set on an element `tag`.
    pub fn for_element(tag: crate::Tag) -> Self {
        Self {
            element: element_bits(tag),
            ..Self::default()
        }
    }

    /// Reports `message`.
    pub fn warn(&mut self, message: impl Into<String>) {
        self.warnings.push(message.into());
    }

    /// Whether a prop of the elements `on` applies to the element decoded
    /// for (always, when it is unknown).
    pub fn on_element(&self, on: u32) -> bool {
        self.element == 0 || self.element & on != 0
    }
}

/// The [`on`] bits of the props element `tag` takes.
pub fn element_bits(tag: crate::Tag) -> u32 {
    use crate::Tag;
    on::ALL
        | match tag {
            Tag::Text => on::TEXT,
            Tag::Image => on::IMAGE,
            Tag::Scroll => on::SCROLLING | on::SCROLL,
            Tag::List => on::SCROLLING | on::LIST,
            Tag::Input => on::INPUT,
            Tag::Screen => on::SCREEN,
            Tag::Modal => on::MODAL,
            Tag::WebView => on::WEBVIEW,
            Tag::Video => on::VIDEO,
            Tag::InputAccessory => on::INPUT_ACCESSORY,
            Tag::View => on::VIEW,
            Tag::Shader => on::VIEW | on::SHADER,
            Tag::ShaderEffect => on::VIEW | on::SHADER | on::EFFECT,
            Tag::ShaderTransition => on::VIEW | on::SHADER | on::TRANSITION,
            Tag::ScreenEffect => on::SHADER | on::EFFECT,
            Tag::ScreenTransition => on::SHADER | on::TRANSITION,
            Tag::Cell | Tag::Stack | Tag::HeaderItem | Tag::Canvas | Tag::Native | Tag::Overlay => {
                0
            }
        }
}

/// A prop whose value is a style over the element's own: the style patch
/// the renderer decodes from it as from a `style` (`None` when empty).
pub type StylePatchProp = fn(Option<std::rc::Rc<crate::StylePatch>>) -> crate::Prop;

/// The prop a state style or a page's header style sets (`activeStyle`,
/// `hoverStyle`, ..., `headerStyle`). They are not [`HostProps`]: each
/// renderer decodes their style with its own style decoder, then makes the
/// prop with this, so QuickJS' and the browser's take the same names.
#[inline]
pub fn style_patch_prop(name: &str) -> Option<StylePatchProp> {
    use crate::Prop;
    Some(match name {
        "activeStyle" => Prop::ActiveStyle,
        "hoverStyle" => Prop::HoverStyle,
        "focusStyle" => Prop::FocusStyle,
        "disabledStyle" => Prop::DisabledStyle,
        "selectedStyle" => Prop::SelectedStyle,
        "checkedStyle" => Prop::CheckedStyle,
        "indeterminateStyle" => Prop::IndeterminateStyle,
        "expandedStyle" => Prop::ExpandedStyle,
        "pressedStyle" => Prop::PressedStyle,
        "focusWithinStyle" => Prop::FocusWithinStyle,
        "focusVisibleStyle" => Prop::FocusVisibleStyle,
        "headerStyle" => Prop::HeaderStyle,
        _ => return None,
    })
}

/// A prop whose value is a class list of a part of an element: the class
/// names as the renderer reads them from the JS string.
pub type PartClassProp = fn(Box<str>) -> crate::Prop;

/// The prop a class list of a part of element `tag` sets: a page's header
/// (`headerClass`) or a list's content (`contentClass`). A scroll view's
/// `contentClass` is not one: it styles the scroll view's content container,
/// which a renderer makes itself rather than the core.
#[inline]
pub fn part_class_prop(name: &str, tag: Option<crate::Tag>) -> Option<PartClassProp> {
    use crate::{Prop, Tag};
    match name {
        "headerClass" => Some(Prop::HeaderClass),
        "contentClass" if tag != Some(Tag::Scroll) => Some(Prop::ListContentClass),
        _ => None,
    }
}

/// A prop's value: how it comes out of a JS value and its TypeScript type.
/// `decode` gives `None` for a value it does not take (`undefined`, another
/// type): the prop then gets its default.
pub trait HostValue: Sized {
    /// The value in `input`.
    fn decode<I: HostInput>(input: &I, cx: &mut HostCx) -> Option<Self>;
    /// Its TypeScript type, declaring the named types it uses.
    fn ts_type(types: &mut TsTypes) -> String;
}

/// Decodes a view's `captureKeys` (`"none"`, `"all"`, `"text"`, or an array of
/// keystroke strings) with the platform's `mod` convention: `apple` says
/// `mod` is Command ([`crate::keymap::apple_keys`]).
///
/// [`HostValue`]'s decoder for [`crate::KeyCapture`] reads the convention
/// from the engine's ambient environment. A JS host decodes outside one (the
/// native JS runtime while it renders, the browser's bridge), so it resolves
/// the convention from the environment it has and calls this. Backend-only
/// (`#[doc(hidden)]`), never an app-facing key conversion API.
#[doc(hidden)]
pub fn decode_capture_keys<I: HostInput>(
    input: &I,
    cx: &mut HostCx,
    apple: bool,
) -> Option<crate::KeyCapture> {
    use crate::{KeyCapture, keymap::Keystroke};
    if let Some(text) = input.as_string() {
        return match text.as_str() {
            "none" => Some(KeyCapture::None),
            "all" => Some(KeyCapture::All),
            "text" => Some(KeyCapture::Text),
            other => {
                cx.warn(format!(
                    "captureKeys: expected \"none\", \"all\", \"text\" or keystrokes, got {other:?}"
                ));
                None
            }
        };
    }
    let list = input.as_list()?;
    let mut keys = Vec::with_capacity(list.len());
    for index in 0..list.len() {
        let Some(item) = list.item(index) else {
            continue;
        };
        let Some(text) = item.as_string() else {
            cx.warn(format!("captureKeys[{index}]: expected a keystroke string"));
            continue;
        };
        match Keystroke::parse_with(&text, apple) {
            Ok(keystroke) => keys.push(keystroke),
            Err(error) => cx.warn(format!("captureKeys: {error}")),
        }
    }
    Some(KeyCapture::Keys(keys.into()))
}

/// Where a host prop applies, for the generated element types.
pub mod on {
    /// Every element.
    pub const ALL: u32 = 1;
    /// `<text>`.
    pub const TEXT: u32 = 1 << 1;
    /// `<image>`.
    pub const IMAGE: u32 = 1 << 2;
    /// Scroll views and lists (their scrolling behavior).
    pub const SCROLLING: u32 = 1 << 3;
    /// `<scroll>` only.
    pub const SCROLL: u32 = 1 << 4;
    /// Lists (`List`).
    pub const LIST: u32 = 1 << 5;
    /// `<input>`.
    pub const INPUT: u32 = 1 << 6;
    /// Screens of a stack.
    pub const SCREEN: u32 = 1 << 7;
    /// `<modal>`.
    pub const MODAL: u32 = 1 << 8;
    /// `<webview>`.
    pub const WEBVIEW: u32 = 1 << 9;
    /// `<video>`.
    pub const VIDEO: u32 = 1 << 10;
    /// Input accessories (iOS).
    pub const INPUT_ACCESSORY: u32 = 1 << 11;
    /// `<overlay>`.
    pub const OVERLAY: u32 = 1 << 12;
    /// `<view>`.
    pub const VIEW: u32 = 1 << 13;
    /// `<shader>` and the shader effects and transitions.
    pub const SHADER: u32 = 1 << 14;
    /// Shader effects (`ShaderEffect`, `ScreenEffect`).
    pub const EFFECT: u32 = 1 << 15;
    /// Shader transitions (`ShaderTransition`, `ScreenTransition`).
    pub const TRANSITION: u32 = 1 << 16;
    /// Set by the runtime itself, not part of the elements' types.
    pub const INTERNAL: u32 = 1 << 31;
}

/// The platform a prop is for: everywhere, or inside `ios={{…}}` /
/// `android={{…}}` / `macos={{…}}` / `windows={{…}}` / `desktop={{…}}` /
/// `web={{…}}`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Platform {
    /// Every platform.
    All,
    /// `ios={{…}}`.
    Ios,
    /// `android={{…}}`.
    Android,
    /// `macos={{…}}`.
    Macos,
    /// `windows={{…}}`.
    Windows,
    /// `desktop={{…}}` (macOS, Windows, Linux, desktop browsers).
    Desktop,
    /// `web={{…}}`.
    Web,
}

/// One host prop, for the generated TypeScript.
#[derive(Debug, Clone)]
pub struct HostPropSpec {
    /// Its JS name.
    pub name: &'static str,
    /// Its TypeScript type.
    pub ts: String,
    /// Documentation.
    pub doc: &'static str,
    /// The elements it applies to ([`on`] bits).
    pub on: u32,
    /// The platform it is for.
    pub platform: Platform,
}

/// An enum of host props (`#[derive(HostProps)]`).
pub trait HostProps: Sized {
    /// The prop JS name `name` sets with `input`: `None` for a name that is
    /// not one of these props, `Some(None)` when nothing is to be set.
    fn decode<I: HostInput>(name: &str, input: &I, cx: &mut HostCx) -> Option<Option<Self>>;
    /// This prop at its default (what clearing it from JS sets); `None` for
    /// the props the runtime sets once.
    fn reset(&self) -> Option<Self>;
    /// Whether `self` and `other` set the same property.
    fn same_kind(&self, other: &Self) -> bool;
    /// The props, for the generated TypeScript.
    fn specs(types: &mut TsTypes, out: &mut Vec<HostPropSpec>);
}

/// Numbers held to a range after decoding (`#[prop(min = …, max = …)]`).
pub trait Clamped {
    /// `self` within `min..=max` (either bound left out).
    fn clamped(self, min: Option<f64>, max: Option<f64>) -> Self;
}

impl Clamped for f32 {
    fn clamped(self, min: Option<f64>, max: Option<f64>) -> Self {
        let v = min.map_or(self, |m| self.max(m as f32));
        max.map_or(v, |m| v.min(m as f32))
    }
}

impl Clamped for Points {
    fn clamped(self, min: Option<f64>, max: Option<f64>) -> Self {
        Points(self.0.clamped(min, max))
    }
}

impl Clamped for u32 {
    fn clamped(self, min: Option<f64>, max: Option<f64>) -> Self {
        let v = min.map_or(self, |m| self.max(m as u32));
        max.map_or(v, |m| v.min(m as u32))
    }
}

impl<T: Clamped> Clamped for Option<T> {
    fn clamped(self, min: Option<f64>, max: Option<f64>) -> Self {
        self.map(|v| v.clamped(min, max))
    }
}

impl HostValue for bool {
    fn decode<I: HostInput>(input: &I, _: &mut HostCx) -> Option<Self> {
        input.as_bool()
    }

    fn ts_type(_: &mut TsTypes) -> String {
        "boolean".into()
    }
}

impl HostValue for f32 {
    fn decode<I: HostInput>(input: &I, _: &mut HostCx) -> Option<Self> {
        input.as_number().map(|n| n as f32)
    }

    fn ts_type(_: &mut TsTypes) -> String {
        "number".into()
    }
}

/// A number of points.
impl HostValue for Points {
    fn decode<I: HostInput>(input: &I, cx: &mut HostCx) -> Option<Self> {
        f32::decode(input, cx).map(Points)
    }

    fn ts_type(_: &mut TsTypes) -> String {
        "number".into()
    }
}

impl HostValue for u32 {
    /// Negative numbers are 0, fractions are cut.
    fn decode<I: HostInput>(input: &I, _: &mut HostCx) -> Option<Self> {
        input.as_number().map(|n| n.max(0.0) as u32)
    }

    fn ts_type(_: &mut TsTypes) -> String {
        "number".into()
    }
}

impl HostValue for u64 {
    /// Negative numbers are 0, fractions are cut (a size in bytes).
    fn decode<I: HostInput>(input: &I, _: &mut HostCx) -> Option<Self> {
        input.as_number().map(|n| n.max(0.0) as u64)
    }

    fn ts_type(_: &mut TsTypes) -> String {
        "number".into()
    }
}

impl HostValue for String {
    /// Anything but a string is the empty string's default.
    fn decode<I: HostInput>(input: &I, _: &mut HostCx) -> Option<Self> {
        input.as_string()
    }

    fn ts_type(_: &mut TsTypes) -> String {
        "string".into()
    }
}

impl HostValue for Arc<str> {
    /// The empty string is no value.
    fn decode<I: HostInput>(input: &I, _: &mut HostCx) -> Option<Self> {
        input.as_string().filter(|s| !s.is_empty()).map(Into::into)
    }

    fn ts_type(_: &mut TsTypes) -> String {
        "string".into()
    }
}

impl HostValue for Box<str> {
    /// The empty string is no value.
    fn decode<I: HostInput>(input: &I, _: &mut HostCx) -> Option<Self> {
        input.as_string().filter(|s| !s.is_empty()).map(Into::into)
    }

    fn ts_type(_: &mut TsTypes) -> String {
        "string".into()
    }
}

impl HostValue for Color {
    /// A CSS color string; anything else, or an invalid color, is none.
    fn decode<I: HostInput>(input: &I, _: &mut HostCx) -> Option<Self> {
        input.as_string().and_then(|s| Color::parse(&s))
    }

    fn ts_type(_: &mut TsTypes) -> String {
        "ColorValue".into()
    }
}

/// `None` when there is no value: the prop is cleared.
impl<T: HostValue> HostValue for Option<T> {
    fn decode<I: HostInput>(input: &I, cx: &mut HostCx) -> Option<Self> {
        Some(T::decode(input, cx))
    }

    fn ts_type(types: &mut TsTypes) -> String {
        T::ts_type(types)
    }
}

/// The items `T` takes, the others skipped.
impl<T: HostValue> HostValue for Vec<T> {
    fn decode<I: HostInput>(input: &I, cx: &mut HostCx) -> Option<Self> {
        Some(
            input
                .items()
                .iter()
                .filter_map(|i| T::decode(i, cx))
                .collect(),
        )
    }

    fn ts_type(types: &mut TsTypes) -> String {
        format!("{}[]", T::ts_type(types))
    }
}

/// `{ top, right, bottom, left }` (each 0 when left out).
impl HostValue for EdgeInsets {
    fn decode<I: HostInput>(input: &I, _: &mut HostCx) -> Option<Self> {
        let side = |key| Points(input.field(key).and_then(|v| v.as_number()).unwrap_or(0.0) as f32);
        Some(EdgeInsets {
            top: side("top"),
            right: side("right"),
            bottom: side("bottom"),
            left: side("left"),
        })
    }

    fn ts_type(_: &mut TsTypes) -> String {
        "Partial<Edges>".into()
    }
}

/// Field `key` of `input` as `T`.
pub fn field<T: HostValue, I: HostInput>(input: &I, key: &str, cx: &mut HostCx) -> Option<T> {
    input.field(key).and_then(|v| T::decode(&v, cx))
}

/// The payloads of the host elements' events, and the shapes they are made
/// of: `nsh_core::geometry`'s `Point`, `Size`, `Bounds` and `Edges` (in
/// points) and `keymap::Modifiers`, with GPUI's field names.
const EVENT_TYPES: &str = r#"/** A position, in points (`nsh_core::geometry::Point`). */
export interface Point {
  x: number;
  y: number;
}

/** An extent, in points (`nsh_core::geometry::Size`). */
export interface Size {
  width: number;
  height: number;
}

/** An image's or a video's own extent, in pixels as decoded
 * (`nsh_core::geometry::Size<DevicePixels>`), not the points it is drawn at. */
export interface PixelSize {
  width: number;
  height: number;
}

/** A position and an extent, in points (`nsh_core::geometry::Bounds`). */
export interface Bounds {
  /** The top left corner. */
  origin: Point;
  size: Size;
}

/** A length on each side, in points (`nsh_core::geometry::Edges`): insets, a safe area. */
export interface Edges {
  top: number;
  right: number;
  bottom: number;
  left: number;
}

/** The modifier keys held (GPUI's `Modifiers`). */
export interface Modifiers {
  control: boolean;
  /** Option on Apple platforms, Alt elsewhere. */
  alt: boolean;
  shift: boolean;
  /** Command on Apple platforms, the Windows / Super key elsewhere. */
  platform: boolean;
  /** AltGr (the DOM's `getModifierState("AltGraph")`): the key's character came from the layout's AltGr level, so `control` and `alt` are held to type text, not for a shortcut. Set by key events on Windows; `false` elsewhere. */
  altGraph: boolean;
}

/** A hardware key transition on the focused view: the payload of `onKeyDown` and `onKeyUp` (`nsh_core::KeyEvent`). */
export interface KeyEvent {
  /** The source's logical key: the DOM's spelling for named keys (`"Enter"`, `"ArrowLeft"`), else the character the key reports, in its case and layout. `"Unidentified"` when the source names none. */
  key: string;
  /** The key's physical identity as the DOM's `KeyboardEvent.code` spells it (`"KeyA"`, `"ArrowLeft"`, `"ShiftLeft"`); `""` when the source has none (GPUI, synthetic input), never guessed from `key`. */
  code: string;
  modifiers: Modifiers;
  /** An auto-repeat of a held key (the press itself is not one). */
  repeat: boolean;
  /** The mounted `captureKeys` policy decided this transition: the platform runs no native default and types no text. It is this transition's decision, observed as the DOM's `defaultPrevented` is; a listener cannot veto handling after the fact. */
  defaultPrevented: boolean;
}

/** Where the core placed an overlay: the payload of `onPosition`. */
export interface OverlayPosition {
  /** The side of the anchor it went on: the one asked for, or the other one when only that one has room. */
  placement: "top" | "bottom" | "left" | "right";
  /** Its frame in the root view (the surface, or the dialog it is in). */
  bounds: Bounds;
  /** The room on that side, inside the safe area less `margin`: what a menu may grow to. */
  available: Size;
}

/** A drag: where it started, translation since then and velocity (points / second). */
export interface PanEvent {
  /** The system cancelled the drag rather than releasing it. */
  cancelled: boolean;
  /** Where the finger (or the pointer) went down, in the surface (as a touch's `pageX` / `pageY`). */
  origin: Point;
  /** How far the finger moved since the drag started. */
  translation: Point;
  /** How fast it moves, in points per second. */
  velocity: Point;
  /** The values `pan` binds now, those of the axes it binds (`pan.x`, `pan.y`). */
  value?: Partial<Point>;
}

/** Where the pointer was at a press, the modifier keys held when it began and its click count: the payload of `onPress`, `onLongPress`, `onPressDown` and `onPressUp` on backends that know it. */
export interface PressEvent {
  /** Position in the pressed view (its transforms undone; a pressed span's text view). */
  position: Point;
  /** Position in the root view the view is in (the surface or its modal), as touches' `pageX` / `pageY`. */
  pagePosition: Point;
  modifiers: Modifiers;
  /** Consecutive clicks at the same place: 1 for a click, 2 for a double click; 0 when not from a pointer (keyboard Enter / Space or an assistive technology's default action), as the DOM's `detail`. */
  clickCount: number;
}

/** Where the mouse is over a view, or where a button was released over it: the payload of `onMouseEnter`, `onMouseMove`, `onMouseLeave` and `onMouseUp` (GPUI's `MouseMoveEvent`). */
export interface MouseEvent {
  /** Position in the view (its transforms undone). */
  position: Point;
  /** Position in the root view the view is in (the surface or its modal), as touches' `pageX` / `pageY`. */
  pagePosition: Point;
  modifiers: Modifiers;
  /** The button held down (a drag), or `null`. */
  pressedButton: MouseButton | null;
  /** The button that changed at this event: the released one on `onMouseUp`, `null` on a move. */
  button: MouseButton | null;
  /** `"mouse"`, or `"pen"` for a pen over a tablet. */
  pointerType: PointerType;
  /** How hard it presses, 0 to 1 (`PointerEvent.pressure`): 0.5 with a button down and 0 without on a device without pressure. */
  pressure: number;
  /** A pen's tilt in degrees, -90 to 90 (`PointerEvent.tiltX` / `tiltY`); 0 when not reported. */
  tiltX: number;
  tiltY: number;
}

/** A wheel over a view: the payload of `onWheel` (GPUI's `ScrollWheelEvent`, the DOM's `WheelEvent`). */
export interface WheelEvent {
  /** Position in the view (its transforms undone). */
  position: Point;
  /** Position in the root view the view is in (the surface or its modal), as touches' `pageX` / `pageY`. */
  pagePosition: Point;
  /** How far it scrolled, in `deltaMode` units: positive `y` scrolls down (toward the end), positive `x` right, as the DOM's `deltaX` / `deltaY`. */
  delta: Point;
  /** `"points"`: a precise device (a touchpad); `"lines"`: mouse wheel notches. */
  deltaMode: "points" | "lines";
  modifiers: Modifiers;
}

/** A touchpad pinch over a view: the payload of `onPinch` (GPUI's `PinchEvent`). */
export interface PinchEvent {
  /** Position in the view (its transforms undone). */
  position: Point;
  /** Position in the root view the view is in (the surface or its modal), as touches' `pageX` / `pageY`. */
  pagePosition: Point;
  /** How much the scale changed since the last event, as a fraction: 0.1 is 10 % bigger, negative smaller. */
  delta: number;
  /** Where it is in the gesture, as a touch's phase. */
  phase: "start" | "move" | "end";
  modifiers: Modifiers;
}

/** A mouse button (GPUI's `MouseButton`). */
export type MouseButton = "left" | "right" | "middle" | "back" | "forward";

/** The kind of pointer behind an event (the DOM's `pointerType`). */
export type PointerType = "mouse" | "pen" | "touch";

/** Where a context menu was asked for: the payload of `onContextMenu`. */
export interface ContextMenuEvent {
  /** Position in the view (its transforms undone): the pointer's, or the view's bottom-start corner when no pointer asked. */
  position: Point;
  /** Position in the root view the view is in: where `Menu.popup` opens a menu (`Menu.popup(items, e.pagePosition)`). */
  pagePosition: Point;
  modifiers: Modifiers;
  /** The pointer that asked for the menu, or `null` when the keyboard (Shift+F10, the Menu key) or an assistive technology's "show menu" action did: a menu opened then puts the keyboard focus on its first item. */
  pointerType: PointerType | null;
}

/** Where a scroll view is: the payload of `onScroll`. */
export interface ScrollEvent {
  /** The content offset. */
  offset: Point;
  /** The content's size (zoomed). */
  contentSize: Size;
  /** The scroll view's size. */
  size: Size;
  /** The zoom scale (1 unless the scroll view zooms). */
  zoomScale: number;
}

/** Where an input's caret is: the payload of `onCaretMove`. */
export interface CaretEvent {
  /** The caret in the input (its border box), the input's own scrolling applied: as wide as the platform draws it, as tall as its line. Empty until the input is laid out. */
  bounds: Bounds;
  /** The caret in the root view the input is in (the surface or its dialog), as of the event: an `Overlay`'s `anchor`. */
  pageBounds: Bounds;
}

/** What the user pasted into an input: the payload of `onPaste`. The platform read the clipboard as the user pasted; the app never reads it for this. */
export interface PasteEvent {
  /** The plain text, `""` when there was none. */
  text: string;
  /** The same content as HTML (`text/html`), with its formatting: what a browser, an office suite or a rich text editor copied; `""` when there was none. */
  html: string;
  /** Files with their bytes: images (a screenshot, an image copied from a page or an app), as `File`s an app can upload. */
  files: File[];
  /** Files larger than the input's `pasteMaxSize`, whose bytes were not read: their names, types and sizes, so the app can tell the user. */
  tooLarge: UnreadFile[];
  /** `file:` URLs of local files copied in a file manager (as `onFileDrop`'s). */
  uris: string[];
}

/** A pasted file whose bytes were not read (`PasteEvent.tooLarge`). */
export interface UnreadFile {
  /** Its name. */
  name: string;
  /** Its media type (`application/pdf`; `application/octet-stream` when unknown). */
  type: string;
  /** Its size in bytes. */
  size: number;
}

/** A line of a laid-out text (in the text's content box). */
export interface TextLayoutLine {
  text: string;
  bounds: Bounds;
  ascender: number;
  descender: number;
  capHeight: number;
  xHeight: number;
}

/** Where a web view is: the payload of its navigation events. */
export interface WebViewNavigation {
  url: string;
  title: string;
  loading: boolean;
  canGoBack: boolean;
  canGoForward: boolean;
}

/** A web view's download (`onDownload`): where it is saved, in the app's data directory's `downloads`. */
export interface WebViewDownload {
  state: "started" | "finished" | "failed" | "cancelled";
  id: string;
  url: string;
  filename: string;
  path: string;
  /** Who owns the bytes: the engine's transfer, a request NSH made, a page, or none (the response is gone). */
  source: "transfer" | "request" | "page" | "none";
  /** Once finished: the file's size in bytes. */
  bytes?: number;
  /** When it failed: why. */
  message?: string;
  /** A stable code on failure or cancellation. */
  code?: string;
  /** Why it was cancelled. */
  reason?: "app" | "timeout" | "teardown" | "noListener";
}
"#;

/// The events of the host elements, as their handler props: the event, the
/// elements whose props declare it ([`on`] bits), the handler's type and
/// its documentation. An event whose payload differs between elements has
/// a row per payload.
pub const EVENTS: &[(EventKind, u32, &str, &str)] = &[
    (
        EventKind::Press,
        on::ALL,
        "(e: PressEvent) => void",
        "A tap. `e` is where the pointer was, with the modifier keys and the click count (at the view's origin, with no keys and one click, on backends that do not know the pointer).",
    ),
    (
        EventKind::LongPress,
        on::ALL,
        "(e: PressEvent) => void",
        "A press held for about half a second. `e` is where the pointer was, with the modifier keys and the click count (at the view's origin, with no keys and one click, on backends that do not know the pointer).",
    ),
    (
        EventKind::PressDown,
        on::ALL,
        "(e: PressEvent) => void",
        "A press went down. `e` is where the pointer was, with the modifier keys and the click count (at the view's origin, with no keys and one click, on backends that do not know the pointer).",
    ),
    (
        EventKind::PressUp,
        on::ALL,
        "(e: PressEvent) => void",
        "The press ended. `e` is where the pointer was, with the modifier keys and the click count (at the view's origin, with no keys and one click, on backends that do not know the pointer).",
    ),
    (
        EventKind::MouseEnter,
        on::ALL,
        "(e: MouseEvent) => void",
        "The mouse came over the view or a view inside it (never on touches; the DOM's `mouseenter`).",
    ),
    (
        EventKind::MouseLeave,
        on::ALL,
        "(e: MouseEvent) => void",
        "The mouse left the view and the views inside it, or the window, from where `e` says (the DOM's `mouseleave`).",
    ),
    (
        EventKind::MouseMove,
        on::ALL,
        "(e: MouseEvent) => void",
        "The mouse moved over the view (or a view inside it), with or without a button down (`e.pressedButton`), or a modifier key changed while it rests there with none down; at most once per frame.",
    ),
    (
        EventKind::Wheel,
        on::ALL,
        "(e: WheelEvent) => void",
        "A mouse wheel or touchpad scrolled over the view (or a view inside it); desktops and the web. The nearest listener hears it. A listener nearer than any scroll view under the pointer takes the wheel: nothing scrolls or zooms natively (a canvas zooms on `e.modifiers.control` and pans otherwise); a scroll view listening, or a view around one, only observes it.",
    ),
    (
        EventKind::Pinch,
        on::ALL,
        "(e: PinchEvent) => void",
        "A touchpad pinch over the view (or a view inside it); macOS, Linux, the GPUI backend and Safari (Windows and the other browsers report a touchpad pinch as Control + wheel: `onWheel`). The nearest listener hears it; a listener nearer than any view that pinches natively takes it, and nothing zooms natively. A pinch stays on the view its start went to. Fingers pinching a touchscreen are `onPinchStart`'s.",
    ),
    (
        EventKind::MouseUp,
        on::ALL,
        "(e: MouseEvent) => void",
        "A mouse button was released over the view (or a view inside it): which button (`e.button`), where, and with the modifiers. The primary button's release also reports `onPressUp`. Desktops, the terminal and the web; a release outside the view is not reported.",
    ),
    (
        EventKind::ContextMenu,
        on::ALL,
        "(e: ContextMenuEvent) => void",
        "The secondary button (a right click, Control-click on Apple platforms) was pressed over the view: open a menu with `Menu.popup(items, e.pagePosition)`. The nearest listener hears it; a text input or selectable text nearer shows its own menu instead.",
    ),
    (
        EventKind::FileDragEnter,
        on::ALL,
        "(e: { uris: string[]; mimeTypes: string[] }) => void",
        "A local file drag entered this destination (desktops, iOS and Android) with its offered file URLs and each one's MIME type by its name (`\"\"` when the name says none); no file is read. GTK waits for its file-list preload; iOS and Android read files only on drop, so they offer no URLs; the Web offers no URLs, only the types the browser shows.",
    ),
    (
        EventKind::FileDragLeave,
        on::ALL,
        "() => void",
        "A local file drag left this destination (desktops, iOS, Android and the Web).",
    ),
    (
        EventKind::FileDrop,
        on::ALL,
        "(e: { uris: string[]; mimeTypes: string[]; pagePosition?: { x: number; y: number }; files: { file: File | null; name: string; type: string; size: number; status: \"read\" | \"tooLarge\" }[] }) => void",
        "Local file URLs dropped on this destination (desktops, iOS, Android and the Web), with each one's MIME type by its name (`\"\"` when the name says none); `pagePosition`: where, in the window's coordinates, on backends that know it (Linux, iOS, Android, Web). On the Web `uris` is empty and `files` holds the dropped regular files with their contents (`status` `\"tooLarge\"` past 512 MiB a drop; at most 100); elsewhere `files` is empty.",
    ),
    (
        EventKind::Layout,
        on::ALL,
        "(e: { bounds: Bounds }) => void",
        "The frame changed: its bounds in the parent.",
    ),
    (EventKind::PanStart, on::ALL, "(e: PanEvent) => void", ""),
    (
        EventKind::PanEnd,
        on::ALL,
        "(e: PanEvent) => void",
        "With the release velocity, e.g. to start a `decay`.",
    ),
    (
        EventKind::PinchStart,
        on::ALL,
        "(e: { scale: number; velocity: number }) => void",
        "",
    ),
    (
        EventKind::PinchEnd,
        on::ALL,
        "(e: { scale: number; velocity: number }) => void",
        "",
    ),
    (
        EventKind::RotationStart,
        on::ALL,
        "(e: { rotation: number; velocity: number }) => void",
        "",
    ),
    (
        EventKind::RotationEnd,
        on::ALL,
        "(e: { rotation: number; velocity: number }) => void",
        "",
    ),
    (
        EventKind::AccessibilityAction,
        on::ALL,
        "(e: { actionName: string }) => void",
        "",
    ),
    (
        EventKind::TextLayout,
        on::TEXT,
        "(e: { lines: TextLayoutLine[] }) => void",
        "The laid-out lines (in the text's content box), after each layout.",
    ),
    (
        EventKind::LoadStart,
        on::IMAGE | on::VIDEO,
        "() => void",
        "",
    ),
    (
        EventKind::LoadStart,
        on::WEBVIEW,
        "(e: WebViewNavigation) => void",
        "",
    ),
    (
        EventKind::Progress,
        on::IMAGE,
        "(e: { loaded: number; total: number }) => void",
        "Download progress in bytes (`total` 0 when unknown); at most every 100 ms.",
    ),
    (
        EventKind::Progress,
        on::VIDEO,
        "(e: { currentTime: number; duration: number }) => void",
        "",
    ),
    (
        EventKind::Load,
        on::IMAGE,
        "(e: { size: PixelSize }) => void",
        "The source image's size in pixels (not the decoded, downsampled one).",
    ),
    (
        EventKind::Load,
        on::WEBVIEW,
        "(e: WebViewNavigation) => void",
        "",
    ),
    (
        EventKind::Load,
        on::VIDEO,
        "(e: { duration: number; size: PixelSize }) => void",
        "",
    ),
    (
        EventKind::Error,
        on::IMAGE | on::WEBVIEW | on::VIDEO,
        "(e: { message: string; code?: string }) => void",
        "The source failed to load. `code` is `\"ERR_ACCESS_DENIED\"` when the app's capabilities do not grant it (an image's or video's URL: `network`; a web view's page or `html`: `webview`), `\"ERR_FEATURE_UNAVAILABLE_ON_PLATFORM\"` for a web view where the platform shows none (`WebView.available()`), and `\"ERR_NSH_WEBVIEW_TERMINATED\"` when a web view's web content process ended (it shows no page until `reload()`, `load()` or a new source).",
    ),
    (
        EventKind::LoadEnd,
        on::IMAGE,
        "() => void",
        "After `onLoad` or `onError`.",
    ),
    (
        EventKind::Scroll,
        on::SCROLLING,
        "(e: ScrollEvent) => void",
        "",
    ),
    (
        EventKind::ScrollDragStart,
        on::SCROLLING,
        "(e: { offset: Point }) => void",
        "",
    ),
    (
        EventKind::ScrollDragEnd,
        on::SCROLLING,
        "(e: { offset: Point }) => void",
        "",
    ),
    (
        EventKind::MomentumStart,
        on::SCROLLING,
        "(e: { offset: Point }) => void",
        "",
    ),
    (
        EventKind::MomentumEnd,
        on::SCROLLING,
        "(e: { offset: Point }) => void",
        "",
    ),
    (
        EventKind::ScrollEnd,
        on::SCROLL | on::LIST,
        "(e: { offset: Point }) => void",
        "",
    ),
    (EventKind::Refresh, on::SCROLL | on::LIST, "() => void", ""),
    (EventKind::LoadMore, on::LIST, "() => void", ""),
    (
        EventKind::LoadMoreAtStart,
        on::LIST,
        "() => void",
        "Within `loadMoreAtStartThreshold` viewports of the start, once the user or a scroll command moved the list: load what comes before the first item.",
    ),
    (
        EventKind::ChangeText,
        on::INPUT,
        "(e: { value: string }) => void",
        "The user edited the text: `value` is the text now.",
    ),
    (
        EventKind::SubmitEditing,
        on::INPUT,
        "(e: { text: string }) => void",
        "",
    ),
    (
        EventKind::Focus,
        on::ALL,
        "() => void",
        "It took the keyboard focus: an input, or a view that takes presses reached by Tab on the platforms that move the focus between pressables (macOS, the web).",
    ),
    (
        EventKind::Blur,
        on::ALL,
        "() => void",
        "It lost the keyboard focus.",
    ),
    (
        EventKind::FocusIn,
        on::ALL,
        "() => void",
        "The keyboard focus came into it: onto it or a view inside it, from outside (it entered `focus-within`). Moving between the views inside it is no new coming in; the content of an overlay is inside the view the overlay is in. A panel notes where its focus is with it, to put it back later with `ref.focus()`.",
    ),
    (
        EventKind::FocusOut,
        on::ALL,
        "() => void",
        "The keyboard focus left it: from it or a view inside it to one outside, or to none (it left `focus-within`). A view removed with the focus inside it hears nothing.",
    ),
    (
        EventKind::SelectionChange,
        on::INPUT,
        "(e: { start: number; end: number }) => void",
        "",
    ),
    (
        EventKind::SelectionChange,
        on::VIEW,
        "(e: { text: string; start?: number; bounds?: Bounds; pageBounds?: Bounds }) => void",
        "The selection across the texts of a `selectable` view changed: `text` is what a copy would give, empty once nothing is selected; `start` is where it starts in the view's whole text (UTF-16 units) when the platform knows it; `bounds` is the box around the selected text in the view and `pageBounds` in the root view it is in, as of the event (an `Overlay`'s `anchor`: a bar of actions on the selection), while something is selected and the platform knows them. Only the outermost selectable view of an area reports it.",
    ),
    (
        EventKind::Paste,
        on::INPUT,
        "(e: PasteEvent) => void",
        "The user pasted (the keyboard's shortcut, a menu's Paste, a keyboard's image): the text, images as `File`s (files larger than `pasteMaxSize` in `tooLarge`, unread), local files' `file:` URLs. It comes before the `onChange` of the text it inserts; `pasteBehavior` decides whether the input inserts the text (images and files never are).",
    ),
    (
        EventKind::CaretMove,
        on::INPUT,
        "(e: CaretEvent) => void",
        "The caret moved: with the selection, or as the text scrolled or wrapped again. `bounds` is where it is in the input, `pageBounds` in the root view (an `Overlay`'s `anchor`): an `@` mention's suggestions open there.",
    ),
    (
        EventKind::KeyPress,
        on::INPUT,
        "(e: { key: string }) => void",
        "Before the text changes: `key` is the typed text, `Backspace` or `Enter`.",
    ),
    (
        EventKind::KeyDown,
        on::ALL,
        "(e: KeyEvent) => void",
        "A hardware key went down on the focused view or a view inside it on desktop and Web. It bubbles to enabled listening ancestors within the same native window. `defaultPrevented` is the mounted `captureKeys` decision for this transition.",
    ),
    (
        EventKind::KeyUp,
        on::ALL,
        "(e: KeyEvent) => void",
        "A hardware key came up on the focused view, with this transition's `defaultPrevented` decision; it bubbles as `onKeyDown` does. A window that loses the focus invents no releases.",
    ),
    (
        EventKind::CompositionChange,
        on::INPUT,
        "(e: { composing: boolean; text: string }) => void",
        "An input method's composition started, changed or ended: `text` is what is being composed (pinyin before a character is chosen), empty once it ends. The committed text reaches `onChange` before the composition's end; while composing, the platform holds the `value` the app sets and shows it once the composition ends.",
    ),
    (
        EventKind::ContentSizeChange,
        on::INPUT,
        "(e: { size: Size }) => void",
        "The size of the text (at the input's width when `multiline`; one empty line's while it is empty, whatever its placeholder): grow a multiline input with it.",
    ),
    (
        EventKind::Message,
        on::WEBVIEW,
        "(e: { data: string }) => void",
        "The page called `window.nsh.postMessage(data)`.",
    ),
    (
        EventKind::NavigationStateChange,
        on::WEBVIEW,
        "(e: WebViewNavigation) => void",
        "",
    ),
    (
        EventKind::Download,
        on::WEBVIEW,
        "(e: WebViewDownload) => void",
        "With a listener, what the page downloads (an attachment, a type the web view cannot show, a `download` link) is saved as a file in the app's data directory, `downloads/<id>/<filename>`: reported as it starts, finishes and fails. Without one, those navigations behave as before.",
    ),
    (EventKind::End, on::VIDEO, "() => void", ""),
    (
        EventKind::Buffer,
        on::VIDEO,
        "(e: { isBuffering: boolean }) => void",
        "",
    ),
    (
        EventKind::PressOutside,
        on::OVERLAY,
        "(e: { pagePosition: Point }) => void",
        "A press went down outside the overlay, its anchor and the overlays inside it (where, in the root view). Closing is the app's: leave the overlay out.",
    ),
    (
        EventKind::Cancel,
        on::OVERLAY,
        "() => void",
        "Escape or the Android back button, while this is the top overlay listening to it (it takes the key from the overlays below, the pages and `BackHandler`). Closing is the app's.",
    ),
    (
        EventKind::DetentChange,
        on::MODAL,
        "(e: { detent: SheetDetent }) => void",
        "The user moved an iOS sheet to another of its `detents` (dragging it, its grabber, a scroll at its edge, a screen reader): set `detent` to it to keep the app's value in step.",
    ),
    (
        EventKind::Position,
        on::OVERLAY,
        "(e: OverlayPosition) => void",
        "The core placed the overlay (again: the anchor moved, the overlay changed size, it went to the other side).",
    ),
];

/// The events no element's props declare: the root's (`BackHandler`'s,
/// `nsh/keymap`, dialogs, `Image.getSize`, clipboard reads, menus, `nsh/host`'s replies and events, links), the runtime's own (touches, insets, native
/// components' events) and those whose payload a module makes
/// (`List`'s viewable items, with the app's items).
pub const INTERNAL_EVENTS: &[EventKind] = &[
    EventKind::Touch,
    EventKind::Insets,
    EventKind::NativeEvent,
    EventKind::Dialog,
    EventKind::BackPress,
    EventKind::BackProgress,
    EventKind::ImageInfo,
    EventKind::WebEvaluation,
    EventKind::WebViewReply,
    EventKind::DownloadPending,
    EventKind::DownloadOffer,
    EventKind::DownloadProgress,
    EventKind::DownloadEnd,
    EventKind::Clipboard,
    EventKind::Host,
    EventKind::Pick,
    EventKind::OpenUrl,
    EventKind::OpenAppSettings,
    EventKind::ClipboardWrite,
    EventKind::ScreenColor,
    EventKind::Share,
    EventKind::AuthSession,
    EventKind::CanvasImage,
    EventKind::FileRequest,
    EventKind::Notification,
    EventKind::UrlScheme,
    EventKind::SecondInstance,
    EventKind::Audio,
    EventKind::Window,
    EventKind::EditAction,
    EventKind::KeyCommand,
    EventKind::Menu,
    EventKind::ViewableItemsChange,
    // Text dropped on a Rust component (`RichEditor` inserts it).
    EventKind::TextDrop,
    // `NavPage`'s `onDismiss` / `onBackPress` and `Dialog`'s `onClose` /
    // `onCancel` / `onOpenChange` / `onOpen`: the navigation's handlers
    // (`nsh-js` `navigation.rs`), declared with its components.
    EventKind::BackRequest,
    EventKind::Dismiss,
    EventKind::RequestClose,
    EventKind::Show,
];

/// The interfaces of the host props, by the elements their props apply
/// to: `HostProps` for every element, `TextHostProps` for `<text>`, ...
const GROUPS: &[(u32, &str, &str)] = &[
    (on::ALL, "", "every element"),
    (on::VIEW, "View", "`<View>`"),
    (on::TEXT, "Text", "`<Text>`"),
    (on::IMAGE, "Image", "`<Image>`"),
    (
        on::SCROLLING,
        "Scrolling",
        "scroll views and lists (how they scroll)",
    ),
    (on::SCROLL, "Scroll", "`<Scroll>`"),
    (on::LIST, "List", "lists"),
    (on::INPUT, "Input", "`<Input>`"),
    (on::SCREEN, "NavPage", "the pages of a `NavStack`"),
    (on::MODAL, "Dialog", "dialogs"),
    (on::WEBVIEW, "WebView", "web views"),
    (on::VIDEO, "Video", "videos"),
    (on::SHADER, "Shader", "shaders"),
    (on::EFFECT, "ShaderEffect", "shader effects"),
    (on::TRANSITION, "ShaderTransition", "shader transitions"),
    (on::INPUT_ACCESSORY, "InputAccessory", "input accessories"),
    (on::OVERLAY, "Overlay", "`<Overlay>`"),
];

/// The declarations of the host elements' props: the named types they use
/// and one interface per element group and platform (`InputHostProps`,
/// `IOSInputHostProps`, ...), which the element types and the components
/// of `nsh` (`WebView`, `Video`, ...) extend, and the options the refs'
/// methods take (`RevealOptions`).
pub fn declarations() -> String {
    use std::fmt::Write as _;
    let mut types = TsTypes::default();
    let mut specs = Vec::new();
    <crate::Prop as HostProps>::specs(&mut types, &mut specs);
    <crate::RevealOptions as HostValue>::ts_type(&mut types);
    let mut out = String::from(EVENT_TYPES);
    for declaration in types.declarations() {
        let _ = write!(out, "\n{declaration}\n");
    }
    for (bit, group, what) in GROUPS {
        for (platform, prefix, inside) in [
            (Platform::All, "", ""),
            (Platform::Ios, "IOS", " inside `ios={{…}}`"),
            (Platform::Android, "Android", " inside `android={{…}}`"),
            (Platform::Macos, "MacOS", " inside `macos={{…}}`"),
            (Platform::Windows, "Windows", " inside `windows={{…}}`"),
            (Platform::Desktop, "Desktop", " inside `desktop={{…}}`"),
            (Platform::Web, "Web", " inside `web={{…}}`"),
        ] {
            let props: Vec<&HostPropSpec> = specs
                .iter()
                .filter(|s| s.on & bit != 0 && s.platform == platform)
                .collect();
            // Events are for every platform.
            let events: Vec<_> = EVENTS
                .iter()
                .filter(|e| e.1 & bit != 0 && platform == Platform::All)
                .collect();
            if props.is_empty() && events.is_empty() {
                continue;
            }
            let _ = write!(
                out,
                "\n/** The props of {what}{inside}. */\nexport interface {prefix}{group}HostProps {{\n"
            );
            for p in props {
                if !p.doc.is_empty() {
                    let _ = writeln!(out, "  /** {} */", p.doc);
                }
                let _ = writeln!(out, "  {}?: {};", p.name, p.ts);
            }
            for (kind, _, handler, doc) in events {
                if !doc.is_empty() {
                    let _ = writeln!(out, "  /** {doc} */");
                }
                let _ = writeln!(out, "  {}?: {handler};", kind.handler_name());
            }
            out.push_str("}\n");
        }
    }
    out
}