ferrokit 0.5.0

Native Mac apps in Rust: every view a real AppKit view (tables, outlines, text), updated in place by fine-grained signals
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
//! The system's services: the Share menu, printing (and saving as PDF),
//! notifications, and files (Reveal in Finder, Move to Trash, their dates).

use ferrokit_core::kept::{UiCell, UiLocal};
use std::cell::{Cell, RefCell};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::rc::Rc;

use block2::RcBlock;
use objc2::rc::Retained;
use objc2::runtime::{AnyObject, Bool, NSObject, NSObjectProtocol, ProtocolObject};
use objc2::{AnyThread, DefinedClass, MainThreadOnly, define_class, msg_send, sel};
use objc2_app_kit::{NSButton, NSImage, NSPrintInfo, NSPrintOperation, NSPrintingPaginationMode, NSSharingServicePicker, NSView, NSWindow};
use objc2_foundation::{NSArray, NSBundle, NSError, NSOperationQueue, NSRectEdge, NSSize, NSString, NSURL};
use objc2_user_notifications::{
    UNAuthorizationOptions, UNMutableNotificationContent, UNNotification, UNNotificationPresentationOptions, UNNotificationRequest, UNNotificationResponse,
    UNNotificationSound, UNUserNotificationCenter, UNUserNotificationCenterDelegate,
};

use crate::appkit_views::ActionTarget;
use crate::view::{AnyView, Cx, Node, Size, View};

// ---- sharing ----

/// Something to share (see [`share_button`]).
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum ShareItem {
    /// Plain text.
    Text(String),
    /// A web address.
    Url(String),
    /// A file, shared as itself (AirDrop sends it, Mail attaches it).
    File(PathBuf),
}

pub(crate) fn share_objects(items: &[ShareItem]) -> Retained<NSArray<AnyObject>> {
    let objects: Vec<Retained<AnyObject>> = items
        .iter()
        .filter_map(|i| match i {
            ShareItem::Text(t) => Some(Retained::into_super(Retained::into_super(NSString::from_str(t)))),
            ShareItem::Url(u) => NSURL::URLWithString(&NSString::from_str(u)).map(|u| Retained::into_super(Retained::into_super(u))),
            ShareItem::File(p) => Some(Retained::into_super(Retained::into_super(NSURL::fileURLWithPath(&NSString::from_str(&p.to_string_lossy()))))),
        })
        .collect();
    NSArray::from_retained_slice(&objects)
}

/// A Share button (see [`share_button`]).
pub struct ShareButton {
    items: Items,
    title: Option<String>,
}

impl ShareButton {
    /// A titled button ("Share…") instead of the icon alone.
    pub fn title(mut self, title: &str) -> Self {
        self.title = Some(title.into());
        self
    }
}

type Items = Rc<dyn Fn() -> Vec<ShareItem>>;

thread_local! {
    /// What each share button shares, for checks.
    static SHARES: UiCell<Vec<(objc2::rc::Weak<NSButton>, Items)>> = const { UiCell::new(Vec::new()) };
}

/// What the share button `button` would share now.
pub(crate) fn shared_by(button: &NSButton) -> Option<Vec<ShareItem>> {
    let f = SHARES.with_borrow(|s| s.iter().find(|(b, _)| b.load().is_some_and(|b| std::ptr::eq(&*b, button))).map(|(_, f)| f.clone()))?;
    Some(f())
}

