ferrokit 0.11.0

Native macOS GUI framework for Rust: every view a real AppKit control (NSTableView, NSOutlineView, NSTextView), 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
//! A native grid of items (`NSCollectionView`): galleries, file browsers,
//! dashboards. Only the items on screen have views; cells are made once
//! and refilled as they scroll in, so a grid of 100,000 items costs about
//! as much as the screenful you see.
//!
//! Each item shows a fixed template: an image (an SF Symbol, a file's icon
//! or a colour swatch), a title and an optional subtitle. Items are keyed by
//! id, so the selection follows them when the list changes.

use std::cell::{Cell, RefCell};
use std::path::PathBuf;
use std::rc::{Rc, Weak};

use objc2::rc::Retained;
use objc2::runtime::{NSObject, NSObjectProtocol, ProtocolObject};
use objc2::{ClassType, DefinedClass, MainThreadMarker, MainThreadOnly, define_class, msg_send};
use objc2_app_kit::{
    NSAccessibility, NSIndexPathNSCollectionViewAdditions,
    NSBox, NSBoxType, NSCollectionView, NSCollectionViewDataSource, NSCollectionViewDelegate, NSCollectionViewFlowLayout, NSCollectionViewItem,
    NSColor, NSEvent, NSImage, NSImageScaling, NSImageView, NSScrollView, NSTextAlignment, NSTextField, NSView, NSViewController, NSWorkspace,
};
use objc2_foundation::{NSEdgeInsets, NSIndexPath, NSInteger, NSPoint, NSRect, NSSet, NSSize, NSString};

use ferrokit_core::text::Color;
use crate::signal::{Effect, Signal, untrack};
use crate::view::{Cx, Identifiable, Kind, Node, Size, View};

/// What an item's image is.
#[derive(Debug, Clone, PartialEq)]
pub enum ItemImage {
    /// An SF Symbol by name.
    Symbol(String),
    /// The Finder's icon for a file or folder.
    FileIcon(PathBuf),
    /// A swatch of a colour.
    Color(Color),
    /// No image.
    None,
}

/// What a collection shows for one item.
#[derive(Debug, Clone, PartialEq)]
pub struct CollectionItem {
    /// The name under the image (and what VoiceOver reads).
    pub title: String,
    /// A second, smaller line.
    pub subtitle: Option<String>,
    /// The image.
    pub image: ItemImage,
}

impl CollectionItem {
    /// An item with a title and an image.
    pub fn new(title: impl Into<String>, image: ItemImage) -> Self {
        CollectionItem { title: title.into(), subtitle: None, image }
    }

    /// Adds a second line.
    pub fn subtitle(mut self, text: impl Into<String>) -> Self {
        self.subtitle = Some(text.into());
        self
    }
}

#[derive(Default)]
struct State {
    entries: Vec<CollectionItem>,
    /// Model indices → (selected now) called with the selected indices.
    on_select: Option<Rc<dyn Fn(Vec<usize>)>>,
    on_activate: Option<Box<dyn Fn(usize)>>,
    /// Set while the collection changes its selection itself.
    quiet: bool,
}

thread_local! {
    /// Cells made so far, over all collections (a diagnostic: they're reused).
    static CELLS_MADE: Cell<u64> = const { Cell::new(0) };
}

/// Collection cells made since launch: stays near a screenful however many
/// items scroll past (diagnostic).
pub fn collection_cells_made() -> u64 {
    CELLS_MADE.with(Cell::get)
}

const CELL_ID: &str = "ferrokit.cell";

struct CellParts {
    highlight: Retained<NSBox>,
    swatch: Retained<NSBox>,
    image: Retained<NSImageView>,
    title: Retained<NSTextField>,
    subtitle: Retained<NSTextField>,
    /// The item's model index, and where to send double-clicks.
    index: Cell<usize>,
    state: RefCell<Weak<RefCell<State>>>,
}

