diff --git a/crates/ui-patterns/src/patterns/dock/area.rs b/crates/ui-patterns/src/patterns/dock/area.rs index 11e95e26b..0ea187702 100644 --- a/crates/ui-patterns/src/patterns/dock/area.rs +++ b/crates/ui-patterns/src/patterns/dock/area.rs @@ -138,6 +138,7 @@ pub struct DockArea<'a, P: Clone + Eq + Hash + Debug + Send + Sync + 'static> { #[prop] flyout: Option>>, rail_footer: Option

, + top_inset: f32, } impl<'a, P: Clone + Eq + Hash + Debug + Send + Sync + 'static> DockArea<'a, P> { @@ -156,6 +157,7 @@ impl<'a, P: Clone + Eq + Hash + Debug + Send + Sync + 'static> DockArea<'a, P> { locked: false, flyout: None, rail_footer: None, + top_inset: 0.0, } } @@ -165,6 +167,15 @@ impl<'a, P: Clone + Eq + Hash + Debug + Send + Sync + 'static> DockArea<'a, P> { DockArea { locked: true, ..Self::new(label, Bind::given(Workspace::new(layout.clone())), panels, config) } } + /// Keeps floating windows below the top `inset` points of the window: the height of the bars (notices) the + /// windows float over. A window's place is clamped to the float bounds, so one at the top of the area sits just + /// below the bars and moves with them as they show and close. A negative or non-finite inset is none. + #[must_use] + pub fn top_inset(mut self, inset: f32) -> Self { + self.top_inset = if inset.is_finite() { inset.max(0.0) } else { 0.0 }; + self + } + /// Adds the host's fixed trailing controls to the docked rail containing `panel`. The panel identifies the /// column as it moves between dock sides. The host draws the controls through [`DockPanels::rail_footer`]; its /// icons scroll above them when the rail is short. Has no effect in flyout mode or on floating columns. @@ -205,6 +216,7 @@ struct Drops<'a, P> { config: &'a DockConfig, specs: &'a HashMap, rtl: bool, + top_inset: f32, } impl Drops<'_, P> { @@ -212,7 +224,7 @@ impl Drops<'_, P> { fn take(&self, cx: &mut Cx<'_, DockRoot<'_, DockDropped

