lurq 0.65.1

A Rust UI toolkit with typed components, retained state, reactive signals, GPU rendering, and DevTools.
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
//! What the shell asks of UIKit directly, where winit has no word for it.
//!
//! **Frames.** winit redraws a view through `setNeedsDisplay` and `drawRect:`, which UIKit runs at 60 Hz on a
//! phone whatever the display can do. A `CADisplayLink` that asks for the display's full rate fires once per
//! refresh instead, and the shell presents from the turn of the loop it wakes ([`FrameLink`]). It runs only
//! while a frame is wanted: paused, it costs nothing and the display drops to its idle rate. An iPhone goes
//! above 60 Hz only for an app whose `Info.plist` sets `CADisableMinimumFrameDurationOnPhone`.
//!
//! Text input is in `ios_text`.
//!
//! **The keyboard** comes up over the bottom of the window, which winit does not tell. UIKit announces where it
//! will be ([`keyboard_overlap`]), and the shell counts what it covers as part of the safe area and brings the
//! focused input into view.
//!
//! **A tap on the status bar** scrolls a list to its top. UIKit does not deliver that tap as a touch: it asks
//! the `UIScrollView` under the tapped point that scrolls to top. The lists here are not scroll views, so an
//! empty scroll view across the top of the window, as high as the status bar, stands in ([`StatusBarTap`]),
//! declines to scroll, and the shell scrolls its own content instead. Touches in that band go to it and not
//! to the app, whose content the safe area keeps out of there.
//!
//! **Scenes.** UIKit gives an app its windows through scenes, and traps at launch in an app linked against the
//! iOS 27 SDK that has not taken them up. winit starts the app with no delegate and makes its window outside any
//! scene. So the scene's delegate is a class of the shell's ([`register_scene_delegate`]), which an app names in
//! its `Info.plist` (`UIApplicationSceneManifest`, `UISceneDelegateClassName` = `LurqSceneDelegate`), and the
//! shell puts winit's window into the scene UIKit connects ([`join_scene`]). An app whose `Info.plist` names no
//! scene runs as before, where UIKit still lets it.

use std::{
  ffi::c_void,
  ptr::NonNull,
  sync::{
    Mutex,
    atomic::{AtomicBool, AtomicPtr, Ordering},
  },
};

use objc2::{
  class,
  encode::{Encode, Encoding},
  msg_send,
  rc::Retained,
  runtime::{AnyClass, AnyObject, Bool, ClassBuilder, Sel},
  sel,
};

/// Set by the display link at each refresh while it runs.
static FRAME_DUE: AtomicBool = AtomicBool::new(false);
/// Set when the status bar was tapped.
static STATUS_BAR_TAPPED: AtomicBool = AtomicBool::new(false);
/// How many times the keyboard has set off, up or down.
static KEYBOARD_MOVES: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
/// Until when the keyboard is on its way up or down, as UIKit announced it.
static KEYBOARD_MOVES_UNTIL: Mutex<Option<std::time::Instant>> = Mutex::new(None);

#[repr(C)]
#[derive(Clone, Copy)]
struct CAFrameRateRange {
  minimum: f32,
  maximum: f32,
  preferred: f32,
}

unsafe impl Encode for CAFrameRateRange {
  const ENCODING: Encoding = Encoding::Struct("CAFrameRateRange", &[f32::ENCODING, f32::ENCODING, f32::ENCODING]);
}

#[repr(C)]
#[derive(Clone, Copy)]
pub(super) struct CGPoint {
  pub(super) x: f64,
  pub(super) y: f64,
}

unsafe impl Encode for CGPoint {
  const ENCODING: Encoding = Encoding::Struct("CGPoint", &[f64::ENCODING, f64::ENCODING]);
}

#[repr(C)]
#[derive(Clone, Copy)]
pub(super) struct CGSize {
  pub(super) width: f64,
  pub(super) height: f64,
}

unsafe impl Encode for CGSize {
  const ENCODING: Encoding = Encoding::Struct("CGSize", &[f64::ENCODING, f64::ENCODING]);
}

#[repr(C)]
#[derive(Clone, Copy)]
pub(super) struct CGRect {
  pub(super) origin: CGPoint,
  pub(super) size: CGSize,
}

unsafe impl Encode for CGRect {
  const ENCODING: Encoding = Encoding::Struct("CGRect", &[CGPoint::ENCODING, CGSize::ENCODING]);
}

#[link(name = "Foundation", kind = "framework")]
unsafe extern "C" {
  static NSRunLoopCommonModes: *const AnyObject;
}

#[link(name = "UIKit", kind = "framework")]
unsafe extern "C" {
  static UIKeyboardWillChangeFrameNotification: *const AnyObject;
  static UIKeyboardFrameEndUserInfoKey: *const AnyObject;
  static UIKeyboardAnimationDurationUserInfoKey: *const AnyObject;
}

extern "C-unwind" fn keyboard_frame_changes(_this: &AnyObject, _cmd: Sel, notification: *mut AnyObject) {
  let Some(notification) = (unsafe { notification.as_ref() }) else {
    return;
  };
  unsafe {
    let info: Option<Retained<AnyObject>> = msg_send![notification, userInfo];
    let Some(info) = info else {
      return;
    };
    let duration: Option<Retained<AnyObject>> = msg_send![&*info, objectForKey: UIKeyboardAnimationDurationUserInfoKey];
    let duration: f64 = match duration {
      Some(duration) => msg_send![&*duration, doubleValue],
      None => 0.0,
    };
    // A little longer than announced: the last frame must find the keyboard at rest.
    let until = std::time::Instant::now() + std::time::Duration::from_secs_f64(duration.clamp(0.0, 2.0) + 0.1);
    *KEYBOARD_MOVES_UNTIL.lock().unwrap() = Some(until);
    KEYBOARD_MOVES.fetch_add(1, Ordering::AcqRel);
  }
}

/// Which of the keyboard's moves this is: another number each time it sets off.
pub(super) fn keyboard_move() -> u64 {
  KEYBOARD_MOVES.load(Ordering::Acquire)
}

/// Whether the keyboard is on its way up or down: the window is laid out anew at each frame while it is.
pub(super) fn keyboard_moving() -> bool {
  KEYBOARD_MOVES_UNTIL
    .lock()
    .unwrap()
    .is_some_and(|until| std::time::Instant::now() < until)
}

/// Where the keyboard is on screen at this moment, its way up and down included.
///
/// UIKit says where the keyboard will be, not where it is on the way there, and it moves along a curve of its
/// own. A view pinned to the top of the window's keyboard layout guide is moved by UIKit in the keyboard's own
/// animation, and its presentation layer is where that animation has it now. Main thread only.
pub(super) struct KeyboardProbe {
  probe: Retained<AnyObject>,
}