define_class!(
    // A cell's view: lays out its parts and turns double-clicks into
    // activation.
    #[unsafe(super(NSView))]
    #[thread_kind = MainThreadOnly]
    #[name = "FerrokitCellView"]
    #[ivars = CellParts]
    struct CellView;

    impl CellView {
        #[unsafe(method(isFlipped))]
        fn is_flipped(&self) -> bool {
            true
        }

        #[unsafe(method(layout))]
        fn layout(&self) {
            crate::failure::guard("a collection (layout)", (), || {
                let p = self.ivars();
                let b = self.bounds().size;
                let line = 17.0;
                let lines = if p.subtitle.isHidden() { 1.0 } else { 2.0 };
                let image_h = (b.height - 8.0 - lines * line - 4.0).max(0.0);
                p.highlight.setFrame(NSRect::new(NSPoint::new(0.0, 0.0), b));
                let art = NSRect::new(NSPoint::new(6.0, 4.0), NSSize::new((b.width - 12.0).max(0.0), image_h));
                p.image.setFrame(art);
                p.swatch.setFrame(art);
                p.title.setFrame(NSRect::new(NSPoint::new(2.0, 4.0 + image_h + 2.0), NSSize::new((b.width - 4.0).max(0.0), line)));
                p.subtitle.setFrame(NSRect::new(NSPoint::new(2.0, 4.0 + image_h + 2.0 + line), NSSize::new((b.width - 4.0).max(0.0), line)));
            })
        }

        #[unsafe(method(mouseDown:))]
        fn mouse_down(&self, event: &NSEvent) {
            crate::failure::guard("a collection (mouse_down)", (), || {
                // SAFETY: the superclass handles the click (selection) first.
                let () = unsafe { msg_send![super(self), mouseDown: event] };
                if event.clickCount() == 2 {
                    activate(&self.ivars().state.borrow(), self.ivars().index.get());
                }
            })
        }
    }
);

fn activate(state: &Weak<RefCell<State>>, index: usize) {
    let Some(state) = state.upgrade() else { return };
    // Take the callback out while it runs: it may change the items.
    let f = state.borrow_mut().on_activate.take();
    if let Some(f) = &f {
        crate::failure::entry("a collection's activation", (), || f(index));
    }
    if state.borrow().on_activate.is_none() {
        state.borrow_mut().on_activate = f;
    }
}

fn label(mtm: MainThreadMarker, small: bool) -> Retained<NSTextField> {
    let l = NSTextField::labelWithString(&NSString::from_str(""), mtm);
    l.setAlignment(NSTextAlignment::Center);
    l.setLineBreakMode(objc2_app_kit::NSLineBreakMode::ByTruncatingMiddle);
    if small {
        l.setTextColor(Some(&NSColor::secondaryLabelColor()));
        l.setFont(Some(&objc2_app_kit::NSFont::systemFontOfSize(objc2_app_kit::NSFont::smallSystemFontSize())));
    }
    l
}

fn filled_box(mtm: MainThreadMarker, radius: f64) -> Retained<NSBox> {
    let b = NSBox::new(mtm);
    b.setBoxType(NSBoxType::Custom);
    b.setBorderWidth(0.0);
    b.setCornerRadius(radius);
    b.setTitlePosition(objc2_app_kit::NSTitlePosition::NoTitle);
    b.setFillColor(&NSColor::clearColor());
    b
}

define_class!(
    // One reusable cell: built once in loadView, refilled per item.
    #[unsafe(super(NSCollectionViewItem, NSViewController))]
    #[thread_kind = MainThreadOnly]
    #[name = "FerrokitCollectionCell"]
    struct GridCell;

    impl GridCell {
        #[unsafe(method(loadView))]
        fn load_view(&self) {
            crate::failure::guard("a collection (load_view)", (), || {
                let mtm = MainThreadMarker::new().expect("main thread");
                CELLS_MADE.with(|c| c.set(c.get() + 1));
                let parts = CellParts {
                    highlight: filled_box(mtm, 8.0),
                    swatch: filled_box(mtm, 6.0),
                    image: NSImageView::new(mtm),
                    title: label(mtm, false),
                    subtitle: label(mtm, true),
                    index: Cell::new(0),
                    state: RefCell::new(Weak::new()),
                };
                parts.image.setImageScaling(NSImageScaling::ScaleProportionallyUpOrDown);
                let view = CellView::alloc(mtm).set_ivars(parts);
                // SAFETY: NSView's designated initialiser.
                let view: Retained<CellView> = unsafe { msg_send![super(view), initWithFrame: NSRect::ZERO] };
                let p = view.ivars();
                for v in [&**p.highlight as &NSView, &p.swatch, &p.image, &p.title, &p.subtitle] {
                    view.addSubview(v);
                }
                // VoiceOver reads the cell as one element named by its title.
                view.setAccessibilityElement(true);
                view.setAccessibilityRole(Some(unsafe { objc2_app_kit::NSAccessibilityImageRole }));
                // Its parts are hidden (on their cells too: macOS 27 reads
                // cell-based controls through the cell).
                for v in [&**p.image as &NSView, &p.title, &p.subtitle, &p.swatch, &p.highlight] {
                    <crate::platform::Native as crate::platform::Platform>::set_accessibility_element(v, false);
                }
                self.setView(&view);
            })
        }

        #[unsafe(method(setSelected:))]
        fn set_selected(&self, selected: bool) {
            crate::failure::guard("a collection (set_selected)", (), || {
                // SAFETY: NSCollectionViewItem's own setter first.
                let () = unsafe { msg_send![super(self), setSelected: selected] };
                if let Some(v) = self.cell_view() {
                    let fill = if selected { NSColor::selectedContentBackgroundColor() } else { NSColor::clearColor() };
                    v.ivars().highlight.setFillColor(&fill);
                    let text = if selected { NSColor::alternateSelectedControlTextColor() } else { NSColor::labelColor() };
                    v.ivars().title.setTextColor(Some(&text));
                }
            })
        }
    }
);

