Skip to content

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

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

sommerper4fdb71dfc3ef
diff --git a/crates/ui-egui/src/color_range_ui.rs b/crates/ui-egui/src/color_range_ui.rs
index f9b12e779..543868e65 100644
--- a/crates/ui-egui/src/color_range_ui.rs
+++ b/crates/ui-egui/src/color_range_ui.rs
@@ -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]
diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs
index a0f88009b..51e4e30ae 100644
--- a/crates/ui-egui/src/dialogs.rs
+++ b/crates/ui-egui/src/dialogs.rs
@@ -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.