impl KeyboardProbe {
  /// Adds the probe, a view of one point that shows nothing, to the window's view.
  pub(super) fn new(parent: NonNull<c_void>) -> Self {
    let parent: &AnyObject = unsafe { parent.cast::<AnyObject>().as_ref() };
    unsafe {
      let probe: Retained<AnyObject> = msg_send![class!(UIView), new];
      let _: () = msg_send![&*probe, setUserInteractionEnabled: false];
      let _: () = msg_send![&*probe, setTranslatesAutoresizingMaskIntoConstraints: false];
      let _: () = msg_send![parent, addSubview: &*probe];
      let guide: Retained<AnyObject> = msg_send![parent, keyboardLayoutGuide];
      let pin = |anchor: Retained<AnyObject>, to: Retained<AnyObject>| {
        let constraint: Retained<AnyObject> = msg_send![&*anchor, constraintEqualToAnchor: &*to];
        let _: () = msg_send![&*constraint, setActive: true];
      };
      pin(msg_send![&*probe, topAnchor], msg_send![&*guide, topAnchor]);
      pin(msg_send![&*probe, leadingAnchor], msg_send![parent, leadingAnchor]);
      for dimension in [sel!(widthAnchor), sel!(heightAnchor)] {
        let anchor: Retained<AnyObject> = msg_send![&*probe, performSelector: dimension];
        let constraint: Retained<AnyObject> = msg_send![&*anchor, constraintEqualToConstant: 1.0f64];
        let _: () = msg_send![&*constraint, setActive: true];
      }
      Self { probe }
    }
  }

  /// How much of a window `height` points high the keyboard covers now, in points; 0 with no keyboard.
  ///
  /// With no keyboard the guide rests at the bottom safe area of the view it is in, which is no cover of the
  /// keyboard's. That is winit's view, whose safe area UIKit does not turn with the device: on a phone on its
  /// side the guide rests where the home indicator's room ended upright, higher than it ends now, and taken
  /// for a keyboard that hid what goes away for one.
  pub(super) fn overlap(&self, height: f64) -> f64 {
    unsafe {
      let layer: Retained<AnyObject> = msg_send![&*self.probe, layer];
      let shown: Option<Retained<AnyObject>> = msg_send![&*layer, presentationLayer];
      let frame: CGRect = match &shown {
        Some(shown) => msg_send![&**shown, frame],
        None => msg_send![&*layer, frame],
      };
      // Before its first layout the probe is at the window's top, which is no place for a keyboard.
      if frame.origin.y < 1.0 {
        return 0.0;
      }
      let covered = (height - frame.origin.y).clamp(0.0, height);
      let parent: Option<Retained<AnyObject>> = msg_send![&*self.probe, superview];
      let rests = match &parent {
        Some(parent) => {
          let insets: UIEdgeInsets = msg_send![&**parent, safeAreaInsets];
          insets.bottom
        }
        None => 0.0,
      };
      if covered <= rests + 0.5 { 0.0 } else { covered }
    }
  }
}

/// Starts listening for where the keyboard goes. The listener lives as long as the app.
pub(super) fn watch_keyboard() {
  unsafe {
    let receiver: Retained<AnyObject> = msg_send![receiver_class(), new];
    let center: Retained<AnyObject> = msg_send![class!(NSNotificationCenter), defaultCenter];
    let _: () = msg_send![
      &*center,
      addObserver: &*receiver,
      selector: sel!(lurqKeyboardFrameChanges:),
      name: UIKeyboardWillChangeFrameNotification,
      object: std::ptr::null::<AnyObject>()
    ];
    // The center does not retain its observers.
    std::mem::forget(receiver);
  }
}

extern "C-unwind" fn frame_due(_this: &AnyObject, _cmd: Sel, _link: *mut AnyObject) {
  FRAME_DUE.store(true, Ordering::Release);
}

extern "C-unwind" fn should_scroll_to_top(_this: &AnyObject, _cmd: Sel, _scroll_view: *mut AnyObject) -> Bool {
  STATUS_BAR_TAPPED.store(true, Ordering::Release);
  // The stand-in itself has nowhere to scroll.
  Bool::NO
}

/// The class of the object both callbacks are sent to, registered once.
fn receiver_class() -> &'static AnyClass {
  if let Some(class) = AnyClass::get(c"LurqShellReceiver") {
    return class;
  }
  let mut builder = ClassBuilder::new(c"LurqShellReceiver", class!(NSObject)).expect("a class name of its own");
  unsafe {
    builder.add_method(sel!(lurqFrameDue:), frame_due as extern "C-unwind" fn(_, _, _));
    builder.add_method(
      sel!(lurqKeyboardFrameChanges:),
      keyboard_frame_changes as extern "C-unwind" fn(_, _, _),
    );
    builder.add_method(
      sel!(scrollViewShouldScrollToTop:),
      should_scroll_to_top as extern "C-unwind" fn(_, _, _) -> _,
    );
    builder.add_method(sel!(lurqPencilHover:), pencil_hover as extern "C-unwind" fn(_, _, _));
  }
  builder.register()
}

/// Where an Apple Pencil hovered over the window since the shell last looked, as UIKit told it: whether it is still
/// over it, and where, in points of the view.
static PENCIL_HOVER: Mutex<Vec<(bool, f64, f64)>> = Mutex::new(Vec::new());

extern "C-unwind" fn pencil_hover(_this: &AnyObject, _cmd: Sel, recognizer: *mut AnyObject) {
  let Some(recognizer) = (unsafe { recognizer.as_ref() }) else {
    return;
  };
  let (state, point): (isize, CGPoint) = unsafe {
    let view: *mut AnyObject = msg_send![recognizer, view];
    (
      msg_send![recognizer, state],
      msg_send![recognizer, locationInView: view],
    )
  };
  // `UIGestureRecognizerStateBegan` and `Changed`: over the view; `Ended`, `Cancelled` and `Failed`: gone.
  let over = matches!(state, 1 | 2);
  if let Ok(mut hovers) = PENCIL_HOVER.lock() {
    hovers.push((over, point.x, point.y));
  }
}

/// Takes what an Apple Pencil's hover did since the last call: whether it is over the view, and where, in points.
pub(super) fn take_pencil_hovers() -> Vec<(bool, f64, f64)> {
  PENCIL_HOVER
    .lock()
    .map(|mut hovers| std::mem::take(&mut *hovers))
    .unwrap_or_default()
}

/// An Apple Pencil hovering over the window (iPadOS 16.1, an Apple Pencil 2 or Pro): a hover gesture of the
/// pencil's only on winit's view, which takes no touch from it. Main thread only.
pub(super) struct PencilHover {
  _recognizer: Retained<AnyObject>,
  _receiver: Retained<AnyObject>,
}

impl PencilHover {
  /// `None` where the system has no hover gesture.
  pub(super) fn new(view: NonNull<c_void>) -> Option<Self> {
    let class = AnyClass::get(c"UIHoverGestureRecognizer")?;
    let view: &AnyObject = unsafe { view.cast::<AnyObject>().as_ref() };
    unsafe {
      let receiver: Retained<AnyObject> = msg_send![receiver_class(), new];
      let recognizer: objc2::rc::Allocated<AnyObject> = msg_send![class, alloc];
      let recognizer: Retained<AnyObject> =
        msg_send![recognizer, initWithTarget: &*receiver, action: sel!(lurqPencilHover:)];
      // `UITouchTypePencil`: a trackpad's or a mouse's hover is not the pencil's.
      let pencil: Retained<AnyObject> = msg_send![class!(NSNumber), numberWithInteger: 2isize];
      let types: Retained<AnyObject> = msg_send![class!(NSArray), arrayWithObject: &*pencil];
      let _: () = msg_send![&*recognizer, setAllowedTouchTypes: &*types];
      let _: () = msg_send![&*recognizer, setCancelsTouchesInView: false];
      let _: () = msg_send![view, addGestureRecognizer: &*recognizer];
      Some(Self {
        _recognizer: recognizer,
        _receiver: receiver,
      })
    }
  }
}