impl GridCell {
    fn cell_view(&self) -> Option<Retained<CellView>> {
        self.view().downcast::<CellView>().ok()
    }

    fn fill(&self, item: &CollectionItem, index: usize, state: &Weak<RefCell<State>>) {
        let Some(v) = self.cell_view() else { return };
        let p = v.ivars();
        p.index.set(index);
        *p.state.borrow_mut() = state.clone();
        p.title.setStringValue(&NSString::from_str(&item.title));
        p.subtitle.setStringValue(&NSString::from_str(item.subtitle.as_deref().unwrap_or("")));
        p.subtitle.setHidden(item.subtitle.is_none());
        let (image, swatch) = match &item.image {
            ItemImage::Symbol(name) => (NSImage::imageWithSystemSymbolName_accessibilityDescription(&NSString::from_str(name), None), None),
            ItemImage::FileIcon(path) => (Some(NSWorkspace::sharedWorkspace().iconForFile(&NSString::from_str(&path.to_string_lossy()))), None),
            ItemImage::Color(c) => (None, Some(c.ns())),
            ItemImage::None => (None, None),
        };
        p.image.setImage(image.as_deref());
        p.image.setHidden(image.is_none());
        p.swatch.setHidden(swatch.is_none());
        if let Some(c) = &swatch {
            p.swatch.setFillColor(c);
        }
        let name = match &item.subtitle {
            Some(s) => format!("{}, {s}", item.title),
            None => item.title.clone(),
        };
        v.setAccessibilityLabel(Some(&NSString::from_str(&name)));
        v.setNeedsLayout(true);
    }
}

define_class!(
    // The grid: Return activates the selected item.
    #[unsafe(super(NSCollectionView))]
    #[thread_kind = MainThreadOnly]
    #[name = "FerrokitCollectionView"]
    #[ivars = RefCell<Weak<RefCell<State>>>]
    struct GridView;

    impl GridView {
        #[unsafe(method(keyDown:))]
        fn key_down(&self, event: &NSEvent) {
            crate::failure::guard("a collection (key_down)", (), || {
                let ret = matches!(event.keyCode(), 36 | 76);
                let first = self.selectionIndexPaths().iter().map(|p| p.item() as usize).min();
                match (ret, first) {
                    (true, Some(i)) => activate(&self.ivars().borrow(), i),
                    // SAFETY: the superclass handles every other key (arrows move the selection).
                    _ => unsafe { msg_send![super(self), keyDown: event] },
                }
            })
        }
    }
);

