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
//! The file dialog on iOS: the system's document picker (`UIDocumentPickerViewController`), shown over the app.
//!
//! - Files to open are taken as copies (`asCopy`): the system puts them in the app's own temporary folder, and the app
//!   reads them by their paths, as on a desktop.
//! - A folder is the folder itself, which the app may read and write while it runs: its access is asked of the system
//!   (`startAccessingSecurityScopedResource`) and kept.
//! - Where to save: iOS chooses no path ahead of a file. An empty file of the dialog's file name is made in the
//!   temporary folder, and the picker has the person put it where they choose (Files, iCloud Drive, a folder on the
//!   phone); the path it has there, with access to it kept, is what the app writes, as on a desktop.
//!
//! The picker is shown on the main thread, over the top of what the app's window shows; one is open at a time.

use std::{ffi::c_void, path::PathBuf, sync::Mutex};

use objc2::{
  ClassType, MainThreadMarker, msg_send,
  rc::Retained,
  runtime::{AnyClass, AnyObject, AnyProtocol, Bool, ClassBuilder, Sel},
  sel,
};
use objc2_foundation::{NSArray, NSObject, NSString, NSURL};

use super::{Answer, Answering, Asks, DroppedFile, FileDialog, FileSource};

#[link(name = "UniformTypeIdentifiers", kind = "framework")]
unsafe extern "C" {}

/// The dialog that is open: what it asks, and what is told what was chosen. One at a time.
struct Open {
  asks: Asks,
  answering: Answering,
  /// The picker, held while it is shown, and the object that hears it.
  _picker: Retained<AnyObject>,
  _delegate: Retained<AnyObject>,
}

// SAFETY: the picker and its delegate are touched on the main thread alone; the slot is only a place to keep them.
unsafe impl Send for Open {}

static OPEN: Mutex<Option<Open>> = Mutex::new(None);

pub(super) fn pick(dialog: FileDialog) -> Answer {
  let (answer, answering) = Answer::new();
  super::on_main(move |main| choose(main, dialog, answering));
  answer
}

/// For files to open, the sources the device has, in a menu where there are more than one; the dialog of what it
/// asks otherwise.
fn choose(main: MainThreadMarker, dialog: FileDialog, answering: Answering) {
  if dialog.asks != Asks::Files {
    present(main, dialog, answering);
    return;
  }
  let mut has = vec![FileSource::PhotoLibrary];
  if camera_ready() {
    has.push(FileSource::Camera);
  }
  has.push(FileSource::Files);
  let offered = dialog.offered(&has);
  if let [source] = offered.as_slice() {
    open_source(main, *source, dialog, answering);
    return;
  }
  menu(main, &offered, dialog, answering);
}

/// Opens one source for the dialog.
fn open_source(main: MainThreadMarker, source: FileSource, dialog: FileDialog, answering: Answering) {
  match source {
    FileSource::Files => present(main, dialog, answering),
    FileSource::PhotoLibrary => super::photos::present(main, &dialog, answering),
    FileSource::Camera => camera(main, answering, &dialog),
  }
}

