diff --git a/crates/ui-egui/src/tools/transform_tool/draw.rs b/crates/ui-egui/src/tools/transform_tool/draw.rs index a8723af30e..dc7d45275b 100644 --- a/crates/ui-egui/src/tools/transform_tool/draw.rs +++ b/crates/ui-egui/src/tools/transform_tool/draw.rs @@ -91,6 +91,10 @@ pub fn show_in_canvas(app: &mut PhotocraftApp, ui: &mut egui::Ui, rect: egui::Re if let Some(t) = app.ui.transform.as_mut() { t.session.settle(now, busy); } + // A pass that ended the session (Cancel, or a commit that finished at once) hands back no picture: its moving + // pixels would paint over the restored document. A commit still running in the background keeps the preview, and + // with it the picture. + let picture = if app.transform_preview.is_some() { picture } else { Vec::new() }; SessionShown { shown: true, snapped, picture } } diff --git a/crates/ui-egui/tests/it/free_transform_preview_canvas.rs b/crates/ui-egui/tests/it/free_transform_preview_canvas.rs index 0b7ff0eb5b..f5890874b7 100644 --- a/crates/ui-egui/tests/it/free_transform_preview_canvas.rs +++ b/crates/ui-egui/tests/it/free_transform_preview_canvas.rs @@ -22,8 +22,13 @@ fn square() -> Rect { } fn harness(gpu_canvas: bool) -> Option { - let built = std::panic::catch_unwind(|| { + // Only a panic before the app callback runs (no adapter, no render state) is a skip; one from the app's own setup + // is a failure. + let entered = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)); + let flag = entered.clone(); + let built = std::panic::catch_unwind(move || { egui_kittest::Harness::builder().with_size(egui::vec2(900.0, 640.0)).with_pixels_per_point(1.0).with_max_steps(64).wgpu().build_eframe(move |cc| { + flag.store(true, std::sync::atomic::Ordering::SeqCst); PhotocraftApp::setup_context(&cc.egui_ctx, Default::default()); let mut app = PhotocraftApp::with_context(cc.egui_ctx.clone(), photocraft_engine::Session::new(), Default::default()); if gpu_canvas && let Some(rs) = cc.wgpu_render_state.as_ref() { @@ -34,6 +39,7 @@ fn harness(gpu_canvas: bool) -> Option { }); match built { Ok(h) => Some(h), + Err(panic) if entered.load(std::sync::atomic::Ordering::SeqCst) => std::panic::resume_unwind(panic), Err(_) => { eprintln!("skipping: no GPU adapter"); None @@ -137,3 +143,38 @@ fn free_transform_shows_the_layer_inside_its_box_on_the_gpu_canvas() { fn free_transform_shows_the_layer_inside_its_box_on_the_cpu_canvas() { shows_the_layer_inside_the_box(false); } + +/// Escape after a move puts the square back in the frame that cancels: the cancelled preview's moving pixels are not +/// painted over the restored document where the box had taken them. +fn cancelling_shows_no_moved_pixels(gpu_canvas: bool) { + let _gpu = photocraft_testkit::gpu_lock(); + let Some(mut h) = harness(gpu_canvas) else { return }; + setup(&mut h); + control(&mut h, "ui.menu.invoke", json!({"id": "edit.freeTransform"})); + let (from, to) = (screen(&h, 150.0, 150.0), screen(&h, 210.0, 150.0)); + pointer_move(&mut h, from); + button(&mut h, from, true); + for i in 1..=6 { + pointer_move(&mut h, from + (to - from) * (i as f32 / 6.0)); + } + button(&mut h, to, false); + assert!(is_red(rgb_at(&mut h, 230.5, 130.5)), "the box moved the square"); + + h.event(egui::Event::Key { key: egui::Key::Escape, physical_key: None, pressed: true, repeat: false, modifiers: Modifiers::NONE }); + h.step(); + assert!(h.state().ui.transform.is_none(), "Escape ended Free Transform"); + let moved = rgb_at(&mut h, 230.5, 130.5); + assert!(is_white(moved), "the cancelling frame shows no moved pixels: {moved:?}"); + let back = rgb_at(&mut h, 150.5, 130.5); + assert!(is_red(back), "the square is back in its place: {back:?}"); +} + +#[test] +fn cancelling_free_transform_shows_no_moved_pixels_on_the_gpu_canvas() { + cancelling_shows_no_moved_pixels(true); +} + +#[test] +fn cancelling_free_transform_shows_no_moved_pixels_on_the_cpu_canvas() { + cancelling_shows_no_moved_pixels(false); +} diff --git a/xtask/src/layers.rs b/xtask/src/layers.rs index c7ed3f95d9..825a35daa3 100644 --- a/xtask/src/layers.rs +++ b/xtask/src/layers.rs @@ -102,8 +102,9 @@ pub const UI_CRATES: &[&str] = &["egui", "eframe", "winit", "egui_kittest", "cra /// Facade crates that are a UI toolkit dependency only with some of their features (#15467): `craft` carries the /// whole craft UI framework behind `ui` (and the features that turn it on), while lower layers use its non-UI parts -/// (imaging, fonts, keymap, settings). Feature names ending in `*` are prefixes. -pub const UI_FEATURES: &[(&str, &[&str])] = &[("craft", &["ui", "ui-*", "screen-testing"])]; +/// (imaging, fonts, keymap, settings). `keymap-egui` adds the keymap's egui key conversions, which bring egui itself. +/// Feature names ending in `*` are prefixes. +pub const UI_FEATURES: &[(&str, &[&str])] = &[("craft", &["ui", "ui-*", "screen-testing", "keymap-egui"])]; /// First layer allowed to use UI crates. pub const UI_MIN_LAYER: u8 = 6; @@ -253,7 +254,11 @@ pub fn from_metadata(meta: &Value) -> Result, String> { _ => DepKind::Normal, }; let workspace = members.contains(&dname.as_str()) || d["path"].is_string(); - let features = d["features"].as_array().into_iter().flatten().filter_map(|f| f.as_str().map(str::to_owned)).collect(); + let mut features: Vec = d["features"].as_array().into_iter().flatten().filter_map(|f| f.as_str().map(str::to_owned)).collect(); + // The package's own `[features]` can turn on a dependency's features (`default = ["craft/ui"]`), named by + // the dependency's key, which is its rename when it has one. Any of them may be on, so all count. + let key = d["rename"].as_str().unwrap_or(&dname); + features.extend(forwarded_features(&p["features"], key)); deps.push(Dep { name: dname, kind, workspace, features }); } out.push(Crate { name, deps }); @@ -262,6 +267,15 @@ pub fn from_metadata(meta: &Value) -> Result, String> { Ok(out) } +/// The features of dependency `key` that the package's `[features]` table (`features` in `cargo metadata`) turns on: +/// `key/feat` and `key?/feat` entries. +fn forwarded_features<'a>(table: &'a Value, key: &'a str) -> impl Iterator + 'a { + table.as_object().into_iter().flat_map(|t| t.values()).filter_map(Value::as_array).flatten().filter_map(Value::as_str).filter_map(move |entry| { + let (dep, feature) = entry.split_once('/')?; + (dep.strip_suffix('?').unwrap_or(dep) == key).then(|| feature.to_owned()) + }) +} + pub fn describe(class: Option) -> String { match class { Some(Class::Layer(l)) => format!("L{l}"), @@ -398,6 +412,36 @@ mod tests { assert!(matches!(check(&g)[..], [Violation::UiBelowL6 { .. }])); } + /// The keymap's egui conversions bring egui; the plain keymap does not. + #[test] + fn keymap_egui_is_a_ui_feature() { + assert!(matches!(check(&[with_features("photocraft-engine", "craft", Normal, &["keymap-egui"])])[..], [Violation::UiBelowL6 { .. }])); + assert!(check(&[with_features("photocraft-engine", "craft", Normal, &["keymap"])]).is_empty()); + } + + /// A UI feature turned on by the package's own `[features]` (also through a renamed dependency) counts; other + /// forwarded features don't. + #[test] + fn metadata_reads_features_forwarded_by_the_package() { + let meta = |features: &str, rename: &str| -> Value { + serde_json::from_str(&format!( + r#"{{"packages":[{{"name":"photocraft-engine","features":{features},"dependencies":[ + {{"name":"craft","kind":null,"features":["keymap"]{rename}}}]}}]}}"# + )) + .unwrap() + }; + for (features, rename) in [(r#"{"default":["craft/ui"]}"#, ""), (r#"{"gui":["c?/ui"]}"#, r#","rename":"c""#)] { + let g = from_metadata(&meta(features, rename)).unwrap(); + assert!(matches!(check(&g)[..], [Violation::UiBelowL6 { .. }]), "{features}"); + } + for (features, rename) in + [(r#"{"default":["craft/imaging"]}"#, ""), (r#"{"gui":["other/ui","dep:craft"]}"#, ""), (r#"{"gui":["craft/ui"]}"#, r#","rename":"c""#)] + { + let g = from_metadata(&meta(features, rename)).unwrap(); + assert!(check(&g).is_empty(), "{features} {rename}: {:?}", check(&g)); + } + } + #[test] fn unregistered_crate_is_error() { let v = check(&[c("photocraft-mystery", &[])]);