impl View for ShareButton {
    fn build(self, cx: &mut Cx) -> Node {
        let mtm = cx.mtm;
        let image = NSImage::imageWithSystemSymbolName_accessibilityDescription(&NSString::from_str("square.and.arrow.up"), Some(&NSString::from_str("Share")));
        let button = match (&self.title, image) {
            (None, Some(img)) => {
                // SAFETY: a plain image button; the target is set below.
                unsafe { NSButton::buttonWithImage_target_action(&img, None, None, mtm) }
            }
            (title, _) => {
                // SAFETY: as above.
                unsafe { NSButton::buttonWithTitle_target_action(&NSString::from_str(title.as_deref().unwrap_or("Share…")), None, None, mtm) }
            }
        };
        let picker: Rc<RefCell<Option<Retained<NSSharingServicePicker>>>> = Rc::default();
        let (items, b, held) = (self.items.clone(), objc2::rc::Weak::from_retained(&button), picker.clone());
        let target = ActionTarget::new(mtm, move || {
            let Some(b) = b.load() else { return };
            // SAFETY: an array of strings and URLs, which the picker takes.
            let p = unsafe { NSSharingServicePicker::initWithItems(NSSharingServicePicker::alloc(), &share_objects(&items())) };
            p.showRelativeToRect_ofView_preferredEdge(b.bounds(), &b, NSRectEdge::MinY);
            *held.borrow_mut() = Some(p);
        });
        // SAFETY: the target answers fire:; the node keeps it.
        unsafe {
            button.setTarget(Some(&target));
            button.setAction(Some(sel!(fire:)));
        }
        SHARES.with_borrow_mut(|s| {
            s.retain(|(b, _)| b.load().is_some());
            s.push((objc2::rc::Weak::from_retained(&button), self.items));
        });
        let (mut node, _) = Node::leaf(cx, Retained::into_super(Retained::into_super(button)), (false, false), Size::default());
        node.keep(Retained::into_super(target));
        node.hold(picker);
        node
    }
}

/// A button showing the system's Share menu (Mail, Messages, AirDrop,
/// Notes…) for what `items()` returns when it's clicked.
pub fn share_button(items: impl Fn() -> Vec<ShareItem> + 'static) -> ShareButton {
    ShareButton { items: Rc::new(items), title: None }
}

// ---- printing ----

/// Lays `view` out at the printable width of `info`'s page.
fn printable(view: AnyView, info: &NSPrintInfo) -> Retained<NSView> {
    let paper = info.paperSize();
    let width = (paper.width - info.leftMargin() - info.rightMargin()).max(100.0);
    let (root, size) = crate::host_view(view);
    // Paper is white: printed in the light appearance, whatever the screen's.
    if let Some(light) = objc2_app_kit::NSAppearance::appearanceNamed(unsafe { objc2_app_kit::NSAppearanceNameAqua }) {
        objc2_app_kit::NSAppearanceCustomization::setAppearance(&*root, Some(&light));
    }
    // Inside a batch (a button's action), the view's effects (labels'
    // text, charts' drawing) haven't run yet: run them, measure again, lay
    // out at the page's width, and run what that changed (charts' sizes).
    crate::signal::run_pending();
    let height = crate::hosted_ideal(&root).map_or(size.height, |s| s.height);
    root.setFrameSize(NSSize::new(width, height));
    crate::signal::run_pending();
    root
}

fn print_info() -> Retained<NSPrintInfo> {
    let info = NSPrintInfo::sharedPrintInfo();
    // SAFETY: NSPrintInfo copies as NSPrintInfo.
    let info: Retained<NSPrintInfo> = unsafe { msg_send![&*info, copy] };
    // Half-inch margins where the printer gives none (no printer set up).
    if info.leftMargin() < 18.0 {
        info.setLeftMargin(36.0);
        info.setRightMargin(36.0);
    }
    if info.topMargin() < 18.0 {
        info.setTopMargin(36.0);
        info.setBottomMargin(36.0);
    }
    info.setHorizontalPagination(NSPrintingPaginationMode::Fit);
    info.setVerticalPagination(NSPrintingPaginationMode::Automatic);
    info.setHorizontallyCentered(false);
    info.setVerticallyCentered(false);
    info
}

define_class!(
    // Frees a printed view's tree when its print sheet ends.
    #[unsafe(super(NSObject))]
    #[thread_kind = MainThreadOnly]
    #[name = "FerrokitPrintDone"]
    #[ivars = RefCell<Option<Retained<NSView>>>]
    struct PrintDone;

    impl PrintDone {
        #[unsafe(method(printOperationDidRun:success:contextInfo:))]
        fn done(&self, _op: &NSPrintOperation, _ok: Bool, _info: *mut std::ffi::c_void) {
            if let Some(root) = self.ivars().borrow_mut().take() {
                crate::drop_hosted(&root);
            }
            PRINTING.with_borrow_mut(|p| p.retain(|d| !std::ptr::eq(&**d, self)));
        }
    }
);

