PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Preferences: Zoom Resizes Windows resizes floating document windows, and the Zoom tool shows it as Resize Windows to Fit (#204)
KLBMinhLong0b5f9df51e98
diff --git a/crates/engine/src/prefs.rs b/crates/engine/src/prefs.rsindex 77acd2e34..dfbeb0cd8 100644--- a/crates/engine/src/prefs.rs+++ b/crates/engine/src/prefs.rs@@ -1029,7 +1029,6 @@ pub const SECTIONS: [(&str, &str); 18] = [pub const HIDDEN_UNTIL_IMPLEMENTED: &[&str] = &["general.colorPicker","general.animatedZoom",- "general.zoomResizesWindows","workspace.autoShowHiddenPanels","workspace.enableFloatingDocumentWindowDocking","fileHandling.saveInBackground",diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rsindex 3ebecc75e..3c59cb105 100644--- a/crates/ui-egui/src/canvas.rs+++ b/crates/ui-egui/src/canvas.rs@@ -6033,13 +6033,15 @@ pub fn extra_windows(app: &mut PhotocraftApp, ctx: &egui::Context) {}let mut view = w.view.clone();let mut close = false;- ctx.show_viewport_immediate(vid, builder, |ui, _class| {+ ctx.show_viewport_immediate(vid, builder, |ui, class| {if ui.ctx().input(|i| i.viewport().close_requested()) {close = true;}egui::CentralPanel::default().frame(egui::Frame::NONE.fill(crate::theme::Tokens::get(ui.ctx()).canvas)).show(ui, |ui| {let rect = ui.available_rect_before_wrap();view = canvas_view(app, ui, w.document, rect, view.clone(), false);+ // Preferences › Zoom Resizes Windows: a zoom wraps the window around the image.+ crate::zoom_window_fit::follow(app, ui.ctx(), class, vid, &w, &view, rect);});});if let Some(win) = app.ui.windows.iter_mut().find(|x| x.id == w.id) {diff --git a/crates/ui-egui/src/i18n/cs.tsv b/crates/ui-egui/src/i18n/cs.tsvindex f6046fc20..5e01701f2 100644--- a/crates/ui-egui/src/i18n/cs.tsv+++ b/crates/ui-egui/src/i18n/cs.tsv@@ -3852,3 +3852,4 @@ navigator Zoom ZvětšeníUse PoužítIn Front VpředuBehind Vzadu+ Resize Windows to Fit Přizpůsobit velikost okendiff --git a/crates/ui-egui/src/i18n/de.tsv b/crates/ui-egui/src/i18n/de.tsvindex 0ad7366c4..7686f0313 100644--- a/crates/ui-egui/src/i18n/de.tsv+++ b/crates/ui-egui/src/i18n/de.tsv@@ -3245,3 +3245,4 @@ curves Smooth GlättenIn Front DavorBehind DahinterBlend Mischen+ Resize Windows to Fit Fenstergröße anpassendiff --git a/crates/ui-egui/src/i18n/el.tsv b/crates/ui-egui/src/i18n/el.tsvindex 83621629f..2e58699d3 100644--- a/crates/ui-egui/src/i18n/el.tsv+++ b/crates/ui-egui/src/i18n/el.tsv@@ -3944,3 +3944,4 @@ curves Smooth ΕξομάλυνσηUse ΧρήσηIn Front ΜπροστάBehind Πίσω+ Resize Windows to Fit Προσαρμογή μεγέθους παραθύρωνdiff --git a/crates/ui-egui/src/i18n/es.tsv b/crates/ui-egui/src/i18n/es.tsvindex e23b4e84c..afbf83be6 100644--- a/crates/ui-egui/src/i18n/es.tsv+++ b/crates/ui-egui/src/i18n/es.tsv@@ -4206,3 +4206,4 @@ navigator Zoom ZoomUse UsarIn Front DelanteBehind Detrás+ Resize Windows to Fit Redimensionar ventanas para ajustardiff --git a/crates/ui-egui/src/i18n/fr.tsv b/crates/ui-egui/src/i18n/fr.tsvindex 3bcca7037..e1f3f1ace 100644--- a/crates/ui-egui/src/i18n/fr.tsv+++ b/crates/ui-egui/src/i18n/fr.tsv@@ -3252,3 +3252,4 @@ curves Smooth LisserUse UtiliserIn Front DevantBehind Derrière+ Resize Windows to Fit Redimensionner les fenêtresdiff --git a/crates/ui-egui/src/i18n/hu.tsv b/crates/ui-egui/src/i18n/hu.tsvindex 4907e2da0..25def1f5b 100644--- a/crates/ui-egui/src/i18n/hu.tsv+++ b/crates/ui-egui/src/i18n/hu.tsv@@ -4163,3 +4163,4 @@ cameraRaw Yellow SárgaUse HasználatIn Front ElölBehind Mögött+ Resize Windows to Fit Ablakok átméretezése a képhezdiff --git a/crates/ui-egui/src/i18n/id.tsv b/crates/ui-egui/src/i18n/id.tsvindex fc0d66afa..d75bb4390 100644--- a/crates/ui-egui/src/i18n/id.tsv+++ b/crates/ui-egui/src/i18n/id.tsv@@ -3239,3 +3239,4 @@ curves Smooth HaluskanUse GunakanIn Front Di DepanBehind Di Belakang+ Resize Windows to Fit Ubah ukuran jendela agar pasdiff --git a/crates/ui-egui/src/i18n/it.tsv b/crates/ui-egui/src/i18n/it.tsvindex 609469635..2437c82ab 100644--- a/crates/ui-egui/src/i18n/it.tsv+++ b/crates/ui-egui/src/i18n/it.tsv@@ -4361,3 +4361,4 @@ shape Appearance AspettoUse UsaIn Front DavantiBehind Dietro+ Resize Windows to Fit Ridimensiona finestre per adattarediff --git a/crates/ui-egui/src/i18n/ja.tsv b/crates/ui-egui/src/i18n/ja.tsvindex 39a38a114..cab60d0cd 100644--- a/crates/ui-egui/src/i18n/ja.tsv+++ b/crates/ui-egui/src/i18n/ja.tsv@@ -3921,3 +3921,4 @@ curves Smooth スムーズUse 使用In Front 前面Behind 背面+ Resize Windows to Fit ウィンドウサイズを合わせるdiff --git a/crates/ui-egui/src/i18n/ko.tsv b/crates/ui-egui/src/i18n/ko.tsvindex 241c81bef..a93d9cc8e 100644--- a/crates/ui-egui/src/i18n/ko.tsv+++ b/crates/ui-egui/src/i18n/ko.tsv@@ -3426,3 +3426,4 @@ curves Smooth 매끄럽게In Front 앞Behind 뒤Blend 혼합+ Resize Windows to Fit 창 크기 맞추기diff --git a/crates/ui-egui/src/i18n/nl.tsv b/crates/ui-egui/src/i18n/nl.tsvindex e0dd17ff5..be897d85d 100644--- a/crates/ui-egui/src/i18n/nl.tsv+++ b/crates/ui-egui/src/i18n/nl.tsv@@ -4004,3 +4004,4 @@ curves Smooth Vloeiend makenUse GebruikenIn Front VoorBehind Achter+ Resize Windows to Fit Venstergrootte aanpassendiff --git a/crates/ui-egui/src/i18n/pl.tsv b/crates/ui-egui/src/i18n/pl.tsvindex 10435771f..3910c494b 100644--- a/crates/ui-egui/src/i18n/pl.tsv+++ b/crates/ui-egui/src/i18n/pl.tsv@@ -3843,3 +3843,4 @@ curves Smooth WygładźUse UżyjIn Front Z przoduBehind Z tyłu+ Resize Windows to Fit Dopasuj rozmiar okiendiff --git a/crates/ui-egui/src/i18n/pt-br.tsv b/crates/ui-egui/src/i18n/pt-br.tsvindex f4dc857f7..5bd05e1b0 100644--- a/crates/ui-egui/src/i18n/pt-br.tsv+++ b/crates/ui-egui/src/i18n/pt-br.tsv@@ -4034,3 +4034,4 @@ layerLabel No Color Sem corUse UsarIn Front Na frenteBehind Atrás+ Resize Windows to Fit Redimensionar janelas para caberdiff --git a/crates/ui-egui/src/i18n/ru.tsv b/crates/ui-egui/src/i18n/ru.tsvindex b497b4185..f640d1a07 100644--- a/crates/ui-egui/src/i18n/ru.tsv+++ b/crates/ui-egui/src/i18n/ru.tsv@@ -3218,3 +3218,4 @@ curves Smooth СгладитьUse ИспользоватьIn Front СпередиBehind Сзади+ Resize Windows to Fit Подгонять размер оконdiff --git a/crates/ui-egui/src/i18n/sr.tsv b/crates/ui-egui/src/i18n/sr.tsvindex dc3608c51..a9dc77550 100644--- a/crates/ui-egui/src/i18n/sr.tsv+++ b/crates/ui-egui/src/i18n/sr.tsv@@ -4317,3 +4317,4 @@ shape Appearance IzgledUse KoristiIn Front IspredBehind Iza+ Resize Windows to Fit Prilagodi veličinu prozoradiff --git a/crates/ui-egui/src/i18n/tr.tsv b/crates/ui-egui/src/i18n/tr.tsvindex 5e5326f9f..4ba07eb76 100644--- a/crates/ui-egui/src/i18n/tr.tsv+++ b/crates/ui-egui/src/i18n/tr.tsv@@ -3974,3 +3974,4 @@ cameraData Lens LensUse KullanIn Front ÖndeBehind Arkada+ Resize Windows to Fit Pencereleri sığacak şekilde boyutlandırdiff --git a/crates/ui-egui/src/i18n/uk.tsv b/crates/ui-egui/src/i18n/uk.tsvindex cc3df8834..52ab13902 100644--- a/crates/ui-egui/src/i18n/uk.tsv+++ b/crates/ui-egui/src/i18n/uk.tsv@@ -3952,3 +3952,4 @@ curves Smooth ЗгладитиUse ВикористатиIn Front СпередуBehind Ззаду+ Resize Windows to Fit Підганяти розмір віконdiff --git a/crates/ui-egui/src/i18n/vi.tsv b/crates/ui-egui/src/i18n/vi.tsvindex d19e3d576..b9b790a56 100644--- a/crates/ui-egui/src/i18n/vi.tsv+++ b/crates/ui-egui/src/i18n/vi.tsv@@ -3731,3 +3731,4 @@ curves Smooth Làm mượtUse DùngIn Front Phía trướcBehind Phía sau+ Resize Windows to Fit Đổi cỡ cửa sổ cho vừa ảnhdiff --git a/crates/ui-egui/src/i18n/zh-hans.tsv b/crates/ui-egui/src/i18n/zh-hans.tsvindex 675a54450..aaa46b410 100644--- a/crates/ui-egui/src/i18n/zh-hans.tsv+++ b/crates/ui-egui/src/i18n/zh-hans.tsv@@ -2868,3 +2868,4 @@ curves Smooth 平滑In Front 前面Behind 后面Blend 混合+ Resize Windows to Fit 调整窗口大小以适合diff --git a/crates/ui-egui/src/i18n/zh-hant.tsv b/crates/ui-egui/src/i18n/zh-hant.tsvindex 982fa9731..b8147c168 100644--- a/crates/ui-egui/src/i18n/zh-hant.tsv+++ b/crates/ui-egui/src/i18n/zh-hant.tsv@@ -2853,3 +2853,4 @@ curves Smooth 平滑In Front 前面Behind 後面Blend 混合+ Resize Windows to Fit 調整視窗大小以符合diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rsindex 89f293a2c..7fd4bc420 100644--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@ -239,6 +239,7 @@ pub mod work_area;pub mod workspace_ui;pub mod zoom_levels;pub mod zoom_tool;+pub mod zoom_window_fit;use std::collections::HashMap;use std::sync::mpsc::{Receiver, Sender};diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rsindex 04e775439..70a04b188 100644--- a/crates/ui-egui/src/panels.rs+++ b/crates/ui-egui/src/panels.rs@@ -1454,6 +1454,12 @@ pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) {}Tool::Eyedropper => crate::eyedropper_ui::options(app, ui),Tool::Zoom => {+ // Resize Windows to Fit is Preferences › Zoom Resizes Windows itself, as in+ // Photoshop: one setting, shown in both places (zoom_window_fit.rs).+ let mut fit = app.session.prefs().general.zoom_resizes_windows;+ if widgets::checkbox(ui, &mut fit, tl!("Resize Windows to Fit")).changed() {+ app.session.prefs.edit(|p| p.general.zoom_resizes_windows = fit);+ }widgets::checkbox(ui, &mut app.ui.tool_options.zoom_scrubby, tl!("Scrubby Zoom"));hint(ui,diff --git a/crates/ui-egui/src/prefs_ui.rs b/crates/ui-egui/src/prefs_ui.rsindex d808ae32f..ca22b1f03 100644--- a/crates/ui-egui/src/prefs_ui.rs+++ b/crates/ui-egui/src/prefs_ui.rs@@ -3629,6 +3629,7 @@ mod tests {assert!(!prefs::is_hidden("rawDefaults.openInCameraRaw"));assert!(prefs::is_hidden("rawDefaults.applyAutoTone"));assert!(!prefs::is_hidden("general.autoShowHomeScreen"));+ assert!(!prefs::is_hidden("general.zoomResizesWindows"));assert!(!prefs::is_hidden("interface.uiScale"));assert!(!prefs::is_hidden("interface.uiFontSize"));// Hidden values still round-trip through the dialog untouched.diff --git a/crates/ui-egui/src/zoom_window_fit.rs b/crates/ui-egui/src/zoom_window_fit.rsnew file mode 100644index 000000000..7628f970f--- /dev/null+++ b/crates/ui-egui/src/zoom_window_fit.rs@@ -0,0 +1,254 @@+//! Preferences › General › Zoom Resizes Windows, which is also the Zoom tool's Resize Windows to+//! Fit (#204).+//!+//! As in Photoshop the two are one setting, so turning it on in either place shows it on in the+//! other. With it on, zooming a document that floats in its own window resizes that window to wrap+//! the image at the new zoom, no bigger than the screen it is on; with it off (the default) the+//! image zooms inside a window that keeps its size. Tabbed documents never resize the main window.+//!+//! Only a change of zoom resizes. Fit on Screen derives the zoom from the window, so a fit leaves+//! the window alone and the two can never chase each other.++use egui::{Context, Id, Rect, Vec2, ViewportClass, ViewportCommand, ViewportId, vec2};++use crate::PhotocraftApp;+use crate::state::{DocWindow, View};++/// The smallest window a zoom asks for (points), so zooming far out keeps the window usable.+pub const MIN_SIZE: Vec2 = vec2(240.0, 160.0);++/// Room left on the screen for the window's title bar and the taskbar or Dock (points).+pub const SCREEN_MARGIN: f32 = 48.0;++/// What wrapping a document window needs besides the view itself.+#[derive(Clone, Copy, Debug, PartialEq)]+pub struct Layout {+ /// The window's device pixels per egui point (`View::zoom` is in device pixels).+ pub ppp: f32,+ /// View › Pixel Aspect Ratio Correction: document pixels are shown this much wider.+ pub aspect: f32,+ /// The window's own chrome around the image, the rulers, in points.+ pub chrome: Vec2,+ /// The size of the screen the window is on, in points, when the platform reports it.+ pub monitor: Option<Vec2>,+}++/// The inner size (points) of a window that wraps `view`'s document at its zoom and rotation,+/// kept between [`MIN_SIZE`] and the screen. `None` until the view has a document size.+pub fn wrapped_size(view: &View, layout: &Layout) -> Option<Vec2> {+ let usable = |x: f32| x.is_finite() && x > 0.0;+ if !usable(view.zoom) || !usable(layout.ppp) {+ return None;+ }+ let aspect = if usable(layout.aspect) { layout.aspect } else { 1.0 };+ let w = view.doc_size[0] as f32 * aspect * view.zoom / layout.ppp;+ let h = view.doc_size[1] as f32 * view.zoom / layout.ppp;+ if !usable(w) || !usable(h) {+ return None;+ }+ // A turned view (View › Rotate View) shows the image's bounding box.+ let (sin, cos) = view.rotation.to_radians().sin_cos();+ let (sin, cos) = (sin.abs(), cos.abs());+ let image = vec2(w * cos + h * sin, w * sin + h * cos);+ let chrome = if layout.chrome.is_finite() { layout.chrome.max(Vec2::ZERO) } else { Vec2::ZERO };+ let screen = layout.monitor.filter(|m| usable(m.x) && usable(m.y)).map_or(Vec2::splat(f32::INFINITY), |m| m - Vec2::splat(SCREEN_MARGIN)).max(MIN_SIZE);+ Some((image + chrome).max(MIN_SIZE).min(screen))+}++/// Where the zoom a window was last wrapped for (or first seen at) is kept between frames.+fn last_zoom_id(window: u64) -> Id {+ Id::new(("pc-zoom-resizes-windows", window))+}++/// The inner size floating window `window` should take this frame, if any: when `on` (the+/// preference) and its zoom moved from `before` (its view as the frame began) to `after` (the view+/// its canvas left), other than by a fit. Every call records the zoom, so the first frame of a new+/// window and the frames with the preference off never resize later.+pub fn resize_for(ctx: &Context, window: u64, on: bool, before: &View, after: &View, layout: &Layout) -> Option<Vec2> {+ let id = last_zoom_id(window);+ let last = ctx.data_mut(|d| {+ let last = d.get_temp::<f32>(id);+ d.insert_temp(id, after.zoom);+ last+ });+ let fitting = before.fit_pending || before.fill_pending || after.fit_pending || after.fill_pending;+ // Compare the bits: any change of zoom counts, and NaN never matches itself into a resize loop.+ if !on || fitting || last?.to_bits() == after.zoom.to_bits() {+ return None;+ }+ wrapped_size(after, layout)+}++/// Called by a floating document window (`canvas::extra_windows`) after its canvas drew in `rect`:+/// with Zoom Resizes Windows on, a zoom resizes the window around the image. A window drawn+/// embedded in the main one (a backend without multiple windows, the web) is never resized, as the+/// command would resize the main window instead. Returns the size asked for.+pub fn follow(app: &PhotocraftApp, ctx: &Context, class: ViewportClass, viewport: ViewportId, window: &DocWindow, after: &View, rect: Rect) -> Option<Vec2> {+ if class != ViewportClass::Immediate {+ return None;+ }+ let layout = Layout {+ ppp: ctx.pixels_per_point(),+ aspect: app.ui.view.display_aspect(),+ chrome: rect.size() - crate::rulers::content_rect(app, rect).size(),+ monitor: ctx.input(|i| i.viewport().monitor_size),+ };+ let size = resize_for(ctx, window.id, app.session.prefs().general.zoom_resizes_windows, &window.view, after, &layout)?;+ ctx.send_viewport_cmd_to(viewport, ViewportCommand::InnerSize(size));+ Some(size)+}++#[cfg(test)]+mod tests {+ use serde_json::json;++ use super::*;++ fn view(doc_size: [u32; 2], zoom: f32) -> View {+ View { zoom, doc_size, fit_pending: false, ..View::default() }+ }++ fn layout() -> Layout {+ Layout { ppp: 1.0, aspect: 1.0, chrome: Vec2::ZERO, monitor: Some(vec2(1920.0, 1080.0)) }+ }++ #[test]+ fn the_window_wraps_the_image_at_its_zoom() {+ assert_eq!(wrapped_size(&view([400, 300], 1.0), &layout()), Some(vec2(400.0, 300.0)));+ assert_eq!(wrapped_size(&view([400, 300], 2.0), &layout()), Some(vec2(800.0, 600.0)));+ // `zoom` is device pixels: on a 2× display 100% is half as many points.+ assert_eq!(wrapped_size(&view([800, 600], 1.0), &Layout { ppp: 2.0, ..layout() }), Some(vec2(400.0, 300.0)));+ // The rulers sit inside the window, around the image.+ assert_eq!(wrapped_size(&view([400, 300], 1.0), &Layout { chrome: vec2(16.0, 16.0), ..layout() }), Some(vec2(416.0, 316.0)));+ // Pixel Aspect Ratio Correction shows the pixels wider.+ assert_eq!(wrapped_size(&view([400, 300], 1.0), &Layout { aspect: 2.0, ..layout() }), Some(vec2(800.0, 300.0)));+ }++ #[test]+ fn the_window_stays_between_the_minimum_and_the_screen() {+ assert_eq!(wrapped_size(&view([400, 300], 0.05), &layout()), Some(MIN_SIZE));+ let screen = vec2(1920.0, 1080.0) - Vec2::splat(SCREEN_MARGIN);+ assert_eq!(wrapped_size(&view([400, 300], 16.0), &layout()), Some(screen));+ // One axis can be clamped while the other still wraps.+ assert_eq!(wrapped_size(&view([4000, 300], 1.0), &layout()), Some(vec2(screen.x, 300.0)));+ // Without a reported screen only the minimum applies.+ assert_eq!(wrapped_size(&view([400, 300], 16.0), &Layout { monitor: None, ..layout() }), Some(vec2(6400.0, 4800.0)));+ // A nonsense screen size is ignored rather than shrinking the window to nothing.+ assert_eq!(wrapped_size(&view([400, 300], 1.0), &Layout { monitor: Some(vec2(0.0, f32::NAN)), ..layout() }), Some(vec2(400.0, 300.0)));+ }++ #[test]+ fn a_turned_view_wraps_the_bounding_box() {+ let turned = View { rotation: 90.0, ..view([400, 300], 1.0) };+ let size = wrapped_size(&turned, &layout()).unwrap();+ assert!((size - vec2(300.0, 400.0)).length() < 0.01, "{size:?}");+ let turned = View { rotation: 45.0, ..view([400, 400], 1.0) };+ let size = wrapped_size(&turned, &layout()).unwrap();+ assert!((size.x - 400.0 * std::f32::consts::SQRT_2).abs() < 0.01, "{size:?}");+ }++ #[test]+ fn nothing_to_wrap_asks_for_nothing() {+ assert_eq!(wrapped_size(&view([0, 0], 1.0), &layout()), None, "the view hasn't shown its document yet");+ assert_eq!(wrapped_size(&view([400, 300], 0.0), &layout()), None);+ assert_eq!(wrapped_size(&view([400, 300], f32::NAN), &layout()), None);+ assert_eq!(wrapped_size(&view([400, 300], 1.0), &Layout { ppp: 0.0, ..layout() }), None);+ }++ #[test]+ fn only_a_zoom_change_with_the_preference_on_resizes() {+ let ctx = Context::default();+ let at = |zoom| view([400, 300], zoom);+ // The first frame of a window only records its zoom.+ assert_eq!(resize_for(&ctx, 7, true, &at(1.0), &at(1.0), &layout()), None);+ // The same zoom again: nothing to do.+ assert_eq!(resize_for(&ctx, 7, true, &at(1.0), &at(1.0), &layout()), None);+ // Zoom in (the Zoom tool, the wheel, Window › Arrange › Match Zoom): the window follows.+ assert_eq!(resize_for(&ctx, 7, true, &at(1.0), &at(2.0), &layout()), Some(vec2(800.0, 600.0)));+ assert_eq!(resize_for(&ctx, 7, true, &at(2.0), &at(2.0), &layout()), None, "once per change");+ // A zoom changed outside the canvas (Match Zoom) still counts on the next frame.+ assert_eq!(resize_for(&ctx, 7, true, &at(0.5), &at(0.5), &layout()), Some(vec2(240.0, 160.0)));+ // Another window keeps its own record.+ assert_eq!(resize_for(&ctx, 8, true, &at(0.5), &at(0.5), &layout()), None);+ }++ #[test]+ fn with_the_preference_off_the_window_keeps_its_size() {+ let ctx = Context::default();+ let at = |zoom| view([400, 300], zoom);+ assert_eq!(resize_for(&ctx, 1, false, &at(1.0), &at(1.0), &layout()), None);+ assert_eq!(resize_for(&ctx, 1, false, &at(1.0), &at(3.0), &layout()), None);+ // Turning it on later doesn't catch up on zooms made while it was off.+ assert_eq!(resize_for(&ctx, 1, true, &at(3.0), &at(3.0), &layout()), None);+ assert_eq!(resize_for(&ctx, 1, true, &at(3.0), &at(1.0), &layout()), Some(vec2(400.0, 300.0)));+ }++ #[test]+ fn a_fit_never_resizes_the_window() {+ let ctx = Context::default();+ let at = |zoom| view([400, 300], zoom);+ assert_eq!(resize_for(&ctx, 2, true, &at(1.0), &at(1.0), &layout()), None);+ // The fit takes its zoom from the window; resizing for it would start a loop.+ let fitting = View { fit_pending: true, ..at(1.0) };+ assert_eq!(resize_for(&ctx, 2, true, &fitting, &at(0.4), &layout()), None);+ let filling = View { fill_pending: true, ..at(0.4) };+ assert_eq!(resize_for(&ctx, 2, true, &filling, &at(0.6), &layout()), None);+ // The zoom the fit chose is the new baseline.+ assert_eq!(resize_for(&ctx, 2, true, &at(0.6), &at(0.6), &layout()), None);+ }++ #[test]+ fn follow_resizes_only_real_floating_windows_with_the_preference_on() {+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());+ app.run("file.new", json!({"width": 400, "height": 300})).unwrap();+ app.sync_views();+ app.ui.extras.rulers = false;+ let ctx = Context::default();+ let viewport = ViewportId::from_hash_of(("docwin", 3_u64));+ let rect = Rect::from_min_size(egui::Pos2::ZERO, vec2(800.0, 600.0));+ let at = |zoom| view([400, 300], zoom);+ let window = DocWindow { id: 3, document: 0, view: at(1.0), open: true, position: None };+ let frame = |app: &PhotocraftApp, class, after: &View| follow(app, &ctx, class, viewport, &window, after, rect);+ // Off by default, as in Photoshop.+ assert!(!app.session.prefs().general.zoom_resizes_windows);+ assert_eq!(frame(&app, ViewportClass::Immediate, &at(1.0)), None);+ assert_eq!(frame(&app, ViewportClass::Immediate, &at(2.0)), None);+ app.run("prefs.set", json!({"path": "general.zoomResizesWindows", "value": true})).unwrap();+ assert_eq!(frame(&app, ViewportClass::Immediate, &at(1.0)), Some(vec2(400.0, 300.0)));+ // Drawn inside the main window, a resize command would resize the main window: skipped.+ assert_eq!(frame(&app, ViewportClass::EmbeddedWindow, &at(2.0)), None);+ // With the rulers on, the window makes room for them.+ app.ui.extras.rulers = true;+ let ruled = frame(&app, ViewportClass::Immediate, &at(2.0)).unwrap();+ assert!(ruled.x > 800.0 && ruled.y > 600.0, "{ruled:?}");+ }++ #[test]+ fn the_zoom_tool_options_bar_shows_the_same_setting() {+ // Photoshop's Zoom tool option Resize Windows to Fit is Preferences › Zoom Resizes Windows.+ use egui_kittest::Harness;+ use egui_kittest::kittest::Queryable;+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());+ app.run("file.new", json!({"width": 64, "height": 64})).unwrap();+ app.sync_views();+ app.ui.tool = crate::state::Tool::Zoom;+ let mut h = Harness::builder().with_size(egui::vec2(1400.0, 60.0)).build_ui_state(+ |ui, app: &mut PhotocraftApp| {+ if !ui.ctx().fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) {+ return;+ }+ crate::panels::options_bar(app, ui);+ },+ app,+ );+ PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::default());+ h.run_steps(4);+ assert!(!h.state().session.prefs().general.zoom_resizes_windows);+ h.get_by_label("Resize Windows to Fit").click();+ h.run_steps(2);+ assert!(h.state().session.prefs().general.zoom_resizes_windows, "the option turns the preference on");+ h.get_by_label("Resize Windows to Fit").click();+ h.run_steps(2);+ assert!(!h.state().session.prefs().general.zoom_resizes_windows, "and off again");+ }+}