PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Document tabs: keep whole names, hide what doesn't fit, list every document in the » menu (#4525)
Marco Arnulfod6ea04299f53
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rsindex 15a5db617..91644b5f7 100644--- a/crates/ui-egui/src/canvas.rs+++ b/crates/ui-egui/src/canvas.rs@@ -2230,7 +2230,7 @@ fn tabs(app: &mut PhotocraftApp, ui: &mut egui::Ui) -> TabStrip {.map_or_else(|| app.session.active_index().unwrap_or(0), |p| tab_count + p);let frame = egui::Frame::NONE.fill(t.canvas).inner_margin(egui::Margin { left: 8, right: 8, top: 6, bottom: 4 }).show(ui, |ui| {let (row, _) = ui.allocate_exact_size(egui::vec2(ui.available_width(), tab_h), Sense::hover());- let f = crate::tab_strip::fit(&natural, selected, row.width(), DOC_TAB_MIN_W, crate::tab_strip::CHEVRON_W);+ let f = fit_doc_tabs(&natural, selected, row.width());let mut x = row.left();let mut placed: Vec<(usize, Rect)> = Vec::with_capacity(f.shown.len());for &(i, w) in &f.shown {@@ -2328,7 +2328,10 @@ fn tabs(app: &mut PhotocraftApp, ui: &mut egui::Ui) -> TabStrip {let labels: Vec<&str> =app.session.documents().iter().map(|st| st.doc.name.as_str()).chain(opening.iter().map(|(_, name, _)| name.as_str())).collect();let mut picked = None;- crate::tab_strip::overflow_button(ui, ui.id().with("dtab-overflow"), r, tl!("More documents"), &labels, &f.overflow, &mut picked);+ // Like Photoshop's, the menu lists every open document, not only the hidden ones, and checks+ // the active one (#4487).+ let every: Vec<usize> = (0..labels.len()).collect();+ crate::tab_strip::overflow_button(ui, ui.id().with("dtab-overflow"), r, tl!("More documents"), &labels, &every, Some(selected), &mut picked);match picked {Some(i) if i < tab_count => activate = Some(i),Some(i) => focus_open = opening.get(i - tab_count).map(|(job, _, _)| *job).or(focus_open),@@ -2502,15 +2505,21 @@ fn open_tab_clicks(}}-/// Document tabs: the chrome around a title (the × and its gaps), and the width a tab shrinks to-/// before it moves into the » overflow menu.+/// Document tabs: the chrome around a title (the × and its gaps).const DOC_TAB_PAD: f32 = 42.0;-const DOC_TAB_MIN_W: f32 = 72.0;/// The Studio strip's tabs carried 4 pt of spacing between them and 44 pt of chrome; `fit` sees/// one number per tab, so the gap rides in the natural width and comes back out when painted.const STUDIO_TAB_PAD: f32 = 48.0;const STUDIO_TAB_GAP: f32 = 4.0;+/// Fit document tabs as Photoshop does (#4487): a tab never shrinks below its whole title, so the+/// tabs that don't fit move into the » menu instead. Only the active tab, when it alone is wider+/// than the strip, is cut.+fn fit_doc_tabs(natural: &[f32], selected: usize, avail: f32) -> crate::tab_strip::TabFit {+ let widest = natural.iter().copied().filter(|w| w.is_finite()).fold(0.0, f32::max);+ crate::tab_strip::fit(natural, selected, avail, widest, crate::tab_strip::CHEVRON_W)+}+/// Photoshop's tab title: "name @ 50% (Layer 1, RGB/8)", "(Layer 1, Layer Mask/8)" when the mask/// is targeted, "(Quick Mask/8)" in Quick Mask mode; the Background layer's name is omitted.fn pro_tab_title(app: &PhotocraftApp, i: usize) -> String {@@ -2542,8 +2551,8 @@ fn studio_tab_name(app: &PhotocraftApp, st: &photocraft_engine::DocState) -> Str}/// Photoshop document tabs: "name @ 33.3% (RGB/8)" on a dark strip; active tab matches panels.-/// Tabs that don't fit shrink and elide, and the ones that still don't fit move into a » menu at-/// the end of the strip; the active document's tab always stays on the strip (#1276).+/// Tabs keep their whole titles, and the ones that don't fit move into a » menu at the end of the+/// strip; the active document's tab always stays on the strip (#1276, #4487).fn pro_tabs(app: &mut PhotocraftApp, ui: &mut egui::Ui) -> TabStrip {let t = crate::theme::Tokens::get(ui.ctx());let active = app.session.active_index().filter(|_| app.jobs.focus.is_none());@@ -2572,7 +2581,7 @@ fn pro_tabs(app: &mut PhotocraftApp, ui: &mut egui::Ui) -> TabStrip {.focus.and_then(|job| opening.iter().position(|(open, _, _)| *open == job)).map_or_else(|| app.session.active_index().unwrap_or(0), |p| tab_count + p);- let f = crate::tab_strip::fit(&natural, selected, strip.width(), DOC_TAB_MIN_W, crate::tab_strip::CHEVRON_W);+ let f = fit_doc_tabs(&natural, selected, strip.width());let mut x = strip.left();let mut placed: Vec<(usize, Rect)> = Vec::with_capacity(f.shown.len());for &(i, w) in &f.shown {@@ -2651,7 +2660,10 @@ fn pro_tabs(app: &mut PhotocraftApp, ui: &mut egui::Ui) -> TabStrip {let r = Rect::from_min_size(egui::pos2(left, strip.top()), egui::vec2(crate::tab_strip::CHEVRON_W, strip.height()));let labels: Vec<&str> = titles.iter().map(String::as_str).collect();let mut picked = None;- crate::tab_strip::overflow_button(ui, ui.id().with("ptab-overflow"), r, tl!("More documents"), &labels, &f.overflow, &mut picked);+ // Like Photoshop's, the menu lists every open document, not only the hidden ones, and checks+ // the active one (#4487).+ let every: Vec<usize> = (0..labels.len()).collect();+ crate::tab_strip::overflow_button(ui, ui.id().with("ptab-overflow"), r, tl!("More documents"), &labels, &every, Some(selected), &mut picked);match picked {Some(i) if i < tab_count => activate = Some(i),Some(i) => focus_open = opening.get(i - tab_count).map(|(job, _, _)| *job).or(focus_open),diff --git a/crates/ui-egui/src/canvas/tabs_tests.rs b/crates/ui-egui/src/canvas/tabs_tests.rsindex 642ea413f..42f5c58d8 100644--- a/crates/ui-egui/src/canvas/tabs_tests.rs+++ b/crates/ui-egui/src/canvas/tabs_tests.rs@@ -1,5 +1,6 @@-//! #1276: a document tab strip with more tabs than fit keeps the active document's tab on the-//! strip, shrinks and elides the others, and moves the rest into a » menu (whole app, real tree).+//! #1276, #4487: a document tab strip with more tabs than fit keeps the active document's tab on+//! the strip, keeps whole titles on the tabs it shows, and moves the rest into a » menu (whole+//! app, real tree).use egui::vec2;use egui_kittest::Harness;@@ -177,3 +178,49 @@ fn quick_mask_mode_names_the_quick_mask_in_the_tab() {h.state_mut().run("select.editInQuickMaskMode", json!({"on": false})).unwrap();assert_eq!(pro_tab_title(h.state(), 0), "ch1-p01.png @ 100% (Layer 1, RGB/8)*");}++/// Whether any title painted on the strip was cut with '…'.+fn elided_on_strip(h: &Harness<'_, PhotocraftApp>) -> bool {+ fn elided(shape: &egui::Shape, strip: egui::Rect) -> bool {+ match shape {+ egui::Shape::Text(text) => text.galley.elided && strip.intersects(text.visual_bounding_rect()),+ egui::Shape::Vec(shapes) => shapes.iter().any(|shape| elided(shape, strip)),+ _ => false,+ }+ }+ let strip = strip(h).rect;+ h.output().shapes.iter().any(|shape| elided(&shape.shape, strip))+}++/// #4487: a crowded strip keeps whole names on the tabs it shows and moves the rest into the »+/// menu instead of shrinking them all, and the menu lists every open document with the active one+/// checked, as Photoshop's does.+#[test]+fn crowded_tabs_keep_their_whole_names_and_the_menu_lists_every_document() {+ for theme in [ThemeKind::Pro, ThemeKind::Studio] {+ let mut h = harness(14, theme);+ let s = strip(&h);+ assert!(s.tabs.len() > 1 && s.tabs.len() < 14, "{theme:?}: {} shown", s.tabs.len());+ assert!(!elided_on_strip(&h), "{theme:?}: no tab title is cut");+ // The last new document is active; then one that was behind the menu, then the first.+ for active in [13, 5, 0] {+ h.state_mut().session.set_active(active);+ h.run_steps(2);+ let pro = crate::theme::Tokens::get(&h.ctx).pro;+ let labels: Vec<String> =+ (0..14).map(|i| if pro { pro_tab_title(h.state(), i) } else { h.state().session.documents()[i].doc.name.clone() }).collect();+ h.get_by_label("More documents").click();+ h.run_steps(2);+ for (i, label) in labels.iter().enumerate() {+ // The menu bar's padding: an unchecked row keeps the check mark's room, so names line up.+ let (on, off) = (format!("✔ {label}"), format!(" {label}"));+ let (want, other) = if i == active { (&on, &off) } else { (&off, &on) };+ assert_eq!(h.query_all_by_label(want).count(), 1, "{theme:?}, active {active}: document {i} is in the menu as {want:?}");+ assert_eq!(h.query_all_by_label(other).count(), 0, "{theme:?}, active {active}: document {i} is not {other:?}");+ }+ h.key_press(egui::Key::Escape);+ h.run_steps(2);+ assert!(h.query_by_label(&format!("✔ {}", labels[active])).is_none(), "{theme:?}: the menu closed");+ }+ }+}diff --git a/crates/ui-egui/src/tab_strip.rs b/crates/ui-egui/src/tab_strip.rsindex 61650a118..1419471f1 100644--- a/crates/ui-egui/src/tab_strip.rs+++ b/crates/ui-egui/src/tab_strip.rs@@ -54,8 +54,10 @@ pub struct StripOut {pub const CHEVRON_W: f32 = 18.0;/// Draw the » overflow button at `r`: hover tip `tip`, listing the tabs named by `labels` at the-/// indices in `overflow`. A picked index is left in `picked`.-pub fn overflow_button(ui: &mut Ui, id: egui::Id, r: Rect, tip: &str, labels: &[&str], overflow: &[usize], picked: &mut Option<usize>) {+/// indices in `overflow`, the `checked` one with a check mark as in the menu bar's menus. A picked+/// index is left in `picked`.+#[allow(clippy::too_many_arguments)]+pub fn overflow_button(ui: &mut Ui, id: egui::Id, r: Rect, tip: &str, labels: &[&str], overflow: &[usize], checked: Option<usize>, picked: &mut Option<usize>) {let t = Tokens::get(ui.ctx());let resp = ui.interact(r, id, Sense::click());let mut paint_ui = ui.new_child(egui::UiBuilder::new().max_rect(r));@@ -69,9 +71,9 @@ pub fn overflow_button(ui: &mut Ui, id: egui::Id, r: Rect, tip: &str, labels: &[egui::Popup::menu(&resp).show(|ui| {ui.set_min_width(140.0);for &i in overflow {- if let Some(name) = labels.get(i)- && ui.button(*name).clicked()- {+ let Some(name) = labels.get(i) else { continue };+ let text = if checked.is_some() { format!("{} {name}", if checked == Some(i) { "✔" } else { " " }) } else { (*name).to_owned() };+ if ui.button(text).clicked() {*picked = Some(i);ui.close();}@@ -161,7 +163,7 @@ fn tabs_in(if !f.overflow.is_empty() && f.overflow_width > 0.0 {let r = Rect::from_min_size(pos2(x, area.top()), vec2(f.overflow_width, area.height()));let mut picked = None;- overflow_button(ui, id.with("tab-overflow"), r, tl!("More panels"), translated_tabs, &f.overflow, &mut picked);+ overflow_button(ui, id.with("tab-overflow"), r, tl!("More panels"), translated_tabs, &f.overflow, None, &mut picked);if let Some(i) = picked {out.clicked = true;*selected = i;@@ -358,7 +360,7 @@ mod tests {let mut picked = None;let input = egui::RawInput { events: vec![egui::Event::PointerMoved(rect.center())], ..Default::default() };let mut output = ctx.run_ui(input, |ui| {- overflow_button(ui, ui.id().with("tiny-overflow"), rect, "More panels", &["Layers"], &[0], &mut picked);+ overflow_button(ui, ui.id().with("tiny-overflow"), rect, "More panels", &["Layers"], &[0], None, &mut picked);});output.textures_delta.clear();let mut icons = 0;