Dialogs keep their top-left when their content resizes (#487)

Dialogs were anchored at the screen centre, so a body that grew (Layer
Style switching effects) re-centred the dialog and moved its title bar.
They now open centred, then pin their top-left after the first laid-out
frame: they grow down and right, and title-bar drags move the pinned
corner (still clamped on screen).

The Color Range resize test now asserts the top-left stays put (it
failed before the fix).

Co-authored-by: Claude Opus 5.5 <[email protected]>
This commit is contained in:
sommerperandClaude Opus 5.5 authored and GitHub committed 2026-10-07 11:40:54 +07:00
1 parent 72e2eb9644
commit 4fdb71dfc3
2 files changed
+24 -12

No files matched your search

+8 -6
View File
@@ -613,15 +613,17 @@ mod tests {
open(h.state_mut());
h.run_steps(4);
let area = egui::Id::new(("dialog", dialog_id(h.state())));
let height = |h: &Harness<'static, PhotocraftApp>| h.ctx.memory(|m| m.area_rect(area)).map_or(0.0, |r| r.height());
let rect = |h: &Harness<'static, PhotocraftApp>| h.ctx.memory(|m| m.area_rect(area)).unwrap_or(egui::Rect::NOTHING);
set(&mut h, "select", json!("outOfGamut"));
let short = height(&h);
let short = rect(&h);
set(&mut h, "select", json!("midtones"));
let tall = height(&h);
assert!(tall > short + 40.0, "Midtones adds two sliders: {short} → {tall}");
let tall = rect(&h);
assert!(tall.height() > short.height() + 40.0, "Midtones adds two sliders: {short:?} → {tall:?}");
assert_eq!(tall.min, short.min, "the dialog grows downward, it doesn't re-centre");
set(&mut h, "select", json!("outOfGamut"));
let back = height(&h);
assert!((back - short).abs() < 1.0, "the dialog must shrink back, not keep an empty band above OK: {short} → {tall} → {back}");
let back = rect(&h);
assert!((back.height() - short.height()).abs() < 1.0, "the dialog must shrink back, not keep an empty band above OK: {short:?} → {tall:?} → {back:?}");
assert_eq!(back.min, short.min);
}
#[test]
+16 -6
View File
@@ -46,12 +46,19 @@ pub fn show(app: &mut PhotocraftApp, ctx: &egui::Context) {
let mut apply_requested = false;
let title = display_title(&d);
let id = egui::Id::new(("dialog", d.id));
// Offset from centre, moved by dragging the title bar (view state only, so egui memory).
let offset: egui::Vec2 = ctx.data(|m| m.get_temp(id)).unwrap_or_default();
// Opens centred, then its top-left stays put (offset from the window's top-left, moved by
// dragging the title bar; view state only, so egui memory): a dialog whose body grows, like
// Layer Style switching effects, extends down and right instead of re-centring.
let pinned: Option<egui::Vec2> = ctx.data(|m| m.get_temp(id));
let mut drag = egui::Vec2::ZERO;
let area = egui::Modal::default_area(id).anchor(egui::Align2::CENTER_CENTER, offset);
let mut sizing = false;
let area = match pinned {
Some(offset) => egui::Modal::default_area(id).anchor(egui::Align2::LEFT_TOP, offset),
None => egui::Modal::default_area(id),
};
// Photoshop doesn't dim the window behind dialogs: previews must be judged at true contrast.
let modal = egui::Modal::new(id).area(area).backdrop_color(egui::Color32::TRANSPARENT).show(ctx, |ui| {
sizing = ui.is_sizing_pass();
ui.set_min_width(380.0);
let wide = crate::prefs_ui::width(&d.fields);
if let Some(w) = wide {
@@ -152,10 +159,13 @@ pub fn show(app: &mut PhotocraftApp, ctx: &egui::Context) {
ui.min_rect().expand(ui.spacing().menu_margin.sum().max_elem() + 2.0)
});
shown.push(modal.inner);
if drag != egui::Vec2::ZERO {
// Pin once laid out at its real size (the first frame is an invisible sizing pass).
if !sizing && (pinned.is_none() || drag != egui::Vec2::ZERO) {
let screen = ctx.content_rect();
// Keep the whole dialog (and so its title bar) on screen.
let room = ((ctx.content_rect().size() - modal.response.rect.size()) / 2.0).max(egui::Vec2::ZERO);
ctx.data_mut(|m| m.insert_temp(id, (offset + drag).clamp(-room, room)));
let room = (screen.size() - modal.response.rect.size()).max(egui::Vec2::ZERO);
let offset = pinned.unwrap_or(modal.response.rect.min - screen.min) + drag;
ctx.data_mut(|m| m.insert_temp(id, offset.clamp(egui::Vec2::ZERO, room)));
}
// Esc cancels (topmost dialog, no popup open). A click outside does nothing: Photoshop keeps
// the dialog, and the pointer may be panning or zooming the canvas under it.