/// The display's refreshes, as turns of the event loop. Main thread only, as the window is.
pub(super) struct FrameLink {
  link: Retained<AnyObject>,
  running: bool,
}

impl FrameLink {
  pub(super) fn new() -> Self {
    let link: Retained<AnyObject> = unsafe {
      let receiver: Retained<AnyObject> = msg_send![receiver_class(), new];
      // The link retains its target.
      let link: Retained<AnyObject> = msg_send![
        class!(CADisplayLink),
        displayLinkWithTarget: &*receiver,
        selector: sel!(lurqFrameDue:)
      ];
      // Whatever the display gives up to 120 Hz; a 60 Hz display gives 60.
      let range = CAFrameRateRange {
        minimum: 60.0,
        maximum: 120.0,
        preferred: 120.0,
      };
      let _: () = msg_send![&*link, setPreferredFrameRateRange: range];
      let _: () = msg_send![&*link, setPaused: true];
      let run_loop: Retained<AnyObject> = msg_send![class!(NSRunLoop), mainRunLoop];
      let _: () = msg_send![&*link, addToRunLoop: &*run_loop, forMode: NSRunLoopCommonModes];
      link
    };
    Self { link, running: false }
  }

  /// Starts or stops the refreshes. Stopped, a refresh already noted is forgotten.
  pub(super) fn set_running(&mut self, running: bool) {
    if self.running == running {
      return;
    }
    self.running = running;
    if !running {
      FRAME_DUE.store(false, Ordering::Release);
    }
    let _: () = unsafe { msg_send![&*self.link, setPaused: !running] };
  }

  /// Whether the display refreshed since the last call, while running.
  pub(super) fn take_due(&mut self) -> bool {
    FRAME_DUE.swap(false, Ordering::AcqRel)
  }
}

impl Drop for FrameLink {
  fn drop(&mut self) {
    let _: () = unsafe { msg_send![&*self.link, invalidate] };
  }
}

/// The scroll view whose finger takes the keyboard down with it. Main thread only.
///
/// Nothing an app can call moves the system's keyboard. A `UIScrollView` whose `keyboardDismissMode` is
/// interactive does: UIKit follows that view's own pan gesture, and once the finger is on the keyboard the
/// keyboard goes down and up with it. The view is not seen and scrolls nothing; its pan gesture is put on
/// winit's view, where the fingers are, and takes no touch from it.
pub(super) struct KeyboardDrag {
  scroll_view: Retained<AnyObject>,
  interactive: bool,
}

impl KeyboardDrag {
  pub(super) fn new(view: NonNull<c_void>) -> Self {
    let view: &AnyObject = unsafe { view.cast::<AnyObject>().as_ref() };
    unsafe {
      let scroll_view: Retained<AnyObject> = msg_send![class!(UIScrollView), new];
      let bounds: CGRect = msg_send![view, bounds];
      let _: () = msg_send![&*scroll_view, setFrame: bounds];
      // `UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight`.
      let _: () = msg_send![&*scroll_view, setAutoresizingMask: 18usize];
      // Not seen, and no finger lands on it: the fingers are winit's view's.
      let _: () = msg_send![&*scroll_view, setUserInteractionEnabled: false];
      let _: () = msg_send![&*scroll_view, setHidden: false];
      let _: () = msg_send![&*scroll_view, setAlpha: 0.0f64];
      let _: () = msg_send![&*scroll_view, setScrollsToTop: false];
      let _: () = msg_send![&*scroll_view, setAlwaysBounceVertical: true];
      let _: () = msg_send![&*scroll_view, setShowsVerticalScrollIndicator: false];
      let _: () = msg_send![&*scroll_view, setShowsHorizontalScrollIndicator: false];
      let _: () = msg_send![&*scroll_view, setContentSize: CGSize { width: bounds.size.width, height: 100_000.0 }];
      let _: () = msg_send![&*scroll_view, setContentOffset: CGPoint { x: 0.0, y: 50_000.0 }];
      let _: () = msg_send![view, insertSubview: &*scroll_view, atIndex: 0isize];
      // The gesture hears the fingers of winit's view, and leaves them to it.
      let pan: Retained<AnyObject> = msg_send![&*scroll_view, panGestureRecognizer];
      let _: () = msg_send![&*pan, setCancelsTouchesInView: false];
      let _: () = msg_send![&*pan, setDelaysTouchesBegan: false];
      let _: () = msg_send![&*pan, setDelaysTouchesEnded: false];
      let _: () = msg_send![&*pan, setEnabled: false];
      let _: () = msg_send![view, addGestureRecognizer: &*pan];
      Self {
        scroll_view,
        interactive: false,
      }
    }
  }

  /// Whether the keyboard follows the finger that has just landed. Said at each landing, before the gesture
  /// begins.
  pub(super) fn set_interactive(&mut self, interactive: bool) {
    unsafe {
      // Back to the middle of its content, wherever the last finger left it: it has to have room both ways.
      let _: () = msg_send![&*self.scroll_view, setContentOffset: CGPoint { x: 0.0, y: 50_000.0 }];
      if self.interactive == interactive {
        return;
      }
      self.interactive = interactive;
      // `UIScrollViewKeyboardDismissModeInteractive`.
      let mode: isize = if interactive { 2 } else { 0 };
      let _: () = msg_send![&*self.scroll_view, setKeyboardDismissMode: mode];
      let pan: Retained<AnyObject> = msg_send![&*self.scroll_view, panGestureRecognizer];
      let _: () = msg_send![&*pan, setEnabled: interactive];
    }
  }
}

/// The scroll view that takes the status bar's tap. Main thread only.
pub(super) struct StatusBarTap {
  scroll_view: Retained<AnyObject>,
  _receiver: Retained<AnyObject>,
  height: f64,
}

impl StatusBarTap {
  /// Adds the stand-in to the window's view, a `UIView` from the window's handle.
  pub(super) fn new(view: NonNull<c_void>) -> Self {
    let view: &AnyObject = unsafe { view.cast::<AnyObject>().as_ref() };
    unsafe {
      let receiver: Retained<AnyObject> = msg_send![receiver_class(), new];
      let scroll_view: Retained<AnyObject> = msg_send![class!(UIScrollView), new];
      // UIKit asks only a scroll view that is away from its top: more content than fits, scrolled by a point.
      let bounds: CGRect = msg_send![view, bounds];
      let frame = CGRect {
        origin: CGPoint { x: 0.0, y: 0.0 },
        size: CGSize {
          width: bounds.size.width,
          height: 0.0,
        },
      };
      let _: () = msg_send![&*scroll_view, setFrame: frame];
      // `UIViewAutoresizingFlexibleWidth`: it stays across the top when the window turns.
      let _: () = msg_send![&*scroll_view, setAutoresizingMask: 2usize];
      let _: () = msg_send![&*scroll_view, setContentSize: CGSize { width: 1.0, height: 10_000.0 }];
      let _: () = msg_send![&*scroll_view, setContentOffset: CGPoint { x: 0.0, y: 1.0 }];
      let _: () = msg_send![&*scroll_view, setScrollsToTop: true];
      let _: () = msg_send![&*scroll_view, setShowsVerticalScrollIndicator: false];
      let _: () = msg_send![&*scroll_view, setShowsHorizontalScrollIndicator: false];
      // A finger must not scroll it to its top, where UIKit would stop asking it.
      let pan: Retained<AnyObject> = msg_send![&*scroll_view, panGestureRecognizer];
      let _: () = msg_send![&*pan, setEnabled: false];
      // The delegate is not retained by the scroll view: `_receiver` keeps it.
      let _: () = msg_send![&*scroll_view, setDelegate: &*receiver];
      let _: () = msg_send![view, addSubview: &*scroll_view];
      Self {
        scroll_view,
        _receiver: receiver,
        height: 0.0,
      }
    }
  }