thread_local! {
    static PRINTING: UiCell<Vec<Retained<PrintDone>>> = const { UiCell::new(Vec::new()) };
}

/// Prints `view` laid out at the page's width, across as many pages as it
/// needs, with the Print sheet on `window` (printer, copies, Save as PDF).
///
/// The view is built for printing on its own: text, pictures, symbols,
/// charts, backgrounds and borders print; shadows and materials don't.
pub fn print(window: &NSWindow, title: &str, view: impl View) {
    let info = print_info();
    let root = printable(AnyView::new(view), &info);
    let op = NSPrintOperation::printOperationWithView_printInfo(&root, &info);
    op.setJobTitle(Some(&NSString::from_str(title)));
    let done = PrintDone::alloc(crate::mtm()).set_ivars(RefCell::new(Some(root)));
    // SAFETY: NSObject's designated initialiser.
    let done: Retained<PrintDone> = unsafe { msg_send![super(done), init] };
    PRINTING.with_borrow_mut(|p| p.push(done.clone()));
    // SAFETY: the delegate answers the selector and is kept until it runs.
    unsafe { op.runOperationModalForWindow_delegate_didRunSelector_contextInfo(window, Some(&done), Some(sel!(printOperationDidRun:success:contextInfo:)), std::ptr::null_mut()) };
}

/// Saves `view` as a PDF at `path`, on letter or A4 pages (the user's
/// default paper) as [`print()`] would print it, with no panel.
pub fn save_pdf(view: impl View, path: impl AsRef<Path>) -> std::io::Result<()> {
    let path = std::path::absolute(path)?;
    let info = print_info();
    // Printing to a file paginates; AppKit's PDF operation makes one page.
    // SAFETY: AppKit's job keys, with a string and a file URL.
    unsafe {
        info.setJobDisposition(objc2_app_kit::NSPrintSaveJob);
        let url = NSURL::fileURLWithPath(&NSString::from_str(&path.to_string_lossy()));
        info.dictionary().setObject_forKey(&*url, objc2::runtime::ProtocolObject::from_ref(objc2_app_kit::NSPrintJobSavingURL));
    }
    let root = printable(AnyView::new(view), &info);
    let op = NSPrintOperation::printOperationWithView_printInfo(&root, &info);
    op.setShowsPrintPanel(false);
    op.setShowsProgressPanel(false);
    let ok = op.runOperation();
    crate::drop_hosted(&root);
    if ok && path.exists() { Ok(()) } else { Err(std::io::Error::other("printing to PDF failed")) }
}

/// `view` as a PDF (see [`save_pdf`]).
pub fn pdf_data(view: impl View) -> Vec<u8> {
    thread_local!(static N: Cell<u32> = const { Cell::new(0) });
    let n = N.with(|c| c.replace(c.get() + 1));
    let path = std::env::temp_dir().join(format!("ferrokit-print-{}-{n}.pdf", std::process::id()));
    let data = save_pdf(view, &path).and_then(|()| std::fs::read(&path)).unwrap_or_default();
    let _ = std::fs::remove_file(&path);
    data
}

// ---- notifications ----

/// A notification (see [`notify`]).
pub struct Notification {
    id: String,
    title: String,
    subtitle: String,
    body: String,
    sound: bool,
    daily: Option<(u32, u32)>,
    on_click: Option<Rc<dyn Fn()>>,
}

impl Notification {
    /// A notification titled `title`.
    pub fn new(title: &str) -> Self {
        thread_local!(static NEXT: Cell<u64> = const { Cell::new(1) });
        let n = NEXT.with(|c| c.replace(c.get() + 1));
        Notification { id: format!("ferrokit-{n}"), title: title.into(), subtitle: String::new(), body: String::new(), sound: false, daily: None, on_click: None }
    }