define_class!(
    // Data source and delegate of one grid.
    #[unsafe(super(NSObject))]
    #[thread_kind = MainThreadOnly]
    #[name = "FerrokitCollectionSource"]
    #[ivars = Weak<RefCell<State>>]
    struct GridSource;

    unsafe impl NSObjectProtocol for GridSource {}

    unsafe impl NSCollectionViewDataSource for GridSource {
        #[unsafe(method(collectionView:numberOfItemsInSection:))]
        fn count(&self, _cv: &NSCollectionView, _section: NSInteger) -> NSInteger {
            crate::failure::guard("a collection (count)", 0, || {
                self.ivars().upgrade().map_or(0, |s| s.borrow().entries.len()) as NSInteger
            })
        }

        #[unsafe(method_id(collectionView:itemForRepresentedObjectAtIndexPath:))]
        fn item(&self, cv: &NSCollectionView, path: &NSIndexPath) -> Retained<NSCollectionViewItem> {
            let made = cv.makeItemWithIdentifier_forIndexPath(&NSString::from_str(CELL_ID), path);
            crate::failure::guard("a collection (item)", (), || {
                if let (Ok(cell), Some(state)) = (made.clone().downcast::<GridCell>(), self.ivars().upgrade()) {
                    let index = path.item() as usize;
                    if let Some(entry) = state.borrow().entries.get(index) {
                        cell.fill(entry, index, self.ivars());
                    }
                }
            });
            made
        }
    }

    unsafe impl NSCollectionViewDelegate for GridSource {
        #[unsafe(method(collectionView:didSelectItemsAtIndexPaths:))]
        fn did_select(&self, cv: &NSCollectionView, _paths: &NSSet<NSIndexPath>) {
            crate::failure::guard("a collection (did_select)", (), || {
                self.selection_changed(cv);
            })
        }

        #[unsafe(method(collectionView:didDeselectItemsAtIndexPaths:))]
        fn did_deselect(&self, cv: &NSCollectionView, _paths: &NSSet<NSIndexPath>) {
            crate::failure::guard("a collection (did_deselect)", (), || {
                self.selection_changed(cv);
            })
        }
    }
);

impl GridSource {
    fn selection_changed(&self, cv: &NSCollectionView) {
        let Some(state) = self.ivars().upgrade() else { return };
        if state.borrow().quiet {
            return;
        }
        let mut rows: Vec<usize> = cv.selectionIndexPaths().iter().map(|p| p.item() as usize).collect();
        rows.sort_unstable();
        // Taken out first: what it sets may reload the collection, which
        // borrows the state.
        let on_select = state.borrow().on_select.clone();
        if let Some(f) = on_select {
            crate::failure::entry("a collection's selection", (), || f(rows));
        }
    }
}

type Describe<T> = Rc<dyn Fn(&T) -> CollectionItem>;

/// A grid of items (see [`collection`]).
pub struct Collection<T: Identifiable + 'static> {
    items: Box<dyn Fn() -> Vec<T>>,
    describe: Describe<T>,
    item_size: (f64, f64),
    selection: Option<Signal<Vec<T::Id>>>,
    multiple: bool,
    on_activate: Option<Rc<dyn Fn(T)>>,
}

impl<T: Identifiable + Clone + 'static> Collection<T> {
    /// The size of each item, in points (default 120 × 110).
    pub fn item_size(mut self, width: f64, height: f64) -> Self {
        self.item_size = (width, height);
        self
    }

    /// Two-way selection by item id (one item; see [`multiple`](Self::multiple)).
    pub fn selection(mut self, selected: &Signal<Vec<T::Id>>) -> Self {
        self.selection = Some(*selected);
        self
    }

    /// Lets the user select several items (⌘-click, ⇧-click, drag).
    pub fn multiple(mut self) -> Self {
        self.multiple = true;
        self
    }

    /// Runs `f` with an item when it's double-clicked, or Return is pressed
    /// on it.
    pub fn on_activate(mut self, f: impl Fn(T) + 'static) -> Self {
        self.on_activate = Some(Rc::new(f));
        self
    }
}

