A real NSTableView from Rust: a million rows
You can drive AppKit’s NSTableView from Rust directly with the objc2-app-kit bindings: implement NSTableViewDataSource and NSTableViewDelegate with define_class!, make cell views, keep the delegate alive, and handle sorting and selection yourself. That works, and it’s the right choice for one table in an otherwise non-AppKit app.
Ferrokit does that part for you. Its table is a real NSTableView, virtualised (only the rows on screen have views), fed from a model of your own Rust rows:
use std::rc::Rc;
use ferrokit::*;
#[derive(Clone)]
struct Order {
id: u32,
customer: String,
total: f64,
paid: bool,
}
fn sample(n: u32) -> Vec<Order> {
let names = ["Ada", "Grace", "Linus", "Margaret", "Ken"];
(1..=n)
.map(|id| Order {
id,
customer: names[id as usize % names.len()].to_string(),
total: f64::from(id % 9_973) / 10.0,
paid: id % 3 != 0,
})
.collect()
}
fn main() {
let all = Rc::new(sample(1_000_000));
let query = Signal::new(String::new());
// Created once; the columns read from your own row type.
let model = Rc::new(TableModel::new(
vec![
Column::number("id", "Order", |o: &Order| f64::from(o.id)).decimals(0),
Column::text("customer", "Customer", |o: &Order| o.customer.clone()).width(200.0),
Column::number("total", "Total", |o: &Order| o.total).decimals(2),
Column::check("paid", "Paid", |o: &Order| o.paid),
],
));
let m = model.clone();
App::new("Orders")
.window(Window::new("Orders").size(640.0, 480.0).view(
vstack((
search_field(&query).accessibility_label("Search customers"),
table(&model)
// Row numbers are positions in what you gave set_rows,
// whatever the user sorted by.
.on_activate(|row| println!("opened model row {row}"))
.accessibility_label("Orders"),
// Runs again whenever `query` changes and updates the
// table in place.
watch(move || {
let q = query.get().to_lowercase();
let rows = all
.iter()
.filter(|o| q.is_empty() || o.customer.to_lowercase().contains(&q))
.cloned()
.collect();
m.set_rows(rows);
}),
))
.padding(12.0),
))
.run();
}
What you get without writing it:
- Scrolling and keyboard are the table’s own: arrow keys, Page Up and Down, type-to-select, ⌘A, ⌘-click and ⇧-click.
- Sorting: clicking a header sorts by that column (numbers as numbers, dates as dates). Row numbers in every event stay model indices, positions in what you gave
set_rows, whatever the sort. - A million rows: only the rows on screen have views; 200,000 rows load in about 5 ms.
set_rowsreplaces the rows in place;update_row,push,insertandremovechange single rows. - Copy and drag: ⌘C copies the selected rows as tab-separated text that Numbers and Excel paste as cells;
.row_drag()drags rows out. - VoiceOver reads it as a table, row by row and cell by cell.
More: editable cells, date and progress columns, icons, grouped rows with floating headers, a focused cell as in a spreadsheet, row menus, drag to reorder, and files dropped from the Finder. All in Tables, outlines and editors, with NSOutlineView for trees and NSBrowser for column views.
To see a million rows for yourself:
git clone https://github.com/lhassa8/ferrokit && cd ferrokit && cargo run --release -p ferrokit --example sql_browser
When to use objc2 directly instead
- Your app’s UI is drawn by something else (egui, Iced, a game engine) and you need one AppKit view inside it: Ferrokit runs the whole window, so it doesn’t fit there.
- You need an
NSTableViewfeature Ferrokit doesn’t expose. Then you can still use Ferrokit and reach for the bindings for that one part:native(..)wraps anyNSView.