diff --git a/crates/ui-panels/src/docking/floating.rs b/crates/ui-panels/src/docking/floating.rs index 7377e3924..68b78461b 100644 --- a/crates/ui-panels/src/docking/floating.rs +++ b/crates/ui-panels/src/docking/floating.rs @@ -326,8 +326,9 @@ pub struct FloatingGroup<'a> { files_at: Option, } -/// Starts the window-unit drag: a move by the title row when the flag is set, else a drag the window took over. -type StartDrag<'a> = Box FnOnce(&'b mut Cx<'c, FloatingGroup<'a>>, bool) + 'a>; +/// Starts the window-unit drag of a window of the given size: a move by the title row when the flag is set, else a +/// drag the window took over. +type StartDrag<'a> = Box FnOnce(&'b mut Cx<'c, FloatingGroup<'a>>, bool, Vec2) + 'a>; type TakeDrop<'a> = Box FnMut(&'b mut Cx<'c, FloatingGroup<'a>>) + 'a>; @@ -387,8 +388,7 @@ impl<'a> FloatingGroup<'a> { #[must_use] pub fn window_of(mut self, anchor: P) -> Self { let active = PanelKey::of(&anchor); - let size = self.rect.size(); - self.start_drag = Some(Box::new(move |cx, title_row| start_window_drag(cx, anchor, active, size, title_row))); + self.start_drag = Some(Box::new(move |cx, title_row, size| start_window_drag(cx, anchor, active, size, title_row))); self } @@ -451,8 +451,8 @@ impl<'a> FloatingGroup<'a> { /// A minimized group, and one with `Sizing::Content`, is placed by its position and width; its height is its /// content's, which is the chrome alone while minimized. fn mount(&self) -> Mount { - // The frame's limits are the body's with the chrome on top. - let frame = self.limits.around(Vec2::new(0.0, self.chrome)); + let frame = self.frame_limits(); + let rect = self.within_limits(); // A collapsed column's width stays within its own bounds, a minimized one's within the panels' limits. let widths = self.column.map(|column| match column.collapsed { Some(_) => column.widths, @@ -475,7 +475,7 @@ impl<'a> FloatingGroup<'a> { let width = self.rect.width().max(frame.min.x).min(frame.max.map_or(f32::INFINITY, |max| max.x)); overlay.placed_at(self.rect.min).size_limits(SizeLimits { min: Vec2::new(width, frame.min.y), max: frame.max }) } else { - overlay.placed(self.rect).size_limits(frame) + overlay.placed(rect).size_limits(frame) }; if let Some(bounds) = self.bounds { overlay = overlay.bounds(bounds); @@ -496,7 +496,21 @@ impl<'a> FloatingGroup<'a> { /// Where the group shows while its column's content fixes its height ([`WindowColumn::shown`]). `None` while the /// rect gives the height. fn held_rect(&self) -> Option { - self.column?.shown(self.rect, self.chrome, self.rtl) + let column = self.column?; + // A collapsed column's width is its own; an expanded one shows at the rect's width within the panels' limits. + let rect = if column.collapsed.is_some() { self.rect } else { self.within_limits() }; + column.shown(rect, self.chrome, self.rtl) + } + + /// The window's size limits: the body's with the chrome on top. + fn frame_limits(&self) -> SizeLimits { + self.limits.around(Vec2::new(0.0, self.chrome)) + } + + /// The rect at its place, its size within [`Self::frame_limits`]: a rect placed or restored outside the panels' + /// limits shows at the nearest size within them, as an OS window's placed rect does. + fn within_limits(&self) -> Rect { + Rect::from_min_size(self.rect.min, self.frame_limits().clamp(self.rect.size())) } /// The double arrow's square at the end of the title row the window keeps to. @@ -564,7 +578,10 @@ impl Component for FloatingGroup<'_> { if self.moving && let Some(start) = self.start_drag.take() { - start(&mut cx, !self.continued); + // A window its column's content sizes keeps the rect's size; any other keeps it within the panels' limits. + let fitted = self.column.is_some_and(|column| column.fitted.is_some()); + let size = if fitted { self.rect.size() } else { self.within_limits().size() }; + start(&mut cx, !self.continued, size); } if let Some(opener) = self.menu_opener { Popup::toggle(opener, &mut cx); diff --git a/crates/ui-patterns/tests/it/docking/floating.rs b/crates/ui-patterns/tests/it/docking/floating.rs index bee7ecd8f..20ca3e255 100644 --- a/crates/ui-patterns/tests/it/docking/floating.rs +++ b/crates/ui-patterns/tests/it/docking/floating.rs @@ -1,11 +1,14 @@ -//! `FloatingGroup`: a window unit drag sees the drop targets under its own group. +//! `FloatingGroup`: a window unit drag sees the drop targets under its own group, and a window shows within its +//! panels' size limits whatever rect it is given. +use craft_behavior::dock::DockEdge; use craft_ui::contract::SizeLimits; use craft_ui::contract::{Common, Component, Cx, DropMark, DropOperation, DropTarget, MeasureCx, Pointer as _, Props as _, Role, Show as _, Sizing}; -use craft_ui::docking::{FloatingGroup, FloatingGroupOutput, GroupDrag}; +use craft_ui::docking::{FloatingGroup, FloatingGroupOutput, GroupDrag, WindowColumn}; use craft_ui::runtime::drag; use craft_ui::style::{ComponentStyle, StyleCx}; use craft_ui::testing::layout::allocate; +use craft_ui::testing::snapshot::layer_rects; use craft_ui::testing::{Frames, pointer, trial}; use egui::{Pos2, Rect, Response, Ui, Vec2}; use std::cell::{Cell, RefCell}; @@ -133,3 +136,67 @@ fn a_window_unit_drag_sees_the_target_under_its_own_group() { assert!(drag::active::>(&frames.ctx).is_some(), "the move started a window drag"); assert!(drag::mark(&frames.ctx).is_some(), "the target under the moving group takes the drop"); } + +/// The rect the layer of a group that fills `rect` shows at, with `limits` on its body, after it settles. +fn shown_at(rect: Rect, limits: SizeLimits, column: Option) -> Rect { + let mut frames = Frames::new(); + let mut show = |ui: &mut Ui| { + let mut group = FloatingGroup::new("Layers", rect).sized(Sizing::Fill).bounds(BOUNDS).limits(limits); + if let Some(column) = column { + group = group.column(column); + } + let _ = group.show_group(ui, |ui, _| { + allocate(ui, LEAST); + }); + }; + frames.idle(4, &mut show); + let layers = layer_rects(&frames.ctx); + assert_eq!(layers.len(), 1, "the group shows one layer: {layers:?}"); + layers[0] +} + +// A window placed or restored from a saved layout at a size outside its panels' limits shows at the nearest size +// within them, at the place it was given; a window already within them keeps its rect. +#[trial] +fn a_window_placed_outside_its_limits_shows_within_them() { + let least = SizeLimits::at_least(LEAST); + let narrow = Rect::from_min_size(START.min, Vec2::new(LEAST.x - 50.0, LEAST.y - 40.0)); + let shown = shown_at(narrow, least, None); + assert!(shown.width() >= LEAST.x - 0.5, "a window placed narrower than its limits widens to them: {shown:?}"); + assert!(shown.height() > LEAST.y, "a window placed shorter than its body's limits grows past them: {shown:?}"); + assert!((shown.min - narrow.min).length() < 0.5, "the window keeps its place: {shown:?}"); + let within = shown_at(START, least, None); + assert!((within.min - START.min).length() < 0.5 && (within.size() - START.size()).length() < 0.5, "{within:?} keeps {START:?}"); + let most = Vec2::new(START.width() - 60.0, BOUNDS.height()); + let shown = shown_at(START, SizeLimits::new(LEAST, most), None); + assert!(shown.width() <= most.x + 0.5, "a window placed wider than its limits narrows to them: {shown:?}"); +} + +// A window placed narrower than its limits is dragged at the size it shows, so a float of the drag does not bring the +// narrower size back. +#[trial] +fn a_window_placed_narrower_than_its_limits_is_dragged_at_the_size_it_shows() { + let (caller, mut frames) = (Caller::new(), Frames::new()); + let narrow = Rect::from_min_size(START.min, Vec2::new(LEAST.x - 50.0, START.height())); + caller.rect.set(narrow); + caller.anchor.set(Some("Layers")); + let mut show = caller.show(); + frames.idle(4, &mut show); + let from = title_of(narrow); + for events in [vec![pointer::moved(from)], vec![pointer::press(from)], vec![pointer::moved(from + MOVE / 2.0)], vec![pointer::moved(from + MOVE)]] + { + frames.frame(events, &mut show); + } + let drag = drag::active::>(&frames.ctx); + let size = drag.as_ref().and_then(|drag| drag.source); + assert!(size.is_some_and(|size| size.x >= LEAST.x - 0.5), "the window drag carries the shown width: {size:?}"); +} + +// A window whose column's groups are all minimized shows at the rect's width, within the panels' limits too. +#[trial] +fn a_window_of_minimized_groups_placed_narrower_than_its_limits_shows_at_them() { + let narrow = Rect::from_min_size(START.min, Vec2::new(LEAST.x - 50.0, START.height())); + let column = WindowColumn::minimized(DockEdge::Start, 24.0); + let held = shown_at(narrow, SizeLimits::at_least(LEAST), Some(column)); + assert!(held.width() >= LEAST.x - 0.5, "a column of minimized groups widens to its limits: {held:?}"); +}