diff --git a/crates/snowbound/src/link.rs b/crates/snowbound/src/link.rs index 623de9a142ca1f804b42ff6198fc27c6fd0a42b9..c31a8a0941067782edf1228d502898e87887b2e1 100644 --- a/crates/snowbound/src/link.rs +++ b/crates/snowbound/src/link.rs @@ -100,9 +100,9 @@ impl State { } let theme = ui.theme.clone(); let row = theme.font_size * 2.0; - let fields = [text_field(), address_field(), search_field()]; + let owners = [id(), text_field(), address_field()]; let entered = - ui::popup::navigation(ui, &fields[..2], &[NamedKey::Enter]).contains(&NamedKey::Enter); + ui::popup::navigation(ui, &owners, &[NamedKey::Enter]).contains(&NamedKey::Enter); ui.open_as( id(), Spec { diff --git a/crates/snowbound/src/linux.rs b/crates/snowbound/src/linux.rs index e6d77b7938341ba157d4943dede2be255db11f94..edd1a48af24d9644365c8b9c9883ffe486e504f3 100644 --- a/crates/snowbound/src/linux.rs +++ b/crates/snowbound/src/linux.rs @@ -1693,6 +1693,9 @@ pub fn pick_new( folder: Option<&std::path::Path>, reply: Reply, ) { + if let Some(path) = crate::replayed_answer() { + return reply.send(path); + } use std::os::unix::ffi::OsStrExt; let mut options = HashMap::from([ ("current_name", Value::from(name.to_owned())), diff --git a/crates/snowbound/src/macos.rs b/crates/snowbound/src/macos.rs index 310a29ca2d1a2487de68d9f7da93929c86912d99..47a4e56fbbc0f9b8c54891e7814cf875d2e59b6f 100644 --- a/crates/snowbound/src/macos.rs +++ b/crates/snowbound/src/macos.rs @@ -888,6 +888,9 @@ pub fn pick_new( folder: Option<&std::path::Path>, reply: Reply, ) { + if let Some(path) = crate::replayed_answer() { + return reply.send(path); + } let mtm = MainThreadMarker::new().expect("Panels belong to the main thread"); unsafe { let panel = NSSavePanel::savePanel(mtm); diff --git a/crates/snowbound/src/main.rs b/crates/snowbound/src/main.rs index 87c773956015cd841993a37ed9ff5fab615b58c4..e4a03436b64f9ebfbb12b0ef6b5a19493332e832 100644 --- a/crates/snowbound/src/main.rs +++ b/crates/snowbound/src/main.rs @@ -432,6 +432,9 @@ enum Replay { ClosePane, /// Writes the settings as they would be saved, as a screenshot's replay saves none. Settings(PathBuf), + /// What the next save panel answers, in place of showing itself. + #[cfg(not(target_arch = "wasm32"))] + Answer(PathBuf), Quit, } @@ -452,6 +455,19 @@ fn automated() -> bool { AUTOMATED.load(Ordering::Relaxed) } +/// What a replay told the next save panel to answer. +#[cfg(not(target_arch = "wasm32"))] +static ANSWER: Mutex> = Mutex::new(None); + +/// The answer a replay gave the save panel about to show, taken. +#[cfg(not(target_arch = "wasm32"))] +fn replayed_answer() -> Option { + ANSWER + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner) + .take() +} + /// With `SNOWBOUND_PROFILE` set, prints how long a phase of the frame took since `start`. fn lap(phase: &str, start: Instant) { static PROFILE: std::sync::OnceLock = std::sync::OnceLock::new(); @@ -6775,6 +6791,12 @@ impl State { eprintln!("{error}"); } } + #[cfg(not(target_arch = "wasm32"))] + Replay::Answer(path) => { + *ANSWER + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner) = Some(path) + } } // A covered window gets no redraws, so each step draws its own frame. if let Err(error) = self.frame() { @@ -7272,6 +7294,7 @@ fn replay(script: String, proxy: EventLoopProxy) -> Result<(), Box Err(std::time::Duration::from_millis(rest.parse()?)), "quit" => Ok(Replay::Quit), + "answer" => Ok(Replay::Answer(rest.into())), "resize" => Ok(Replay::Resize( numbers()? .try_into() diff --git a/crates/snowbound/src/options.rs b/crates/snowbound/src/options.rs index ddcb6e3ec4b9cad86b4d28818f719d74f0ddb0fa..e06f7f0b6aeffe52dbf8fa3ff46816f1cdbbdcf9 100644 --- a/crates/snowbound/src/options.rs +++ b/crates/snowbound/src/options.rs @@ -425,7 +425,7 @@ impl State { let row = row_height(&theme); let ui = &mut self.ui; let column = labels_column(ui); - let entered = ui::popup::navigation(ui, &[user_name()], &[NamedKey::Enter]) + let entered = ui::popup::navigation(ui, &[id(), user_name()], &[NamedKey::Enter]) .contains(&NamedKey::Enter); // As tall as its contents up to the window, which the list then scrolls within. let loose = Extent { diff --git a/crates/snowbound/src/print.rs b/crates/snowbound/src/print.rs index e496b28fe0910a7b3bddcc58d7285a8cac5896d8..338d105121cd0e721bdb4ee17198c7a41c83d56c 100644 --- a/crates/snowbound/src/print.rs +++ b/crates/snowbound/src/print.rs @@ -169,7 +169,8 @@ impl State { session.tabs[session.tab].name.clone() }); let theme = ui.theme.clone(); - let entered = ui::popup::navigation(ui, &[], &[NamedKey::Enter]).contains(&NamedKey::Enter); + let entered = + ui::popup::navigation(ui, &[id()], &[NamedKey::Enter]).contains(&NamedKey::Enter); let title = if dialog.export { "Export as PDF" } else { diff --git a/crates/snowbound/src/properties.rs b/crates/snowbound/src/properties.rs index a2df2051173db40adef944ffba950395d187b6fb..c3e3db21d637085e7ac099dd1675a91f19172440 100644 --- a/crates/snowbound/src/properties.rs +++ b/crates/snowbound/src/properties.rs @@ -58,7 +58,7 @@ impl State { } let theme = ui.theme.clone(); let row = theme.font_size * 2.0; - let entered = ui::popup::navigation(ui, &[name_field()], &[NamedKey::Enter]) + let entered = ui::popup::navigation(ui, &[id(), name_field()], &[NamedKey::Enter]) .contains(&NamedKey::Enter); ui.open_as( id(), diff --git a/crates/snowbound/src/protection.rs b/crates/snowbound/src/protection.rs index b917d23c104ec689916f0c1f799cb631be642394..708dd8c1a0d32f7f5aae16fe73b38179cef1c353 100644 --- a/crates/snowbound/src/protection.rs +++ b/crates/snowbound/src/protection.rs @@ -388,7 +388,8 @@ impl State { ui.set_focus(Some(field(0))); } let entered = - ui::popup::navigation(ui, &owners, &[NamedKey::Enter]).contains(&NamedKey::Enter); + ui::popup::navigation(ui, &[&[id()], &owners[..]].concat(), &[NamedKey::Enter]) + .contains(&NamedKey::Enter); let protected = asking.library.protected(&asking.path); let title = match asking.dialog { Dialog::Unlock(..) => "Protected Section", diff --git a/crates/snowbound/src/save_as.rs b/crates/snowbound/src/save_as.rs index 17a1b3776232b6256897a1d6ee5d244228cde154..b880eef386fc45ffd26d863eb89669041ddbd4d4 100644 --- a/crates/snowbound/src/save_as.rs +++ b/crates/snowbound/src/save_as.rs @@ -85,7 +85,8 @@ impl State { } let theme = ui.theme.clone(); let row = theme.font_size * 2.0; - let entered = ui::popup::navigation(ui, &[], &[NamedKey::Enter]).contains(&NamedKey::Enter); + let entered = + ui::popup::navigation(ui, &[id()], &[NamedKey::Enter]).contains(&NamedKey::Enter); ui.open_as( id(), Spec { diff --git a/crates/snowbound/src/server.rs b/crates/snowbound/src/server.rs index d748e2afe55ddb803dc53edb6a3582c90c8327cd..a53c544d571106339b8de4b12f24160cf505dcb9 100644 --- a/crates/snowbound/src/server.rs +++ b/crates/snowbound/src/server.rs @@ -586,8 +586,9 @@ impl State { Step::SignIn => &[user_field(), password_field(), domain_field()], Step::Browse => &[], }; + let owners: Vec = [id()].into_iter().chain(fields.iter().copied()).collect(); let entered = - ui::popup::navigation(ui, fields, &[NamedKey::Enter]).contains(&NamedKey::Enter); + ui::popup::navigation(ui, &owners, &[NamedKey::Enter]).contains(&NamedKey::Enter); ui.open_as( id(), Spec { diff --git a/crates/snowbound/src/tags.rs b/crates/snowbound/src/tags.rs index 85cffd06ed6651a8215a896d49f637226d90ab5e..8430f0c1f43739c1093af7d4528bb8041c40d5ee 100644 --- a/crates/snowbound/src/tags.rs +++ b/crates/snowbound/src/tags.rs @@ -402,6 +402,8 @@ impl State { return; } let theme = ui.theme.clone(); + let entered = + ui::popup::navigation(ui, &[id()], &[NamedKey::Enter]).contains(&NamedKey::Enter); dialog(ui, id(), "Customize Tags", WIDTH); ui.leaf( "all", @@ -525,7 +527,9 @@ impl State { }); ui.close(); crate::buttons(ui); - let [cancel, ok] = ui::dialog_buttons(ui, "OK", list.tags != self.tags); + let changed = list.tags != self.tags; + let [cancel, ok] = ui::dialog_buttons(ui, "OK", changed); + let ok = ok || entered && changed; ui.close(); if let Some(place) = list.selected { if up { @@ -657,8 +661,8 @@ fn tag_editor( } let (tag, place) = editor.as_mut()?; let theme = ui.theme.clone(); - let entered = - ui::popup::navigation(ui, &[name_field()], &[NamedKey::Enter]).contains(&NamedKey::Enter); + let entered = ui::popup::navigation(ui, &[editor_id(), name_field()], &[NamedKey::Enter]) + .contains(&NamedKey::Enter); let title = if place.is_some() { "Modify Tag" } else { diff --git a/crates/snowbound/src/unpack.rs b/crates/snowbound/src/unpack.rs index 76d6b49de22c52cb2c5b418abb46b35ffd09bc42..85e549c7de44ee7a9a265ec2eac4aa9fab43e2a2 100644 --- a/crates/snowbound/src/unpack.rs +++ b/crates/snowbound/src/unpack.rs @@ -63,7 +63,7 @@ impl State { } let theme = ui.theme.clone(); let row = theme.font_size * 2.0; - let entered = ui::popup::navigation(ui, &[name_field()], &[NamedKey::Enter]) + let entered = ui::popup::navigation(ui, &[id(), name_field()], &[NamedKey::Enter]) .contains(&NamedKey::Enter); ui.open_as( id(), diff --git a/crates/snowbound/src/windows.rs b/crates/snowbound/src/windows.rs index 31207459d1e884ea7e1c588f7395c86db719a026..5c0f488728b5176758a4b40da5e92c18d4833e1e 100644 --- a/crates/snowbound/src/windows.rs +++ b/crates/snowbound/src/windows.rs @@ -1710,6 +1710,9 @@ pub fn pick_new( folder: Option<&std::path::Path>, reply: Reply, ) { + if let Some(path) = crate::replayed_answer() { + return reply.send(path); + } pick( title, &[("All files", "*.*")], diff --git a/crates/snowbound/tests/replay.rs b/crates/snowbound/tests/replay.rs index 89d1db5638bdbf547a78d0047dba74f2c40e8f19..6fdc9cc0bfc8e115bf922f3fdbdc4d8713dc71c4 100644 --- a/crates/snowbound/tests/replay.rs +++ b/crates/snowbound/tests/replay.rs @@ -1793,6 +1793,76 @@ fn closing_split_panes_leaves_the_window_as_it_was() { ); } +/// Opens Save As with its chord. +const SAVE_AS: [&str; 5] = [ + "modifiers shift command", + "key s", + "modifiers", + "settle", + "wait 300", +]; + +/// Enter takes a dialog's default button, which assistive technology is told is the default: +/// Save As saves the page where the save panel answers, and Export as PDF writes the PDF. +#[test] +fn enter_takes_the_default_button_of_save_as_and_export_as_pdf() { + let scratch = Scratch::new("dialog-enter"); + let notebook = + Path::new(env!("CARGO_MANIFEST_DIR")).join("../../corpus/cross-container/candidate"); + let (saved, pdf) = (scratch.0.join("Saved.one"), scratch.0.join("Exported.pdf")); + let answer_saved = format!("answer {}", saved.display()); + let answer_pdf = format!("answer {}", pdf.display()); + let mut steps: Vec<&str> = SAVE_AS.to_vec(); + steps.extend([ + "accessibility save-as", + &answer_saved, + "key Enter", + "settle", + "modifiers shift command", + "key p", + "modifiers", + "settle", + "type Export as PDF", + "settle", + "key Enter", + "settle", + "wait 300", + "accessibility export", + &answer_pdf, + "key Enter", + "settle", + "accessibility after", + ]); + let [save_as, export, after] = replay(&scratch, Some(¬ebook), &steps) + .try_into() + .unwrap(); + assert!(save_as.contains(r#"DefaultButton "Save As""#), "{save_as}"); + assert!(export.contains(r#"DefaultButton "Export""#), "{export}"); + assert!(!after.contains("Dialog"), "{after}"); + assert!(std::fs::metadata(&saved).is_ok_and(|file| file.len() > 0)); + assert!(std::fs::read(&pdf).is_ok_and(|bytes| bytes.starts_with(b"%PDF"))); +} + +/// Escape closes a dialog without doing what it offers. +#[test] +fn escape_closes_save_as() { + let scratch = Scratch::new("dialog-escape"); + let notebook = + Path::new(env!("CARGO_MANIFEST_DIR")).join("../../corpus/cross-container/candidate"); + let mut steps: Vec<&str> = SAVE_AS.to_vec(); + steps.extend([ + "accessibility open", + "key Escape", + "settle", + "accessibility closed", + ]); + let [open, closed] = replay(&scratch, Some(¬ebook), &steps) + .try_into() + .unwrap(); + assert!(open.contains(r#"Dialog "Save As""#), "{open}"); + assert!(!closed.contains(r#"Dialog "Save As""#), "{closed}"); +} + /// A layout of panes as the settings keep it, on pages of `notebook`'s sections: a pane on /// the first, then a part below it, the second above the third. A page of `None` is gone. fn layout(notebook: &Path, panes: [(&str, Option<&str>); 3]) -> serde_json::Value { diff --git a/crates/ui/src/widgets.rs b/crates/ui/src/widgets.rs index 705b7671088bc5879efea9fea4831433cef6ec8c..685a6a04b72922fa8e8425a54379b75171115341 100644 --- a/crates/ui/src/widgets.rs +++ b/crates/ui/src/widgets.rs @@ -54,7 +54,7 @@ pub fn dialog_buttons(ui: &mut Ui, text: &str, enabled: bool) -> [bool; 2] { radius: 4.0, pad: [theme.font_size * 0.75, 0.0], center: true, - role: Some(Role::Button), + role: Some(Role::DefaultButton), ..Spec::default() }; let clicked = ui.leaf("default", spec).clicked; diff --git a/tools/canvas/README.md b/tools/canvas/README.md index b60fc7997d88127950ceb950f794ee250a5eb56b..1419143420ed1d710a8db5bfcb31cc431cedf1c6 100644 --- a/tools/canvas/README.md +++ b/tools/canvas/README.md @@ -56,7 +56,7 @@ As in OneNote 2010, Notebook Recycle Bin (File menu, a notebook's context menu, The `ui` crate builds every frame from the application's state. A cache keyed by stable box ids keeps hover, press, focus, scroll and animation values, and the previous frame's layout routes the frame's input before building, so a frame answers input one layout late and paints with its own. Sizes are solved per axis after building: fixed, label-sized, a fraction of an ancestor, or the sum of children, with overflow shared out by each box's strictness. The page is a custom box: `ui` routes it the pointer, wheel, key and input-method events that land on it, in order, and the host hands them to `PageView` and paints the page in a clipped layer of the same frame. Page scrollbars are `ui` widgets over that box; the canvas reports only its scroll bounds. Text fields edit through `draw::edit`, as the page does, so keys, clicks and drags select and move the same way in both. Work the frame asks for (page requests, saving) runs after the frame is painted and requests the frame that shows it. Opening a section or page reads and lays it out on a thread of its own while the current page stays live; the newest replaces the page once the pictures it shows first are drawn, or after 200 ms. Its tab is selected at once, and an open that takes over 80 ms shows a page's outline in place of the page leaving, which takes no input. The pages beside the open one, the sections beside the open section and a hovered page or section tab are read ahead on a thread of their own. A notebook keeps the last three sections left or read ahead open, which `Library::open` hands out, and the last 32 pages shown or read ahead keep their scenes, with up to 128 MiB of decoded pictures, so a page shown again is laid out afresh around pictures already drawn. -A covered window receives no redraws, so interaction can be scripted: `SNOWBOUND_REPLAY` names a file of `move X Y`, `press [right]`, `release [right]`, `wheel DX DY`, `pinch FACTOR`, `key NAME`, `type TEXT`, `modifiers [shift] [command]`, `wait MS`, `snapshot PNG_PATH`, `accessibility TEXT_PATH` (the window's accessibility tree, a node a line, as the platform shows it), `appearance light|dark`, `resize WIDTH HEIGHT`, `renderer NAME` (switches as Options' Renderer does) and `quit` lines in logical pixels, and each step and every 16 ms of a wait draws a frame. With `--screenshot` as well, the window stays hidden and only the replay's snapshots capture it. Snapshots wait for the page's pictures, file icons and background art to finish rasterizing; `SNOWBOUND_FRAMES` frames never wait, so art appears in the first frame drawn after it is ready. Snapshots omit the window's traffic lights, which AppKit draws. Replays edit and save like a user, so point them at a copy of a notebook. Their edits carry the settings' user name, so give fixture runs a `--settings` file holding `{"user_name": "Snowbound Test"}`. A replay or `--screenshot` run never reads the account's own settings or iCloud: without `--settings` it starts from none, and its iCloud folder is only the one `SNOWBOUND_ICLOUD_FOLDER` names. +A covered window receives no redraws, so interaction can be scripted: `SNOWBOUND_REPLAY` names a file of `move X Y`, `press [right]`, `release [right]`, `wheel DX DY`, `pinch FACTOR`, `key NAME`, `type TEXT`, `modifiers [shift] [command]`, `wait MS`, `snapshot PNG_PATH`, `accessibility TEXT_PATH` (the window's accessibility tree, a node a line, as the platform shows it), `appearance light|dark`, `resize WIDTH HEIGHT`, `renderer NAME` (switches as Options' Renderer does), `answer PATH` (what the next save panel answers, without showing) and `quit` lines in logical pixels, and each step and every 16 ms of a wait draws a frame. With `--screenshot` as well, the window stays hidden and only the replay's snapshots capture it. Snapshots wait for the page's pictures, file icons and background art to finish rasterizing; `SNOWBOUND_FRAMES` frames never wait, so art appears in the first frame drawn after it is ready. Snapshots omit the window's traffic lights, which AppKit draws. Replays edit and save like a user, so point them at a copy of a notebook. Their edits carry the settings' user name, so give fixture runs a `--settings` file holding `{"user_name": "Snowbound Test"}`. A replay or `--screenshot` run never reads the account's own settings or iCloud: without `--settings` it starts from none, and its iCloud folder is only the one `SNOWBOUND_ICLOUD_FOLDER` names. ```sh cp -RL SOURCE_NOTEBOOK /tmp/notebook-copy && chmod u+w /tmp/notebook-copy/*.one