The app shell
Menus
Every app gets the standard menu bar. Add your own menus with App::menu;
a menu named “File” is merged above the standard Close item.
use ferrokit::*;
let items = Signal::new(0);
let show_sidebar = Signal::new(true);
App::new("Notes")
.menu("File", vec![
menu_item("New Note", move || items.update(|n| *n += 1)).key("n"), // ⌘N
menu_item("Delete Note", move || items.update(|n| *n -= 1))
.key_with("n", &[Modifier::Option]) // ⌥⌘N
.disabled(move || !(items.get() > 0)), // greyed out at 0
])
.menu("View", vec![
menu_item("Show Sidebar", move || show_sidebar.update(|s| *s = !*s))
.key_with("s", &[Modifier::Control])
.checked(move || show_sidebar.get()), // ✓ when shown
menu_separator(),
submenu("Sort By", vec![menu_item("Name", || {}), menu_item("Date", || {})]),
])
.window(Window::new("Notes"))
.run();
A menu item’s title, .disabled(..) and .checked(..) take a value or a
closure; closures are read when the menu opens or a shortcut is pressed. dynamic_submenu(title, || items) rebuilds its items
each time it opens, for menus that list current things (servers, recent
files).
Commands: menus that act on the focus
A menu item made with menu_command doesn’t run a fixed closure. It runs
the Command handler nearest the focus:
- the focused view’s handler, then each enclosing view’s up to the window’s root;
- then the window’s;
- then the app’s.
The item is enabled only while some handler answers, as with AppKit’s responder chain: below, ⌘K clears whichever field has focus, and is greyed out while the title field has focus and is already empty.
use ferrokit::*;
const CLEAR: Command = Command::new("clear");
const REFRESH: Command = Command::new("refresh");
let (title, tag) = (Signal::new(String::from("Groceries")), Signal::new(String::new()));
let view = vstack((
text_field(&title)
.accessibility_label("Title")
.on_command_when(CLEAR, move || !title.with(String::is_empty), move || title.set(String::new()))
.focused_value("field", "title"),
text_field(&tag)
.accessibility_label("Tag")
.on_command(CLEAR, move || tag.set(String::new()))
.focused_value("field", "tag"),
))
.padding(20.0);
App::new("Notes")
.menu("Note", vec![menu_command("Clear Field", CLEAR).key("k")]) // ⌘K clears the focused field
.on_command(REFRESH, || println!("refresh everything")) // app-wide fallback
.window(Window::new("Notes").view(view).toolbar(vec![toolbar_command("refresh", "Refresh", "arrow.clockwise", REFRESH)]))
.run();
on_command_when(cmd, enabled, action)answers but greys the item out whileenabledis false. Handlers further out aren’t asked.- A handler on a view answers only while the focus is in that view. With
the focus elsewhere, the command’s menu item and its
toolbar_commandbutton are greyed out unless the window or the app handles it too. Put Delete (⌘⌫) on the table, so it doesn’t take the key from a text field; for a toolbar button that works wherever the focus is, addWindow::on_command_when, or usetoolbar_buttonwith its own action. - Shortcut keys without a letter are their characters:
.key("\u{8}")is ⌘⌫,"\r"⌘↩,"\u{f700}"⌘↑ (the list is onMenuItem::key). Window::on_commandandApp::on_commandhandle a command for the whole window or app..focused_value(name, value)publishes a value to the focus (a selection signal, say).focused_value::<T>(name)reads the nearest one, for menu items that need to know what they act on.perform(cmd)andcan_perform(cmd)do the same from code;perform_in(window, cmd)names the window.- Commands go to the key window, or, when the app isn’t active, the most
recently opened visible window (
focused_window()).
Document apps
App::documents makes an app document-based, like TextEdit or Numbers.
Each window is one file. A document’s window comes back where it was
when the file is opened again, and if the user keeps windows when
quitting (System Settings › Desktop & Dock, “Close windows when quitting
an application” off), the documents open at quit reopen at the next
launch, as in Apple’s apps.
- The File menu: New (⌘N), Open… (⌘O), Open Recent, Save (⌘S), Save As… (⇧⌘S) and Revert to Saved.
- The title bar: shows the file’s name and proxy icon, and the dot in the close button while there are unsaved changes.
- Closing or quitting with changes: asks whether to save.
use ferrokit::*;
let text_files = DocumentType::new(&["txt", "md"], String::new, |doc: &Document<String>| {
let model = TextModel::new(&doc.content().get_untracked());
text_editor(&model).bind(doc.content()).accessibility_label("Text")
})
.read(|path| std::fs::read_to_string(path))
.write(|text, path| std::fs::write(path, text));
App::new("Notes").documents(text_files).run(); // starts with an untitled document
- A
Document<T>’s content is a signal: bind views todoc.content()or change it. The document is edited while the content differs from the file (soT: PartialEq): undoing back to the saved text clears the dot. - Without
.write, documents are read-only (a viewer: Save is disabled). .write_returning(|content, path| ..)is for content that saving changes: it returns what the document holds afterwards (a database with staged changes commits them and returns none staged), which becomes the saved state..related(&["-wal", "-journal"])names files beside each document that it reads and writes too (a database’s journal): the document holds access to them while it’s open, which the App Sandbox needs.- Undo is per document, since each window has its own history.
new_document(),open_document(path),save_document(window),save_document_as(window, path),revert_document(window),document_edited(window)andrecent_documents()do the same from code, without panels.choose_documents()shows the Open panel, as Open… does.DocumentType::window(|w| ..)styles each document window with [Window]’s chrome methods, e.g.|w| w.full_size_content()for a sidebar that runs up under the title bar.
A welcome window
App::welcome(make) shows a window at launch instead of an untitled
document, as Xcode does: recent documents, a sample, Open…. It closes when
a document opens, and Window ▸ Welcome to … (⇧⌘1) or show_welcome()
brings it back, built again so its list is current.
use ferrokit::*;
fn welcome() -> Window {
let recent: Vec<AnyView> = recent_documents()
.into_iter()
.map(|path| {
let name = path.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default();
AnyView::new(text(name).padding(6.0).hover_highlight(Color::Quaternary).on_click(move || {
open_document(&path).ok();
}))
})
.collect();
Window::new("Welcome").size(600.0, 400.0).view(hstack((
vstack((button("Open…", choose_documents), button("New", || {
new_document();
}))),
vstack(recent).width(240.0).material(Material::Sidebar),
)))
}
let text_files = DocumentType::new(&["txt"], String::new, |_: &Document<String>| text(""));
App::new("Notes").documents(text_files).welcome(welcome).run();
The SQL browser’s welcome window is a fuller one: a large symbol, rows that highlight under the pointer, and recent databases in a sidebar.
Files opened from the Finder (double-click, Open With, a drop on the Dock
icon, open file.txt) open as documents; an untouched Untitled window
from launch closes. For the Finder to offer your app, declare the types
in Cargo.toml, and cargo ferrokit bundle writes them into the app:
[[package.metadata.ferrokit.documents]]
name = "Plain Text"
extensions = ["txt", "md"]
The notes example is a complete text editor built this way.
Links into the app: URL schemes
An app can have its own kind of link, myapp://…, that opens it from a
web page, an email, another app or open in Terminal (sign-in callbacks,
“open this item” links). Declare the scheme in Cargo.toml; cargo ferrokit bundle writes it into the app’s Info.plist:
[package.metadata.ferrokit]
url-schemes = ["notes"]
App::on_open_url gets each link, whether it launched the app or came
while it ran, after the windows are up:
use ferrokit::*;
let shown = Signal::new(String::new());
App::new("Notes")
.on_open_url(move |url| {
// notes://open/42 opens note 42.
if let Some(id) = url.strip_prefix("notes://open/") {
shown.set(id.to_string());
}
})
.window(Window::new("Notes").view(text(move || shown.get())))
.run();
macOS learns the scheme the first time the bundled app runs (cargo ferrokit run); after that, a link starts the app if it isn’t running.
Until then, opening a link fails with “no application” (a bundle that’s
built but never run doesn’t count), and a bare cargo run never gets
links. When several copies of the app declare the same scheme (an old
build, a copy in /Applications), macOS picks one of them, not
necessarily the one you just built. The link arrives as macOS gives it, percent-encoded
(notes://open/my%20note); decode it with a crate such as url if you
need to. Anyone can send your app a link, so treat it like any other
untrusted input: check it before acting on it, and ask before anything
destructive.
Toolbar and split view
use ferrokit::*;
let query = Signal::new(String::new());
let window = Window::new("Library")
.size(900.0, 600.0)
.toolbar(vec![
toolbar_button("add", "Add", "plus", || {}),
toolbar_space(),
toolbar_search("search", &query),
])
.view(split((text("Sidebar"), text("Content"))).position(220.0).remember("library"));
let _ = window;
Toolbar items show their symbols alone, as in Apple’s apps; their labels
are what VoiceOver reads and what the overflow menu shows. With
ToolbarStyle::Expanded or Preference (a Settings window), the labels
show under the symbols.
A sidebar and an inspector: the window shell
Window::sidebar(view) and Window::inspector(view) lay the window out
as Mail, Notes and Keynote are: AppKit’s split view controller, with a
full-height sidebar on the sidebar material, the window’s view beside
it, and an inspector after it. The title starts beside the sidebar,
Toggle Sidebar leads the toolbar and Toggle Inspector ends it, and the
user can drag either pane wider, narrower or shut.
.sidebar_hidden(&signal) and .inspector_hidden(&signal) bind whether
they’re hidden, both ways.
use ferrokit::*;
let (no_sidebar, no_inspector) = (Signal::new(false), Signal::new(true));
let window = Window::new("Library")
.toolbar(vec![toolbar_button("add", "Add", "plus", || {})])
.sidebar(text("Mailboxes"))
.sidebar_hidden(&no_sidebar)
.view(text("Messages"))
.inspector(text("Details"))
.inspector_hidden(&no_inspector);
let _ = window;
Each pane has its own tree; a closed window leaves no more behind than a
plain AppKit one built the same way (about 1 KB). The split view below
is for splits inside a window’s content.
Groups, pickers and customisation
toolbar_group puts related buttons in one capsule (zoom out and in,
back and forward); toolbar_picker is a choice of one as segments,
bound to a signal (Finder’s view modes). toolbar_fixed_space() is a
fixed gap, toolbar_space() a flexible one.
Window::customizable_toolbar(name) lets the user change the toolbar:
Customize Toolbar… on its right-click menu, or customize_toolbar_item()
in a View menu, opens AppKit’s sheet, where items and spaces can be added,
removed and moved. Items marked .optional() are offered there without
being shown at first. The user’s layout is remembered under name;
until they change it, the app’s own layout shows (so a later version’s
new default still reaches them).
use ferrokit::*;
#[derive(Clone, Copy, PartialEq)]
enum Mode { List, Grid }
let (zoom, mode) = (Signal::new(1.0), Signal::new(Mode::List));
let window = Window::new("Photos")
.toolbar(vec![
toolbar_group("zoom", "Zoom", vec![
toolbar_button("out", "Zoom Out", "minus.magnifyingglass", move || zoom.update(|z| *z /= 1.25)),
toolbar_button("in", "Zoom In", "plus.magnifyingglass", move || zoom.update(|z| *z *= 1.25)),
]),
toolbar_space(),
toolbar_picker("mode", "View", &mode, vec![("List", "list.bullet", Mode::List), ("Grid", "square.grid.2x2", Mode::Grid)]),
toolbar_button("share", "Share", "square.and.arrow.up", || {}).optional(),
])
.customizable_toolbar("photos");
let _ = window;
.remember(name) keeps the divider’s position across launches;
Window::remember_frame(name) does the same for the window’s frame.
.stacked() puts the panes top to bottom (an editor over its results).
Sidebars and inspectors that collapse
.collapsible(index, &signal) hides a pane while the signal is true and
sets the signal when the user drags the pane shut. The first pane and
collapsible ones keep their size as the window resizes, and a pane reopens
at the size it had. .trailing(points) gives the last pane (an inspector)
its first width. The menu items follow the Mac convention: ⌃⌘S and ⌥⌘I,
titled “Hide…” or “Show…” by a closure as the title:
use ferrokit::*;
let (no_sidebar, no_inspector) = (Signal::new(false), Signal::new(false));
let app = App::new("Library")
.menu(
"View",
vec![
menu_item(move || format!("{} Sidebar", if no_sidebar.get() { "Show" } else { "Hide" }), move || no_sidebar.update(|h| *h = !*h))
.key_with("s", &[Modifier::Control]),
menu_item(move || format!("{} Inspector", if no_inspector.get() { "Show" } else { "Hide" }), move || no_inspector.update(|h| *h = !*h))
.key_with("i", &[Modifier::Option]),
],
)
.window(Window::new("Library").view(
split((text("Sidebar"), text("Content"), text("Inspector")))
.collapsible(0, &no_sidebar)
.collapsible(2, &no_inspector)
.position(220.0)
.trailing(260.0),
));
let _ = app;
In a document app, make these commands (see above) so they act on the focused window: the SQL browser example does.
Popovers
.popover(&shown, || content) shows content in a popover pointing at a
view while shown is true. Escape or a click outside closes it and sets
shown back to false. The content is built each time it opens and freed
when it closes. popover_on(Edge::Trailing, …) picks the side.
use ferrokit::*;
let shown = Signal::new(false);
let info = button("Info", move || shown.set(true))
.popover(&shown, || vstack((text("Version 1.0"), text("Made with ferrokit"))).padding(12.0));
let _ = info;
Sheets, alerts and panels
use ferrokit::*;
fn delete(window: &WindowRef) {
let alert_box = Alert::new("Delete the note?", "This can't be undone.").button("Delete").button("Cancel").destructive(0);
alert(window, alert_box, |index| if index == 0 { println!("deleted") });
}
fn rename(window: &WindowRef) {
// Built in `sheet_with`: the sheet's signals are freed when it closes.
let handle = sheet_with(window, || {
let name = Signal::new(String::from("Untitled"));
Window::new("Rename").size(320.0, 100.0).view(text_field(&name).accessibility_label("Name").padding(16.0))
}, || println!("sheet closed"));
// handle.close() ends it, e.g. from a Done button.
let _ = handle;
}
fn choose(window: &WindowRef) {
open_panel(window, OpenOptions::new().extensions(&["csv"]), |paths| {
println!("{paths:?}");
});
save_panel(window, "Untitled.csv", None, |path| println!("{path:?}"));
}
fn main() {}
A WindowRef is a handle to one of the app’s windows: open returns
one, windows() lists them and focused_window() gives the one commands
go to. It has the window’s title, size and close(); .ns() reaches
AppKit’s NSWindow for anything else.
Where signals live. A signal belongs to what was being built when it
was made, and goes with it: a window’s view, a row of a list. One made in
an action (a button, a menu item), outside any view, belongs to the window
or app the action is in, and lives as long as that. So build a sheet or a
new window with sheet_with(parent, || …) or open_with(|| …): the
signals its builder makes (the form’s fields) are freed when it closes.
The habits soak found New Habit keeping three signals per sheet before it
did this.
Settings
App::settings(window) adds Settings… (⌘,) to the app menu. The window is
built the first time it opens and kept; pair it with stored signals so
the values persist.
use ferrokit::*;
let dark_tables = stored("dark tables", false);
App::new("Viewer")
.settings(Window::new("Settings").size(360.0, 120.0).view(toggle("Dark tables", &dark_tables).padding(20.0)))
.window(Window::new("Viewer"))
.run();
Undo
Every window has its own undo history, shared by its text fields. Make your own changes undoable, and they appear in the Edit menu by name (“Undo Rename”):
use ferrokit::*;
let title = Signal::new(String::from("Draft"));
set_undoable("Rename", &title, "Final".into()); // ⌘Z puts "Draft" back
let count = Signal::new(0);
undoable("Add Item", move || count.update(|n| *n += 1), move || count.update(|n| *n -= 1));
Steps go to the frontmost window’s history (undoable_in names the
window). Closing a window clears its history.
Windows, appearance, quitting
open(window)opens a window; the close button, ⌘W andclose_windowtear it down.set_appearance(Appearance::Dark)pins the app’s look;Systemfollows the system setting.App::on_eventdeliversAppEvent::AppearanceChanged.quit()ends the app, as ⌘Q does (starting on the next run-loop pass, so a document app’s save prompts don’t block the UI).
Tabs, full screen and the Dock
use ferrokit::*;
App::new("Notes")
// The Dock icon's menu, built each time it opens.
.dock_menu(|| vec![menu_item("New Note", || {})])
.window(Window::new("Note").tabbing(Tabbing::Preferred, Some("notes")).view(text("…")))
.on_launch(|| set_dock_badge(Some("2")))
.run();
- Tabs:
.tabbing(Tabbing::Preferred, group)opens windows as tabs of the group’s other windows (Automaticfollows the user’s “Prefer tabs” setting;Disallowednever).open_tab(&window, Window::new(..))adds one. The Window menu’s tab commands work as in any Mac app. - Full screen: resizable windows can go full screen;
.no_full_screen()stops it for palettes and small utilities.toggle_full_screen(&w)andis_full_screen(&w)for your own commands. - Dock:
set_dock_badge(Some("3"))shows a badge on the Dock icon (Noneclears it);App::dock_menuadds items above the system’s.
Languages
Strings the user reads go through t, with the English text as the key.
The app’s translations are .strings tables, one per language:
use ferrokit::*;
App::new("Notes")
.translations(&[("de", "\"Run Query\" = \"Abfrage ausführen\";\n\"{n} row\" = \"{n} Zeile\";\n\"{n} rows\" = \"{n} Zeilen\";")])
.menu(&t("Query"), vec![menu_item(&t("Run Query"), || {})])
.window(Window::new(&t("Notes")).view(text(|| t_plural(3, "{n} row", "{n} rows"))))
.run();
In a real app the table is a file, include_str!("../locales/de.strings"):
"Run Query" = "Abfrage ausführen";
"{n} row" = "{n} Zeile";
"{n} rows" = "{n} Zeilen";
- The language is the first of the user’s preferred languages (System Settings, or the app’s own setting there) that the app has a table for. Strings without a translation stay in English.
- The framework’s own strings (the standard menus, document alerts, “Untitled”) come in German, French and Japanese. An app’s table can override them.
t_with("Open “{file}”", &[("file", &name)])fills named placeholders, which a translation can reorder.t_plural(n, "{n} row", "{n} rows")picks the language’s plural form. For languages with more than two (Russian, Polish), the table gives"{n} rows#one","#few","#many"and"#other".{n}is formatted for the user’s region, as isformat_number(value, decimals).format_date(seconds, template)writes a date as the user’s locale does from a template:"EEEEdMMMM"is “Friday, 31 October” in English and “Freitag, 31. Oktober” in German.today(),local_day(seconds),local_day_start(day)andweekday(day)count calendar days in the user’s time zone, through daylight saving: the key for “done today”, streaks and calendars.- Try another language without changing the system:
my-app -AppleLanguages "(de)". - In the bundle, list the languages under
languagesin[package.metadata.ferrokit]. macOS then shows AppKit’s own panels and menu items (Close All, the Open panel) in that language too.
Right-to-left languages
In Arabic, Hebrew, Persian or Urdu (an app localised in one, for a user
who prefers it), the layout mirrors, as AppKit’s own does: a row’s first
view is on the right, padding and spacing flip, a form’s labels sit to the
right of their controls, and a slide from the leading edge comes from the
right. AppKit mirrors the controls themselves (sliders, switches, the
window’s buttons). Text keeps its natural direction. right_to_left()
says which way the app lays out, for drawing of your own on a canvas.
To try a layout right to left without translating it, launch the app with
-AppleTextDirection YES -NSForceRightToLeftWritingDirection YES; checks
can call testing::set_right_to_left(Some(true)) before opening a window.