  /// Makes the band as high as the status bar: the safe area's top inset, in points.
  pub(super) fn set_height(&mut self, height: f64) {
    if self.height == height {
      return;
    }
    self.height = height;
    unsafe {
      let mut frame: CGRect = msg_send![&*self.scroll_view, frame];
      frame.size.height = height;
      let _: () = msg_send![&*self.scroll_view, setFrame: frame];
      let _: () = msg_send![&*self.scroll_view, setContentOffset: CGPoint { x: 0.0, y: 1.0 }];
    }
  }

  /// Whether the status bar was tapped since the last call.
  pub(super) fn take_tapped(&mut self) -> bool {
    STATUS_BAR_TAPPED.swap(false, Ordering::AcqRel)
  }
}

/// Plays a tap of the haptic engine through UIKit's feedback generators. Main thread only.
pub(super) fn play_haptic(haptic: crate::app::window::Haptic) {
  use crate::app::window::Haptic;
  unsafe {
    match haptic {
      Haptic::Light | Haptic::Medium | Haptic::Heavy => {
        // `UIImpactFeedbackStyle`: light 0, medium 1, heavy 2.
        let style: isize = match haptic {
          Haptic::Light => 0,
          Haptic::Medium => 1,
          _ => 2,
        };
        let generator: objc2::rc::Allocated<AnyObject> = msg_send![class!(UIImpactFeedbackGenerator), alloc];
        let generator: Retained<AnyObject> = msg_send![generator, initWithStyle: style];
        let _: () = msg_send![&*generator, impactOccurred];
      }
      Haptic::Selection => {
        let generator: Retained<AnyObject> = msg_send![class!(UISelectionFeedbackGenerator), new];
        let _: () = msg_send![&*generator, selectionChanged];
      }
      Haptic::Success | Haptic::Warning | Haptic::Error => {
        // `UINotificationFeedbackType`: success 0, warning 1, error 2.
        let kind: isize = match haptic {
          Haptic::Success => 0,
          Haptic::Warning => 1,
          _ => 2,
        };
        let generator: Retained<AnyObject> = msg_send![class!(UINotificationFeedbackGenerator), new];
        let _: () = msg_send![&*generator, notificationOccurred: kind];
      }
    }
  }
}

/// The scene UIKit connected, retained; null before it does and in an app that has no scenes.
static SCENE: AtomicPtr<AnyObject> = AtomicPtr::new(std::ptr::null_mut());
/// A window of the scene that shows nothing and is asked for the safe area; null until a scene is connected.
static SAFE_AREA_PROBE: AtomicPtr<AnyObject> = AtomicPtr::new(std::ptr::null_mut());
/// winit's window, which lives as long as the app does; null until the shell has made it.
static SCENE_WINDOW: AtomicPtr<AnyObject> = AtomicPtr::new(std::ptr::null_mut());

/// Makes the class UIKit creates as the scene's delegate, by the name an app's `Info.plist` gives:
/// `LurqSceneDelegate`. To be called before the event loop runs, which is when UIKit starts the app.
pub(super) fn register_scene_delegate() {
  if AnyClass::get(c"LurqSceneDelegate").is_some() {
    return;
  }
  extern "C-unwind" fn will_connect(
    _this: &AnyObject,
    _cmd: Sel,
    scene: *mut AnyObject,
    session: *mut AnyObject,
    options: *mut AnyObject,
  ) {
    let Some(scene) = (unsafe { Retained::retain(scene) }) else {
      return;
    };
    let is_window_scene: bool = unsafe { msg_send![&*scene, isKindOfClass: class!(UIWindowScene)] };
    if !is_window_scene {
      return;
    }
    // A display plugged in has a scene of its own, by a role of its own
    // (`UIWindowSceneSessionRoleExternalDisplayNonInteractive`).
    let external = unsafe { session.as_ref() }.is_some_and(|session| {
      let role: *mut AnyObject = unsafe { msg_send![session, role] };
      unsafe { role.as_ref() }
        .and_then(string_of)
        .is_some_and(|role| role.contains("External"))
    });
    // The app's first scene shows its first window. Any after it, and a display's, is for another window.
    if external || !SCENE.load(Ordering::Acquire).is_null() {
      other_scene_connected(scene, external);
      return;
    }
    // What the app was started with: a link, a shortcut.
    if let Some(options) = unsafe { options.as_ref() } {
      let contexts: *mut AnyObject = unsafe { msg_send![options, URLContexts] };
      push_opened_urls(contexts);
      let shortcut: *mut AnyObject = unsafe { msg_send![options, shortcutItem] };
      push_shortcut(shortcut);
    }
    let previous = SCENE.swap(Retained::into_raw(scene), Ordering::AcqRel);
    if !previous.is_null() {
      drop(unsafe { Retained::from_raw(previous) });
    }
    show_window_in_scene();
  }
  extern "C-unwind" fn did_disconnect(_this: &AnyObject, _cmd: Sel, scene: *mut AnyObject) {
    if SCENE
      .compare_exchange(scene, std::ptr::null_mut(), Ordering::AcqRel, Ordering::Acquire)
      .is_ok()
    {
      drop(unsafe { Retained::from_raw(scene) });
    } else {
      other_scene_disconnected(scene);
    }
  }
  // What a scene says of itself is told of the app when it is the app's first: another window going to the
  // background leaves the app in front.
  fn first(scene: *mut AnyObject) -> bool {
    std::ptr::eq(SCENE.load(Ordering::Acquire), scene)
  }
  extern "C-unwind" fn did_become_active(_this: &AnyObject, _cmd: Sel, scene: *mut AnyObject) {
    if first(scene) {
      push_app_event(AppEvent::Active);
    }
  }
  extern "C-unwind" fn will_resign_active(_this: &AnyObject, _cmd: Sel, scene: *mut AnyObject) {
    if first(scene) {
      push_app_event(AppEvent::Inactive);
    }
  }
  extern "C-unwind" fn did_enter_background(_this: &AnyObject, _cmd: Sel, scene: *mut AnyObject) {
    if first(scene) {
      push_app_event(AppEvent::Background);
    }
  }
  extern "C-unwind" fn will_enter_foreground(_this: &AnyObject, _cmd: Sel, scene: *mut AnyObject) {
    if first(scene) {
      push_app_event(AppEvent::Foreground);
    }
  }
  extern "C-unwind" fn open_urls(_this: &AnyObject, _cmd: Sel, _scene: *mut AnyObject, contexts: *mut AnyObject) {
    push_opened_urls(contexts);
  }
  extern "C-unwind" fn perform_shortcut(
    _this: &AnyObject,
    _cmd: Sel,
    _scene: *mut AnyObject,
    shortcut: *mut AnyObject,
    completion: *mut c_void,
  ) {
    push_shortcut(shortcut);
    // The block `void (^)(BOOL)` UIKit waits on: its function is the third word after its `isa`.
    #[repr(C)]
    struct Block {
      isa: *const c_void,
      flags: i32,
      reserved: i32,
      invoke: unsafe extern "C" fn(*mut c_void, Bool),
    }
    if let Some(block) = unsafe { completion.cast::<Block>().as_ref() } {
      unsafe { (block.invoke)(completion, Bool::YES) };
    }
  }
  extern "C-unwind" fn window(_this: &AnyObject, _cmd: Sel) -> *mut AnyObject {
    SCENE_WINDOW.load(Ordering::Acquire)
  }
  /// The window is winit's to make.
  extern "C-unwind" fn set_window(_this: &AnyObject, _cmd: Sel, _window: *mut AnyObject) {}

  let mut builder = ClassBuilder::new(c"LurqSceneDelegate", class!(UIResponder)).expect("a class name of its own");
  if let Some(protocol) = objc2::runtime::AnyProtocol::get(c"UIWindowSceneDelegate") {
    builder.add_protocol(protocol);
  }
  unsafe {
    builder.add_method(
      sel!(scene:willConnectToSession:options:),
      will_connect as extern "C-unwind" fn(_, _, _, _, _),
    );
    builder.add_method(
      sel!(sceneDidDisconnect:),
      did_disconnect as extern "C-unwind" fn(_, _, _),
    );
    builder.add_method(
      sel!(sceneDidBecomeActive:),
      did_become_active as extern "C-unwind" fn(_, _, _),
    );
    builder.add_method(
      sel!(sceneWillResignActive:),
      will_resign_active as extern "C-unwind" fn(_, _, _),
    );
    builder.add_method(
      sel!(sceneDidEnterBackground:),
      did_enter_background as extern "C-unwind" fn(_, _, _),
    );
    builder.add_method(
      sel!(sceneWillEnterForeground:),
      will_enter_foreground as extern "C-unwind" fn(_, _, _),
    );
    builder.add_method(
      sel!(scene:openURLContexts:),
      open_urls as extern "C-unwind" fn(_, _, _, _),
    );
    builder.add_method(
      sel!(windowScene:performActionForShortcutItem:completionHandler:),
      perform_shortcut as extern "C-unwind" fn(_, _, _, _, _),
    );
    builder.add_method(sel!(window), window as extern "C-unwind" fn(_, _) -> _);
    builder.add_method(sel!(setWindow:), set_window as extern "C-unwind" fn(_, _, _));
  }
  builder.register();
}