    /// A second line under the title, in bold.
    pub fn subtitle(mut self, text: &str) -> Self {
        self.subtitle = text.into();
        self
    }

    /// The message.
    pub fn body(mut self, text: &str) -> Self {
        self.body = text.into();
        self
    }

    /// Plays the system's notification sound.
    pub fn sound(mut self) -> Self {
        self.sound = true;
        self
    }

    /// Its identifier: a new notification with the same one replaces it in
    /// Notification Center (a download's progress, today's summary).
    pub fn id(mut self, id: &str) -> Self {
        self.id = id.into();
        self
    }

    /// Shows it every day at `hour:minute` local time (a habit's reminder)
    /// instead of now, until [`cancel_notification`] with its
    /// [`id`](Self::id). Posting another with the same id replaces it.
    pub fn daily_at(mut self, hour: u32, minute: u32) -> Self {
        self.daily = Some((hour.min(23), minute.min(59)));
        self
    }

    /// Runs `action` when the user clicks the notification (the app comes
    /// to the front first), while the app is still running.
    pub fn on_click(mut self, action: impl Fn() + 'static) -> Self {
        self.on_click = Some(Rc::new(crate::signal::with_current_owner(action)));
        self
    }
}

/// A sent notification's text, as [`testing::sent_notifications`](crate::testing::sent_notifications) reports it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SentNotification {
    /// Its identifier.
    pub id: String,
    /// Its title.
    pub title: String,
    /// Its subtitle.
    pub subtitle: String,
    /// Its message.
    pub body: String,
    /// Whether it plays a sound.
    pub sound: bool,
    /// The time it repeats at every day, if it does.
    pub daily: Option<(u32, u32)>,
}

thread_local! {
    static CLICKS: UiCell<HashMap<String, Rc<dyn Fn()>>> = UiCell::new(HashMap::new());
    static DELEGATE: UiCell<Option<Retained<NotificationDelegate>>> = const { UiCell::new(None) };
    pub(crate) static HOLD: Cell<bool> = const { Cell::new(false) };
    pub(crate) static SENT: UiCell<Vec<SentNotification>> = const { UiCell::new(Vec::new()) };
}

define_class!(
    // Shows notifications while the app is in front too, and routes clicks
    // to their actions. UserNotifications may call it on any thread.
    #[unsafe(super(NSObject))]
    #[name = "FerrokitNotifications"]
    struct NotificationDelegate;

    unsafe impl NSObjectProtocol for NotificationDelegate {}

    unsafe impl UNUserNotificationCenterDelegate for NotificationDelegate {
        #[unsafe(method(userNotificationCenter:willPresentNotification:withCompletionHandler:))]
        fn will_present(&self, _c: &UNUserNotificationCenter, _n: &UNNotification, done: &block2::DynBlock<dyn Fn(UNNotificationPresentationOptions)>) {
            done.call((UNNotificationPresentationOptions::Banner | UNNotificationPresentationOptions::List | UNNotificationPresentationOptions::Sound,));
        }

        #[unsafe(method(userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:))]
        fn received(&self, _c: &UNUserNotificationCenter, response: &UNNotificationResponse, done: &block2::DynBlock<dyn Fn()>) {
            let id = response.notification().request().identifier().to_string();
            let block = RcBlock::new(move || {
                crate::failure::guard("a notification's click", false, || click(&id));
            });
            // SAFETY: the block runs on the main queue, where the actions live.
            unsafe { NSOperationQueue::mainQueue().addOperationWithBlock(&block) };
            done.call(());
        }
    }
);

/// Runs the click action of the notification `id` (on the main thread).
pub(crate) fn click(id: &str) -> bool {
    let action = CLICKS.with_borrow(|c| c.get(id).cloned());
    if let Some(a) = &action {
        crate::signal::batch(|| a());
    }
    action.is_some()
}

