diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs index 8261324fde3..6703f383f00 100644 --- a/crates/ui-egui/src/canvas.rs +++ b/crates/ui-egui/src/canvas.rs @@ -237,6 +237,10 @@ mod pattern_drag_tests; #[cfg(test)] mod tabs_tests; +#[cfg(test)] +#[path = "screen_mode_floating_documents_tests.rs"] +mod screen_mode_floating_documents_tests; + #[cfg(test)] mod canvas_settle_trials; diff --git a/crates/ui-egui/src/canvas/document_area.rs b/crates/ui-egui/src/canvas/document_area.rs index 3bbcdf823a3..95bd4f42643 100644 --- a/crates/ui-egui/src/canvas/document_area.rs +++ b/crates/ui-egui/src/canvas/document_area.rs @@ -154,9 +154,11 @@ pub(crate) fn publish_shown(app: &PhotocraftApp) { let docs = app.session.documents(); let mut shown: Vec = match arranged_panes(app, app.last_canvas_rect) { Some(cells) => cells.into_iter().map(|(d, _)| d).collect(), - None => main_canvas_document(app).into_iter().collect(), + None => single_canvas_document(app).into_iter().collect(), }; - shown.extend(app.ui.document_windows.floating.iter().filter_map(|(group, _)| group.shown).filter_map(|id| document_index(app, id))); + if !super::document_windows::full_screen_shows_active(app) { + shown.extend(app.ui.document_windows.floating.iter().filter_map(|(group, _)| group.shown).filter_map(|id| document_index(app, id))); + } let mut shown: Vec<(u64, u64)> = shown.into_iter().filter_map(|d| docs.get(d)).map(|st| (st.doc.id.0, gpu.canvas_bytes(&st.doc))).collect(); shown.sort_unstable(); shown.dedup(); @@ -201,9 +203,19 @@ fn documents(app: &mut PhotocraftApp, ui: &mut egui::Ui) { } } +/// The document the single canvas of the main window shows: the active document while the full-screen modes show it +/// in place of the floating windows (#16846), else the active docked document or the first docked one. +pub(super) fn single_canvas_document(app: &PhotocraftApp) -> Option { + if super::document_windows::full_screen_shows_active(app) { + return app.session.active_index(); + } + main_canvas_document(app) +} + /// The document pane: an opening file's progress, the arranged panes, or the single canvas. With no docked document /// it shows nothing over the surround: a floating document belongs to its own window, and drawing it here too would -/// show the same page twice. +/// show the same page twice. The full-screen modes are the exception: with every document floating they show the +/// active one here, without its window ([`single_canvas_document`]). fn document(app: &mut PhotocraftApp, ui: &mut egui::Ui, rect: Rect) { // A tab click switches documents before any canvas is drawn this pass. publish_shown(app); @@ -213,7 +225,7 @@ fn document(app: &mut PhotocraftApp, ui: &mut egui::Ui, rect: Rect) { return; } let Some(active) = app.session.active_index() else { return }; - let Some(idx) = main_canvas_document(app) else { return }; + let Some(idx) = single_canvas_document(app) else { return }; // The panel's document area (`CanvasPanelOutput::document`). app.last_canvas_rect = rect; app.last_canvas_layer = ui.layer_id(); diff --git a/crates/ui-egui/src/canvas/document_display.rs b/crates/ui-egui/src/canvas/document_display.rs index 2fcaa80868a..20ed2343488 100644 --- a/crates/ui-egui/src/canvas/document_display.rs +++ b/crates/ui-egui/src/canvas/document_display.rs @@ -68,6 +68,10 @@ impl CanvasHost { /// the dialogs' canvas picks work on: the floating document window that draws it, else the main document area. /// A New Window for Document keeps a view of its own, so it is never the document's own canvas. pub(crate) fn primary(app: &PhotocraftApp, idx: usize) -> Self { + // The full-screen modes show a floating document in the main document area, its window put away (#16846). + if super::document_windows::full_screen_shows_active(app) { + return Self::main(idx); + } let window = app .ui .document_windows diff --git a/crates/ui-egui/src/canvas/document_windows.rs b/crates/ui-egui/src/canvas/document_windows.rs index ee480bb0ed2..c61c2dbeb41 100644 --- a/crates/ui-egui/src/canvas/document_windows.rs +++ b/crates/ui-egui/src/canvas/document_windows.rs @@ -8,7 +8,7 @@ use craft::host::target::WindowState; use craft::ui::canvas::ToolHost; use craft::ui::contract::{ShowContainer as _, SizeLimits}; use craft::ui::runtime::windows::WindowHost; -use craft::ui::shell::{DocumentWindow, Title, TitleBar, Wrapped}; +use craft::ui::shell::{DocumentWindow, ScreenMode, Title, TitleBar, Wrapped}; use craft::window::policy::Frame; use egui::{Rect, Vec2, vec2}; use photocraft_doc::DocId; @@ -58,9 +58,24 @@ pub(crate) fn sync_documents(app: &mut PhotocraftApp, ids: &[DocId]) { } } +/// Whether the main window's document area shows the active document in place of the floating windows: in Full +/// Screen Mode With Menu Bar and Full Screen Mode while every open document floats (#16846). The windows are not +/// shown then, and they keep their places and sizes, so Standard Screen Mode floats them again where they were. A mix +/// of docked and floating documents keeps the windows. +pub(crate) fn full_screen_shows_active(app: &PhotocraftApp) -> bool { + app.ui.view.screen_mode != ScreenMode::Standard + && !app.ui.document_windows.floating.is_empty() + && app.session.active().is_some() + && super::tabs::docked_document_indices(app).is_empty() +} + /// The floating document windows, each in its own OS window (or on the fallback layer where there is -/// none). `ui` is the frame's root `Ui`: `DocumentWindow` is a container shown in a parent. +/// none). `ui` is the frame's root `Ui`: `DocumentWindow` is a container shown in a parent. None shows while the +/// document area shows the active document full screen ([`full_screen_shows_active`]). pub fn extra_windows(app: &mut PhotocraftApp, ui: &mut egui::Ui) { + if full_screen_shows_active(app) { + return; + } let windows: Vec<(u64, Rect)> = app.ui.document_windows.floating.iter().map(|(g, rect)| (g.id, *rect)).collect(); let mut closed = Vec::new(); let mut docked = Vec::new(); diff --git a/crates/ui-egui/src/screen_mode_floating_documents_tests.rs b/crates/ui-egui/src/screen_mode_floating_documents_tests.rs new file mode 100644 index 00000000000..362e61edf34 --- /dev/null +++ b/crates/ui-egui/src/screen_mode_floating_documents_tests.rs @@ -0,0 +1,152 @@ +//! #16846: with every document in a floating window, Full Screen Mode With Menu Bar and Full Screen Mode show only the +//! active document, without its window, in the main window's document area. Ctrl+Tab shows the next document there, +//! and Standard Screen Mode floats every window again at its old place and size, the last active document still +//! active. The measurement had two documents floating, a big one and a small active one. + +use craft::ui::canvas::ToolHost; +use craft::ui::shell::ScreenMode; +use egui::Rect; +use egui_kittest::Harness; +use egui_kittest::kittest::{NodeT, Queryable}; +use serde_json::json; + +use super::document_area::single_canvas_document; +use super::document_display::CanvasHost; +use super::document_windows::full_screen_shows_active; +use super::testing; +use crate::PhotocraftApp; + +type AppHarness = Harness<'static, (Box, bool)>; + +/// The measured start: a 1200 x 800 document and a 400 x 300 one, the small one active, each floated into its own +/// window by Window › Arrange › Float All in Windows; then the document area and the floating windows drawn in one +/// window (the windows on the fallback layer, where they have no OS window of their own). +fn floating_pair() -> AppHarness { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 1200, "height": 800, "name": "big"})).unwrap(); + app.run("file.new", json!({"width": 400, "height": 300, "name": "small"})).unwrap(); + app.sync_views(); + let ctx = app.ctx().clone(); + crate::menus::invoke(&mut app, &ctx, "window.arrange.floatAllInWindows", json!({})).expect("both documents float"); + let mut h = Harness::builder().with_size(egui::vec2(1440.0, 900.0)).with_context(ctx).build_ui_state( + |ui, (app, ready): &mut (Box, bool)| { + if *ready { + egui::CentralPanel::default().frame(egui::Frame::NONE).show(ui, |ui| testing::document_area(app, ui)); + crate::canvas::extra_windows(app, ui); + } + }, + (app, false), + ); + PhotocraftApp::setup_context(&h.ctx, crate::theme::AppTheme::pro()); + h.state_mut().1 = true; + h.run_steps(3); + h +} + +fn app(h: &AppHarness) -> &PhotocraftApp { + &h.state().0 +} + +fn set_mode(h: &mut AppHarness, mode: ScreenMode) { + h.state_mut().0.ui.view.screen_mode = mode; + h.run_steps(3); +} + +/// The titles of the floating windows drawn this pass: each window, a group on the fallback layer, is named +/// " — window ". +fn windows_shown(h: &AppHarness) -> Vec { + h.query_all_by_role(egui::accesskit::Role::Group).filter_map(|n| n.accesskit_node().label()).filter(|label| label.contains("— window")).collect() +} + +fn index_of(app: &PhotocraftApp, name: &str) -> usize { + app.session.documents().iter().position(|st| st.doc.name == name).expect("the document is open") +} + +/// The rect the main area's canvas of document `idx` showed in its last pass, `None` before its first. +fn main_canvas_rect(app: &PhotocraftApp, idx: usize) -> Option { + ToolHost::shown_space(app.ctx(), CanvasHost::main(idx).canvas_id()).map(|space| space.view.rect) +} + +#[test] +fn standard_screen_mode_keeps_both_windows_and_no_document_in_the_main_area() { + let h = floating_pair(); + let app = app(&h); + assert_eq!(app.ui.view.screen_mode, ScreenMode::Standard); + assert!(!full_screen_shows_active(app), "Standard Screen Mode leaves the floating windows as they are"); + assert_eq!(windows_shown(&h).len(), 2, "both floating windows show: {:?}", windows_shown(&h)); + assert_eq!(single_canvas_document(app), None, "the main area shows no document of its own"); + assert!(app.drop_canvas_rect.is_none(), "so no canvas drew there"); +} + +#[test] +fn the_full_screen_modes_show_only_the_active_document_without_its_window() { + for mode in [ScreenMode::FullScreenWithMenuBar, ScreenMode::FullScreen] { + let mut h = floating_pair(); + let small = index_of(app(&h), "small"); + assert_eq!(app(&h).session.active_index(), Some(small), "the small document is active"); + set_mode(&mut h, mode); + let app = app(&h); + assert!(full_screen_shows_active(app), "{mode:?}"); + assert_eq!(windows_shown(&h), Vec::::new(), "{mode:?}: no floating window shows, framed or not"); + assert_eq!(single_canvas_document(app), Some(small), "{mode:?}: the main area shows the active document"); + let area = app.drop_canvas_rect.expect("a canvas drew in the main area"); + assert_eq!(main_canvas_rect(app, small), Some(area), "{mode:?}: the active document's canvas takes the whole document area"); + assert_eq!(CanvasHost::primary(app, small), CanvasHost::main(small), "{mode:?}: the tools and dialogs work on that canvas"); + let big = index_of(app, "big"); + assert_eq!(main_canvas_rect(app, big), None, "{mode:?}: the other document is not drawn"); + } +} + +#[test] +fn ctrl_tab_in_a_full_screen_mode_shows_the_next_document_in_the_same_place() { + for mode in [ScreenMode::FullScreenWithMenuBar, ScreenMode::FullScreen] { + let mut h = floating_pair(); + set_mode(&mut h, mode); + let area = app(&h).drop_canvas_rect.expect("the active document shows"); + // Ctrl+Tab runs document.activate one document on. + h.state_mut().0.run("document.activate", json!({"offset": 1})).expect("the next document activates"); + h.run_steps(3); + let app = app(&h); + let big = index_of(app, "big"); + assert_eq!(app.session.active_index(), Some(big), "{mode:?}: Ctrl+Tab wrapped to the big document"); + assert_eq!(single_canvas_document(app), Some(big), "{mode:?}: the main area now shows it"); + assert_eq!(main_canvas_rect(app, big), Some(area), "{mode:?}: in the place the other one had"); + assert_eq!(windows_shown(&h), Vec::::new(), "{mode:?}: still no floating window"); + } +} + +#[test] +fn standard_screen_mode_floats_every_window_again_where_it_was() { + let mut h = floating_pair(); + let before: Vec<(u64, Rect)> = app(&h).ui.document_windows.floating.iter().map(|(g, rect)| (g.id, *rect)).collect(); + assert_eq!(before.len(), 2); + set_mode(&mut h, ScreenMode::FullScreenWithMenuBar); + set_mode(&mut h, ScreenMode::FullScreen); + h.state_mut().0.run("document.activate", json!({"offset": 1})).expect("the next document activates"); + h.run_steps(3); + let last_active = app(&h).session.active_index(); + set_mode(&mut h, ScreenMode::Standard); + let app = app(&h); + let after: Vec<(u64, Rect)> = app.ui.document_windows.floating.iter().map(|(g, rect)| (g.id, *rect)).collect(); + assert_eq!(after, before, "every window is back at its old place and size"); + assert_eq!(windows_shown(&h).len(), 2, "both windows float again: {:?}", windows_shown(&h)); + assert_eq!(app.session.active_index(), last_active, "the document active last stays active"); + assert_eq!(single_canvas_document(app), None, "the main area gives the document back to its window"); +} + +/// Not measured: a mix of docked and floating documents keeps today's behaviour, the floating window over the full +/// screen and the docked document in the main area. +#[test] +fn a_mix_of_docked_and_floating_documents_keeps_its_windows() { + let mut h = floating_pair(); + let big = index_of(app(&h), "big"); + let big_id = app(&h).session.documents()[big].doc.id; + let window = app(&h).ui.document_windows.floating.iter().find(|(g, _)| g.documents.contains(&big_id)).map(|(g, _)| g.id).expect("big floats"); + // Docking the big document back: its window closes and it joins the tabs. + h.state_mut().0.ui.document_windows.close_window(window); + h.run_steps(2); + set_mode(&mut h, ScreenMode::FullScreen); + let app = app(&h); + assert!(!full_screen_shows_active(app), "a docked document keeps the full-screen modes as they were"); + assert_eq!(windows_shown(&h).len(), 1, "the floating window still shows: {:?}", windows_shown(&h)); +}