Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Wrapping any NSView in a Rust UI

Updated 2026-11-23 · Ferrokit 0.15 · Markdown

Ferrokit wraps a lot of AppKit, but not every view, and not the frameworks beside it: MapKit’s map, SceneKit, PDFKit’s thumbnails, a third-party view, or a view your app draws itself. native(..) takes any NSView you make with the objc2 bindings and makes it a Ferrokit view: it’s laid out with the others, updates from signals, takes part in focus and VoiceOver, and is torn down with its window.

use ferrokit::*;
use objc2_app_kit::NSLevelIndicator;

fn main() {
    let level = Signal::new(3.0);
    App::new("Meter")
        .window(Window::new("Meter").size(320.0, 120.0).view(
            vstack((
                // Any NSView, made once when the window is built…
                native(|mtm| {
                    let meter = NSLevelIndicator::new(mtm);
                    meter.setMaxValue(5.0);
                    meter
                })
                // …and changed in place whenever a signal it reads changes.
                .update(move |meter| meter.setDoubleValue(level.get()))
                .measure(|_| Size { w: 120.0, h: 18.0 })
                .flexible(false, false)
                .accessibility_label("Signal strength"),
                slider(&level, 0.0, 5.0).accessibility_label("Level"),
            ))
            .padding(20.0),
        ))
        .run();
}

The objc2 crates go in your Cargo.toml, with the features for the classes you call:

[dependencies]
ferrokit = "0.15"
objc2-app-kit = { version = "0.3", features = ["NSResponder", "NSView", "NSControl", "NSLevelIndicator"] }
  • native(make) calls make(mtm) once, on the main thread, when the window is built.
  • .update(f) runs f as an effect: now, and again whenever a signal it read changes. Give unrelated properties their own updates.
  • .measure(..) and .flexible(..) size it; .keep(delegate) keeps a delegate alive (AppKit holds delegates weakly); .on_remove(..) cleans up.
  • A wrapped view is a leaf: Ferrokit views can’t go inside it.

The native_views example wraps MKMapView and a clock face the app draws itself:

git clone https://github.com/lhassa8/ferrokit && cd ferrokit && cargo run -p ferrokit --example native_views

Custom native views has delegates, drawing, focus and accessibility. For a SwiftUI view (Swift Charts, MapKit’s Map), see SwiftUI views in a Rust Mac app.