diff --git a/crates/layers/tests/it/blend_rows.rs b/crates/layers/tests/it/blend_rows.rs new file mode 100644 index 000000000..00a2c88aa --- /dev/null +++ b/crates/layers/tests/it/blend_rows.rs @@ -0,0 +1,36 @@ +//! `BlendRows`: a percent field keeps its side label while the field's tooltip shows. + +use craft_layers::ui::mock::FakeLayers; +use craft_layers::ui::rows::BlendRows; +use craft_layers::ui::tree::{LayerProperties, LayerTree as _}; +use craft_ui::contract::tooltip::DELAY; +use craft_ui::testing::pointer::moved; +use craft_ui::testing::{Fixture, trial}; +use egui::Rect; + +/// The rect of the run whose text starts with `start`, if the last frame painted one. +fn run(fixture: &Fixture, start: &str) -> Option { + fixture.texts().iter().find(|run| run.text.starts_with(start)).map(|run| run.rect) +} + +#[trial] +fn a_field_keeps_its_side_label_while_its_tooltip_shows() { + let modes = FakeLayers::sample().blend_modes(false, "normal"); + let mut properties = LayerProperties::new("normal"); + properties.opacity = 80.0; + properties.editable = true; + let mut fixture = Fixture::new(move |ui| { + ui.set_width(320.0); + let _ = BlendRows::new(Some(&properties), &modes).show(ui); + }); + fixture.settle(); + let label = run(&fixture, "Opacity").expect("the Opacity label shows"); + let value = run(&fixture, "80").expect("the Opacity value shows"); + fixture.frame(vec![moved(value.center())]); + fixture.advance(DELAY); + fixture.settle(); + let texts: Vec<_> = fixture.texts().iter().map(|run| (run.text.clone(), run.rect)).collect(); + let shown: Vec = fixture.texts().iter().filter(|run| run.text.starts_with("Opacity")).map(|run| run.rect).collect(); + assert!(shown.len() >= 2, "the tooltip shows beside the label: {texts:?}"); + assert!(shown.contains(&label), "the label stays where it was while the tooltip shows: {label:?} in {texts:?}"); +} diff --git a/crates/layers/tests/it/main.rs b/crates/layers/tests/it/main.rs index 59e498702..ea1c4429a 100644 --- a/crates/layers/tests/it/main.rs +++ b/crates/layers/tests/it/main.rs @@ -1,5 +1,6 @@ //! The integration binary of craft-layers: every test is a registered trial. +mod blend_rows; mod panel_drag_menu; mod panel_filter; mod panel_input; diff --git a/crates/ui-contract/src/contract/overlay/host.rs b/crates/ui-contract/src/contract/overlay/host.rs index 98f19d7df..c13730664 100644 --- a/crates/ui-contract/src/contract/overlay/host.rs +++ b/crates/ui-contract/src/contract/overlay/host.rs @@ -187,8 +187,8 @@ pub(crate) fn host(ctx: &Context, layer: &Layer<'_>, body: &mut dyn FnMut(&mut U ui.set_clip_rect(ui.clip_rect().intersect(region)); } areas::fade(ui, layer.fade * overlay.opacity); - // The layer is not inside its opener's arena, roving group, scroll or row: its content starts from the - // opener's inherited env with the layer-local fields fresh. + // The layer is not inside its opener's arena, roving group, scroll, row or label scope: its content starts + // from the opener's inherited env with the layer-local fields fresh, and outside the opener's scopes. let installed = Env::install_for(ui, layer.env.capture().env()); let real = !ui.is_sizing_pass(); if real && (layer.exiting || matches!(overlay.rank, Rank::Popup | Rank::Modal { .. })) { @@ -202,7 +202,7 @@ pub(crate) fn host(ctx: &Context, layer: &Layer<'_>, body: &mut dyn FnMut(&mut U if overlay.fit == OverlayFit::Resizable { ui.set_min_size(overlay.size_limits.min.min(placed.room)); } - body(ui); + access::scope::outside(ctx, || body(ui)); if scope.is_some() && real { focus::pop_scope(ctx); } diff --git a/crates/ui-contract/src/runtime/access/scope.rs b/crates/ui-contract/src/runtime/access/scope.rs index d282558ac..39abbb736 100644 --- a/crates/ui-contract/src/runtime/access/scope.rs +++ b/crates/ui-contract/src/runtime/access/scope.rs @@ -187,6 +187,15 @@ pub(crate) fn depth(ctx: &Context) -> usize { pass_state::with::(ctx, |stack| stack.0.len()) } +/// Runs `body` outside every open label and merge scope, as a layer's content runs: a tooltip or popup shown from a +/// control is not part of the field the control sits in, so it neither names, lines up nor sizes that field's label. +pub(crate) fn outside(ctx: &Context, body: impl FnOnce() -> R) -> R { + let open = pass_state::with::(ctx, |stack| std::mem::take(&mut stack.0)); + let out = body(); + pass_state::with::(ctx, |stack| stack.0 = open); + out +} + /// Opens the scope of `container`; its body shows inside it. pub(crate) fn begin(ctx: &Context, container: Id, owner: Id, kind: Kind) { pass_state::with::(ctx, |stack| { diff --git a/crates/ui-widgets/tests/it/label/tooltip.rs b/crates/ui-widgets/tests/it/label/tooltip.rs index ef84c67fc..80ca2757c 100644 --- a/crates/ui-widgets/tests/it/label/tooltip.rs +++ b/crates/ui-widgets/tests/it/label/tooltip.rs @@ -1,5 +1,5 @@ //! The tooltip of a control that paints no label: its own label shows there, inside a field or not; a control named -//! only by its field label shows none. +//! only by its field label shows none, and the field's label stays where it was. use super::kit::PlainStyle; use craft_behavior::Sense; @@ -52,9 +52,19 @@ fn texts_added_by_hover(show: impl Fn(&mut Ui) -> Rect + 'static) -> Vec fixture.settle(); // A cut run carries its full text too: runs are told apart by whether they are cut. let before: Vec<(String, bool)> = fixture.texts().iter().map(|run| (run.text.clone(), run.elided)).collect(); + let placed: Vec<(String, Rect)> = fixture.texts().iter().map(|run| (run.text.clone(), run.rect)).collect(); fixture.frame(vec![moved(at.get().center())]); fixture.advance(DELAY); fixture.settle(); + // The tooltip shows on its own layer: every text shown before stays where it was, a field's label included. + for shown in &placed { + assert!( + fixture.texts().iter().any(|run| run.text == shown.0 && run.rect == shown.1), + "{:?} stays at {:?} under the tooltip", + shown.0, + shown.1 + ); + } let after = fixture.texts().iter().map(|run| (run.text.clone(), run.elided)); after.filter(|run| !before.contains(run)).map(|(text, _)| text).collect() }