From 825a1d169dc500bfff80c4d997464667ff0c855d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hartmut=20N=C3=B6renberg?= <55043719+hnoerenberg@users.noreply.github.com> Date: Tue, 3 Nov 2026 15:22:33 +0100 Subject: [PATCH] fix(ui): Save for Web Matte takes only whole colours while typed (#8830) (#8890) * fix(ui): Save for Web Matte takes only whole colours while typed (#8830) The Matte field wrote every keystroke into the export settings, so a partial colour reached the previews: mid-type the settings held `#1111`, or nothing at all after a select-all plus Backspace, and the pane's encode failed (its error row shows above the panes). `matte_field` shows exactly what is typed while the field has focus and commits only `#rrggbb`, normalised through `color_picker_ui::parse_hex`, or `none` - the pattern the Color panel's hex field already uses; anything else snaps back when focus leaves. aedab302b had already moved the encode error out of the pane row, so the dialog no longer widens on an invalid matte; this removes the cause and brings back this PR's first version's width test as a guard for that half. Co-Authored-By: Claude Opus 5.5 * fix(ui): the Save for Web matte field keeps typed text across window focus (#8830) CodeRabbit 4431801542 flagged that the Matte field reads its cached text with ui.memory(|m| m.has_focus(id)) but caches/clears it with resp.has_focus(). The pinned egui 0.37 fork shows the difference is real: Response::has_focus ANDs the OS-window flag (response.rs 405-407: self.ctx.input(|i| i.focused) && self.ctx.memory(|mem| mem.has_focus(self.id))), while Memory::has_focus is only self.focused() == Some(id) (memory/mod.rs 913-915), and egui never clears the focused widget when the window loses focus - Event::WindowFocused(false) (input_state/mod.rs 443-451) only clears keys and modifiers. So alt-tabbing away deleted the typed text and the field came back showing the last whole colour. Both sides now ask the same question. The new test focuses the Matte field, types a partial #1111, drops and restores RawInput::focused, and asserts the partial text survives and that finishing the colour still reaches the settings; on the old write side it fails with the partial Matte is still in the field after the window came back: ["#ffffff"]. Gates: cargo test -p photocraft-ui-egui --lib -- file_ui = 46 passed, 0 failed; cargo clippy -p photocraft-ui-egui --all-targets -- -D warnings = clean; cargo xtask layers = OK: 31 crates, no layering violations. Formatting: CI's `cargo fmt --all -- --check` caught an unwrapped assert in the first version of the test (tests.rs:586) - I had run test, clippy and layers locally but skipped fmt, so the gate I left out is the one that broke. The assert is now wrapped as rustfmt wants it and `cargo fmt -p photocraft-ui-egui -- --check` is clean; that last push is formatting only, no behaviour or test-logic change. Co-Authored-By: Claude Opus 5.5 * fix(ui): the Save for Web matte field caches text when focus arrives mid-edit (#8830) The Matte field asked Memory::has_focus once, before ui.add, and used that one value for both sides of the cache: what the field shows, and whether typed text is kept. Those are two different questions. The display side must keep using the pre-add state: that is the frame as it started. The cache side must ask again after ui.add, because egui can move focus into the field while the add runs. In the pinned egui fork (6d518fc) the MiddleClickPaste branch of TextEdit inserts the pasted text into an unfocused field and only then calls Memory::request_focus inside the add: text_edit/builder.rs:933-974 (insert at 953-959, request_focus at 968), and Memory::request_focus sets the focus immediately, memory/mod.rs:945-950, so has_focus is true right after ui.add in the same frame. Reading focus only before the add left the pasted partial text uncached: the settings parse rejects the pasted value (the click pos inserts it inside "#ffffff"), the remove branch ran with the stale focused=false, and the next frame restored the saved colour although the field now had focus. Fix: read the pre-add focus for the display value, re-read has_focus after ui.add for the insert_temp/remove decision. Regression test drives a real focus-gain-and-text-in-one-frame: hover over the Matte field, then a single frame carrying egui::Event::MiddleClickPaste (fork-only event, data/input/event.rs:40) at the field's centre, with run_steps(1) so the one stale frame is visible. Fail-first output: the pasted partial must survive into the next frame, not be restored to the saved colour: ["#ffffff"] test result: FAILED. 46 passed; 1 failed; 0 ignored; 0 measured; 4439 filtered out; finished in 0.49s After the fix: test result: ok. 47 passed; 0 failed. clippy -D warnings, cargo fmt --check and cargo xtask layers are clean. Co-Authored-By: Claude Opus 5.5 * test(ui): pin Save for Web Matte short form, case, paste and blur (#8830) Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- crates/ui-egui/src/dialogs/file_ui/tests.rs | 216 ++++++++++++++++++++ crates/ui-egui/src/dialogs/file_ui/web.rs | 45 +++- 2 files changed, 254 insertions(+), 7 deletions(-) diff --git a/crates/ui-egui/src/dialogs/file_ui/tests.rs b/crates/ui-egui/src/dialogs/file_ui/tests.rs index 6de4ed340d..3f1ca59d08 100644 --- a/crates/ui-egui/src/dialogs/file_ui/tests.rs +++ b/crates/ui-egui/src/dialogs/file_ui/tests.rs @@ -475,6 +475,222 @@ fn save_for_web_authorizes_generated_palette_before_opening_a_picker() { } } +/// #8830: an invalid Matte (`#1111`, empty; set here the way `ui.dialog.set` would) makes the +/// preview's encode fail, and the error the dialog shows must not widen it. +#[test] +fn save_for_web_keeps_its_width_while_the_matte_is_invalid() { + let (mut app, _) = app(); + let id = open_web(&mut app); + app.ui.dialog_mut(id).unwrap().fields.insert("format".into(), json!("png8")); + app.ui.dialog_mut(id).unwrap().fields.insert("matte".into(), json!("#111111")); + let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(1600.0, 1100.0)).build_ui_state( + |ui, app| { + if ui.ctx().fonts(|f| f.families().contains(&egui::FontFamily::Name("semibold".into()))) { + crate::dialogs::show(app, ui.ctx()); + } + }, + app, + ); + PhotocraftApp::setup_context(&h.ctx, Default::default()); + h.run_steps(5); + let dialog = |h: &egui_kittest::Harness<'_, PhotocraftApp>| h.ctx.data(|m| m.get_temp::>(egui::Id::unique("pc-dialog-rects"))).unwrap()[0]; + let before = dialog(&h); + for matte in ["#1111", "", "#22222", "#111111"] { + h.state_mut().ui.dialog_mut(id).unwrap().fields.insert("matte".into(), json!(matte)); + h.run_steps(4); + let after = dialog(&h); + assert_eq!(after.width(), before.width(), "matte {matte:?}: before {before:?}, after {after:?}"); + } +} + +/// #8830: a typed Matte reaches the settings only once it is a whole colour (or `none`), so the +/// previews never see a partial one such as `#1111` while the user is still typing, and the dialog +/// does not resize under them (the report: "the whole interface jumps wider"). +#[test] +fn save_for_web_matte_takes_only_whole_colours() { + use egui_kittest::kittest::{NodeT, Queryable}; + let (app, _) = app(); + let (mut h, id) = web_size_dialog(app); + h.state_mut().ui.dialog_mut(id).unwrap().fields.insert("format".into(), json!("png8")); + h.run_steps(3); + let matte = |h: &egui_kittest::Harness<'static, PhotocraftApp>| h.state().ui.dialogs.iter().find(|d| d.id == id).unwrap().fields["matte"].clone(); + let dialog = + |h: &egui_kittest::Harness<'static, PhotocraftApp>| h.ctx.data(|m| m.get_temp::>(egui::Id::unique("pc-dialog-rects"))).unwrap()[0]; + let before = dialog(&h); + let field = h + .query_all_by_role(egui::accesskit::Role::TextInput) + .find(|n| n.accesskit_node().value().as_deref() == Some("#ffffff")) + .map(|n| n.rect()) + .expect("the Matte field"); + h.hover_at(field.center()); + h.run_steps(1); + h.drag_at(field.center()); + h.run_steps(1); + h.drop_at(field.center()); + h.run_steps(2); + for (typed, want) in [("#1111", "#ffffff"), ("", "#ffffff"), ("#22222g", "#ffffff"), ("#2A2a2A", "#2a2a2a"), ("None", "none")] { + h.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::A); + h.key_press(egui::Key::Backspace); + if !typed.is_empty() { + h.event(egui::Event::Text(typed.into())); + } + h.run_steps(2); + let now = dialog(&h); + assert_eq!((now.width(), now.height()), (before.width(), before.height()), "typing {typed:?} resized the dialog: {before:?} -> {now:?}"); + assert_eq!(matte(&h), json!(want), "typed {typed:?}"); + let shown = h.query_all_by_role(egui::accesskit::Role::TextInput).any(|n| n.accesskit_node().value().unwrap_or_default() == typed); + assert!(shown, "the field shows {typed:?} as typed"); + } +} + +/// #8830, review follow-ups: the field takes `#rrggbb` only (no `#rgb` short form: `parse_hex` +/// wants six digits), accepts upper case (stored lower case) and a paste, keeps the typed draft in +/// the field while the setting holds the last whole colour, and on blur shows that committed colour +/// instead of the invalid draft. +#[test] +fn save_for_web_matte_short_form_case_paste_and_blur() { + use egui_kittest::kittest::{NodeT, Queryable}; + let (app, _) = app(); + let (mut h, id) = web_size_dialog(app); + h.state_mut().ui.dialog_mut(id).unwrap().fields.insert("format".into(), json!("png8")); + h.run_steps(3); + let matte = |h: &egui_kittest::Harness<'static, PhotocraftApp>| h.state().ui.dialogs.iter().find(|d| d.id == id).unwrap().fields["matte"].clone(); + let values = |h: &egui_kittest::Harness<'static, PhotocraftApp>| -> Vec { + h.query_all_by_role(egui::accesskit::Role::TextInput).map(|n| n.accesskit_node().value().unwrap_or_default()).collect() + }; + let field = h + .query_all_by_role(egui::accesskit::Role::TextInput) + .find(|n| n.accesskit_node().value().as_deref() == Some("#ffffff")) + .map(|n| n.rect()) + .expect("the Matte field"); + h.hover_at(field.center()); + h.run_steps(1); + h.drag_at(field.center()); + h.run_steps(1); + h.drop_at(field.center()); + h.run_steps(2); + let replace_with = |h: &mut egui_kittest::Harness<'static, PhotocraftApp>, text: &str, paste: bool| { + h.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::A); + h.key_press(egui::Key::Backspace); + h.event(if paste { egui::Event::Paste(text.into()) } else { egui::Event::Text(text.into()) }); + h.run_steps(2); + }; + // (text, pasted, setting afterwards) + for (typed, paste, want) in [ + ("#fff", false, "#ffffff"), // short form is not a colour here: draft only, setting unchanged + ("#ABCDEF", false, "#abcdef"), // upper case is accepted and stored lower case + ("#12AB3", true, "#abcdef"), // a partial paste is a draft like partial typing + ("#12AB34", true, "#12ab34"), // a whole pasted colour is taken + ] { + replace_with(&mut h, typed, paste); + assert_eq!(matte(&h), json!(want), "{typed:?} (paste: {paste})"); + assert!(values(&h).iter().any(|v| v == typed), "the field keeps {typed:?} as typed: {:?}", values(&h)); + } + // Blur with an invalid draft: the field shows the last committed colour, not the draft. + replace_with(&mut h, "#22222g", false); + assert_eq!(matte(&h), json!("#12ab34")); + assert!(values(&h).iter().any(|v| v == "#22222g"), "the draft is shown while focused"); + h.key_press(egui::Key::Tab); + h.run_steps(3); + let shown = values(&h); + assert!(shown.iter().any(|v| v == "#12ab34"), "blur restores the committed colour: {shown:?}"); + assert!(!shown.iter().any(|v| v == "#22222g"), "the invalid draft is gone after blur: {shown:?}"); + assert_eq!(matte(&h), json!("#12ab34")); +} + +/// #8830, the review follow-up: alt-tabbing away from the window and back must not throw away a +/// partial Matte that is still being typed. egui keeps keyboard focus on the field while the OS +/// window is unfocused (`Memory::has_focus` ignores the window, `Response::has_focus` does not), +/// so the side that caches the typed text has to ask the same question the read side asks. +#[test] +fn save_for_web_matte_keeps_typed_text_across_window_focus_loss() { + use egui_kittest::kittest::{NodeT, Queryable}; + let (app, _) = app(); + let (mut h, id) = web_size_dialog(app); + h.state_mut().ui.dialog_mut(id).unwrap().fields.insert("format".into(), json!("png8")); + h.run_steps(3); + let shown = |h: &egui_kittest::Harness<'static, PhotocraftApp>, want: &str| { + h.query_all_by_role(egui::accesskit::Role::TextInput).any(|n| n.accesskit_node().value().unwrap_or_default() == want) + }; + let matte = |h: &egui_kittest::Harness<'static, PhotocraftApp>| h.state().ui.dialogs.iter().find(|d| d.id == id).unwrap().fields["matte"].clone(); + let field = h + .query_all_by_role(egui::accesskit::Role::TextInput) + .find(|n| n.accesskit_node().value().as_deref() == Some("#ffffff")) + .map(|n| n.rect()) + .expect("the Matte field"); + h.hover_at(field.center()); + h.run_steps(1); + h.drag_at(field.center()); + h.run_steps(1); + h.drop_at(field.center()); + h.run_steps(2); + h.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::A); + h.key_press(egui::Key::Backspace); + h.event(egui::Event::Text("#1111".into())); + h.run_steps(2); + assert!(shown(&h, "#1111"), "the field shows the partial value as typed"); + assert_eq!(matte(&h), json!("#ffffff"), "a partial value must not reach the settings"); + let focused = h.ctx.memory(|m| m.focused()); + assert!(focused.is_some(), "the Matte field holds egui focus"); + // The OS window loses focus (alt-tab away): only `RawInput::focused` goes false, egui itself + // keeps focus on the field. + h.input_mut().focused = false; + h.run_steps(1); + assert!(!h.ctx.input(|i| i.focused), "the frame ran with the window unfocused"); + assert_eq!(h.ctx.memory(|m| m.focused()), focused, "egui keeps focus on the field while the window is unfocused"); + // …and comes back to the same window. + h.input_mut().focused = true; + h.run_steps(2); + assert!( + shown(&h, "#1111"), + "the partial Matte is still in the field after the window came back: {:?}", + h.query_all_by_role(egui::accesskit::Role::TextInput).map(|n| n.accesskit_node().value().unwrap_or_default()).collect::>() + ); + assert_eq!(matte(&h), json!("#ffffff"), "the settings still hold the last whole colour"); + // Finishing the colour takes it, so the field was live, not frozen. + h.event(egui::Event::Text("11".into())); + h.run_steps(2); + assert_eq!(matte(&h), json!("#111111"), "typing on completes as usual"); +} + +/// #8830, the second follow-up: a middle-click paste into an unfocused Matte field inserts the text +/// and moves focus to the field inside the same `ui.add`. In the pinned egui fork +/// (`crates/egui/src/widgets/text_edit/builder.rs:933-974`) the `MiddleClickPaste` branch writes the +/// text into the field's buffer and then calls `Memory::request_focus`, which takes effect the same +/// frame (`crates/egui/src/memory/mod.rs:945-950`). If the cache write side looks at the focus state +/// read before `add`, it drops the pasted partial text, and the following frame restores the saved +/// colour even though the field now has focus. +#[test] +fn save_for_web_matte_caches_a_middle_click_paste_that_arrives_with_focus() { + use egui_kittest::kittest::{NodeT, Queryable}; + let (app, _) = app(); + let (mut h, id) = web_size_dialog(app); + h.state_mut().ui.dialog_mut(id).unwrap().fields.insert("format".into(), json!("png8")); + h.run_steps(3); + let matte = |h: &egui_kittest::Harness<'static, PhotocraftApp>| h.state().ui.dialogs.iter().find(|d| d.id == id).unwrap().fields["matte"].clone(); + let values = |h: &egui_kittest::Harness<'static, PhotocraftApp>| -> Vec { + h.query_all_by_role(egui::accesskit::Role::TextInput).map(|n| n.accesskit_node().value().unwrap_or_default()).collect() + }; + let field = h + .query_all_by_role(egui::accesskit::Role::TextInput) + .find(|n| n.accesskit_node().value().as_deref() == Some("#ffffff")) + .map(|n| n.rect()) + .expect("the Matte field"); + assert!(h.ctx.memory(|m| m.focused()).is_none(), "the Matte field starts unfocused"); + h.hover_at(field.center()); + h.run_steps(1); + // One frame: the PRIMARY-selection "#1111" is pasted at the click pos while the field gains + // focus, all inside `ui.add` - run_steps(1) so the single stale frame is visible, not a run() + // that would settle past the restore. + h.event(egui::Event::MiddleClickPaste { pos: field.center(), text: "#1111".into() }); + h.run_steps(1); + assert!(h.ctx.memory(|m| m.focused()).is_some(), "the paste moved focus to the field"); + h.run_steps(1); + let shown = values(&h); + assert!(shown.iter().any(|v| v.contains("#1111")), "the pasted partial must survive into the next frame, not be restored to the saved colour: {shown:?}"); + assert_eq!(matte(&h), json!("#ffffff"), "a partial value must not reach the settings"); +} + #[test] fn web_preview_grows_with_resize_and_accepts_zoom_and_pan() { let (mut app, _) = app(); diff --git a/crates/ui-egui/src/dialogs/file_ui/web.rs b/crates/ui-egui/src/dialogs/file_ui/web.rs index 865215d4b5..fe9941f9e7 100644 --- a/crates/ui-egui/src/dialogs/file_ui/web.rs +++ b/crates/ui-egui/src/dialogs/file_ui/web.rs @@ -380,13 +380,7 @@ pub(super) fn web_body(app: &mut PhotocraftApp, ui: &mut egui::Ui, f: &mut Map) { + let t = Tokens::get(ui.ctx()); + let label = ui.label(egui::RichText::new(tl!("Matte")).color(t.text_dim)); + let id = ui.make_persistent_id("web-matte"); + let was_focused = ui.memory(|m| m.has_focus(id)); + let typed = if was_focused { ui.data(|d| d.get_temp::(id)) } else { None }; + let mut text = typed.unwrap_or_else(|| s(f, "matte", "#ffffff")); + let resp = ui.add(egui::TextEdit::singleline(&mut text).id(id).desired_width(80.0)).labelled_by(label.id); + if ui.memory(|m| m.has_focus(id)) { + ui.data_mut(|d| d.insert_temp(id, text.clone())); + } else { + ui.data_mut(|d| d.remove::(id)); + } + if !resp.changed() { + return; + } + let matte = crate::color_picker_ui::parse_hex(&text) + .map(crate::color_picker_ui::hex) + .or_else(|| text.trim().eq_ignore_ascii_case("none").then(|| "none".to_string())); + if let Some(matte) = matte { + f.insert("matte".into(), json!(matte)); + } +} + /// A size row in a two-column grid: the label, then a field showing `shown`. Returns the typed /// value when it changed, compared with the value the widget was given so a field that only /// clamps does not count as an edit.