Skip to content

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

Preferences: the settings fill the resized dialog, buttons at the bottom (#7420) (#7462)

skylar taylor-barrick92c4457b16f2
diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs
index 43b4f3d3f..81e04e570 100644
--- a/crates/ui-egui/src/dialogs.rs
+++ b/crates/ui-egui/src/dialogs.rs
@@ -104,15 +104,22 @@ fn older_dialog_above(ctx: &egui::Context, live: egui::LayerId, dialogs: &[Dialo
ctx.memory(|m| m.layer_ids().skip_while(|l| *l != live).skip(1).any(|l| others.contains(&l)))
}
-fn resizable_dialog(fields: &serde_json::Map<String, Value>) -> bool {
- fields.contains_key("__web") || crate::prefs_ui::is_preferences(fields)
+/// The size a resizable dialog opens at, or `None` for a dialog sized by its contents.
+fn resizable_dialog(fields: &serde_json::Map<String, Value>) -> Option<egui::Vec2> {
+ if crate::prefs_ui::is_preferences(fields) {
+ Some(crate::prefs_ui::PREFERENCES_SIZE)
+ } else if fields.contains_key("__web") {
+ Some(egui::vec2(960.0, 900.0))
+ } else {
+ None
+ }
}
fn show_modal<T>(
modal: egui::Modal,
ctx: &egui::Context,
id: egui::Id,
- resizable: bool,
+ resizable: Option<egui::Vec2>,
interactive: bool,
contents: impl FnOnce(&mut egui::Ui) -> T,
) -> egui::ModalResponse<T> {
@@ -121,14 +128,9 @@ fn show_modal<T>(
if !interactive {
ui.disable();
}
- if resizable {
+ if let Some(size) = resizable {
let max = (ctx.content_rect().size() - egui::vec2(40.0, 40.0)).max(egui::vec2(560.0, 400.0));
- egui::Resize::default()
- .id(id.with("resize"))
- .default_size(egui::vec2(960.0, 900.0).min(max))
- .min_size(egui::vec2(560.0, 400.0))
- .max_size(max)
- .show(ui, contents)
+ egui::Resize::default().id(id.with("resize")).default_size(size.min(max)).min_size(egui::vec2(560.0, 400.0)).max_size(max).show(ui, contents)
} else {
contents(ui)
}
@@ -207,7 +209,8 @@ pub fn show(app: &mut PhotocraftApp, ctx: &egui::Context) {
// Photoshop doesn't dim the window behind dialogs: previews must be judged at true contrast.
let web = fields.contains_key("__web");
let web_popup_open = web && egui::Popup::is_any_open(ctx);
- let modal = show_modal(modal, ctx, id, resizable_dialog(&fields), interactive, |ui| {
+ let resizable = resizable_dialog(&fields);
+ let modal = show_modal(modal, ctx, id, resizable, interactive, |ui| {
sizing = ui.is_sizing_pass();
let available = ui.available_size();
// A dialog of a few short fields (`__compact`, Fit Image) is narrower (#4673): the title
@@ -218,7 +221,9 @@ pub fn show(app: &mut PhotocraftApp, ctx: &egui::Context) {
if !compact {
ui.set_min_width(if curves { 300.0 } else { 380.0 });
}
- let wide = crate::prefs_ui::width(&d.fields, (ctx.content_rect().width() - 48.0).max(380.0));
+ // A resizable dialog's contents fill its resizable area (#7420), the others the window.
+ let room = if resizable.is_some() { available.x } else { (ctx.content_rect().width() - 48.0).max(380.0) };
+ let wide = crate::prefs_ui::width(&d.fields, room);
if let Some(w) = wide {
ui.set_min_width(w.min(460.0));
}
@@ -1132,9 +1137,68 @@ mod preference_resize_tests {
let ordinary = json!({});
let web = json!({"__web": true});
- assert!(resizable_dialog(prefs.as_object().unwrap()));
- assert!(!resizable_dialog(shortcuts.as_object().unwrap()));
- assert!(!resizable_dialog(ordinary.as_object().unwrap()));
- assert!(resizable_dialog(web.as_object().unwrap()));
+ assert_eq!(resizable_dialog(prefs.as_object().unwrap()), Some(crate::prefs_ui::PREFERENCES_SIZE));
+ assert_eq!(resizable_dialog(shortcuts.as_object().unwrap()), None);
+ assert_eq!(resizable_dialog(ordinary.as_object().unwrap()), None);
+ assert_eq!(resizable_dialog(web.as_object().unwrap()), Some(egui::vec2(960.0, 900.0)));
+ }
+
+ /// Drags the dialog's resize grip (its frame's bottom-right corner) by `by`.
+ fn drag_corner(h: &mut egui_kittest::Harness<'_, PhotocraftApp>, by: egui::Vec2) {
+ let corner = frame(h).max - egui::vec2(14.0, 14.0);
+ h.event(egui::Event::PointerMoved(corner));
+ h.step();
+ h.event(egui::Event::PointerButton { pos: corner, button: egui::PointerButton::Primary, pressed: true, modifiers: egui::Modifiers::NONE });
+ h.step();
+ h.event(egui::Event::PointerMoved(corner + by));
+ h.step();
+ h.event(egui::Event::PointerButton { pos: corner + by, button: egui::PointerButton::Primary, pressed: false, modifiers: egui::Modifiers::NONE });
+ h.run_steps(4);
+ }
+
+ fn frame(h: &egui_kittest::Harness<'_, PhotocraftApp>) -> egui::Rect {
+ let rects: Vec<egui::Rect> = h.ctx.data(|m| m.get_temp(egui::Id::new(RECTS))).unwrap();
+ rects[0]
+ }
+
+ /// The button row sits in the frame's bottom-right corner (its order follows the platform):
+ /// the contents fill the dialog, no empty space.
+ fn assert_buttons_in_corner(h: &egui_kittest::Harness<'_, PhotocraftApp>, when: &str) {
+ use egui_kittest::kittest::Queryable;
+ let f = frame(h);
+ let row = ["OK", "Cancel", "Apply"].map(|l| h.get_by_label(l).rect()).into_iter().reduce(|a, b| a.union(b)).unwrap();
+ // Inside the frame (not past its edge) and near it.
+ let near = |gap: f32| (0.0..24.0).contains(&gap);
+ assert!(near(f.bottom() - row.bottom()) && near(f.right() - row.right()), "{when}: frame {f:?}, buttons {row:?}");
+ }
+
+ #[test]
+ fn preferences_contents_follow_the_dialog_when_it_is_resized() {
+ // #7420: the dialog resized but its contents kept a size from the window, leaving the
+ // buttons part-way up a dialog that opened, or was dragged, larger than them.
+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
+ crate::prefs_ui::open_preferences(&mut app, "interface");
+ let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(1600.0, 1000.0)).build_ui_state(
+ |ui, app| {
+ if ui.ctx().fonts(|f| f.families().contains(&egui::FontFamily::Name("semibold".into()))) {
+ show(app, ui.ctx());
+ }
+ },
+ app,
+ );
+ PhotocraftApp::setup_context(&h.ctx, Default::default());
+ h.run_steps(5);
+ assert_buttons_in_corner(&h, "on open");
+ let opened = frame(&h);
+ drag_corner(&mut h, egui::vec2(220.0, 80.0));
+ assert!(frame(&h).width() > opened.width() + 150.0 && frame(&h).height() > opened.height() + 50.0, "wider: {:?}", frame(&h));
+ assert_buttons_in_corner(&h, "wider");
+ drag_corner(&mut h, egui::vec2(-500.0, -300.0));
+ let smaller = frame(&h);
+ assert!(smaller.width() < opened.width() - 200.0 && smaller.height() < opened.height() - 150.0, "smaller: {smaller:?}");
+ assert_buttons_in_corner(&h, "smaller");
+ // The contents never ask for more than the dialog gives them, so it keeps its size.
+ h.run_steps(10);
+ assert_eq!(frame(&h), smaller);
}
}
diff --git a/crates/ui-egui/src/prefs_ui.rs b/crates/ui-egui/src/prefs_ui.rs
index 6c910865b..721340702 100644
--- a/crates/ui-egui/src/prefs_ui.rs
+++ b/crates/ui-egui/src/prefs_ui.rs
@@ -968,10 +968,22 @@ pub fn apply(app: &mut PhotocraftApp, id: u64) -> Result<Value, String> {
Ok(result)
}
-/// Max dialog width for our dialogs.
+/// The size Preferences opens at: room for the widest section and about 30 settings rows. The
+/// dialog's resize grip changes it, and its contents follow (#7420).
+pub const PREFERENCES_SIZE: egui::Vec2 = egui::vec2(1040.0, 800.0);
+
+/// The height of the settings area (section list and fields) when `available` is left below the
+/// search field: everything but the dialog's button row (`item_spacing` + 8 pt + the buttons) and
+/// a little slack, never less than a few rows.
+fn settings_height(available: f32, item_spacing: f32, button_height: f32) -> f32 {
+ (available - (item_spacing + 8.0 + button_height + 4.0)).max(160.0)
+}
+
+/// Max dialog width for our dialogs. Preferences fills the width it is given (`available`, its
+/// resizable area).
pub fn width(fields: &Map<String, Value>, available: f32) -> Option<f32> {
match fields.get("__prefsui").and_then(Value::as_str)? {
- "prefs" => Some(available.clamp(380.0, 1040.0)),
+ "prefs" => Some(available.max(380.0)),
"shortcuts" => Some(720.0),
_ => Some(460.0),
}
@@ -1246,7 +1258,10 @@ fn prefs_body(ui: &mut egui::Ui, f: &mut Map<String, Value>, system: Option<egui
}
}
});
- let content_height = (ui.ctx().content_rect().height() - 240.0).clamp(240.0, 680.0);
+ // The settings fill the dialog's resizable area down to the button row (#7420). Sizing them
+ // from the window instead left a resized dialog with empty space and the buttons mid-way.
+ // The area grows to fit its content, so this must never ask for more than it was given.
+ let content_height = settings_height(ui.available_height(), ui.spacing().item_spacing.y, crate::widgets::button_height(ui.ctx()));
crate::widgets::vline(ui, content_height);
ui.vertical(|ui| {
ui.set_width((ui.available_width() - 12.0).max(340.0));
@@ -2558,15 +2573,30 @@ mod tests {
}
#[test]
- fn preferences_dialog_width_follows_the_window() {
+ fn preferences_dialog_width_follows_its_resizable_area() {
let prefs = serde_json::json!({"__prefsui": "prefs"});
let prefs = prefs.as_object().unwrap();
assert_eq!(super::width(prefs, 200.0), Some(380.0));
assert_eq!(super::width(prefs, 800.0), Some(800.0));
- assert_eq!(super::width(prefs, 3000.0), Some(1040.0));
+ // A dialog resized wider than it opens is filled, not left with empty space (#7420).
+ assert_eq!(super::width(prefs, 3000.0), Some(3000.0));
let shortcuts = serde_json::json!({"__prefsui": "shortcuts"});
assert_eq!(super::width(shortcuts.as_object().unwrap(), 3000.0), Some(720.0));
}
+
+ #[test]
+ fn preferences_settings_fill_the_dialog_down_to_the_buttons() {
+ // The settings area plus the button row (spacing, 8 pt gap, buttons) stays inside what the
+ // resizable area gives it, at any size: the area grows to fit its content, so asking for
+ // more would make the dialog grow every frame. The area follows the dialog's size (#7420).
+ for available in [300.0, 600.0, 1400.0] {
+ let h = super::settings_height(available, 4.0, 30.0);
+ assert!(h + 4.0 + 8.0 + 30.0 <= available, "{available}");
+ assert!(h + 4.0 + 8.0 + 30.0 + 8.0 >= available, "{available}");
+ }
+ assert_eq!(super::settings_height(1400.0, 4.0, 30.0) - super::settings_height(600.0, 4.0, 30.0), 800.0);
+ assert_eq!(super::settings_height(50.0, 4.0, 30.0), 160.0);
+ }
use super::*;
use std::sync::{Arc, Mutex};
diff --git a/crates/ui-egui/src/widgets.rs b/crates/ui-egui/src/widgets.rs
index 533f1ee05..b393c0787 100644
--- a/crates/ui-egui/src/widgets.rs
+++ b/crates/ui-egui/src/widgets.rs
@@ -1276,9 +1276,14 @@ fn button_size(ui: &Ui, label: &str, min_width: f32) -> Vec2 {
button_layout(ui, label, min_width, None, Tokens::get(ui.ctx()).text).1
}
+/// The height of a primary or secondary button (and so of a dialog's button row).
+pub fn button_height(ctx: &egui::Context) -> f32 {
+ if Tokens::get(ctx).pro { 28.0 } else { 30.0 }
+}
+
fn button_layout(ui: &Ui, label: &str, min_width: f32, height: Option<f32>, fg: Color32) -> (std::sync::Arc<egui::Galley>, Vec2) {
let galley = ui.painter().layout_no_wrap(tl!(label).to_owned(), theme::medium(13.0), fg);
- let h = height.unwrap_or(if Tokens::get(ui.ctx()).pro { 28.0 } else { 30.0 });
+ let h = height.unwrap_or(button_height(ui.ctx()));
let size = vec2((galley.size().x + 28.0).max(min_width), h);
(galley, size)
}