/// Whether this app can post notifications: it must run from an app
/// bundle with a bundle identifier (a plain `cargo run` binary can't).
pub fn notifications_supported() -> bool {
    NSBundle::mainBundle().bundleIdentifier().is_some()
}

/// Posts `notification` through Notification Center. The first time, macOS
/// asks the user whether to allow the app's notifications; while the app is
/// in front they show as banners too. Without an app bundle
/// ([`notifications_supported`]) it does nothing.
pub fn notify(notification: Notification) {
    if let Some(a) = &notification.on_click {
        CLICKS.with_borrow_mut(|c| c.insert(notification.id.clone(), a.clone()));
    }
    if HOLD.with(Cell::get) {
        let n = &notification;
        SENT.with_borrow_mut(|s| s.retain(|old| old.id != n.id));
        SENT.with_borrow_mut(|s| s.push(SentNotification { id: n.id.clone(), title: n.title.clone(), subtitle: n.subtitle.clone(), body: n.body.clone(), sound: n.sound, daily: n.daily }));
        return;
    }
    if !notifications_supported() {
        return;
    }
    let center = UNUserNotificationCenter::currentNotificationCenter();
    DELEGATE.with_borrow_mut(|d| {
        if d.is_none() {
            // SAFETY: NSObject's designated initialiser.
            let delegate: Retained<NotificationDelegate> = unsafe { msg_send![NotificationDelegate::alloc(), init] };
            center.setDelegate(Some(ProtocolObject::from_ref(&*delegate)));
            *d = Some(delegate);
        }
    });
    let content = UNMutableNotificationContent::new();
    content.setTitle(&NSString::from_str(&notification.title));
    content.setSubtitle(&NSString::from_str(&notification.subtitle));
    content.setBody(&NSString::from_str(&notification.body));
    if notification.sound {
        content.setSound(Some(&UNNotificationSound::defaultSound()));
    }
    let trigger = notification.daily.map(|(hour, minute)| {
        let when = objc2_foundation::NSDateComponents::new();
        when.setHour(hour as isize);
        when.setMinute(minute as isize);
        Retained::into_super(objc2_user_notifications::UNCalendarNotificationTrigger::triggerWithDateMatchingComponents_repeats(&when, true))
    });
    let request = UNNotificationRequest::requestWithIdentifier_content_trigger(&NSString::from_str(&notification.id), &content, trigger.as_deref());
    // Asking again is free once the user has answered; the request goes
    // out when they have.
    let block = RcBlock::new(move |granted: Bool, _e: *mut NSError| {
        if granted.as_bool() {
            UNUserNotificationCenter::currentNotificationCenter().addNotificationRequest_withCompletionHandler(&request, None);
        }
    });
    center.requestAuthorizationWithOptions_completionHandler(UNAuthorizationOptions::Alert | UNAuthorizationOptions::Sound, &block);
}

/// Removes the notification `id`: a daily one stops repeating, and one
/// already shown leaves Notification Center.
pub fn cancel_notification(id: &str) {
    CLICKS.with_borrow_mut(|c| c.remove(id));
    if HOLD.with(Cell::get) {
        SENT.with_borrow_mut(|s| s.retain(|n| n.id != id));
        return;
    }
    if !notifications_supported() {
        return;
    }
    let center = UNUserNotificationCenter::currentNotificationCenter();
    let ids = NSArray::from_retained_slice(&[NSString::from_str(id)]);
    center.removePendingNotificationRequestsWithIdentifiers(&ids);
    center.removeDeliveredNotificationsWithIdentifiers(&ids);
}

// ---- files ----

thread_local! {
    /// What `reveal_in_finder` was asked to show, while checks capture it
    /// (`testing::capture_reveals`) instead of opening Finder windows.
    static REVEALS: UiCell<Option<Vec<Vec<PathBuf>>>> = const { UiCell::new(None) };
}

pub(crate) fn capture_reveals(on: bool) {
    REVEALS.with_borrow_mut(|r| *r = on.then(Vec::new));
}

pub(crate) fn take_reveals() -> Vec<Vec<PathBuf>> {
    REVEALS.with_borrow_mut(|r| r.as_mut().map(std::mem::take).unwrap_or_default())
}

