PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Window › Arrange: tiles keep their documents, and every tile has rulers (#6017)
itsF4LCON116a60769712
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rsindex 391ad464f..6e9a65b07 100644--- a/crates/ui-egui/src/canvas.rs+++ b/crates/ui-egui/src/canvas.rs@@ -2020,9 +2020,11 @@ pub(crate) fn arranged_cells(app: &PhotocraftApp, rect: Rect) -> Option<Vec<(usilet idx = app.session.active_index()?;let n = app.session.documents().len();let cells = crate::view_cmds::cells(&app.ui.view.arrange, rect, n)?;- let mut shown: Vec<(usize, Rect)> = (0..n).map(|k| (idx + k) % n).zip(cells).collect();- shown.sort_by_key(|(d, _)| *d);- Some(shown)+ // Each document keeps its tile: a click into another one activates it where it is (#6017; it+ // used to move to the first tile). With more documents than tiles, the shown run slides+ // only as far as it must to include the active one.+ let start = (idx + 1).saturating_sub(cells.len());+ Some((start..n).zip(cells).collect())}fn documents(app: &mut PhotocraftApp, ui: &mut egui::Ui) {@@ -2904,7 +2906,9 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect:crate::monitor_status::check_window(app, &ctx);}let full = rect;- let rect = if primary { crate::rulers::content_rect(app, rect) } else { rect };+ // Every document view has its rulers, the tiles of Window › Arrange and floating windows+ // too, as in Photoshop (#6017).+ let rect = crate::rulers::content_rect(app, rect);let doc = app.session.documents()[idx].doc.clone();let size = [doc.size.width, doc.size.height];if view.doc_size != size {@@ -4087,6 +4091,8 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect:}if primary && app.ui.extras.rulers {crate::rulers::draw_rulers(app, ui, full, &xf);+ } else if app.ui.extras.rulers {+ crate::rulers::paint_rulers(app, ui, full, &xf, &doc);}crate::rotate_view::overlay(app, &ctx, &painter, &xf, &mut view, &response);if primary {diff --git a/crates/ui-egui/src/rulers.rs b/crates/ui-egui/src/rulers.rsindex 61ea25d96..004b4cb32 100644--- a/crates/ui-egui/src/rulers.rs+++ b/crates/ui-egui/src/rulers.rs@@ -329,10 +329,14 @@ fn ruler_pos(xf: &ViewXform, vertical: bool, u: f64, px_per_unit: f64, zero: [f6/// (`y_axis` false) or the left one, from Preferences › Units & Rulers and the zoom. Minor ticks/// sit at tenths of the step.fn ruler_scale(app: &PhotocraftApp, xf: &ViewXform, y_axis: bool) -> (f64, f64) {+ ruler_scale_of(app, app.session.active().map(|d| &*d.doc), xf, y_axis)+}++/// [`ruler_scale`] for the view of `doc`.+fn ruler_scale_of(app: &PhotocraftApp, doc: Option<&photocraft_doc::Document>, xf: &ViewXform, y_axis: bool) -> (f64, f64) {let ur = &app.session.prefs().units_and_rulers;let (unit, ppi) = (ur.rulers, ur.point_size.per_inch());- let (dpi, size) =- app.session.active().map_or((72.0, [1.0, 1.0]), |d| (d.doc.resolution_dpi.max(1.0) as f64, [d.doc.size.width as f64, d.doc.size.height as f64]));+ let (dpi, size) = doc.map_or((72.0, [1.0, 1.0]), |d| (d.resolution_dpi.max(1.0) as f64, [d.size.width as f64, d.size.height as f64]));let px_per_unit = unit.to_px(1.0, dpi, if y_axis { size[1] } else { size[0] }, ppi).max(1e-9);// The top ruler runs along document x, which pixel aspect correction stretches.let zoom = if y_axis { xf.zoom } else { xf.zoom_x() };@@ -356,9 +360,16 @@ fn ruler_label(v: f64, step: f64, whole: bool) -> String {if step >= 1.0 || whole { format!("{}", v.round() as i64) } else { crate::widgets::fmt_num2(v) }}-/// Rulers along the top and left of `full` (the canvas rect before `content_rect`), with the-/// pointer position marked; dragging out of a ruler creates a guide.-pub fn draw_rulers(app: &mut PhotocraftApp, ui: &mut egui::Ui, full: Rect, xf: &ViewXform) {+/// The rulers of `doc`'s view in `full`: background, ticks and labels from its zero point.+/// Returns the top, left and corner rects, the painter, the zero point and the minor tick sizes.+#[allow(clippy::type_complexity)]+fn paint(+ app: &PhotocraftApp,+ ui: &egui::Ui,+ full: Rect,+ xf: &ViewXform,+ doc: &photocraft_doc::Document,+) -> (Rect, Rect, Rect, egui::Painter, [f64; 2], [f64; 2]) {let t = Tokens::get(ui.ctx());let top = Rect::from_min_max(pos2(full.left() + RULER, full.top()), pos2(full.right(), full.top() + RULER));let left = Rect::from_min_max(pos2(full.left(), full.top() + RULER), pos2(full.left() + RULER, full.bottom()));@@ -376,11 +387,11 @@ pub fn draw_rulers(app: &mut PhotocraftApp, ui: &mut egui::Ui, full: Rect, xf: &let unit = app.session.prefs().units_and_rulers.rulers;let whole = unit == Unit::Pixels;// The zero point (#2955): rulers read 0 there and count the distance from it.- let origin = app.session.active().map_or([0.0, 0.0], |d| d.doc.ruler_origin);+ let origin = doc.ruler_origin;let mut ticks = [1.0; 2];for vertical in [false, true] {let p = p.with_clip_rect(if vertical { left } else { top });- let (px_per_unit, step) = ruler_scale(app, xf, vertical);+ let (px_per_unit, step) = ruler_scale_of(app, Some(doc), xf, vertical);ticks[usize::from(vertical)] = step * px_per_unit / 10.0;let (u0, u1) = if vertical {ruler_range(xf, left.left_top(), left.left_bottom(), 1, px_per_unit, origin)@@ -413,6 +424,23 @@ pub fn draw_rulers(app: &mut PhotocraftApp, ui: &mut egui::Ui, full: Rect, xf: &v += step;}}+ (top, left, corner, p, origin, ticks)+}++/// Rulers for a document view that isn't the active one (another tile of Window › Arrange, a+/// floating window): shown as the active view's are, but guides and the zero point are dragged+/// in the active view (#6017).+pub fn paint_rulers(app: &PhotocraftApp, ui: &egui::Ui, full: Rect, xf: &ViewXform, doc: &photocraft_doc::Document) {+ paint(app, ui, full, xf, doc);+}++/// Rulers along the top and left of `full` (the canvas rect before `content_rect`), with the+/// pointer position marked; dragging out of a ruler creates a guide.+pub fn draw_rulers(app: &mut PhotocraftApp, ui: &mut egui::Ui, full: Rect, xf: &ViewXform) {+ let Some(doc) = app.session.active().map(|d| d.doc.clone()) else { return };+ let (top, left, corner, p, origin, ticks) = paint(app, ui, full, xf, &doc);+ let t = Tokens::get(ui.ctx());+ let unit = app.session.prefs().units_and_rulers.rulers;// Pointer position markers.if let Some(h) = ui.ctx().pointer_hover_pos().filter(|h| full.contains(*h)) {let m = Stroke::new(1.0, t.text);diff --git a/crates/ui-egui/src/view_cmds/tests.rs b/crates/ui-egui/src/view_cmds/tests.rsindex 2205b6e15..ba1b61aa2 100644--- a/crates/ui-egui/src/view_cmds/tests.rs+++ b/crates/ui-egui/src/view_cmds/tests.rs@@ -506,3 +506,60 @@ fn esc_leaves_full_screen_modes_unless_something_else_owns_it() {assert_eq!(app.ui.view.screen_mode, "standard");assert!(app.session.active().unwrap().doc.selection.is_some());}++/// #6017: a click into another tile activated that document and moved it to the first tile, so+/// the two side-by-side documents swapped places. Each document keeps its tile; with more+/// documents than tiles, the shown run slides just enough to include the active one.+#[test]+fn arranged_documents_keep_their_tiles() {+ let rect = egui::Rect::from_min_max(egui::pos2(0.0, 0.0), egui::pos2(900.0, 600.0));+ let (mut app, _) = app_with(3);+ app.ui.view.arrange = "tileAllVertically".into();+ for active in 0..3 {+ app.session.set_active(active);+ let shown = crate::canvas::arranged_cells(&app, rect).unwrap();+ let order: Vec<usize> = shown.iter().map(|(d, _)| *d).collect();+ assert_eq!(order, vec![0, 1, 2], "active {active}");+ assert!(shown.windows(2).all(|w| w[0].1.left() < w[1].1.left()), "document 0 on the left");+ }+ app.ui.view.arrange = "twoUpVertical".into();+ let docs = |app: &mut PhotocraftApp, active: usize| {+ app.session.set_active(active);+ crate::canvas::arranged_cells(app, rect).unwrap().iter().map(|(d, _)| *d).collect::<Vec<_>>()+ };+ assert_eq!(docs(&mut app, 0), vec![0, 1]);+ assert_eq!(docs(&mut app, 1), vec![0, 1], "the second tile's document stays on the right");+ assert_eq!(docs(&mut app, 2), vec![1, 2], "the third document slides in next to the second");+}++/// #6017: rulers showed in the active tile only; the other tile had none and no room for them.+#[test]+fn every_arranged_tile_has_rulers() {+ use egui_kittest::Harness;+ let (mut app, _) = app_with(2);+ app.ui.view.arrange = "tileAllVertically".into();+ app.ui.extras.rulers = true;+ let mut h = Harness::builder().with_size(egui::vec2(1000.0, 600.0)).build_ui_state(+ |ui, app: &mut PhotocraftApp| {+ if !ui.ctx().fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) {+ return;+ }+ egui::CentralPanel::default().show(ui, |ui| crate::canvas::document_area(app, ui));+ },+ app,+ );+ PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::ALL[0]);+ h.run_steps(4);+ let right = h.state().last_canvas_rect.center().x;+ // Ruler labels are 9 pt text: some lie in each half of the document area.+ let mut labels = [0, 0];+ for s in &h.output().shapes {+ if let egui::Shape::Text(t) = &s.shape+ && t.galley.job.sections.first().is_some_and(|sec| sec.format.font_id.size == 9.0)+ && t.galley.text().chars().all(|c| c.is_ascii_digit())+ {+ labels[usize::from(t.pos.x > right)] += 1;+ }+ }+ assert!(labels[0] > 0 && labels[1] > 0, "ruler labels per tile: {labels:?}");+}