/// Puts the window of a view of winit's into the app's scene, now or when UIKit connects one. A window outside
/// a scene is not shown in an app that has scenes.
pub(super) fn join_scene(view: NonNull<c_void>) {
  let view: &AnyObject = unsafe { view.cast::<AnyObject>().as_ref() };
  let window: Option<Retained<AnyObject>> = unsafe { msg_send![view, window] };
  let Some(window) = window else {
    return;
  };
  SCENE_WINDOW.store(Retained::as_ptr(&window).cast_mut(), Ordering::Release);
  if SCENE.load(Ordering::Acquire).is_null() {
    // Connected before the shell's delegate could hear of it, or by a delegate that is not the shell's.
    let scene = unsafe {
      let app: Retained<AnyObject> = msg_send![class!(UIApplication), sharedApplication];
      let scenes: Retained<AnyObject> = msg_send![&*app, connectedScenes];
      let scenes: Retained<AnyObject> = msg_send![&*scenes, allObjects];
      let count: usize = msg_send![&*scenes, count];
      (0..count)
        .map(|index| -> Retained<AnyObject> { msg_send![&*scenes, objectAtIndex: index] })
        .find(|scene| msg_send![&**scene, isKindOfClass: class!(UIWindowScene)])
    };
    if let Some(scene) = scene {
      SCENE.store(Retained::into_raw(scene), Ordering::Release);
    }
  }
  show_window_in_scene();
}

/// Gives winit's window to the scene, once there are both and the window has none.
fn show_window_in_scene() {
  let (scene, window) = (SCENE.load(Ordering::Acquire), SCENE_WINDOW.load(Ordering::Acquire));
  let (Some(scene), Some(window)) = (unsafe { scene.as_ref() }, unsafe { window.as_ref() }) else {
    return;
  };
  if SAFE_AREA_PROBE.load(Ordering::Acquire).is_null() {
    unsafe {
      let probe: objc2::rc::Allocated<AnyObject> = msg_send![class!(UIWindow), alloc];
      let probe: Retained<AnyObject> = msg_send![probe, initWithWindowScene: scene];
      let controller: Retained<AnyObject> = msg_send![class!(UIViewController), new];
      let _: () = msg_send![&*probe, setRootViewController: &*controller];
      let _: () = msg_send![&*probe, setUserInteractionEnabled: false];
      let _: () = msg_send![&*probe, setAlpha: 0.0f64];
      let _: () = msg_send![&*probe, setWindowLevel: -1.0f64];
      let _: () = msg_send![&*probe, setHidden: false];
      SAFE_AREA_PROBE.store(Retained::into_raw(probe), Ordering::Release);
    }
  }
  unsafe {
    let current: Option<Retained<AnyObject>> = msg_send![window, windowScene];
    if current.is_some() {
      return;
    }
    let _: () = msg_send![window, setWindowScene: scene];
    let _: () = msg_send![window, makeKeyAndVisible];
    // A window made before its scene is left with no orientation of its own once it is put in one
    // (`_windowInterfaceOrientation` is unknown), and what reads it lays out as if it stood on its side: the share
    // sheet, which the system draws in a process of its own, comes as a narrow card at the screen's right edge.
    // Giving the window its controller again, now that it is in the scene, has it take the scene's.
    let controller: Option<Retained<AnyObject>> = msg_send![window, rootViewController];
    if let Some(controller) = controller {
      let _: () = msg_send![window, setRootViewController: std::ptr::null::<AnyObject>()];
      let _: () = msg_send![window, setRootViewController: &*controller];
    }
  }
}

/// A scene UIKit connected beside the app's first: another window of the app on an iPad, or a display plugged
/// in.
struct OtherScene {
  /// The scene, retained.
  scene: usize,
  external: bool,
  /// The window shown in it, one of the app's further windows; 0 while it has none.
  window: usize,
}

/// The scenes beside the first, in the order UIKit connected them.
static OTHER_SCENES: Mutex<Vec<OtherScene>> = Mutex::new(Vec::new());
/// Windows of the app that the shell made and UIKit has no scene for yet, the first made first.
static WAITING_WINDOWS: Mutex<Vec<usize>> = Mutex::new(Vec::new());
/// Windows whose scene the person or the system closed, since the shell last asked.
static CLOSED_WINDOWS: Mutex<Vec<usize>> = Mutex::new(Vec::new());