/// Shows `paths` selected in the Finder (File ▸ Show in Finder, ⌘R in
/// many apps): one Finder window per folder, the items selected, the
/// Finder brought to the front.
pub fn reveal_in_finder<P: AsRef<Path>>(paths: &[P]) {
    let paths: Vec<PathBuf> = paths.iter().map(|p| p.as_ref().to_path_buf()).collect();
    if REVEALS.with_borrow_mut(|r| r.as_mut().map(|r| r.push(paths.clone())).is_some()) {
        return;
    }
    let urls: Vec<Retained<NSURL>> = paths.iter().map(|p| NSURL::fileURLWithPath(&NSString::from_str(&p.to_string_lossy()))).collect();
    objc2_app_kit::NSWorkspace::sharedWorkspace().activateFileViewerSelectingURLs(&NSArray::from_retained_slice(&urls));
}

/// Moves `path` (a file or a folder) to the Trash of its volume, as the
/// Finder's Move to Trash does, and returns where it is now: keep it to
/// put the item back (undo) with [`std::fs::rename`] to the old path. The
/// name in the Trash can differ from the old one when the Trash already
/// has an item with that name.
pub fn move_to_trash(path: impl AsRef<Path>) -> std::io::Result<PathBuf> {
    let path = path.as_ref();
    if std::fs::symlink_metadata(path).is_err() {
        return Err(std::io::Error::new(std::io::ErrorKind::NotFound, format!("{} doesn't exist", path.display())));
    }
    let url = NSURL::fileURLWithPath(&NSString::from_str(&path.to_string_lossy()));
    let mut result: Option<Retained<NSURL>> = None;
    objc2_foundation::NSFileManager::defaultManager()
        .trashItemAtURL_resultingItemURL_error(&url, Some(&mut result))
        .map_err(|e| std::io::Error::other(e.localizedDescription().to_string()))?;
    result.and_then(|u| u.path()).map(|p| PathBuf::from(p.to_string())).ok_or_else(|| std::io::Error::other("the Trash didn't say where the item went"))
}

/// A file's dates (see [`file_dates`]); `None` where the file system
/// doesn't keep one.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub struct FileDates {
    /// When its content last changed.
    pub modified: Option<std::time::SystemTime>,
    /// When it was made.
    pub created: Option<std::time::SystemTime>,
    /// When it was put in its folder (moved, copied or downloaded there).
    /// Copying files to a new Mac, or restoring them, resets it.
    pub added: Option<std::time::SystemTime>,
    /// When it was last opened in an app (the date Spotlight calls Last
    /// Opened, `kMDItemLastUsedDate`); `None` if never.
    pub last_opened: Option<std::time::SystemTime>,
}

/// The dates of the file or folder at `path` (not following a symbolic
/// link): for "last opened" lists, "date added" columns and the like.
/// Reading them is cheap (no Spotlight query): a few thousand files take a
/// few milliseconds.
pub fn file_dates(path: impl AsRef<Path>) -> std::io::Result<FileDates> {
    use std::os::unix::ffi::OsStrExt;
    let path = path.as_ref();
    let meta = std::fs::symlink_metadata(path)?;
    let c = std::ffi::CString::new(path.as_os_str().as_bytes()).map_err(std::io::Error::other)?;
    let at = |secs: i64| (secs > 0).then(|| std::time::UNIX_EPOCH + std::time::Duration::from_secs(secs as u64));
    Ok(FileDates { modified: meta.modified().ok(), created: meta.created().ok(), added: date_added(&c).and_then(at), last_opened: last_opened(&c).and_then(at) })
}

/// The extended attribute macOS keeps the last-opened date in (a
/// `timespec`, seconds first, little-endian).
const LAST_USED: &std::ffi::CStr = c"com.apple.lastuseddate#PS";

