Merge pull request #18947

Co-authored-by: autocraft-bot[bot] <354215646+autocraft-bot[bot]@users.noreply.github.com>
This commit is contained in:
davidmann4
2026-12-02 08:06:59 +00:00
committed by autocraft-bot[bot]
3 changed files with 140 additions and 3 deletions
+1 -1
View File
@@ -320,7 +320,7 @@ fn arranged(app: &mut PhotocraftApp, ui: &mut egui::Ui, rect: Rect, active: usiz
}
// 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);
tabs::close_tab(app, ui.ctx(), id);
}
true
}
+17 -2
View File
@@ -88,7 +88,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)) => close_document(app, ctx, id),
DocumentTabsEvent::CloseRequested(DocTab::Doc(id)) => close_tab(app, ctx, id),
DocumentTabsEvent::CloseRequested(DocTab::Opening(job)) => {
let _ = crate::jobs_ui::cancel(app, job);
}
@@ -120,7 +120,18 @@ 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.
/// The close affordance shared by the tab strip and a tiled document's title.
pub(super) fn close_tab(app: &mut PhotocraftApp, ctx: &egui::Context, id: DocId) {
if app.ui.tool == crate::Tool::PerspectiveCrop
&& app.session.active().is_some_and(|document| document.doc.id == id)
&& crate::perspective_crop_ui::quad(app).is_some()
{
return;
}
close_document(app, ctx, id);
}
/// Closes an open document, showing its unsaved-changes prompt when needed.
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}));
@@ -307,6 +318,10 @@ mod file_info_copyright_title_tests;
#[path = "tab_indexed_bitmap_label_tests.rs"]
mod tab_indexed_bitmap_label_tests;
#[cfg(test)]
#[path = "tabs_perspective_crop_close_tests.rs"]
mod tabs_perspective_crop_close_tests;
#[cfg(test)]
#[path = "../tab_title_channel_target_tests.rs"]
mod tab_title_channel_target_tests;
@@ -0,0 +1,122 @@
use egui::accesskit::Role;
use egui::{Event, Modifiers, PointerButton, Pos2, vec2};
use egui_kittest::Harness;
use egui_kittest::kittest::{NodeT, Queryable};
use serde_json::json;
use crate::canvas::testing::to_screen;
use crate::perspective_crop_ui;
use crate::theme::AppTheme;
use crate::{PhotocraftApp, Tool};
type App = Harness<'static, Box<PhotocraftApp>>;
fn two_documents(tiled: bool) -> Result<App, String> {
let mut h = Harness::builder().with_size(vec2(1440.0, 900.0)).with_max_steps(64).build_eframe(|cc| {
PhotocraftApp::setup_context(&cc.egui_ctx, AppTheme::pro());
PhotocraftApp::with_context(cc.egui_ctx.clone(), photocraft_engine::Session::new(), crate::Services::default())
});
let app = h.state_mut();
for name in ["Reference.psd", "Edited.psd"] {
app.run("file.new", json!({"width": 400, "height": 300, "name": name}))?;
}
app.run("layer.new.layer", json!({"name": "Retouch"}))?;
h.run_steps(4);
let ctx = h.ctx.clone();
if tiled {
crate::menus::invoke(h.state_mut(), &ctx, "window.arrange.tile", json!({}))?;
}
h.state_mut().ui.tool = Tool::PerspectiveCrop;
h.state_mut().ui.extras.snap = false;
h.run_steps(6);
assert!(h.state().session.active().is_some_and(|doc| doc.doc.name == "Edited.psd" && doc.is_dirty()));
Ok(h)
}
fn point(h: &mut App, at: Pos2) {
h.event(Event::PointerMoved(at));
h.run_steps(1);
}
fn press(h: &mut App, pos: Pos2, pressed: bool) {
h.event(Event::PointerButton { pos, button: PointerButton::Primary, pressed, modifiers: Modifiers::NONE });
h.run_steps(1);
}
fn click(h: &mut App, at: Pos2) {
point(h, at);
press(h, at, true);
press(h, at, false);
h.run_steps(3);
}
fn draw_quad(h: &mut App) {
let from = to_screen(h.state(), [40.0, 30.0]);
let to = to_screen(h.state(), [360.0, 270.0]);
point(h, from);
press(h, from, true);
point(h, to);
press(h, to, false);
h.run_steps(2);
assert!(perspective_crop_ui::quad(h.state()).is_some());
}
fn click_close(h: &mut App, name: &str, tiled: bool) -> Result<(), String> {
let label = format!("Close {name}");
let canvas = h.state().last_canvas_rect;
let at = h
.query_all_by_role(Role::Button)
.filter(|node| node.accesskit_node().label().is_some_and(|text| text.starts_with(label.as_str())))
.map(|node| node.rect().center())
.find(|at| canvas.contains(*at) == tiled)
.ok_or_else(|| format!("Missing {label} button for tiled={tiled}"))?;
click(h, at);
Ok(())
}
/// The active tab and active tiled title must leave the same quad editing session in place.
#[test]
fn a_complete_quad_protects_its_dirty_document_from_tab_and_tile_close() -> Result<(), String> {
for tiled in [false, true] {
let mut h = two_documents(tiled)?;
draw_quad(&mut h);
let quad = perspective_crop_ui::quad(h.state());
let active = h.state().session.active().map(|doc| doc.doc.id);
click_close(&mut h, "Edited.psd", tiled)?;
assert!(h.state().discard.is_none(), "close opened a save prompt for tiled={tiled}");
assert_eq!(h.state().session.documents().len(), 2);
assert_eq!(h.state().session.active().map(|doc| doc.doc.id), active);
assert_eq!(perspective_crop_ui::quad(h.state()), quad);
// Reset through the tool before testing a partial session, independently of the tab's focus.
perspective_crop_ui::cancel(h.state_mut());
h.run_steps(3);
assert!(perspective_crop_ui::quad(h.state()).is_none());
let corner = to_screen(h.state(), [40.0, 30.0]);
click(&mut h, corner);
assert!(perspective_crop_ui::pending(h.state()), "one corner starts a partial session");
assert!(perspective_crop_ui::quad(h.state()).is_none());
click_close(&mut h, "Edited.psd", tiled)?;
assert!(h.state().discard.is_some(), "without a quad the existing dirty-document prompt is available for tiled={tiled}");
assert_eq!(h.state().session.documents().len(), 2);
}
Ok(())
}
#[test]
fn a_complete_quad_leaves_other_tabs_and_the_file_close_command_available() -> Result<(), String> {
let mut h = two_documents(false)?;
draw_quad(&mut h);
let quad = perspective_crop_ui::quad(h.state());
let active = h.state().session.active().map(|doc| doc.doc.id);
click_close(&mut h, "Reference.psd", false)?;
assert!(h.state().discard.is_none());
assert_eq!(h.state().session.documents().len(), 1);
assert_eq!(h.state().session.active().map(|doc| doc.doc.id), active);
assert_eq!(perspective_crop_ui::quad(h.state()), quad);
let ctx = h.ctx.clone();
crate::menus::invoke(h.state_mut(), &ctx, "file.close", json!({}))?;
assert!(h.state().discard.is_some(), "File Close still asks before closing the dirty document");
Ok(())
}