diff --git a/crates/ui-contract/src/runtime/egui/areas.rs b/crates/ui-contract/src/runtime/egui/areas.rs index 2de7e26257..1cd5be9d8f 100644 --- a/crates/ui-contract/src/runtime/egui/areas.rs +++ b/crates/ui-contract/src/runtime/egui/areas.rs @@ -419,6 +419,11 @@ pub(crate) fn set_layer_order(ctx: &Context, order: &[LayerId]) { ctx.set_layer_order(order); } +/// Puts `layer` at the top of egui's area order, as a press on it does. +pub(crate) fn move_to_top(ctx: &Context, layer: LayerId) { + ctx.move_to_top(layer); +} + /// Makes `layer` the modal layer of the running pass: layers below it take no input. pub(crate) fn set_modal_layer(ctx: &Context, layer: LayerId) { ctx.memory_mut(|memory| memory.set_modal_layer(layer)); diff --git a/crates/ui-contract/src/runtime/overlay.rs b/crates/ui-contract/src/runtime/overlay.rs index ab53bbff0e..b1b7700947 100644 --- a/crates/ui-contract/src/runtime/overlay.rs +++ b/crates/ui-contract/src/runtime/overlay.rs @@ -659,7 +659,8 @@ pub(crate) fn released_inside(ctx: &Context, rect: Rect) -> bool { } /// Brings the family of the open layer `id` in front of the other families of its class at the end of this pass, as a -/// press on it does. +/// press on it does: a floating window goes to the top of egui's area order, which it shares with the app's own egui +/// windows. pub fn raise(ctx: &Context, id: Id) { with_pass(ctx, |pass| pass.raise.push(id)); } @@ -867,6 +868,8 @@ pub(crate) fn end_pass(ctx: &Context) { with_pass(ctx, |pass| (pass.current.clone(), pass.last.iter().map(|entry| entry.id).collect::>(), pass.raise.clone())); let pressed = pointer::frame_raw(ctx).press.map(|press| press.origin); let now = pass::root_frame_nr(ctx); + // The floating windows raised in this pass: they keep egui's own area order, so they go to its top there. + let mut windows_raised = Vec::new(); let order = with_persist(ctx, viewport, |persist| { let stamp = |id: Id, persist: &mut Persist| { persist.counter += 1; @@ -890,6 +893,9 @@ pub(crate) fn end_pass(ctx: &Context) { } if let Some(root) = at { stamp(root.id, persist); + if root.rank == Rank::Window && !root.window { + windows_raised.push(root.layer); + } } } persist.raised.retain(|id, _| roots.contains(id)); @@ -949,6 +955,9 @@ pub(crate) fn end_pass(ctx: &Context) { order }); areas::set_layer_order(ctx, &order); + for layer in windows_raised { + areas::move_to_top(ctx, layer); + } // An owner takes a close in the frame after the request at the latest; one nobody took is for a layer that is not shown. pass_state::with::(ctx, |closes| closes.0.retain(|(.., queued, carried)| *carried || queued + 1 >= now)); let top_modal = current.iter().rev().find(|entry| entry.blocks_window() && !entry.exiting); diff --git a/crates/ui-panels/src/docking/floating.rs b/crates/ui-panels/src/docking/floating.rs index 647a2ca339..4928d9f1c8 100644 --- a/crates/ui-panels/src/docking/floating.rs +++ b/crates/ui-panels/src/docking/floating.rs @@ -310,6 +310,10 @@ pub struct FloatingGroup<'a> { /// ([`native`](FloatingGroup::native)) moves only when pressed again. #[prop(quantity = "grab point")] continue_move: Option, + /// The window comes to the front in this pass: an OS window ([`native`](FloatingGroup::native)) gets the focus, + /// a window on a layer goes to the top of the floating windows. A request of the pass. + #[prop(flag)] + raise: bool, /// The files dropped on the group's OS window that no target there took. files: Vec, } @@ -361,6 +365,7 @@ impl<'a> FloatingGroup<'a> { take_drop: None, moving: false, continue_move: None, + raise: false, files: Vec::new(), } .label(title) @@ -565,7 +570,7 @@ impl Component for FloatingGroup<'_> { grip: cx.move_band(), resize: cx.resize_grips(), }; - native::raise(&cx.core.ctx, &output); + native::raise(&cx.core.ctx, &output, self.raise); native::fade(&cx.core.ctx, &output); output } diff --git a/crates/ui-panels/src/docking/native.rs b/crates/ui-panels/src/docking/native.rs index 4ef2618896..3574fc62c8 100644 --- a/crates/ui-panels/src/docking/native.rs +++ b/crates/ui-panels/src/docking/native.rs @@ -139,12 +139,21 @@ pub fn send_to(ctx: &Context, viewport: ViewportId, recorded: Rect, rect: Rect) } } -/// Raises the window of the native group that `output` reports on when the user began moving or resizing it this -/// pass, so a palette comes to the front of the windows it overlaps. False when nothing was raised. -pub(crate) fn raise(ctx: &Context, output: &FloatingGroupOutput) -> bool { +/// Raises the window of the group that `output` reports on when the host `requested` it, and an OS window also when +/// the user began moving or resizing it this pass, so a palette comes to the front of the windows it overlaps. A +/// window on a layer goes to the top of egui's area order, where a press already raises it. False when nothing was +/// raised. +pub(crate) fn raise(ctx: &Context, output: &FloatingGroupOutput, requested: bool) -> bool { let began = output.placed.is_some_and(|change| change.phase == GesturePhase::Begin); let host = WindowHost::of(ctx); - began && host.viewport_of(output.response.id).is_some_and(|viewport| host.act(viewport, WindowAction::Focus)) + match host.viewport_of(output.response.id) { + Some(viewport) => (began || requested) && host.act(viewport, WindowAction::Focus), + None if requested => { + crate::runtime::overlay::raise(ctx, output.response.id); + true + } + None => false, + } } /// Fades the native window of the group that `output` reports on while its drag highlights a drop target, and makes diff --git a/crates/ui-patterns/src/patterns/dock/area.rs b/crates/ui-patterns/src/patterns/dock/area.rs index 9b4f465e4e..22364fd27f 100644 --- a/crates/ui-patterns/src/patterns/dock/area.rs +++ b/crates/ui-patterns/src/patterns/dock/area.rs @@ -167,6 +167,13 @@ pub struct DockArea<'a, P: Clone + Eq + Hash + Debug + Send + Sync + 'static> { /// neither; `DockAreaOutput::focused` tells the host where focus is. #[prop] focus: Option