/// The system's menu of sources, as Safari's file input has it, and Cancel; over the top of what the app shows.
fn menu(main: MainThreadMarker, offered: &[FileSource], dialog: FileDialog, answering: Answering) {
  let Some(over) = top_controller() else {
    answering.give(Vec::new());
    return;
  };
  let words = dialog
    .words
    .clone()
    .unwrap_or_else(|| super::SourceWords::of(&crate::core::Locale::english()));
  // What the menu answers with: taken by the first of its actions to run.
  let held = std::sync::Arc::new(Mutex::new(Some((dialog, answering))));
  // SAFETY: on the main thread, with UIKit's classes; each action's block is called once, on the main thread.
  unsafe {
    let alert: Retained<AnyObject> = msg_send![
      objc2::class!(UIAlertController),
      alertControllerWithTitle: std::ptr::null::<NSString>(),
      message: std::ptr::null::<NSString>(),
      preferredStyle: 0isize
    ];
    for source in offered {
      let title = match source {
        FileSource::PhotoLibrary => &words.photo_library,
        FileSource::Camera => &words.take_photo,
        FileSource::Files => &words.choose_file,
      };
      let (source, held) = (*source, held.clone());
      let block = block2::RcBlock::new(move |_action: *mut AnyObject| {
        if let Some((dialog, answering)) = held.lock().unwrap().take() {
          open_source(main, source, dialog, answering);
        }
      });
      let action: Retained<AnyObject> = msg_send![
        objc2::class!(UIAlertAction),
        actionWithTitle: &*NSString::from_str(title),
        style: 0isize,
        handler: &*block
      ];
      let _: () = msg_send![&*alert, addAction: &*action];
    }
    let cancelled = held.clone();
    let block = block2::RcBlock::new(move |_action: *mut AnyObject| {
      if let Some((_, answering)) = cancelled.lock().unwrap().take() {
        answering.give(Vec::new());
      }
    });
    let cancel: Retained<AnyObject> = msg_send![
      objc2::class!(UIAlertAction),
      actionWithTitle: &*NSString::from_str(&words.cancel),
      style: 1isize,
      handler: &*block
    ];
    let _: () = msg_send![&*alert, addAction: &*cancel];
    // On an iPad the menu stands in a popover, which hangs from the middle of what is shown.
    let popover: Option<Retained<AnyObject>> = msg_send![&*alert, popoverPresentationController];
    if let Some(popover) = popover {
      let view: Retained<AnyObject> = msg_send![&*over, view];
      let bounds: Rect = msg_send![&*view, bounds];
      let middle = Rect {
        origin: Point {
          x: bounds.size.width / 2.0,
          y: bounds.size.height / 2.0,
        },
        size: Size {
          width: 0.0,
          height: 0.0,
        },
      };
      let _: () = msg_send![&*popover, setSourceView: &*view];
      let _: () = msg_send![&*popover, setSourceRect: middle];
      let _: () = msg_send![&*popover, setPermittedArrowDirections: 0usize];
    }
    let _: () =
      msg_send![&*over, presentViewController: &*alert, animated: Bool::YES, completion: std::ptr::null::<c_void>()];
  }
}

/// Whether a picture can be taken: the device has a camera, and the app's Info.plist says why it uses it, without
/// which iOS ends the app that asks for it. Where it has the camera and not the words, the log says once what to add.
fn camera_ready() -> bool {
  // SAFETY: class methods of UIKit and Foundation that take what is given here.
  unsafe {
    let has: Bool = msg_send![objc2::class!(UIImagePickerController), isSourceTypeAvailable: 1isize];
    if !has.as_bool() {
      return false;
    }
    let bundle: Retained<AnyObject> = msg_send![objc2::class!(NSBundle), mainBundle];
    let said: Option<Retained<AnyObject>> =
      msg_send![&*bundle, objectForInfoDictionaryKey: &*NSString::from_str("NSCameraUsageDescription")];
    if said.is_none() {
      static SAID: std::sync::Once = std::sync::Once::new();
      SAID.call_once(|| {
        tracing::warn!(
          "lurq: the file dialog offers no camera: the app's Info.plist has no NSCameraUsageDescription, the words \
           iOS shows when it asks for the camera"
        );
      });
      return false;
    }
    true
  }
}

/// The camera that is open for one picture, and what is told it.
struct Shooting {
  answering: Answering,
  picker: Retained<AnyObject>,
  _delegate: Retained<AnyObject>,
}

// SAFETY: touched on the main thread alone.
unsafe impl Send for Shooting {}

static SHOOTING: Mutex<Option<Shooting>> = Mutex::new(None);

#[link(name = "UIKit", kind = "framework")]
unsafe extern "C" {
  fn UIImageJPEGRepresentation(image: *mut AnyObject, quality: f64) -> *mut AnyObject;
}

/// The camera: the system's for one picture, with its review ("Use Photo"); lurq's over the system's where the
/// dialog takes several, which stays open between them ([`super::camera`]).
fn camera(main: MainThreadMarker, answering: Answering, dialog: &FileDialog) {
  if dialog.multiple {
    let words = dialog
      .words
      .clone()
      .unwrap_or_else(|| super::SourceWords::of(&crate::core::Locale::english()));
    super::camera::shoot(main, answering, words);
    return;
  }
  system_camera(answering);
}

/// The system's camera, for one picture with its review; where the app may not use the camera, it says so.
pub(super) fn system_camera(answering: Answering) {
  let mut shooting = SHOOTING.lock().unwrap();
  let over = top_controller();
  let (Some(over), None) = (over, shooting.as_ref()) else {
    answering.give(Vec::new());
    return;
  };
  // SAFETY: on the main thread, with UIKit's picker of the camera.
  unsafe {
    let picker: Retained<AnyObject> = msg_send![objc2::class!(UIImagePickerController), new];
    let _: () = msg_send![&*picker, setSourceType: 1isize];
    let delegate: Retained<AnyObject> = msg_send![camera_delegate_class(), new];
    let _: () = msg_send![&*picker, setDelegate: &*delegate];
    let _: () =
      msg_send![&*over, presentViewController: &*picker, animated: Bool::YES, completion: std::ptr::null::<c_void>()];
    *shooting = Some(Shooting {
      answering,
      picker,
      _delegate: delegate,
    });
  }
}

