From e18d2a21477d87288f0e41678b7c26cf5af59102 Mon Sep 17 00:00:00 2001 From: haranishi <223786623+haranishi@users.noreply.github.com> Date: Thu, 19 Nov 2026 23:37:57 +0900 Subject: [PATCH] fix(canvas): show document titles and close controls in Tile --- crates/ui-egui/src/canvas/document_area.rs | 102 +++++++++++++++++++-- crates/ui-egui/src/canvas/tabs.rs | 12 ++- crates/ui-egui/src/canvas/tabs_tests.rs | 97 ++++++++++++++++++++ crates/ui-egui/src/file_open.rs | 6 +- 4 files changed, 205 insertions(+), 12 deletions(-) diff --git a/crates/ui-egui/src/canvas/document_area.rs b/crates/ui-egui/src/canvas/document_area.rs index 79aa49bb3f..bfc1345247 100644 --- a/crates/ui-egui/src/canvas/document_area.rs +++ b/crates/ui-egui/src/canvas/document_area.rs @@ -4,10 +4,15 @@ use std::cell::RefCell; use craft::behavior::PressPhase; +use craft::behavior::dock::Arrangement; +use craft::ui::bars::{Bar, BarKind}; use craft::ui::canvas::{SplitAt, SplitViews}; -use craft::ui::contract::{Bind, ShowContainer as _}; +use craft::ui::contract::{Bind, Props as _, Show as _, ShowContainer as _}; +use craft::ui::display::{Elide, Text}; use craft::ui::docking::Arranged; use craft::ui::shell::CanvasPanel; +use craft::ui::style::StyleCx; +use craft::ui::tabs::{Tab, TabPartEvent}; use egui::Rect; use super::document_display::CanvasHost; @@ -34,7 +39,7 @@ pub fn document_area(app: &mut PhotocraftApp, ui: &mut egui::Ui) { /// docked group `i` of `app.ui.document_windows`, showing that group's shown document, in the rect `SplitViews` /// places it in with the dividers at `app.ui.split_at`. `None` with no arrangement or fewer than two docked groups: /// the single canvas fills the area. -pub(crate) fn arranged_cells(app: &PhotocraftApp, rect: Rect) -> Option> { +pub(super) fn arranged_panes(app: &PhotocraftApp, rect: Rect) -> Option> { let windows = &app.ui.document_windows; let layout = windows.arrangement?; let count = windows.docked.len(); @@ -55,6 +60,46 @@ pub(crate) fn arranged_cells(app: &PhotocraftApp, rect: Rect) -> Option Option> { + let mut panes = arranged_panes(app, rect)?; + if app.ui.document_windows.arrangement == Some(Arrangement::Tile) { + let heights = tile_bar_heights(app); + for (_, pane) in &mut panes { + *pane = TilePane::new(*pane, heights).canvas; + } + } + Some(panes) +} + +fn tile_bar_heights(app: &PhotocraftApp) -> (f32, f32) { + StyleCx::with_root(app.ctx(), |cx| { + let size = &cx.tokens.size.bar; + let title = if app.session.prefs().workspace.large_tabs { size.title } else { size.tab }; + (title, size.status) + }) +} + +/// Tile chrome takes its own rows so it is outside the tools and drop targets of the canvas. +struct TilePane { + title: Rect, + canvas: Rect, + status: Rect, +} + +impl TilePane { + fn new(pane: Rect, (title, status): (f32, f32)) -> Self { + // With an exceptionally short pane, preserve a canvas before adding either full-height bar. + let top = pane.top() + if pane.height() > title { title } else { 0.0 }; + let bottom = pane.bottom() - if pane.bottom() - top > status { status } else { 0.0 }; + Self { + title: Rect::from_x_y_ranges(pane.x_range(), pane.top()..=top), + canvas: Rect::from_x_y_ranges(pane.x_range(), top..=bottom), + status: Rect::from_x_y_ranges(pane.x_range(), bottom..=pane.bottom()), + } + } +} + /// Tell the GPU canvas which documents are on screen (the arranged panes or the main canvas's, plus the shown /// document of every floating window): they split the display texture budget, and the textures of the others are /// freed first. Only a change of that set (or a document's size) re-splits it, so switching between shown documents @@ -62,7 +107,7 @@ pub(crate) fn arranged_cells(app: &PhotocraftApp, rect: Rect) -> Option = match arranged_cells(app, app.last_canvas_rect) { + let mut shown: Vec = match arranged_panes(app, app.last_canvas_rect) { Some(cells) => cells.into_iter().map(|(d, _)| d).collect(), None => main_canvas_document(app).into_iter().collect(), }; @@ -151,24 +196,67 @@ fn arranged(app: &mut PhotocraftApp, ui: &mut egui::Ui, rect: Rect, active: usiz group.shown = Some(active_id); } } - let Some(cells) = arranged_cells(app, rect) else { return false }; + let Some(panes) = arranged_panes(app, rect) else { return false }; + let heights = (layout == Arrangement::Tile).then(|| tile_bar_heights(app)); let count = app.ui.document_windows.docked.len(); let active_pane = app.ui.document_windows.docked.iter().position(|group| group.shown == Some(active_id)).unwrap_or(0); let shown: Vec> = app.ui.document_windows.docked.iter().map(|group| group.shown.and_then(|id| document_index(app, id))).collect(); // The panes are laid out from the dividers as this pass starts; an edit of them lands after the pass. let mut at = app.ui.split_at.clone(); let out = SplitViews::new(tl!("Documents"), layout, Bind::bound(&mut at)).count(count).active(active_pane).show(ui, |ui, _| { - for &(d, cell) in &cells { + for &(d, pane) in &panes { let view = app.ui.views[d].clone(); - let (v, _) = canvas_view(app, ui, CanvasHost::main(d), cell, view, d == active); + let tile = heights.map(|heights| TilePane::new(pane, heights)); + let canvas = tile.as_ref().map_or(pane, |tile| tile.canvas); + let (v, _) = canvas_view(app, ui, CanvasHost::main(d), canvas, view, d == active); + if let Some(tile) = &tile + && tile.status.height() > 0.0 + { + let id = app.session.documents()[d].doc.id; + let zoom = format!("{}%", super::fmt_zoom(v.zoom * 100.0)); + Bar::new("", BarKind::Status).id_salt(("tile-status", id)).show_at(ui, tile.status, |ui, _| { + Text::new(zoom.clone()).elide(Elide::End).show(ui); + }); + } if d != active { app.ui.views[d] = v; } } }); app.ui.split_at = at; - if let Some(d) = out.output.activated.and_then(|pane| shown.get(pane).copied().flatten()) { + let mut selected = out.output.activated.and_then(|pane| shown.get(pane).copied().flatten()); + let mut closing = Vec::new(); + if let Some(heights) = heights { + // SplitViews places a hit surface above each inactive canvas to handle its first press. + // Title controls are siblings above those surfaces so Close works even on an inactive tile. + for &(d, pane) in &panes { + let title = TilePane::new(pane, heights).title; + if title.height() <= 0.0 { + continue; + } + let doc = &app.session.documents()[d]; + let label = tabs::tab_title_at(app, d, app.ui.views[d].zoom * 100.0); + let mut tab = Tab::new(label).id_salt(("tile-title", doc.doc.id)).selected(d == active); + if app.session.prefs().workspace.large_tabs { + tab = tab.large(); + } + if doc.is_dirty() { + tab = tab.modified(); + } + let out = tab.show_at(ui, title); + if matches!(out.event, Some(TabPartEvent::Close)) { + closing.push(doc.doc.id); + } else if out.response.clicked() { + selected = Some(d); + } + } + } + if let Some(d) = selected { app.session.set_active(d); } + // A clean document can leave the session immediately, so close only after the pane indices were used. + for id in closing { + tabs::close_document(app, ui.ctx(), id); + } true } diff --git a/crates/ui-egui/src/canvas/tabs.rs b/crates/ui-egui/src/canvas/tabs.rs index 2cd3458963..a015fb892c 100644 --- a/crates/ui-egui/src/canvas/tabs.rs +++ b/crates/ui-egui/src/canvas/tabs.rs @@ -84,10 +84,7 @@ pub(super) fn apply(app: &mut PhotocraftApp, ctx: &egui::Context, out: DocumentT for event in out.events { match event { DocumentTabsEvent::Selected(tab) | DocumentTabsEvent::Picked(tab) | DocumentTabsEvent::Activated(tab) => show_tab(app, tab), - DocumentTabsEvent::CloseRequested(DocTab::Doc(id)) => { - // The guarded close: a document with unsaved changes gets the save prompt. - let _ = crate::menus::invoke(app, ctx, "file.close", json!({"document": id.0})); - } + DocumentTabsEvent::CloseRequested(DocTab::Doc(id)) => close_document(app, ctx, id), DocumentTabsEvent::CloseRequested(DocTab::Opening(job)) => { let _ = crate::jobs_ui::cancel(app, job); } @@ -119,6 +116,13 @@ pub(super) fn apply(app: &mut PhotocraftApp, ctx: &egui::Context, out: DocumentT menu } +/// The guarded close shared by the tab strip and a tiled document's title. +pub(super) fn close_document(app: &mut PhotocraftApp, ctx: &egui::Context, id: DocId) { + if let Some(index) = document_index(app, id) { + let _ = crate::menus::invoke(app, ctx, "file.close", json!({"document": index})); + } +} + /// A chosen tab: a document tab shows that document, an opening tab its progress. fn show_tab(app: &mut PhotocraftApp, tab: DocTab) { match tab { diff --git a/crates/ui-egui/src/canvas/tabs_tests.rs b/crates/ui-egui/src/canvas/tabs_tests.rs index b36c509fe1..a1cf638516 100644 --- a/crates/ui-egui/src/canvas/tabs_tests.rs +++ b/crates/ui-egui/src/canvas/tabs_tests.rs @@ -136,3 +136,100 @@ fn quick_mask_mode_names_the_quick_mask_in_the_tab() { let detail = title(&h); assert!(detail.contains("Layer 1") && detail.contains("RGB") && !detail.contains("Quick Mask"), "{detail}"); } + +#[test] +fn a_tab_close_resolves_the_document_to_its_current_index() { + let mut h = harness(2); + let other = h.state().session.active().unwrap().doc.id; + h.query_all_by_role(Role::Button) + .find(|n| n.accesskit_node().label().is_some_and(|label| label.starts_with("Close ch1-p01.png"))) + .expect("the other document tab's close button") + .click(); + h.run_steps(3); + let docs = h.state().session.documents(); + assert_eq!(docs.len(), 1); + assert_eq!(docs[0].doc.id, other); +} + +fn tiled_app(width: f32) -> Harness<'static, Box> { + let mut h = Harness::builder().with_size(vec2(width, 720.0)).with_pixels_per_point(1.25).with_max_steps(64).build_eframe(|cc| { + PhotocraftApp::setup_context(&cc.egui_ctx, AppTheme::pro()); + let mut app = PhotocraftApp::with_context(cc.egui_ctx.clone(), photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 180, "height": 120, "name": "Edited photograph with a long title.psd", "depth": 16})).unwrap(); + app.run("layer.new.layer", json!({"name": "Retouch"})).unwrap(); + app.run("file.new", json!({"width": 160, "height": 100, "name": "Reference photograph.psd"})).unwrap(); + app + }); + h.run_steps(3); + let ctx = h.ctx.clone(); + crate::menus::invoke(h.state_mut(), &ctx, "window.arrange.tile", json!({})).unwrap(); + h.run_steps(4); + h +} + +fn tiled_control<'a>(h: &'a Harness<'_, Box>, role: Role, label: &str) -> Option> { + let area = h.state().last_canvas_rect; + h.query_all_by_role(role).filter(|n| n.accesskit_node().label().is_some_and(|text| text.contains(label))).find(|n| area.contains(n.rect().center())) +} + +#[test] +fn tiled_document_titles_follow_individual_views() { + for width in [1120.0, 652.0] { + let mut h = tiled_app(width); + let original = h.state().ui.views[1].zoom; + h.state_mut().ui.views[0].zoom = 0.5; + h.state_mut().ui.views[0].clear_pending(); + h.run_steps(3); + let app = h.state(); + let name = &app.session.documents()[0].doc.name; + let title = tiled_control(&h, Role::Tab, name).expect("the edited document's title is inside its tile"); + let label = title.accesskit_node().label().unwrap(); + assert!(label.contains("@ 50% (Retouch, RGB/16)") && label.ends_with(", modified"), "{label}"); + let panes = super::document_area::arranged_panes(app, app.last_canvas_rect).unwrap(); + let cell = panes.iter().find(|(i, _)| *i == 0).unwrap().1; + let canvases = super::arranged_cells(app, app.last_canvas_rect).unwrap(); + let canvas = canvases.iter().find(|(i, _)| *i == 0).unwrap().1; + let close = tiled_control(&h, Role::Button, &format!("Close {name}")).expect("close is available in the tile"); + assert!(cell.contains_rect(title.rect()) && cell.contains_rect(close.rect()), "title and close stay inside the tile at {width} points"); + let zoom = h.query_all_by_label("50%").find(|n| cell.contains_rect(n.rect())).expect("the edited document's tile shows its own zoom"); + assert!(title.rect().bottom() <= canvas.top() && zoom.rect().top() >= canvas.bottom(), "chrome is outside its canvas"); + assert!(matches!(app.drop_to(&h.ctx, Some(title.rect().center())), crate::file_open::DropTo::Window)); + assert_eq!(h.state().ui.views[1].zoom, original, "the other tile keeps its zoom"); + title.click(); + h.run_steps(3); + assert_eq!(h.state().session.active_index(), Some(0)); + } +} + +#[test] +fn tiled_document_close_uses_the_clicked_document_and_save_prompt() { + let mut h = tiled_app(1120.0); + let edited = h.state().session.documents()[0].doc.id; + assert!(h.state().session.documents()[0].is_dirty()); + assert!(!h.state().session.documents()[1].is_dirty()); + assert_eq!(h.state().session.active_index(), Some(1)); + h.state_mut().ui.tool = crate::state::Tool::Brush; + let revisions: Vec<_> = h.state().session.documents().iter().map(|d| d.revision).collect(); + let at = tiled_control(&h, Role::Button, "Close Edited photograph").expect("the inactive tile has its own close button").rect().center(); + h.event(egui::Event::PointerMoved(at)); + h.run_steps(1); + for pressed in [true, false] { + h.event(egui::Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed, modifiers: egui::Modifiers::NONE }); + h.run_steps(1); + } + h.run_steps(3); + assert!(h.state().discard.is_some(), "closing edits asks whether to save"); + assert!(h.state().session.documents().iter().map(|d| d.revision).eq(revisions), "the close press produces no brush edit"); + assert_eq!(h.state().session.documents().len(), 2); + h.get_by_label("Cancel").click(); + h.run_steps(3); + assert!(h.state().discard.is_none()); + assert_eq!(h.state().session.documents().len(), 2, "Cancel leaves both tiles open"); + tiled_control(&h, Role::Button, "Close Reference photograph").expect("the clean tile has its own close button").click(); + h.run_steps(3); + assert!(h.state().discard.is_none()); + let docs = h.state().session.documents(); + assert_eq!(docs.len(), 1); + assert_eq!(docs[0].doc.id, edited, "the close button removed only the clean document"); + assert!(tiled_control(&h, Role::Tab, "Edited photograph").is_none(), "one document returns to the original single canvas"); +} diff --git a/crates/ui-egui/src/file_open.rs b/crates/ui-egui/src/file_open.rs index 8d4d58dbbc..ce783482a9 100644 --- a/crates/ui-egui/src/file_open.rs +++ b/crates/ui-egui/src/file_open.rs @@ -451,7 +451,11 @@ impl PhotocraftApp { /// Where a web-build drop at `at` lands (the web build's own drop path until FW-264). Over a /// window or dialog it's not the canvas. On the canvas it carries the document point under `at`. pub(crate) fn drop_to(&self, ctx: &egui::Context, at: Option) -> DropTo { - let uncovered = at.is_some_and(|p| ctx.layer_id_at(p).is_none_or(|l| l.order == egui::Order::Background)); + let uncovered = at.is_some_and(|p| { + ctx.layer_id_at(p).is_none_or(|l| l.order == egui::Order::Background) + && (self.ui.document_windows.arrangement != Some(craft::behavior::dock::Arrangement::Tile) + || crate::canvas::arranged_cells(self, self.last_canvas_rect).is_none_or(|cells| cells.iter().any(|(_, rect)| rect.contains(p)))) + }); match target_at(at, uncovered, self.drop_canvas_rect, self.session.active().is_some()) { DropTo::Canvas { .. } => DropTo::Canvas { at: at.and_then(|p| {