, + /// The floating window holding the panel comes to the front in this pass: an OS window gets + /// `WindowAction::Focus`, an in-app window goes to the top of the floating windows. A request of the pass: a host + /// that opens or activates a floating panel (its Window menu, a command) passes the panel in the pass after. When + /// no floating window holds the panel, or the window opens in this pass (a new window opens in front), it does + /// nothing. + #[prop] + raise: Option

, /// 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. @@ -200,6 +207,7 @@ impl<'a, P: Clone + Eq + Hash + Debug + Send + Sync + 'static> DockArea<'a, P> { flyout: None, continue_move: None, focus: None, + raise: None, rail_footer: None, top_inset: 0.0, } @@ -337,6 +345,8 @@ struct Scene<'s, P> { continue_move: Option<(&'s P, Vec2)>, /// The panel whose group takes key focus in this pass (`DockArea::focus`). focus: Option<&'s P>, + /// The panel whose window comes to the front (`DockArea::raise`). + raise: Option<&'s P>, rtl: bool, /// The docked places floating panels left, for Dock Panel. floated: &'s [(P, Location

)], @@ -549,6 +559,7 @@ impl<'a, P: Clone + Eq + Hash + Debug + Send + Sync + 'static> DockArea<'a, P> { mut flyout, continue_move, focus, + raise, rail_footer, top_inset, } = self; @@ -610,6 +621,7 @@ impl<'a, P: Clone + Eq + Hash + Debug + Send + Sync + 'static> DockArea<'a, P> { auto_collapse, continue_move: continue_move.as_ref().map(|(panel, grab)| (panel, *grab)), focus: focus.as_ref(), + raise: raise.as_ref(), sizes, rtl, rail_footer: rail_footer.as_ref(), @@ -719,6 +731,7 @@ struct Input<'i, P> { auto_collapse: bool, continue_move: Option<(&'i P, Vec2)>, focus: Option<&'i P>, + raise: Option<&'i P>, sizes: DockSizes, rtl: bool, rail_footer: Option<&'i P>, @@ -767,6 +780,7 @@ fn show_area( auto_collapse, continue_move, focus, + raise, sizes, rtl, rail_footer, @@ -795,6 +809,7 @@ fn show_area( auto_collapse, continue_move, focus, + raise, rtl, floated: current.floated(), rail_footer, @@ -1709,6 +1724,9 @@ impl Pass<'_, P> { if let Some((_, grab)) = scene.continue_move.filter(|(panel, _)| window.panels().any(|held| held == *panel)) { group = group.continue_move(grab); } + if scene.raise.is_some_and(|raised| window.panels().any(|held| held == raised)) { + group = group.raise(); + } group = match opening.spec.clone() { Some(window_spec) => group.native(window_spec), None => group.bounds(scene.bounds), diff --git a/crates/ui-patterns/tests/it/docking/float_order.rs b/crates/ui-patterns/tests/it/docking/float_order.rs index 07a6651ca6..55f4ef2fd3 100644 --- a/crates/ui-patterns/tests/it/docking/float_order.rs +++ b/crates/ui-patterns/tests/it/docking/float_order.rs @@ -52,3 +52,39 @@ fn an_app_dialog_opened_over_a_floating_window_shows_above_it_until_the_window_i fixture.frame(vec![moved(WINDOW_ONLY)]).frame(vec![press(WINDOW_ONLY)]).frame(vec![release(WINDOW_ONLY)]).settle(); assert_eq!(fixture.ctx().layer_id_at(BOTH), Some(float), "a press raises the floating window above the dialog"); } + +const LOWER_AT: Rect = Rect { min: Pos2::new(100.0, 100.0), max: Pos2::new(300.0, 260.0) }; +const UPPER_AT: Rect = Rect { min: Pos2::new(200.0, 150.0), max: Pos2::new(400.0, 310.0) }; +/// Inside both windows. +const OVERLAP: Pos2 = Pos2::new(250.0, 220.0); +/// Inside the lower window only. +const LOWER_ONLY: Pos2 = Pos2::new(130.0, 240.0); + +/// `DockArea::raise` brings the floating window holding the panel to the front for the pass it is given in, and the +/// window stays there after it. +#[trial] +fn a_raised_floating_panel_comes_to_the_front_of_the_window_it_overlaps() { + let layout = + Layout::new(Some(Node::tabs(vec![1], 0)), vec![Floating::single(100, vec![2], 0, LOWER_AT), Floating::single(101, vec![3], 0, UPPER_AT)]); + let mut workspace = Arc::new(Workspace::new(layout)); + let raise = Rc::new(Cell::new(None)); + let asked = Rc::clone(&raise); + let specs = Tracked::new(Numbered); + let mut fixture = Fixture::new(move |ui| { + let mut panels = Panels; + let mut area = DockArea::new("Dock", Bind::bound(&mut workspace), &specs, &mut panels, DockConfig::default()); + if let Some(panel) = asked.take() { + area = area.raise(panel); + } + area.show(ui); + }); + fixture.room(Vec2::new(800.0, 600.0)).settle(); + let lower = fixture.ctx().layer_id_at(LOWER_ONLY).expect("the lower window shows"); + assert_ne!(fixture.ctx().layer_id_at(OVERLAP), Some(lower), "the window shown last is on top"); + raise.set(Some(2)); + fixture.frame(Vec::new()).settle(); + assert_eq!(fixture.ctx().layer_id_at(OVERLAP), Some(lower), "the raised window is on top"); + raise.set(Some(1)); + fixture.frame(Vec::new()).settle(); + assert_eq!(fixture.ctx().layer_id_at(OVERLAP), Some(lower), "a docked panel's raise moves no window"); +} diff --git a/crates/ui-patterns/tests/it/docking/native.rs b/crates/ui-patterns/tests/it/docking/native.rs index 81f0909430..75f8db9d7e 100644 --- a/crates/ui-patterns/tests/it/docking/native.rs +++ b/crates/ui-patterns/tests/it/docking/native.rs @@ -276,6 +276,8 @@ struct NativeDock { palette_shapes: Vec, /// Whether Auto-Collapse Iconic Panels is on in the frames of a collapsed palette. auto_collapse: bool, + /// The panel the next frame asks the dock to raise (`DockArea::raise`). + raise: Option, } impl NativeDock { @@ -307,6 +309,7 @@ impl NativeDock { palette_tree: None, palette_shapes: Vec::new(), auto_collapse: false, + raise: None, }; for _ in 0..3 { dock.frame(vec![]); @@ -326,8 +329,13 @@ impl NativeDock { let (mut palettes, mut stepped) = (Palettes, SteppedPalette); let panels: &mut dyn DockPanels = if self.stepped { &mut stepped } else { &mut palettes }; let specs = &self.specs; + let raise = self.raise.take(); let out = self.windows.frame(root, child, |ui| { - actions.extend(DockArea::new("Native dock", Bind::bound(workspace), specs, &mut *panels, DockConfig::default()).show(ui).actions); + let mut area = DockArea::new("Native dock", Bind::bound(workspace), specs, &mut *panels, DockConfig::default()); + if let Some(panel) = raise { + area = area.raise(panel); + } + actions.extend(area.show(ui).actions); }); if self.palette.is_none() { self.palette = out.builders.iter().map(|(viewport, _)| *viewport).find(|viewport| *viewport != ViewportId::ROOT); @@ -348,6 +356,21 @@ impl NativeDock { } } +/// `DockArea::raise` of a panel in a native palette focuses the palette's OS window once, in the pass it is given in; +/// a pass without it sends nothing. +#[trial] +fn a_raised_native_palette_gets_the_os_focus_once() { + let mut dock = NativeDock::on(Os::Linux, Arc::new(FakeSystem::new())); + let focus = |commands: &[ViewportCommand]| commands.iter().filter(|command| matches!(command, ViewportCommand::Focus)).count(); + let (_, settled) = dock.frame(vec![]); + assert_eq!(focus(&settled), 0, "a pass without a raise focuses nothing"); + dock.raise = Some(1); + let (_, raised) = dock.frame(vec![]); + assert_eq!(focus(&raised), 1, "the raised palette's window gets the focus"); + let (_, after) = dock.frame(vec![]); + assert_eq!(focus(&after), 0, "the raise is a request of its pass"); +} + fn docks_window(actions: &[Action]) -> bool { actions.iter().any(|action| matches!(action, Action::MoveWindow { .. })) }