PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Workspace: list saved workspaces in Window › Workspace and the switcher (storytold/photocraft#5610)
nrq6993-a11y579e0f1d287c
diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rsindex 7f47e6ea6..11c949f02 100644--- a/crates/ui-egui/src/menus.rs+++ b/crates/ui-egui/src/menus.rs@@ -1101,6 +1101,22 @@ pub fn menu_items(app: &PhotocraftApp) -> Vec<MenuItem> {items.insert(after + 1 + k, it);}}+ // Window › Workspace: saved workspaces follow the presets, checked while current.+ let saved = crate::workspace_ui::saved_names(app);+ if !saved.is_empty()+ && let Some(after) = items.iter().position(|i| i.id == "window.workspace.motion")+ {+ let wp: Vec<String> = vec!["Window".into(), "Workspace".into()];+ let separator = MenuItem { id: "---".into(), label: "---".into(), path: wp.clone(), shortcut: None, enabled: false, checked: None, color: None };+ let entries = saved.into_iter().enumerate().map(|(i, name)| {+ let id = format!("{}{i}", crate::workspace_ui::SAVED);+ let checked = checked(app, &id);+ MenuItem { id, label: name, path: wp.clone(), shortcut: None, enabled: true, checked, color: None }+ });+ for (k, it) in std::iter::once(separator).chain(entries).enumerate() {+ items.insert(after + 1 + k, it);+ }+ }// Help: the link items, a separator, then System Info and About.if let Some(at) = items.iter().position(|i| i.id == "help.systemInfo" || i.id == "help.about") {items.insert(diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rsindex 522c74dc1..d0787cafb 100644--- a/crates/ui-egui/src/panels.rs+++ b/crates/ui-egui/src/panels.rs@@ -642,16 +642,8 @@ pub fn title_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) {let ui = &mut group_ui;ui.spacing_mut().item_spacing.x = 6.0;let mut ws = app.ui.workspace.clone();- let opts = [- // Keys stay English (`apply_workspace` matches them); the dropdown- // shows the labels in the UI language.- ("Essentials".to_string(), tl!("Essentials")),- ("Photography".to_string(), tl!("Photography")),- ("Painting".to_string(), tl!("Painting")),- ("Graphic and Web".to_string(), tl!("Graphic and Web")),- ("Pixel Art".to_string(), tl!("Pixel Art")),- ("Motion".to_string(), tl!("Motion")),- ];+ let options = crate::workspace_ui::switcher_options(app);+ let opts: Vec<(String, &str)> = options.iter().map(|(k, l)| (k.clone(), l.as_str())).collect();// A narrow bar drops what is also in a menu, the theme toggle first// (Preferences), then search (Edit › Search), and narrows the switcher// (Window › Workspace) before anything runs over.diff --git a/crates/ui-egui/src/workspace_ui.rs b/crates/ui-egui/src/workspace_ui.rsindex 1c9327263..4fb914002 100644--- a/crates/ui-egui/src/workspace_ui.rs+++ b/crates/ui-egui/src/workspace_ui.rs@@ -45,13 +45,45 @@ const IDS: [&str; 7] = ["view.show.showExtrasOptions",];+/// Window › Workspace items of saved workspaces: `window.workspace.saved.<index>`, an index into+/// [`saved_names`].+pub const SAVED: &str = "window.workspace.saved.";+/// Menu ids handled here (live menu items).pub fn handles(id: &str) -> bool {- IDS.contains(&id)+ IDS.contains(&id) || id.starts_with(SAVED)+}++/// Saved workspace names, in menu order.+pub fn saved_names(app: &PhotocraftApp) -> Vec<String> {+ app.session.prefs().workspaces.keys().cloned().collect()+}++fn saved_name(app: &PhotocraftApp, id: &str) -> Option<String> {+ let i = id.strip_prefix(SAVED)?.parse::<usize>().ok()?;+ app.session.prefs().workspaces.keys().nth(i).cloned()+}++/// The top-bar workspace switcher's options as (key, label): the presets, then saved workspaces.+pub fn switcher_options(app: &PhotocraftApp) -> Vec<(String, String)> {+ // Keys stay English (`apply_workspace` matches them); the dropdown shows the preset labels+ // in the UI language.+ let presets = [+ ("Essentials", tl!("Essentials")),+ ("Photography", tl!("Photography")),+ ("Painting", tl!("Painting")),+ ("Graphic and Web", tl!("Graphic and Web")),+ ("Pixel Art", tl!("Pixel Art")),+ ("Motion", tl!("Motion")),+ ];+ let mut opts: Vec<(String, String)> = presets.iter().map(|(k, l)| ((*k).to_string(), (*l).to_string())).collect();+ opts.extend(saved_names(app).into_iter().map(|n| (n.clone(), n)));+ opts}pub fn checked(app: &PhotocraftApp, id: &str) -> Option<bool> {match id {+ i if i.starts_with(SAVED) => Some(saved_name(app, i).is_some_and(|n| n == app.ui.workspace)),"window.panel.modifierKeys" => Some(app.ui.shell.modifier_keys),"window.workspace.lockWorkspace" => Some(app.session.prefs().workspace_locked),"view.pixelAspectRatio.custom" => Some(app.ui.view.pixel_aspect.starts_with("custom:")),@@ -140,6 +172,10 @@ pub fn menu(app: &mut PhotocraftApp, id: &str, params: &Value) -> Option<Result<}"window.workspace.deleteWorkspace" => delete_workspace(app, params),"window.workspace.select" => select_workspace(app, params),+ i if i.starts_with(SAVED) => match saved_name(app, i) {+ Some(name) => select_workspace(app, &json!({"name": name})),+ None => Err(format!("no saved workspace for `{i}`")),+ },"view.pixelAspectRatio.custom" if no_params(params) => open(app, "customPar", json!({"name": "", "ratio": 1.0})),"view.pixelAspectRatio.custom" => custom_par(app, params),"view.show.showExtrasOptions" if no_params(params) => {@@ -609,6 +645,40 @@ mod tests {assert!(s2.prefs().workspaces.contains_key("Mine"));}+ #[test]+ fn saved_workspaces_are_listed_checked_and_selectable() {+ let (mut app, ctx) = app();+ let ws_items =+ |app: &PhotocraftApp| crate::menus::menu_items(app).into_iter().filter(|i| i.path == ["Window", "Workspace"] && i.id != "---").collect::<Vec<_>>();+ assert!(!ws_items(&app).iter().any(|i| i.id.starts_with(SAVED)));+ app.ui.panels.history = true;+ crate::menus::invoke(&mut app, &ctx, "window.workspace.newWorkspace", json!({"name": "Retouch"})).unwrap();+ crate::menus::invoke(&mut app, &ctx, "window.workspace.newWorkspace", json!({"name": "Comp"})).unwrap();+ // Listed right after the presets, in name order; only the current one is checked.+ let items = ws_items(&app);+ let motion = items.iter().position(|i| i.id == "window.workspace.motion").unwrap();+ let saved: Vec<(&str, Option<bool>, bool)> = items[motion + 1..].iter().take(2).map(|i| (i.label.as_str(), i.checked, i.enabled)).collect();+ assert_eq!(saved, [("Comp", Some(true), true), ("Retouch", Some(false), true)]);+ assert!(items.iter().filter(|i| !i.id.starts_with(SAVED)).all(|i| i.checked != Some(true) || i.id == "window.workspace.lockWorkspace"));+ assert!(crate::menus::is_live(&format!("{SAVED}1")));+ // Switch to a preset, then back through the menu item.+ crate::menus::invoke(&mut app, &ctx, "window.workspace.painting", json!({})).unwrap();+ assert!(!app.ui.panels.history);+ let retouch = ws_items(&app).into_iter().find(|i| i.label == "Retouch").unwrap();+ crate::menus::invoke(&mut app, &ctx, &retouch.id, json!({})).unwrap();+ assert_eq!(app.ui.workspace, "Retouch");+ assert!(app.ui.panels.history);+ assert_eq!(checked(&app, &retouch.id), Some(true));+ // Stale or malformed ids fail instead of panicking.+ for bad in ["9", "-1", "x", ""] {+ assert!(crate::menus::invoke(&mut app, &ctx, &format!("{SAVED}{bad}"), json!({})).is_err(), "{bad}");+ }+ // The top-bar switcher lists them after the presets.+ let keys: Vec<String> = switcher_options(&app).into_iter().map(|(k, _)| k).collect();+ assert_eq!(keys[6..], ["Comp", "Retouch"]);+ assert!(keys.contains(&app.ui.workspace));+ }+#[test]fn lock_workspace_toggles_and_persists() {let (mut app, ctx) = app();