/// Where a new photo is kept: a JPEG of its own in a folder of the temporary folder.
pub(super) fn photo_path() -> Option<PathBuf> {
  let folder = std::env::temp_dir().join(format!("lurq-camera-{}", std::process::id()));
  std::fs::create_dir_all(&folder).ok()?;
  let millis = std::time::SystemTime::now()
    .duration_since(std::time::UNIX_EPOCH)
    .map(|since| since.as_millis())
    .unwrap_or_default();
  let mut path = folder.join(format!("Photo-{millis}.jpg"));
  let mut more = 1;
  while path.exists() {
    path = folder.join(format!("Photo-{millis}-{more}.jpg"));
    more += 1;
  }
  Some(path)
}

/// The picture of a camera's info, kept as a JPEG ([`photo_path`]).
fn keep_photo(info: &AnyObject) -> Option<PathBuf> {
  // SAFETY: the picker's info, a dictionary that holds the picture taken under its key.
  unsafe {
    let image: Option<Retained<AnyObject>> =
      msg_send![info, objectForKey: &*NSString::from_str("UIImagePickerControllerOriginalImage")];
    let image = image?;
    let data = UIImageJPEGRepresentation(Retained::as_ptr(&image).cast_mut(), 0.9);
    if data.is_null() {
      return None;
    }
    let path = photo_path()?;
    let written: Bool = msg_send![data, writeToFile: &*NSString::from_str(path.to_str()?), atomically: Bool::YES];
    written.as_bool().then_some(path)
  }
}

/// The picture taken and kept ("Use Photo"): given.
extern "C-unwind" fn shot(_this: &AnyObject, _cmd: Sel, _picker: &AnyObject, info: &AnyObject) {
  let Some(shooting) = SHOOTING.lock().unwrap().take() else {
    return;
  };
  let taken = keep_photo(info).map(DroppedFile::from_path);
  // SAFETY: on the main thread, the picker shown.
  unsafe {
    let _: () =
      msg_send![&*shooting.picker, dismissViewControllerAnimated: Bool::YES, completion: std::ptr::null::<c_void>()];
  }
  shooting.answering.give(taken.into_iter().collect());
}

/// The camera was put away: nothing is given.
extern "C-unwind" fn shot_none(_this: &AnyObject, _cmd: Sel, _picker: &AnyObject) {
  if let Some(shooting) = SHOOTING.lock().unwrap().take() {
    // SAFETY: on the main thread, the picker shown.
    unsafe {
      let _: () =
        msg_send![&*shooting.picker, dismissViewControllerAnimated: Bool::YES, completion: std::ptr::null::<c_void>()];
    }
    shooting.answering.give(Vec::new());
  }
}

/// The class of what hears the camera, registered once.
fn camera_delegate_class() -> &'static AnyClass {
  if let Some(class) = AnyClass::get(c"LurqCameraDelegate") {
    return class;
  }
  let mut builder = ClassBuilder::new(c"LurqCameraDelegate", NSObject::class()).expect("a class name of its own");
  for protocol in [c"UIImagePickerControllerDelegate", c"UINavigationControllerDelegate"] {
    if let Some(protocol) = AnyProtocol::get(protocol) {
      builder.add_protocol(protocol);
    }
  }
  // SAFETY: the methods have the selectors' types.
  unsafe {
    builder.add_method(
      sel!(imagePickerController:didFinishPickingMediaWithInfo:),
      shot as extern "C-unwind" fn(_, _, _, _),
    );
    builder.add_method(
      sel!(imagePickerControllerDidCancel:),
      shot_none as extern "C-unwind" fn(_, _, _),
    );
  }
  builder.register()
}