impl<T: Identifiable + Clone + 'static> View for Collection<T> {
    fn build(self, cx: &mut Cx) -> Node {
        let mtm = cx.mtm;
        let state = Rc::new(RefCell::new(State::default()));
        let grid = GridView::alloc(mtm).set_ivars(RefCell::new(Rc::downgrade(&state)));
        // SAFETY: NSView's designated initialiser.
        let grid: Retained<GridView> = unsafe { msg_send![super(grid), initWithFrame: NSRect::ZERO] };
        let cv: Retained<NSCollectionView> = Retained::into_super(grid);
        let layout = NSCollectionViewFlowLayout::new(mtm);
        layout.setItemSize(NSSize::new(self.item_size.0, self.item_size.1));
        layout.setMinimumInteritemSpacing(8.0);
        layout.setMinimumLineSpacing(8.0);
        layout.setSectionInset(NSEdgeInsets { top: 12.0, left: 12.0, bottom: 12.0, right: 12.0 });
        cv.setCollectionViewLayout(Some(&layout));
        // SAFETY: GridCell is an NSCollectionViewItem subclass.
        unsafe { cv.registerClass_forItemWithIdentifier(Some(GridCell::class()), &NSString::from_str(CELL_ID)) };
        cv.setSelectable(true);
        cv.setAllowsMultipleSelection(self.multiple);
        cv.setAllowsEmptySelection(true);
        cv.setBackgroundColors(Some(&objc2_foundation::NSArray::from_retained_slice(&[NSColor::controlBackgroundColor()])));

        let source = GridSource::alloc(mtm).set_ivars(Rc::downgrade(&state));
        // SAFETY: NSObject's designated initialiser.
        let source: Retained<GridSource> = unsafe { msg_send![super(source), init] };
        // Data source and delegate are weak in AppKit; the node keeps the source.
        cv.setDataSource(Some(ProtocolObject::from_ref(&*source)));
        cv.setDelegate(Some(ProtocolObject::from_ref(&*source)));
        let scroll = NSScrollView::new(mtm);
        scroll.setDocumentView(Some(&cv));
        scroll.setHasVerticalScroller(true);
        scroll.setAutohidesScrollers(true);

        // The items (typed) and their ids, index-aligned with the entries.
        let values: Rc<RefCell<Vec<T>>> = Rc::default();
        if let Some(sel) = &self.selection {
            let (sel, values) = (*sel, values.clone());
            state.borrow_mut().on_select = Some(Rc::new(move |rows| {
                let ids = {
                    let v = values.borrow();
                    rows.iter().filter_map(|&i| v.get(i).map(Identifiable::id)).collect()
                };
                sel.set(ids);
            }));
        }
        if let Some(f) = self.on_activate.clone() {
            let values = values.clone();
            state.borrow_mut().on_activate = Some(Box::new(move |i| {
                let item = values.borrow().get(i).cloned();
                if let Some(item) = item {
                    f(item);
                }
            }));
        }

        let (mut node, _) = Node::leaf(cx, Retained::into_super(scroll), (true, true), Size { w: 240.0, h: 200.0 });
        if let Kind::Leaf(l) = &node.kind {
            l.content.set(Some(Size { w: 240.0, h: 200.0 }));
        }

        // Selects the items whose ids the signal holds (quietly): when the
        // signal changes, and after a reload (which clears the selection).
        let restore: Rc<dyn Fn()> = {
            let (st, values, cv, sel) = (state.clone(), values.clone(), cv.clone(), self.selection);
            Rc::new(move || {
                let Some(sel) = &sel else { return };
                let want = sel.get();
                untrack(|| {
                    // Looked up in a set: by scanning, selecting everything
                    // in a large collection took items × selected steps.
                    let wanted: std::collections::HashSet<&T::Id> = want.iter().collect();
                    let rows: Vec<usize> = values.borrow().iter().enumerate().filter(|(_, v)| wanted.contains(&v.id())).map(|(i, _)| i).collect();
                    let mut now: Vec<usize> = cv.selectionIndexPaths().iter().map(|p| p.item() as usize).collect();
                    now.sort_unstable();
                    if now != rows {
                        st.borrow_mut().quiet = true;
                        let paths: Vec<Retained<NSIndexPath>> = rows.iter().map(|&i| NSIndexPath::indexPathForItem_inSection(i as NSInteger, 0)).collect();
                        let set = NSSet::from_retained_slice(&paths);
                        cv.setSelectionIndexPaths(&set);
                        st.borrow_mut().quiet = false;
                    }
                });
            })
        };

        // Reload when the items' signals change.
        let (items_fn, describe) = (self.items, self.describe);
        let (st, vals, cv2, restore2) = (state.clone(), values.clone(), cv.clone(), restore.clone());
        node.own(Effect::new(move || {
            let items = items_fn();
            untrack(|| {
                let entries: Vec<CollectionItem> = items.iter().map(|i| describe(i)).collect();
                *vals.borrow_mut() = items;
                st.borrow_mut().entries = entries;
                st.borrow_mut().quiet = true;
                cv2.reloadData();
                st.borrow_mut().quiet = false;
                restore2();
            });
        }));
        if self.selection.is_some() {
            node.own(Effect::new(move || restore()));
        }
        node.keep(Retained::into_super(source));
        node.hold(state);
        node
    }
}

/// A native grid: `items` (reading signals) gives the items, `describe`
/// what each shows (see [`CollectionItem`]).
pub fn collection<T: Identifiable + Clone + 'static>(items: impl Fn() -> Vec<T> + 'static, describe: impl Fn(&T) -> CollectionItem + 'static) -> Collection<T> {
    Collection { items: Box::new(items), describe: Rc::new(describe), item_size: (120.0, 110.0), selection: None, multiple: false, on_activate: None }
}