>>) -> Option> { let ctx = cx.core.ctx.clone(); let sizes = *cx.style.sizes(); - let bounds = cx.env.get::().0.as_ref().map_or(cx.rect, ShellCx::float_bounds); + let bounds = inset_top(cx.env.get::().0.as_ref().map_or(cx.rect, ShellCx::float_bounds), self.top_inset); let space = float_space(bounds, &sizes, self.config, self.specs); let mut target = DockDropTarget::new(&ctx, self.dock, self.layout, self.permissions); target.rtl = self.rtl; @@ -365,7 +377,7 @@ struct Pass<'s, P> { impl<'a, P: Clone + Eq + Hash + Debug + Send + Sync + 'static> DockArea<'a, P> { /// Shows the area across the room offered. pub fn show(self, ui: &mut Ui) -> DockAreaOutput

{ - let DockArea { label, mut workspace, panels, config, hide, maximized, auto_collapse, locked, mut flyout, rail_footer } = self; + let DockArea { label, mut workspace, panels, config, hide, maximized, auto_collapse, locked, mut flyout, rail_footer, top_inset } = self; let ctx = ui.ctx().clone(); let (rtl, window) = { let env = Env::of(ui); @@ -391,7 +403,7 @@ impl<'a, P: Clone + Eq + Hash + Debug + Send + Sync + 'static> DockArea<'a, P> { .collect(); let policy = |panel: &P| rights.get(panel).copied().unwrap_or(Permissions::ALL); let specs: HashMap = current.layout.panels().into_iter().map(|panel| (panel.clone(), panels.spec(panel))).collect(); - let drops = Drops { dock: id, layout: ¤t.layout, permissions: &policy, config: &config, specs: &specs, rtl }; + let drops = Drops { dock: id, layout: ¤t.layout, permissions: &policy, config: &config, specs: &specs, rtl, top_inset }; let shows = Arrangement { dock: id, revision: current.revision() }; end_replaced_drags::

(&ctx, shows); let dropped: RefCell>> = RefCell::new(None); @@ -419,6 +431,7 @@ impl<'a, P: Clone + Eq + Hash + Debug + Send + Sync + 'static> DockArea<'a, P> { sizes, rtl, rail_footer: rail_footer.as_ref(), + top_inset, }; let mut result = show_area(ui, body, &input, &rights, &mut *panels, flyout.as_mut()); for edit in &result.edit { @@ -518,6 +531,13 @@ struct Input<'i, P> { sizes: DockSizes, rtl: bool, rail_footer: Option<&'i P>, + top_inset: f32, +} + +/// `bounds` without its top `inset` points, never past its bottom. +fn inset_top(bounds: Rect, inset: f32) -> Rect { + let top = (bounds.min.y + inset).min(bounds.max.y); + Rect::from_min_max(egui::pos2(bounds.min.x, top), bounds.max) } fn open_of( @@ -542,14 +562,14 @@ fn show_area( panels: &mut dyn DockPanels

, mut flyout: Option<&mut Bind<'_, Option

>>, ) -> Passed

{ - let Input { id, ctx, current, config, specs, hide, maximized, auto_collapse, sizes, rtl, rail_footer } = *input; + let Input { id, ctx, current, config, specs, hide, maximized, auto_collapse, sizes, rtl, rail_footer, top_inset } = *input; let shell = body.shell(); let mut memory = body.memory::>().clone(); let keys: HashMap = current.layout.panels().into_iter().map(|panel| (PanelKey::of(panel), panel.clone())).collect(); let protected = |panel: &P| rights.get(panel).is_some_and(|rights| rights.is_protected()); let Shown { view, floating } = hide::shown(¤t.layout, maximized, hide, shell.as_ref(), &protected); let layout: &Layout

= &view.layout; - let bounds = shell.as_ref().map_or(body.rect, ShellCx::float_bounds); + let bounds = inset_top(shell.as_ref().map_or(body.rect, ShellCx::float_bounds), top_inset); let space = float_space(bounds, &sizes, config, specs); let scene = Scene { dock: id, diff --git a/crates/ui-patterns/tests/it/docking/floating_area.rs b/crates/ui-patterns/tests/it/docking/floating_area.rs index 4d94e4682..d406c96a3 100644 --- a/crates/ui-patterns/tests/it/docking/floating_area.rs +++ b/crates/ui-patterns/tests/it/docking/floating_area.rs @@ -502,3 +502,33 @@ fn a_settled_collapsed_window_runs_no_more_passes_than_an_expanded_one() { let (collapsed, expanded) = (passes(collapsed_at(WINDOW_AT)), passes(with_window(None))); assert!(collapsed <= expanded, "a still frame runs {collapsed} passes collapsed, {expanded} expanded"); } + +/// A floating window at the top of the area sits below the top inset (the notice bars it floats over), and goes back up +/// when the inset does. +#[trial] +fn a_floating_window_clears_the_top_inset_and_follows_it() { + let inset = Rc::new(Cell::new(0.0_f32)); + let seen = Rc::clone(&inset); + let layout = Layout::new(Some(Node::tabs(vec![DOCKED], 0)), vec![Floating::single(vec![WINDOW], 0, Rect::from_min_size(Pos2::new(130.0, 0.0), Vec2::new(200.0, 120.0)))]); + let held = Rc::new(RefCell::new(Workspace::new(layout))); + let shown = Rc::clone(&held); + let mut fixture = Fixture::new(move |ui| { + let given = shown.borrow().clone(); + let mut panels = Panels { rights: |_| Permissions::ALL }; + DockArea::new("Inset dock", Bind::given(given), &mut panels, DockConfig::default()).top_inset(seen.get()).show(ui); + }); + fixture.settle(); + let window_top = |fixture: &Fixture| { + fixture.by_role(Role::TAB).iter().filter(|tab| tab.rect.left() >= 130.0).map(|tab| tab.rect.top()).fold(f32::INFINITY, f32::min) + }; + let free = window_top(&fixture); + assert!(free < 60.0, "without an inset the window is at the top: {free}"); + inset.set(60.0); + fixture.settle(); + let clear = window_top(&fixture); + assert!(clear >= free + 59.0, "the window moves down by the 60 point inset: {free} to {clear}"); + inset.set(0.0); + fixture.settle(); + let back = window_top(&fixture); + assert!((back - free).abs() < 1.0, "the window goes back up when the notices close: {back}"); +}