fn last_opened(path: &std::ffi::CStr) -> Option<i64> {
    let mut value = [0u8; 16];
    // SAFETY: NUL-terminated strings; `value` has room for the length given.
    let n = unsafe { libc::getxattr(path.as_ptr(), LAST_USED.as_ptr(), value.as_mut_ptr().cast(), value.len(), 0, libc::XATTR_NOFOLLOW) };
    (n >= 8).then(|| i64::from_le_bytes(value[..8].try_into().expect("8 bytes")))
}

fn date_added(path: &std::ffi::CStr) -> Option<i64> {
    // The reply: its length, then the attribute.
    #[repr(C, packed(4))]
    struct Reply {
        length: u32,
        added: libc::timespec,
    }
    // SAFETY: plain C structs, valid zeroed.
    let mut list: libc::attrlist = unsafe { std::mem::zeroed() };
    list.bitmapcount = libc::ATTR_BIT_MAP_COUNT;
    list.commonattr = libc::ATTR_CMN_ADDEDTIME;
    let mut reply: Reply = unsafe { std::mem::zeroed() };
    // SAFETY: `list` asks for one timespec, which `reply` has room for.
    let r = unsafe { libc::getattrlist(path.as_ptr(), (&raw mut list).cast(), (&raw mut reply).cast(), size_of::<Reply>(), libc::FSOPT_NOFOLLOW) };
    let (length, secs) = (reply.length, reply.added.tv_sec);
    (r == 0 && length as usize >= size_of::<Reply>()).then_some(secs)
}

/// Sets a file's last-opened date, for tests (`None` removes it).
pub(crate) fn set_last_opened(path: &Path, secs: Option<i64>) -> std::io::Result<()> {
    use std::os::unix::ffi::OsStrExt;
    let c = std::ffi::CString::new(path.as_os_str().as_bytes()).map_err(std::io::Error::other)?;
    // SAFETY: NUL-terminated strings; a buffer of the length given.
    let r = match secs {
        Some(s) => {
            let mut value = [0u8; 16];
            value[..8].copy_from_slice(&s.to_le_bytes());
            unsafe { libc::setxattr(c.as_ptr(), LAST_USED.as_ptr(), value.as_ptr().cast(), value.len(), 0, libc::XATTR_NOFOLLOW) }
        }
        None => unsafe { libc::removexattr(c.as_ptr(), LAST_USED.as_ptr(), libc::XATTR_NOFOLLOW) },
    };
    if r == 0 { Ok(()) } else { Err(std::io::Error::last_os_error()) }
}

#[cfg(test)]
mod file_tests {
    use super::*;

    #[test]
    fn dates_of_a_file() {
        let dir = std::env::temp_dir().join(format!("ferrokit-dates-{}", std::process::id()));
        std::fs::create_dir_all(&dir).unwrap();
        let file = dir.join("a.txt");
        std::fs::write(&file, "a").unwrap();
        let fresh = file_dates(&file).unwrap();
        let now = std::time::SystemTime::now();
        let recent = |t: Option<std::time::SystemTime>| t.is_some_and(|t| now.duration_since(t).map_or(true, |d| d.as_secs() < 60));
        assert!(recent(fresh.modified) && recent(fresh.created) && recent(fresh.added), "{fresh:?}");
        assert_eq!(fresh.last_opened, None);
        set_last_opened(&file, Some(1_600_000_000)).unwrap();
        assert_eq!(file_dates(&file).unwrap().last_opened, Some(std::time::UNIX_EPOCH + std::time::Duration::from_secs(1_600_000_000)));
        assert!(file_dates(dir.join("missing")).is_err());
        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn reveals_are_captured() {
        capture_reveals(true);
        reveal_in_finder(&["/tmp/a", "/tmp/b"]);
        assert_eq!(take_reveals(), vec![vec![PathBuf::from("/tmp/a"), PathBuf::from("/tmp/b")]]);
        capture_reveals(false);
    }

    #[test]
    fn trashing_a_missing_file_fails() {
        assert_eq!(move_to_trash("/nonexistent/ferrokit-nothing").unwrap_err().kind(), std::io::ErrorKind::NotFound);
    }
}