Skip to content

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

Dialogs: keep the live dialog in front of older ones (#3140)

kayrockscreenprinting1f742629bb88
diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs
index 1823542fe..7cc87b262 100644
--- a/crates/ui-egui/src/dialogs.rs
+++ b/crates/ui-egui/src/dialogs.rs
@@ -92,6 +92,13 @@ fn place_id(d: &Dialog) -> egui::Id {
}
}
+/// Whether another dialog is drawn above `live` (egui's layer order, back to front).
+fn older_dialog_above(ctx: &egui::Context, live: egui::LayerId, dialogs: &[Dialog]) -> bool {
+ let others: Vec<egui::LayerId> =
+ dialogs.iter().map(|d| egui::LayerId::new(egui::Order::Foreground, egui::Id::new(("dialog", d.id)))).filter(|l| *l != live).collect();
+ ctx.memory(|m| m.layer_ids().skip_while(|l| *l != live).skip(1).any(|l| others.contains(&l)))
+}
+
pub fn show(app: &mut PhotocraftApp, ctx: &egui::Context) {
crate::layer_style::color_picker::prune(app);
crate::type_panels_ui::color_picker::prune(app);
@@ -124,6 +131,14 @@ pub fn show(app: &mut PhotocraftApp, ctx: &egui::Context) {
Some(offset) => egui::Modal::default_area(id).anchor(egui::Align2::LEFT_TOP, offset),
None => egui::Modal::default_area(id),
};
+ // Only the last dialog takes input, and egui only lets the modal drawn on top take input,
+ // so the last one must be drawn above the others. egui can raise an older one (it raises an
+ // area that is pressed or wasn't drawn the frame before), which left a greyed-out dialog
+ // in front and the live one unreachable behind it. Raise the live one only then: every
+ // frame would also cover its own popups.
+ if interactive && older_dialog_above(ctx, area.layer(), &app.ui.dialogs) {
+ ctx.move_to_top(area.layer());
+ }
let color_picker = crate::color_picker_ui::owns(&fields);
let mut modal = egui::Modal::new(id).area(area).backdrop_color(egui::Color32::TRANSPARENT);
if color_picker {
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs
index 68e71ce1b..39ca677e7 100644
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@ -2139,6 +2139,9 @@ impl PhotocraftApp {
#[cfg(test)]
mod color_swatch_tests;
+#[cfg(test)]
+mod profile_prompt_tests;
+
#[cfg(test)]
mod input_tests;
diff --git a/crates/ui-egui/src/profile_prompt_tests.rs b/crates/ui-egui/src/profile_prompt_tests.rs
new file mode 100644
index 000000000..ca206b6a5
--- /dev/null
+++ b/crates/ui-egui/src/profile_prompt_tests.rs
@@ -0,0 +1,113 @@
+//! Embedded Profile Mismatch prompts when several files open at once (File › Open with several
+//! selected): every prompt can be answered with a real click, one after another, even when the
+//! user clicks while the next file's prompt is appearing.
+
+use crate::{PhotocraftApp, Services};
+use egui::{Modifiers, PointerButton, Pos2, vec2};
+use egui_kittest::{
+ Harness,
+ kittest::{NodeT, Queryable},
+};
+use photocraft_doc::{Color, ColorMode, Document, SampleType, Size};
+use photocraft_engine::Session;
+
+/// A PNG tagged with Adobe RGB, which doesn't match the sRGB working space.
+fn adobe_png() -> Vec<u8> {
+ let mut d = Document::with_background("t", Size::new(8, 8), ColorMode::Rgb, SampleType::U8, Color::rgba(0.2, 0.6, 0.9, 1.0));
+ d.icc_profile = Some(photocraft_cms::Builtin::from_id("adobe-rgb-compat").unwrap().profile().to_bytes());
+ photocraft_io::export(&d, "t.png", &Default::default()).unwrap().bytes
+}
+
+fn harness(background: bool) -> Harness<'static, PhotocraftApp> {
+ let mut h = Harness::builder().with_size(vec2(1400.0, 900.0)).with_max_steps(64).build_eframe(|cc| {
+ PhotocraftApp::setup_context(&cc.egui_ctx, Default::default());
+ PhotocraftApp::new(
+ Session::new(),
+ Services {
+ import: Some(Box::new(|name, bytes, depth| {
+ photocraft_io::import_with_svg_group_depth(name, bytes, depth).map(|r| (r.document, r.warnings)).map_err(|e| e.to_string())
+ })),
+ ..Default::default()
+ },
+ )
+ });
+ h.state_mut().background_jobs = background;
+ h.run_steps(3);
+ h
+}
+
+/// Open `n` mismatched files in one go and wait until their prompts are up and laid out.
+fn open_together(h: &mut Harness<'static, PhotocraftApp>, n: usize) {
+ let png = adobe_png();
+ for i in 0..n {
+ h.state_mut().open_file(&format!("shot{i}.png"), &png).unwrap();
+ }
+ for _ in 0..100 {
+ h.run_steps(2);
+ if h.state().ui.dialogs.len() >= n {
+ h.run_steps(6);
+ return;
+ }
+ std::thread::sleep(std::time::Duration::from_millis(10));
+ }
+ panic!("{n} prompts, got {}", h.state().ui.dialogs.len());
+}
+
+fn press(h: &mut Harness<'static, PhotocraftApp>, p: Pos2, pressed: bool) {
+ h.event(egui::Event::PointerButton { pos: p, button: PointerButton::Primary, pressed, modifiers: Modifiers::NONE });
+}
+
+/// A real click (pointer events, hit-tested like a mouse) rather than an accessibility action.
+fn click_at(h: &mut Harness<'static, PhotocraftApp>, p: Pos2) {
+ h.event(egui::Event::PointerMoved(p));
+ h.run_steps(1);
+ for pressed in [true, false] {
+ press(h, p, pressed);
+ h.run_steps(1);
+ }
+ h.run_steps(3);
+}
+
+/// Where the live prompt's `button` is: the one prompt that isn't greyed out.
+fn live_button(h: &Harness<'static, PhotocraftApp>, button: &str) -> Pos2 {
+ let live: Vec<_> = h.query_all_by_label(button).filter(|n| !n.accesskit_node().is_disabled()).map(|n| n.rect().center()).collect();
+ assert_eq!(live.len(), 1, "exactly one prompt is live, {} open", h.state().ui.dialogs.len());
+ live[0]
+}
+
+#[test]
+fn prompts_for_files_opened_together_answer_one_at_a_time_with_real_clicks() {
+ for background in [false, true] {
+ for button in ["OK", "Cancel"] {
+ let mut h = harness(background);
+ open_together(&mut h, 3);
+ for left in (0..3).rev() {
+ let at = live_button(&h, button);
+ click_at(&mut h, at);
+ assert_eq!(h.state().ui.dialogs.len(), left, "background {background}: {button} answers the prompt in front");
+ }
+ }
+ }
+}
+
+#[test]
+fn an_older_prompt_raised_in_front_gives_way_to_the_live_one() {
+ // Only the newest dialog takes input; egui only lets the modal drawn on top take input. When
+ // egui raised an older prompt (it raises an area that is pressed or wasn't drawn the frame
+ // before), the one in front was greyed out and the live one was unreachable behind it: no
+ // click or key could answer either, so the app had to be quit (three screenshots opened
+ // together from File › Open).
+ for background in [false, true] {
+ let mut h = harness(background);
+ open_together(&mut h, 3);
+ let ok = live_button(&h, "OK");
+ let oldest = h.state().ui.dialogs[0].id;
+ h.ctx.memory_mut(|m| m.areas_mut().move_to_top(egui::LayerId::new(egui::Order::Foreground, egui::Id::new(("dialog", oldest)))));
+ h.run_steps(4);
+ // All three are the same size in the same place: a click there must reach the live one.
+ for left in [2, 1, 0] {
+ click_at(&mut h, ok);
+ assert_eq!(h.state().ui.dialogs.len(), left, "background {background}: OK on the prompt in front answers it");
+ }
+ }
+}