/// Puts a window into a scene, as large as the scene is, and shows it.
fn show_window_in(scene: usize, window: usize) {
  let (scene, window) = (scene as *mut AnyObject, window as *mut AnyObject);
  let (Some(scene), Some(window)) = (unsafe { scene.as_ref() }, unsafe { window.as_ref() }) else {
    return;
  };
  unsafe {
    let _: () = msg_send![window, setWindowScene: scene];
    let space: Retained<AnyObject> = msg_send![scene, coordinateSpace];
    let bounds: CGRect = msg_send![&*space, bounds];
    let _: () = msg_send![window, setFrame: bounds];
    let _: () = msg_send![window, setHidden: false];
  }
}

/// UIKit connected a scene beside the app's first. A window that waited for one is shown in it; with none
/// waiting the app is told, and the window it opens then is.
fn other_scene_connected(scene: Retained<AnyObject>, external: bool) {
  // A window the app opened asked for a window on the device's own screen, not for a display.
  let window = if external {
    None
  } else {
    let mut waiting = WAITING_WINDOWS.lock().unwrap();
    (!waiting.is_empty()).then(|| waiting.remove(0))
  };
  let scene = Retained::into_raw(scene) as usize;
  OTHER_SCENES.lock().unwrap().push(OtherScene {
    scene,
    external,
    window: window.unwrap_or(0),
  });
  match window {
    Some(window) => show_window_in(scene, window),
    None => push_app_event(AppEvent::WindowAsked { external }),
  }
}

/// A scene beside the app's first is gone: the person closed the window, or the display was unplugged. The
/// shell closes the window that was shown in it.
fn other_scene_disconnected(scene: *mut AnyObject) {
  let gone = {
    let mut scenes = OTHER_SCENES.lock().unwrap();
    scenes
      .iter()
      .position(|other| other.scene == scene as usize)
      .map(|index| scenes.remove(index))
  };
  let Some(gone) = gone else {
    return;
  };
  drop(unsafe { Retained::from_raw(gone.scene as *mut AnyObject) });
  if gone.window != 0 {
    CLOSED_WINDOWS.lock().unwrap().push(gone.window);
  }
}

/// Shows a further window of the app, by a view of winit's in it: in a scene that has no window yet, the one
/// connected first, or in one UIKit is asked for now. Where an app has one scene and no more, a phone, the
/// window waits for a display to be plugged in.
pub(super) fn join_other_scene(view: NonNull<c_void>) {
  let view: &AnyObject = unsafe { view.cast::<AnyObject>().as_ref() };
  let window: Option<Retained<AnyObject>> = unsafe { msg_send![view, window] };
  let Some(window) = window else {
    return;
  };
  let window = Retained::as_ptr(&window) as usize;
  let free = OTHER_SCENES
    .lock()
    .unwrap()
    .iter_mut()
    .find(|other| other.window == 0)
    .map(|other| {
      other.window = window;
      other.scene
    });
  if let Some(scene) = free {
    show_window_in(scene, window);
    return;
  }
  WAITING_WINDOWS.lock().unwrap().push(window);
  unsafe {
    let app: Retained<AnyObject> = msg_send![class!(UIApplication), sharedApplication];
    let several: bool = msg_send![&*app, supportsMultipleScenes];
    if several {
      let none = std::ptr::null_mut::<AnyObject>();
      let _: () = msg_send![
        &*app,
        requestSceneSessionActivation: none,
        userActivity: none,
        options: none,
        errorHandler: std::ptr::null_mut::<c_void>()
      ];
    }
  }
}

/// A further window of the app is closed by the app: its scene is closed with it, or it waits for one no
/// longer.
pub(super) fn leave_other_scene(view: NonNull<c_void>) {
  let view: &AnyObject = unsafe { view.cast::<AnyObject>().as_ref() };
  let window: Option<Retained<AnyObject>> = unsafe { msg_send![view, window] };
  let Some(window) = window else {
    return;
  };
  let window = Retained::as_ptr(&window) as usize;
  WAITING_WINDOWS.lock().unwrap().retain(|waiting| *waiting != window);
  // The scene is told apart from one the person closes: its going closes no window.
  let scene = OTHER_SCENES
    .lock()
    .unwrap()
    .iter_mut()
    .find(|other| other.window == window)
    .map(|other| {
      other.window = 0;
      (other.scene, other.external)
    });
  // A display stays as long as it is plugged in, for the next window.
  if let Some((scene, false)) = scene
    && let Some(scene) = unsafe { (scene as *mut AnyObject).as_ref() }
  {
    unsafe {
      let app: Retained<AnyObject> = msg_send![class!(UIApplication), sharedApplication];
      let session: Retained<AnyObject> = msg_send![scene, session];
      let _: () = msg_send![
        &*app,
        requestSceneSessionDestruction: &*session,
        options: std::ptr::null_mut::<AnyObject>(),
        errorHandler: std::ptr::null_mut::<c_void>()
      ];
      // The window that was closed may have been the one in front, with the first behind it: UIKit shows
      // the home screen then, and the app, which is still open, is asked for in front again.
      if let Some(first) = SCENE.load(Ordering::Acquire).as_ref() {
        let session: Retained<AnyObject> = msg_send![first, session];
        let none = std::ptr::null_mut::<AnyObject>();
        let _: () = msg_send![
          &*app,
          requestSceneSessionActivation: &*session,
          userActivity: none,
          options: none,
          errorHandler: std::ptr::null_mut::<c_void>()
        ];
      }
    }
  }
}

/// Whether the person or the system closed the scene of the window a view of winit's is in, since the shell
/// last asked of that window.
pub(super) fn other_scene_closed(view: NonNull<c_void>) -> bool {
  let view: &AnyObject = unsafe { view.cast::<AnyObject>().as_ref() };
  let window: Option<Retained<AnyObject>> = unsafe { msg_send![view, window] };
  let Some(window) = window else {
    return false;
  };
  let window = Retained::as_ptr(&window) as usize;
  let mut closed = CLOSED_WINDOWS.lock().unwrap();
  let before = closed.len();
  closed.retain(|closed| *closed != window);
  closed.len() != before
}

#[repr(C)]
#[derive(Clone, Copy)]
struct UIEdgeInsets {
  top: f64,
  left: f64,
  bottom: f64,
  right: f64,
}

unsafe impl Encode for UIEdgeInsets {
  const ENCODING: Encoding = Encoding::Struct(
    "UIEdgeInsets",
    &[f64::ENCODING, f64::ENCODING, f64::ENCODING, f64::ENCODING],
  );
}

/// The safe area of the window a view of winit's is in, for the way the device is now held: top, right, bottom
/// and left, in logical pixels. Measured on an iPhone 17 held on its side, winit's window and its view both
/// still said 62 at the top and nothing at the sides; a window made in the scene said 0, and 62 at each side.
pub(super) fn safe_area(view: NonNull<c_void>) -> (f32, f32, f32, f32) {
  let view: &AnyObject = unsafe { view.cast::<AnyObject>().as_ref() };
  let insets: UIEdgeInsets = unsafe { msg_send![view, safeAreaInsets] };
  // winit's window was made outside the scene and put into it after, and UIKit does not turn its safe area
  // with the device. A window made in the scene, unseen, says what it is.
  let probe = SAFE_AREA_PROBE.load(Ordering::Acquire);
  let insets = match unsafe { probe.as_ref() } {
    Some(probe) => unsafe { msg_send![probe, safeAreaInsets] },
    None => insets,
  };
  (
    insets.top as f32,
    insets.right as f32,
    insets.bottom as f32,
    insets.left as f32,
  )
}

