Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Arrange: the shown tiles stay put when a visible document is activated (#6017)

itsF4LCON1302e0879173
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs
index 05ee6ba85..cd6d3739c 100644
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@ -2058,9 +2058,10 @@ pub(crate) fn arranged_cells(app: &PhotocraftApp, rect: Rect) -> Option<Vec<(usi
let n = app.session.documents().len();
let cells = crate::view_cmds::cells(&app.ui.view.arrange, rect, n)?;
// 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());
+ // used to move to the first tile). With more documents than tiles, the shown run stays put
+ // while the active document is in it, and otherwise slides only as far as it must.
+ let start = app.arrange_start.min(n.saturating_sub(cells.len()));
+ let start = if idx < start { idx } else { start.max((idx + 1).saturating_sub(cells.len())) };
Some((start..n).zip(cells).collect())
}
@@ -2107,6 +2108,7 @@ fn documents(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
// Window › Arrange: tiled / n-up layouts show several documents side by side; the active one
// takes input, a click elsewhere activates that document.
if let Some(shown) = arranged_cells(app, rect) {
+ app.arrange_start = shown.first().map_or(0, |(d, _)| *d);
let t = crate::theme::Tokens::get(ui.ctx());
for (d, cell) in shown {
let cell = cell.shrink(1.0);
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs
index bde51ff27..986719bdd 100644
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@ -642,6 +642,9 @@ pub struct PhotocraftApp {
fonts_ready: bool,
/// Screen rect of the main canvas last frame (for overlays and the navigator).
pub last_canvas_rect: egui::Rect,
+ /// Window › Arrange: the first document of the shown run of tiles, when there are more
+ /// documents than tiles (`canvas::arranged_cells`).
+ pub(crate) arrange_start: usize,
/// Physical pixels per egui point of the canvas last frame (`ctx.pixels_per_point`, which
/// folds in both the display scale and Interface › UI Scale). The canvas maps document pixels
/// to physical pixels, so point-space geometry divides the view zoom by this (see
@@ -867,6 +870,7 @@ impl PhotocraftApp {
last_window_title: String::new(),
fonts_ready: false,
last_canvas_rect: egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(800.0, 600.0)),
+ arrange_start: 0,
ppp: 1.0,
drop_canvas_rect: None,
tab_strip: None,
diff --git a/crates/ui-egui/src/view_cmds/tests.rs b/crates/ui-egui/src/view_cmds/tests.rs
index ba1b61aa2..4e0aeda4b 100644
--- a/crates/ui-egui/src/view_cmds/tests.rs
+++ b/crates/ui-egui/src/view_cmds/tests.rs
@@ -523,13 +523,19 @@ fn arranged_documents_keep_their_tiles() {
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();
+ // As `documents` draws them: the shown run is kept from frame to frame.
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<_>>()
+ let shown: Vec<usize> = crate::canvas::arranged_cells(app, rect).unwrap().iter().map(|(d, _)| *d).collect();
+ app.arrange_start = shown[0];
+ shown
};
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");
+ assert_eq!(docs(&mut app, 1), vec![1, 2], "a click into the visible document 1 leaves the tiles as they are");
+ assert_eq!(docs(&mut app, 0), vec![0, 1], "a hidden document slides in from the left");
+ assert_eq!(docs(&mut app, 1), vec![0, 1]);
}
/// #6017: rulers showed in the active tile only; the other tile had none and no room for them.