/// Shows the picker the dialog asks for. Where none can be shown, or one is open already, nothing is chosen.
fn present(_main: MainThreadMarker, dialog: FileDialog, answering: Answering) {
  let mut open = OPEN.lock().unwrap();
  if open.is_some() {
    answering.give(Vec::new());
    return;
  }
  let Some(over) = top_controller() else {
    answering.give(Vec::new());
    return;
  };
  // SAFETY: on the main thread, with UIKit's classes and the selectors they answer.
  let picker = unsafe {
    let class = objc2::class!(UIDocumentPickerViewController);
    let picker: Option<Retained<AnyObject>> = match dialog.asks {
      Asks::Files => {
        let types = content_types(&dialog.filters);
        msg_send![msg_send![class, alloc], initForOpeningContentTypes: &*types, asCopy: Bool::YES]
      }
      Asks::Folder => {
        let types = NSArray::from_retained_slice(&[type_of("public.folder")]);
        msg_send![msg_send![class, alloc], initForOpeningContentTypes: &*types, asCopy: Bool::NO]
      }
      Asks::Save => {
        let Some(made) = empty_file(dialog.file_name.as_deref()) else {
          answering.give(Vec::new());
          return;
        };
        let urls = NSArray::from_retained_slice(&[made]);
        // Moved, not copied: the file is then the one where the person put it, which the app may write.
        msg_send![msg_send![class, alloc], initForExportingURLs: &*urls, asCopy: Bool::NO]
      }
    };
    let Some(picker) = picker else {
      answering.give(Vec::new());
      return;
    };
    if dialog.asks == Asks::Files && dialog.multiple {
      let _: () = msg_send![&*picker, setAllowsMultipleSelection: Bool::YES];
    }
    if let Some(folder) = &dialog.folder
      && let Some(url) = file_url(folder)
    {
      let _: () = msg_send![&*picker, setDirectoryURL: &*url];
    }
    picker
  };
  let delegate: Retained<AnyObject> = unsafe { msg_send![delegate_class(), new] };
  unsafe {
    let _: () = msg_send![&*picker, setDelegate: &*delegate];
    let _: () =
      msg_send![&*over, presentViewController: &*picker, animated: Bool::YES, completion: std::ptr::null::<c_void>()];
  }
  *open = Some(Open {
    asks: dialog.asks,
    answering,
    _picker: picker,
    _delegate: delegate,
  });
}

/// The kinds of files the filters name, by their extensions; every file where they name none.
fn content_types(filters: &[(String, Vec<String>)]) -> Retained<NSArray<AnyObject>> {
  let class = AnyClass::get(c"UTType");
  let mut types: Vec<Retained<AnyObject>> = Vec::new();
  if let Some(class) = class {
    for extension in filters.iter().flat_map(|(_, extensions)| extensions) {
      // SAFETY: `typeWithFilenameExtension:` takes a string and gives a type or nil.
      let found: Option<Retained<AnyObject>> =
        unsafe { msg_send![class, typeWithFilenameExtension: &*NSString::from_str(extension)] };
      types.extend(found);
    }
  }
  if types.is_empty() {
    types.push(type_of("public.item"));
  }
  NSArray::from_retained_slice(&types)
}

/// The type of the identifier, as `UTType` knows it.
fn type_of(identifier: &str) -> Retained<AnyObject> {
  let class = AnyClass::get(c"UTType").expect("UniformTypeIdentifiers is linked");
  // SAFETY: `typeWithIdentifier:` takes a string; the identifiers given here are the system's own.
  unsafe { msg_send![class, typeWithIdentifier: &*NSString::from_str(identifier)] }
}

fn file_url(path: &std::path::Path) -> Option<Retained<NSURL>> {
  let path = path.to_str()?;
  Some(NSURL::fileURLWithPath(&NSString::from_str(path)))
}

/// An empty file of `name` in a folder of its own in the temporary folder, to be put where the person says.
fn empty_file(name: Option<&str>) -> Option<Retained<AnyObject>> {
  let name = name.filter(|name| !name.is_empty()).unwrap_or("Untitled");
  let folder = std::env::temp_dir().join(format!("lurq-save-{}", std::process::id()));
  std::fs::create_dir_all(&folder).ok()?;
  let path = folder.join(name);
  std::fs::write(&path, []).ok()?;
  file_url(&path).map(Retained::into_super).map(Retained::into_super)
}

/// The path of a URL the picker gave, with its access kept where it is outside the app's own folders.
fn path_of(url: &AnyObject, keep_access: bool) -> Option<PathBuf> {
  // SAFETY: an `NSURL` the picker gave; `path` gives its path, and access is asked of a URL the picker gave.
  unsafe {
    if keep_access {
      let _: Bool = msg_send![url, startAccessingSecurityScopedResource];
    }
    let path: Option<Retained<NSString>> = msg_send![url, path];
    path.map(|path| PathBuf::from(path.to_string()))
  }
}