/// The safe area of the window a view of winit's is in, as that window says it: for a further window of the
/// app, in a scene of its own, which the first scene's unseen window says nothing of.
pub(super) fn own_safe_area(view: NonNull<c_void>) -> (f32, f32, f32, f32) {
  let view: &AnyObject = unsafe { view.cast::<AnyObject>().as_ref() };
  let insets: UIEdgeInsets = unsafe { msg_send![view, safeAreaInsets] };
  (
    insets.top as f32,
    insets.right as f32,
    insets.bottom as f32,
    insets.left as f32,
  )
}

use crate::app::window::AppEvent;

/// What the scene said of the app since the shell last asked.
static APP_EVENTS: Mutex<Vec<AppEvent>> = Mutex::new(Vec::new());

fn push_app_event(event: AppEvent) {
  APP_EVENTS.lock().unwrap().push(event);
}

/// The links of a set of `UIOpenURLContext`.
fn push_opened_urls(contexts: *mut AnyObject) {
  let Some(contexts) = (unsafe { contexts.as_ref() }) else {
    return;
  };
  unsafe {
    let contexts: Retained<AnyObject> = msg_send![contexts, allObjects];
    let count: usize = msg_send![&*contexts, count];
    for index in 0..count {
      let context: Retained<AnyObject> = msg_send![&*contexts, objectAtIndex: index];
      let url: Retained<AnyObject> = msg_send![&*context, URL];
      let text: Option<Retained<AnyObject>> = msg_send![&*url, absoluteString];
      if let Some(text) = text.and_then(|text| string_of(&text)) {
        push_app_event(AppEvent::OpenUrl(text));
      }
    }
  }
}

/// The type of a `UIApplicationShortcutItem`.
fn push_shortcut(shortcut: *mut AnyObject) {
  let Some(shortcut) = (unsafe { shortcut.as_ref() }) else {
    return;
  };
  let kind: Option<Retained<AnyObject>> = unsafe { msg_send![shortcut, type] };
  if let Some(kind) = kind.and_then(|kind| string_of(&kind)) {
    push_app_event(AppEvent::Shortcut(kind));
  }
}

fn string_of(string: &AnyObject) -> Option<String> {
  let utf8: *const std::ffi::c_char = unsafe { msg_send![string, UTF8String] };
  if utf8.is_null() {
    return None;
  }
  Some(unsafe { std::ffi::CStr::from_ptr(utf8) }.to_string_lossy().into_owned())
}

/// What the scene said of the app since the last call, in the order it said it.
pub(super) fn take_app_events() -> Vec<AppEvent> {
  std::mem::take(&mut *APP_EVENTS.lock().unwrap())
}

/// A key of a keyboard attached to the device, pressed or let go.
pub(super) struct HardwareKey {
  pub(super) key: String,
  pub(super) code: String,
  pub(super) shift: bool,
  pub(super) ctrl: bool,
  pub(super) alt: bool,
  pub(super) meta: bool,
  pub(super) down: bool,
}

/// The keys of an attached keyboard since the shell last asked. winit passes none of them on.
static HARDWARE_KEYS: Mutex<Vec<HardwareKey>> = Mutex::new(Vec::new());

pub(super) fn take_hardware_keys() -> Vec<HardwareKey> {
  std::mem::take(&mut *HARDWARE_KEYS.lock().unwrap())
}

/// The name and the code the tree knows a key by, from its HID usage and what it types.
fn key_names(usage: isize, typed: &str) -> Option<(String, String)> {
  let named = |name: &str| Some((name.to_owned(), name.to_owned()));
  match usage {
    0x04..=0x1D => {
      let letter = (b'A' + (usage - 0x04) as u8) as char;
      let key = if typed.chars().count() == 1 {
        typed.to_owned()
      } else {
        letter.to_ascii_lowercase().to_string()
      };
      Some((key, format!("Key{letter}")))
    }
    0x1E..=0x27 => {
      let digit = (usage - 0x1E + 1) % 10;
      let key = if typed.chars().count() == 1 {
        typed.to_owned()
      } else {
        digit.to_string()
      };
      Some((key, format!("Digit{digit}")))
    }
    0x28 => named("Enter"),
    0x29 => named("Escape"),
    0x2A => named("Backspace"),
    0x2B => named("Tab"),
    0x2C => Some((" ".to_owned(), "Space".to_owned())),
    0x4A => named("Home"),
    0x4B => named("PageUp"),
    0x4C => named("Delete"),
    0x4D => named("End"),
    0x4E => named("PageDown"),
    0x4F => named("ArrowRight"),
    0x50 => named("ArrowLeft"),
    0x51 => named("ArrowDown"),
    0x52 => named("ArrowUp"),
    _ if typed.chars().count() == 1 => Some((typed.to_owned(), String::new())),
    _ => None,
  }
}

/// Takes the keys of a set of `UIPress` for the tree: all of them, or those named in `only`, which is what a
/// view that types leaves to the tree, the rest being the text system's. Tells whether every press was taken.
pub(super) fn take_presses(presses: *mut AnyObject, down: bool, only: Option<&[&str]>) -> bool {
  let Some(presses) = (unsafe { presses.as_ref() }) else {
    return true;
  };
  let mut all = true;
  unsafe {
    let presses: Retained<AnyObject> = msg_send![presses, allObjects];
    let count: usize = msg_send![&*presses, count];
    for index in 0..count {
      let press: Retained<AnyObject> = msg_send![&*presses, objectAtIndex: index];
      let key: Option<Retained<AnyObject>> = msg_send![&*press, key];
      let Some(key) = key else {
        all = false;
        continue;
      };
      let usage: isize = msg_send![&*key, keyCode];
      let typed: Option<Retained<AnyObject>> = msg_send![&*key, charactersIgnoringModifiers];
      let typed = typed.and_then(|typed| string_of(&typed)).unwrap_or_default();
      let flags: isize = msg_send![&*key, modifierFlags];
      let Some((name, code)) = key_names(usage, &typed) else {
        all = false;
        continue;
      };
      if only.is_some_and(|only| !only.contains(&name.as_str())) {
        all = false;
        continue;
      }
      HARDWARE_KEYS.lock().unwrap().push(HardwareKey {
        key: name,
        code,
        shift: flags & (1 << 17) != 0,
        ctrl: flags & (1 << 18) != 0,
        alt: flags & (1 << 19) != 0,
        meta: flags & (1 << 20) != 0,
        down,
      });
    }
  }
  all
}

