use ferrokit::*;
#[derive(Clone)]
struct Settings {
name: Signal<String>,
email: Signal<String>,
password: Signal<String>,
theme: Signal<Appearance>,
notify: Signal<bool>,
sounds: Signal<bool>,
volume: Signal<f64>,
keep_days: Signal<f64>,
syncing: Signal<bool>,
status: Signal<String>,
}
impl Settings {
fn new() -> Self {
Settings {
name: Signal::new("Ada Lovelace".into()),
email: Signal::new("ada@example.com".into()),
password: Signal::new(String::new()),
theme: Signal::new(Appearance::System),
notify: Signal::new(true),
sounds: Signal::new(false),
volume: Signal::new(0.6),
keep_days: Signal::new(30.0),
syncing: Signal::new(false),
status: Signal::new("Not synced yet".into()),
}
}
fn email_ok(&self) -> bool {
self.email.with(|e| e.split_once('@').is_some_and(|(user, host)| !user.is_empty() && host.contains('.')))
}
fn sync(&self) {
self.syncing.set(true);
self.status.set("Syncing…".into());
let s = self.clone();
spawn(|| std::thread::sleep(std::time::Duration::from_millis(300)), move |_| {
s.syncing.set(false);
s.status.set(format!("Synced {} settings", 7));
});
}
}
fn account(s: &Settings) -> Section {
let s1 = s.clone();
section("Account", vec![
form_row("Name", text_field(&s.name).placeholder("Full name")),
form_row("Email", text_field(&s.email).placeholder("name@example.com")),
form_row("Password", secure_field(&s.password).placeholder("Required")),
form_content(text(move || if s1.email_ok() { String::new() } else { "Enter a valid email address.".into() })),
])
}
fn appearance(s: &Settings) -> Section {
let options = vec![("System", Appearance::System), ("Light", Appearance::Light), ("Dark", Appearance::Dark)];
section("Appearance", vec![form_row("Theme", picker(&s.theme, options).accessibility_label("Theme"))])
}
fn notifications(s: &Settings) -> Section {
let days = s.keep_days;
section("Notifications", vec![
form_content(toggle("Show notifications", &s.notify)),
form_content(checkbox("Play sounds", &s.sounds)),
form_row("Volume", hstack((image("speaker.fill"), slider(&s.volume, 0.0, 1.0)))),
form_row("Keep for", hstack((stepper(&s.keep_days, 1.0, 365.0, 1.0), text(move || format!("{} days", days.get()))))).help("Older notifications are deleted."),
])
}
fn sync_row(s: &Settings) -> impl View {
let (s1, s2, s3, s4) = (s.clone(), s.clone(), s.clone(), s.clone());
hstack((
button("Sync Now", move || s1.sync()).disabled(move || s2.syncing.get()),
progress(move || if s3.syncing.get() { None } else { Some(1.0) }),
text(move || s4.status.get()),
))
}
fn settings_view(s: &Settings) -> impl View {
let s1 = s.clone();
let can_save = move || s1.email_ok() && !s1.name.with(String::is_empty);
let theme = s.theme;
vstack((
form(vec![account(s), appearance(s), notifications(s)]).style(FormStyle::Columns),
watch(move || set_appearance(theme.get())),
divider(),
sync_row(s),
spacer(),
hstack((spacer(), button("Save", || {}).disabled(move || !can_save()).default_action())),
))
.padding(20.0)
}
fn main() {
let s = Settings::new();
let view = settings_view(&s);
let checks = std::env::var("CHECK").is_ok_and(|v| v == "all");
App::new("Settings")
.window(Window::new("Settings").size(460.0, 560.0).view(view))
.on_launch(move || {
if checks {
self_checks::run(s);
}
})
.run();
}
mod self_checks {
use std::cell::Cell;
use ferrokit::testing::{ax_tree, has_ax, post_edit, shortcut, snapshot_windows};
use ferrokit::*;
use objc2::rc::Retained;
use objc2_app_kit::{NSAppearanceCustomization, NSButton, NSSecureTextField, NSTextField, NSView};
use objc2_foundation::NSString;
use super::Settings;
thread_local! {
static FAILED: Cell<u32> = const { Cell::new(0) };
}
fn check(name: &str, pass: bool, note: String) {
eprintln!("[check] {name} {} {note}", if pass { "PASS" } else { "FAIL" });
if !pass {
FAILED.with(|f| f.set(f.get() + 1));
}
}
fn views() -> Vec<Retained<NSView>> {
windows()[0].ns().contentView().unwrap().subviews().to_vec()
}
fn fields() -> Vec<Retained<NSTextField>> {
views().into_iter().filter_map(|v| v.downcast::<NSTextField>().ok()).filter(|f| f.isEditable()).collect()
}
fn button(title: &str) -> Retained<NSButton> {
views().into_iter().filter_map(|v| v.downcast::<NSButton>().ok()).find(|b| b.title().to_string() == title).unwrap()
}
fn label_with(text: &str) -> Option<Retained<NSTextField>> {
views().into_iter().filter_map(|v| v.downcast::<NSTextField>().ok()).find(|f| f.stringValue().to_string().contains(text))
}
pub fn run(s: Settings) {
let w = windows()[0].clone();
w.ns().makeKeyAndOrderFront(None);
let labels: Vec<_> = ["Name:", "Email:", "Password:"].iter().map(|t| label_with(t).unwrap().frame()).collect();
let right = |r: &objc2_foundation::NSRect| r.origin.x + r.size.width;
let f = fields();
check(
"form-layout",
labels.iter().all(|r| (right(r) - right(&labels[0])).abs() < 0.5) && f.iter().take(3).all(|x| x.frame().origin.x == f[0].frame().origin.x),
format!("label right edges {:?}; fields start at x {:.0}", labels.iter().map(right).collect::<Vec<_>>(), f[0].frame().origin.x),
);
let save = button("Save");
let ok_before = save.isEnabled();
post_edit(&f[1], "ada@");
let hint = label_with("valid email").is_some();
let disabled = !save.isEnabled();
post_edit(&f[1], "ada@example.org");
check(
"validation",
ok_before && hint && disabled && save.isEnabled() && label_with("valid email").is_none(),
format!("Save enabled {ok_before} → {} (bad email, hint shown {hint}) → {}", !disabled, save.isEnabled()),
);
let secure = views().into_iter().find_map(|v| v.downcast::<NSSecureTextField>().ok()).unwrap();
w.ns().makeFirstResponder(Some(&f[0]));
let tab = |field: &NSTextField| {
if let Some(e) = field.currentEditor() {
let _: () = unsafe { objc2::msg_send![&*e, insertTab: Option::<&objc2::runtime::AnyObject>::None] };
}
};
tab(&f[0]);
let at_email = f[1].currentEditor().is_some();
tab(&f[1]);
check("tab-order", at_email && secure.currentEditor().is_some(), format!("Name → Email: {at_email}; Email → Password: {}", secure.currentEditor().is_some()));
w.ns().makeFirstResponder(Some(&f[0]));
let all = shortcut(&w, "a", false) && f[0].currentEditor().is_some_and(|e| e.selectedRange().length == s.name.get_untracked().chars().count());
check("select-all", all, "⌘A selects the whole name".into());
let tree = ax_tree(&w);
let roles = [
("AXTextField", ""),
("AXTextField", "AXSecureTextField"),
("AXPopUpButton", ""),
("AXCheckBox", ""),
("AXSlider", ""),
("AXIncrementor", ""),
("AXButton", ""),
("AXProgressIndicator", ""),
];
let missing: Vec<_> = roles.iter().filter(|(r, sr)| !has_ax(&tree, r, sr, "")).collect();
check(
"accessibility-tree",
missing.is_empty() && tree.iter().any(|(_, sr, _)| sr == "AXSwitch") && has_ax(&tree, "AXSlider", "", "Volume"),
format!("{} elements; missing {missing:?}", tree.len()),
);
let problems = ferrokit::testing::ax_audit(&windows()[0]);
check("accessibility-audit", problems.is_empty(), format!("controls VoiceOver can't name: {problems:?}"));
unsafe { button("Sync Now").performClick(None) };
let busy = s.syncing.get_untracked() && !button("Sync Now").isEnabled();
let s2 = s.clone();
let poll = std::rc::Rc::new(Cell::new(0));
fn wait(s: Settings, busy: bool, tries: std::rc::Rc<Cell<u32>>) {
later(move || {
if s.syncing.get_untracked() && tries.get() < 2000 {
tries.set(tries.get() + 1);
std::thread::sleep(std::time::Duration::from_millis(1));
wait(s.clone(), busy, tries.clone());
return;
}
check(
"sync",
busy && !s.syncing.get_untracked() && label_with("Synced 7").is_some() && button("Sync Now").isEnabled(),
format!("busy while running: {busy}; status {:?}", s.status.get_untracked()),
);
let s = s.clone();
spawn(|| std::thread::sleep(std::time::Duration::from_millis(2500)), move |()| {
progress_on_screen();
let s = s.clone();
later(move || appearance(s.clone()));
});
});
}
wait(s2, busy, poll);
}
fn progress_on_screen() {
let bar = views().into_iter().find(|v| v.class().name().to_string_lossy() == "NSProgressIndicator").unwrap();
let w = bar.window().unwrap();
let Some(rep) = ferrokit::testing::capture_window(&WindowRef::from_ns(&w)) else {
return check("progress-on-screen", true, "skipped: no capture of the window's content (screen recording not allowed here)".into());
};
let rows = ferrokit::testing::captured_pixels(&rep, &bar, 1);
let filled = |(r, g, b): (f64, f64, f64)| r.max(g).max(b) - r.min(g).min(b) > 0.15 || (0.45..0.8).contains(&((r + g + b) / 3.0));
let columns = rows.first().map_or(0, Vec::len);
let full = (0..columns).filter(|&x| rows.iter().any(|row| filled(row[x]))).count();
let share = full as f64 / columns.max(1) as f64;
if let (true, Ok(dir)) = (share <= 0.9, std::env::var("CHECK_ALL_LOGS")) {
let png = unsafe { rep.representationUsingType_properties(objc2_app_kit::NSBitmapImageFileType::PNG, &objc2_foundation::NSDictionary::new()) };
if let Some(png) = png {
png.writeToFile_atomically(&NSString::from_str(&format!("{dir}/settings-progress.png")), true);
}
}
check("progress-on-screen", share > 0.9, format!("{:.0}% of the bar's width filled on screen, 2.5 s after the sync ({columns} px)", share * 100.0));
}
fn appearance(s: Settings) {
let names = unsafe {
objc2_foundation::NSArray::from_slice(&[objc2_app_kit::NSAppearanceNameAqua, objc2_app_kit::NSAppearanceNameDarkAqua])
};
let dark_count = || {
views()
.iter()
.filter(|v| {
v.effectiveAppearance()
.bestMatchFromAppearancesWithNames(&names)
.is_some_and(|n: Retained<NSString>| n.to_string().contains("Dark"))
})
.count()
};
batch(|| s.theme.set(Appearance::Light));
let snap = std::env::var("SNAP").ok();
if let Some(d) = &snap {
snapshot_windows(&format!("{d}/light"));
}
let light = dark_count();
batch(|| s.theme.set(Appearance::Dark));
if let Some(d) = &snap {
snapshot_windows(&format!("{d}/dark"));
}
let dark = dark_count();
let total = views().len();
batch(|| s.theme.set(Appearance::System));
check("theme-picker", light == 0 && dark == total, format!("Light: {light} of {total} views dark; Dark: {dark} of {total}"));
let failed = FAILED.with(Cell::get);
eprintln!("[check] done, {failed} failed");
std::process::exit(if failed == 0 { 0 } else { 1 });
}
}