/// The picker chose: what it chose is given to the dialog's future.
extern "C-unwind" fn picked(_this: &AnyObject, _cmd: Sel, _picker: &AnyObject, urls: &NSArray<AnyObject>) {
  let Some(open) = OPEN.lock().unwrap().take() else {
    return;
  };
  // A copy is the app's own; a folder and a file put somewhere else are reached with the access the system gives.
  let keep_access = open.asks != Asks::Files;
  let files = urls
    .iter()
    .filter_map(|url| path_of(&url, keep_access))
    .map(DroppedFile::from_path)
    .collect();
  open.answering.give(files);
}

/// The picker was dismissed: nothing is chosen.
extern "C-unwind" fn cancelled(_this: &AnyObject, _cmd: Sel, _picker: &AnyObject) {
  if let Some(open) = OPEN.lock().unwrap().take() {
    open.answering.give(Vec::new());
  }
}

/// The class of what hears the picker, registered once.
fn delegate_class() -> &'static AnyClass {
  if let Some(class) = AnyClass::get(c"LurqDocumentPickerDelegate") {
    return class;
  }
  let mut builder =
    ClassBuilder::new(c"LurqDocumentPickerDelegate", NSObject::class()).expect("a class name of its own");
  if let Some(protocol) = AnyProtocol::get(c"UIDocumentPickerDelegate") {
    builder.add_protocol(protocol);
  }
  // SAFETY: the methods have the selectors' types: an object and an array, and an object.
  unsafe {
    builder.add_method(
      sel!(documentPicker:didPickDocumentsAtURLs:),
      picked as extern "C-unwind" fn(_, _, _, _),
    );
    builder.add_method(
      sel!(documentPickerWasCancelled:),
      cancelled as extern "C-unwind" fn(_, _, _),
    );
  }
  builder.register()
}

/// The view controller over everything the app shows: the key window's root, or what it presents.
pub(super) fn top_controller() -> Option<Retained<AnyObject>> {
  // SAFETY: on the main thread; the scenes, windows and controllers are UIKit's own.
  unsafe {
    let app: Retained<AnyObject> = msg_send![objc2::class!(UIApplication), sharedApplication];
    let scenes: Retained<AnyObject> = msg_send![&*app, connectedScenes];
    let scenes: Retained<NSArray<AnyObject>> = msg_send![&*scenes, allObjects];
    let mut top: Option<Retained<AnyObject>> = None;
    for scene in scenes.iter() {
      let is_window_scene: Bool = msg_send![&*scene, isKindOfClass: objc2::class!(UIWindowScene)];
      if !is_window_scene.as_bool() {
        continue;
      }
      let window: Option<Retained<AnyObject>> = msg_send![&*scene, keyWindow];
      let window = match window {
        Some(window) => Some(window),
        None => {
          let windows: Retained<NSArray<AnyObject>> = msg_send![&*scene, windows];
          windows.iter().next()
        }
      };
      if let Some(window) = window {
        top = msg_send![&*window, rootViewController];
        if top.is_some() {
          break;
        }
      }
    }
    let mut top = top?;
    loop {
      let presented: Option<Retained<AnyObject>> = msg_send![&*top, presentedViewController];
      match presented {
        Some(presented) => top = presented,
        None => return Some(top),
      }
    }
  }
}

/// A rectangle of UIKit's (`CGRect`), as a view's bounds and a popover's place are.
#[repr(C)]
#[derive(Clone, Copy)]
pub(super) struct Rect {
  pub(super) origin: Point,
  pub(super) size: Size,
}

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

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

// SAFETY: the layouts are CoreGraphics', of `CGFloat`s, which are `f64` on every device iOS runs on.
unsafe impl objc2::Encode for Point {
  const ENCODING: objc2::Encoding = objc2::Encoding::Struct("CGPoint", &[f64::ENCODING, f64::ENCODING]);
}
unsafe impl objc2::Encode for Size {
  const ENCODING: objc2::Encoding = objc2::Encoding::Struct("CGSize", &[f64::ENCODING, f64::ENCODING]);
}
unsafe impl objc2::Encode for Rect {
  const ENCODING: objc2::Encoding = objc2::Encoding::Struct("CGRect", &[Point::ENCODING, Size::ENCODING]);
}