/// Has a view of winit's hear the keys of an attached keyboard, which its class leaves to the responders
/// after it: the two methods are added to the class, once.
pub(super) fn hear_hardware_keys(view: NonNull<c_void>) {
  extern "C-unwind" fn began(_this: &AnyObject, _cmd: Sel, presses: *mut AnyObject, _event: *mut AnyObject) {
    take_presses(presses, true, None);
  }
  extern "C-unwind" fn ended(_this: &AnyObject, _cmd: Sel, presses: *mut AnyObject, _event: *mut AnyObject) {
    take_presses(presses, false, None);
  }
  let view: &AnyObject = unsafe { view.cast::<AnyObject>().as_ref() };
  let class = view.class();
  if class
    .instance_method(sel!(pressesBegan:withEvent:))
    .is_some_and(|method| {
      // Its own, not the one every responder has.
      class!(UIResponder)
        .instance_method(sel!(pressesBegan:withEvent:))
        .is_none_or(|inherited| !std::ptr::eq(method, inherited))
    })
  {
    return;
  }
  let types = c"v@:@@";
  unsafe {
    let class = std::ptr::from_ref(class).cast_mut();
    objc2::ffi::class_addMethod(
      class.cast(),
      sel!(pressesBegan:withEvent:),
      std::mem::transmute::<
        extern "C-unwind" fn(&AnyObject, Sel, *mut AnyObject, *mut AnyObject),
        unsafe extern "C-unwind" fn(),
      >(began),
      types.as_ptr(),
    );
    objc2::ffi::class_addMethod(
      class.cast(),
      sel!(pressesEnded:withEvent:),
      std::mem::transmute::<
        extern "C-unwind" fn(&AnyObject, Sel, *mut AnyObject, *mut AnyObject),
        unsafe extern "C-unwind" fn(),
      >(ended),
      types.as_ptr(),
    );
  }
}

use crate::app::window::{Orientation, Orientations};

/// The four ways, with the number UIKit has for each as an interface orientation. UIKit names a side by where
/// the home button was, the other end from the device's top.
const ORIENTATIONS: [(Orientation, usize); 4] = [
  (Orientation::Portrait, 1),
  (Orientation::PortraitUpsideDown, 2),
  (Orientation::LandscapeLeft, 3),
  (Orientation::LandscapeRight, 4),
];

/// The ways the app allows, as a `UIInterfaceOrientationMask`; 0 leaves it to winit, which allows every way
/// the app's `Info.plist` names.
static ALLOWED_ORIENTATIONS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
/// The interface orientation last told to the app, or 0.
static TOLD_ORIENTATION: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);

/// Says which ways the app's content may be turned. To be called before the window is made.
pub(super) fn allow_orientations(allowed: Orientations) {
  let mask = ORIENTATIONS
    .iter()
    .filter(|(orientation, _)| allowed.contains(*orientation))
    .fold(0, |mask, (_, number)| mask | (1 << number));
  ALLOWED_ORIENTATIONS.store(mask, Ordering::Release);
}

/// Has the view controller of a view of winit's answer UIKit with the ways the app allows, in place of
/// winit's answer, which can tell upright from on its side and not one side from the other.
pub(super) fn apply_orientations(view: NonNull<c_void>) {
  extern "C-unwind" fn supported(_this: &AnyObject, _cmd: Sel) -> usize {
    ALLOWED_ORIENTATIONS.load(Ordering::Acquire)
  }
  if ALLOWED_ORIENTATIONS.load(Ordering::Acquire) == 0 {
    return;
  }
  let view: &AnyObject = unsafe { view.cast::<AnyObject>().as_ref() };
  unsafe {
    let window: Option<Retained<AnyObject>> = msg_send![view, window];
    let controller: Option<Retained<AnyObject>> = match &window {
      Some(window) => msg_send![&**window, rootViewController],
      None => None,
    };
    let Some(controller) = controller else {
      return;
    };
    let class = std::ptr::from_ref(controller.class()).cast_mut();
    objc2::ffi::class_replaceMethod(
      class.cast(),
      sel!(supportedInterfaceOrientations),
      std::mem::transmute::<extern "C-unwind" fn(&AnyObject, Sel) -> usize, unsafe extern "C-unwind" fn()>(supported),
      c"Q@:".as_ptr(),
    );
    let _: () = msg_send![&*controller, setNeedsUpdateOfSupportedInterfaceOrientations];
  }
}

/// Tells the app the way its content is turned, the first time and whenever it changes: an
/// [`AppEvent::Rotated`] among what [`take_app_events`] gives. To be called each turn of the loop.
pub(super) fn watch_orientation() {
  let scene = SCENE.load(Ordering::Acquire);
  let Some(scene) = (unsafe { scene.as_ref() }) else {
    return;
  };
  let number: isize = unsafe { msg_send![scene, interfaceOrientation] };
  let Some((orientation, number)) = ORIENTATIONS
    .iter()
    .find(|(_, known)| *known as isize == number)
    .copied()
  else {
    return;
  };
  if TOLD_ORIENTATION.swap(number, Ordering::AcqRel) != number {
    push_app_event(AppEvent::Rotated(orientation));
  }
}

use crate::app::window::{Appearance, SystemSettings};

#[link(name = "UIKit", kind = "framework")]
unsafe extern "C" {
  fn UIAccessibilityIsBoldTextEnabled() -> bool;
  fn UIAccessibilityIsReduceMotionEnabled() -> bool;
  fn UIAccessibilityDarkerSystemColorsEnabled() -> bool;
  fn UIAccessibilityIsReduceTransparencyEnabled() -> bool;
  fn UIAccessibilityShouldDifferentiateWithoutColor() -> bool;
  fn UIAccessibilityButtonShapesEnabled() -> bool;
  fn UIAccessibilityIsOnOffSwitchLabelsEnabled() -> bool;
}

/// What the person set for the whole system, as it is now. Main thread only.
///
/// The appearance is the scene's: winit's window was made outside the scene, and a window made in it says
/// what the scene is in. The text scale is what the system makes of a size of 100 for the person's text
/// size, the body style's curve.
pub(super) fn system_settings(view: NonNull<c_void>) -> SystemSettings {
  let view: &AnyObject = unsafe { view.cast::<AnyObject>().as_ref() };
  unsafe {
    let probe = SAFE_AREA_PROBE.load(Ordering::Acquire);
    let traits: Retained<AnyObject> = match probe.as_ref() {
      Some(probe) => msg_send![probe, traitCollection],
      None => msg_send![view, traitCollection],
    };
    // `UIUserInterfaceStyleDark`.
    let style: isize = msg_send![&*traits, userInterfaceStyle];
    let metrics: Retained<AnyObject> = msg_send![class!(UIFontMetrics), defaultMetrics];
    let scaled: f64 = msg_send![&*metrics, scaledValueForValue: 100.0f64, compatibleWithTraitCollection: &*traits];
    SystemSettings {
      appearance: if style == 2 {
        Appearance::Dark
      } else {
        Appearance::Light
      },
      text_scale: (scaled / 100.0) as f32,
      bold_text: UIAccessibilityIsBoldTextEnabled(),
      reduce_motion: UIAccessibilityIsReduceMotionEnabled(),
      increase_contrast: UIAccessibilityDarkerSystemColorsEnabled(),
      reduce_transparency: UIAccessibilityIsReduceTransparencyEnabled(),
      differentiate_without_color: UIAccessibilityShouldDifferentiateWithoutColor(),
      button_shapes: UIAccessibilityButtonShapesEnabled(),
      on_off_labels: UIAccessibilityIsOnOffSwitchLabelsEnabled(),
    }
  }
}