From c7dbf52f594ae385dfaae29ce76e3d664ee95f91 Mon Sep 17 00:00:00 2001 From: aiongg Date: Tue, 24 Nov 2026 04:04:36 +0000 Subject: [PATCH] ui-contract: a tooltip keeps its text width in an inspector cell, a press hides it until the pointer leaves, and a sampled press does not nudge its modal - Scoped::start_layer clears inside_label, cell_fills and least: a layer is outside its opener's flow, so the tooltip of a control beside an inspector label no longer fills the layer's cap of the window (photocraft#16593). - A press on a tooltip's subject keeps the tooltip hidden until neither the pointer nor the keyboard is on it, so a popover the press opens is not covered. Tooltips shown at the pointer keep hiding only while pressed. - A press the canvas a modal keeps open takes (a colour sampled under the Color Picker) is not a press outside that modal, so it neither nudges nor closes it. --- .../src/contract/overlay/dismissal.rs | 9 ++- crates/ui-contract/src/contract/tooltip.rs | 17 ++++- crates/ui-contract/src/runtime/env.rs | 7 ++ crates/ui-widgets/tests/it/label/tooltip.rs | 74 ++++++++++++++++++- crates/ui-widgets/tests/it/overlay/sampler.rs | 33 +++++++++ 5 files changed, 131 insertions(+), 9 deletions(-) diff --git a/crates/ui-contract/src/contract/overlay/dismissal.rs b/crates/ui-contract/src/contract/overlay/dismissal.rs index e2c8379e3..7801d2f64 100644 --- a/crates/ui-contract/src/contract/overlay/dismissal.rs +++ b/crates/ui-contract/src/contract/overlay/dismissal.rs @@ -4,9 +4,10 @@ use super::host::{Layer, LayerMemory}; use super::props::Outside; use crate::contract::{Component, Cx}; use crate::runtime::commands::FireSource; +use crate::runtime::egui::pass; use crate::runtime::focus::{self, FocusPolicy, Initial}; use crate::runtime::overlay::{self, CloseReason, Rank}; -use crate::runtime::{frame, keys, pointer}; +use crate::runtime::{frame, keys, pointer, sampler}; use craft_behavior::CancelRule; use craft_keymap::Mods; use egui::{Context, Rect}; @@ -94,13 +95,15 @@ pub(super) fn popup_dismissal( (here || away || crate::runtime::windows::pressed_elsewhere(ctx, popup, appears)).then_some(CloseReason::Outside) } -/// Whether a primary press landed outside the layer, its anchor and the layers opened from inside it. +/// Whether a primary press landed outside the layer, its anchor and the layers opened from inside it. A press the +/// canvas the layer keeps open took (a colour sampled under the Color Picker) is the canvas's, not one beside the layer. fn press_outside(ctx: &Context, layer: &Layer<'_>, rect: Rect, anchor: Rect) -> bool { let Some(press) = pointer::primary_press(ctx) else { return false; }; let inside_family = overlay::family(ctx, layer.id).iter().filter_map(|id| overlay::entry(ctx, *id)).any(|entry| entry.rect.contains(press)); - !rect.contains(press) && !anchor.contains(press) && !inside_family + let sampled = || sampler::through(ctx, pass::current_viewport(ctx), layer.id).is_some() && sampler::admitted_press(ctx, press); + !rect.contains(press) && !anchor.contains(press) && !inside_family && !sampled() } /// Whether the focused stop sits in no scope that belongs to a shown layer. diff --git a/crates/ui-contract/src/contract/tooltip.rs b/crates/ui-contract/src/contract/tooltip.rs index 09ca81d2e..53464e1f2 100644 --- a/crates/ui-contract/src/contract/tooltip.rs +++ b/crates/ui-contract/src/contract/tooltip.rs @@ -2,7 +2,8 @@ //! //! A tooltip shows after one delay while the pointer rests on the component, or on keyboard focus after the same //! delay, and at once while a neighbour's tooltip was hiding a moment ago. A press, a menu that owns keys and a -//! running drag hide it. +//! running drag hide it. A press on the component keeps its tooltip hidden until the pointer leaves it, so what the +//! press opens (a popover under a swatch) is not covered. //! //! A component with no tooltip shows its own label as the tooltip when its type declares `Component::SHOWS_LABEL` //! false (or an instance does, through `Component::shows_label`): no text paints that name, inside a field or not. A @@ -42,6 +43,8 @@ struct TipState { shown: bool, /// The part the tooltip belongs to; `None` for the component's own. subject: Option, + /// A press on the subject hid the tooltip; it stays hidden until neither the pointer nor the keyboard is on it. + dismissed: bool, } /// When the last tooltip of the context hid. @@ -279,7 +282,8 @@ struct TipCall<'a> { text: &'a dyn Fn() -> Option, rich: Option>, spec: Option<&'a TooltipSpec>, - /// The tooltip shows at the pointer whatever its spec's `at` says. + /// The tooltip shows at the pointer whatever its spec's `at` says. Such a tip has no subject a press could + /// dismiss it from: it hides only while the press lasts. at_pointer: bool, /// The pointer rests on the subject or the keyboard is on it. wants_input: bool, @@ -313,7 +317,12 @@ fn show_tip(call: TipCall<'_>) { } let when_now = spec.is_some_and(|spec| spec.when == TooltipWhen::Now); let screen = areas::layer_to_screen(ctx, rect_layer) * rect; - let suppressed = pressed || overlay_blocks(ctx, screen.center()) || pointer::press_captured(ctx) || drag::running(ctx); + if !wants_input { + state.dismissed = false; + } else if pressed && !at_pointer { + state.dismissed = true; + } + let suppressed = pressed || state.dismissed || overlay_blocks(ctx, screen.center()) || pointer::press_captured(ctx) || drag::running(ctx); if wants { state.since.get_or_insert(now); } else { @@ -334,7 +343,7 @@ fn show_tip(call: TipCall<'_>) { state.shown = show; let tip_id = sub(id, "tip"); let presence = motion::layer_presence(ctx, tip_id, owner, env.motion, show, overlay_motion()); - if state.shown || state.since.is_some() || presence.visible { + if state.shown || state.since.is_some() || state.dismissed || presence.visible { store::put(ctx, state_id, state); } if !presence.visible { diff --git a/crates/ui-contract/src/runtime/env.rs b/crates/ui-contract/src/runtime/env.rs index ec0063526..f878fd922 100644 --- a/crates/ui-contract/src/runtime/env.rs +++ b/crates/ui-contract/src/runtime/env.rs @@ -412,8 +412,15 @@ impl Scoped { /// Starts a layer from its opener's scope: the inherited group stays, the layer-local fields start fresh, and the /// layer is no line of a bar it was opened from. A layer's content measures and runs in this. + /// + /// The layer is outside its opener's flow: an inside label and a filling cell belong to the beside label's own + /// control, and the opener's least measure is not the layer's. A tooltip of a control in an inspector cell keeps + /// its text width, not the layer's cap. pub(crate) fn start_layer(&mut self) { self.start_room(); + self.inside_label = None; + self.cell_fills = false; + self.least = false; self.bracket = None; self.measure = None; self.arena = None; diff --git a/crates/ui-widgets/tests/it/label/tooltip.rs b/crates/ui-widgets/tests/it/label/tooltip.rs index 1028f6a80..b27928077 100644 --- a/crates/ui-widgets/tests/it/label/tooltip.rs +++ b/crates/ui-widgets/tests/it/label/tooltip.rs @@ -8,9 +8,9 @@ use craft_behavior::Sense; use craft_ui::buttons::Button; use craft_ui::contract::tooltip::DELAY; use craft_ui::contract::{ - CommandSet, Common, Component, Cx, GlyphLabel, LabelKind, MeasureCx, Props as _, Role, Show as _, ShowContainer as _, SideLabel, + CommandSet, Common, Component, Cx, GlyphLabel, LabelKind, LabelScheme, MeasureCx, Props as _, Role, Show as _, ShowContainer as _, SideLabel, }; -use craft_ui::form::Field; +use craft_ui::form::{Field, FieldGroup}; use craft_ui::runtime::commands::CommandState; use craft_ui::style::StyleCx; use craft_ui::testing::pointer::moved; @@ -298,3 +298,73 @@ fn a_tooltip_shown_from_inside_a_body_keeps_its_rect_frame_to_frame() { let first = frames[0].1; assert!(first.is_some() && frames.iter().all(|(passes, rect)| *passes == 1 && *rect == first), "one pass a frame and one rect: {frames:?}"); } + +/// A tooltip longer than a mark's cell and far shorter than the layer's cap. +const STROKE_TIP: &str = "Set shape stroke type"; + +/// The width of the tooltip a rest on a mark labelled `STROKE_TIP` shows in a wide window, with `inspector` in an +/// inspector's field, whose control fills the cell its label leaves; and the window's width. +fn stroke_tip_width(inspector: bool) -> (f32, f32) { + let at = Rc::new(Cell::new(Rect::NOTHING)); + let seen = Rc::clone(&at); + let mut fixture = Fixture::new(move |ui| { + let field = |ui: &mut Ui| { + Field::new("Stroke").show(ui, |ui, _| seen.set(Mark { common: Common::new() }.label(STROKE_TIP).show(ui).rect)); + }; + if inspector { + FieldGroup::new("Shape").labels(LabelScheme::Inspector).show(ui, |ui, _| field(ui)); + } else { + field(ui); + } + }); + let room = Vec2::new(1600.0, 600.0); + fixture.room(room); + fixture.settle(); + fixture.frame(vec![moved(at.get().center())]); + fixture.advance(DELAY); + fixture.settle(); + let tip = fixture.by_role(Role::TOOLTIP).first().map(|tip| tip.rect).expect("the tooltip shows"); + (tip.width(), room.x) +} + +/// A control beside an inspector label fills its cell, its tooltip does not: the tooltip layer takes its text's width, +/// as it does outside an inspector, not the layer's cap of the window (photocraft#16593). +#[trial] +fn a_tooltip_of_a_control_in_an_inspector_cell_keeps_its_text_width() { + let (plain, _) = stroke_tip_width(false); + let (inspector, room) = stroke_tip_width(true); + assert!(inspector <= plain + 0.5, "the tooltip in an inspector cell is as wide as outside one: {inspector} vs {plain}"); + assert!(inspector < room / 4.0, "the tooltip does not span the window: {inspector} of {room}"); +} + +/// A press on a control hides its tooltip until the pointer leaves the control, so what the press opens (a popover +/// under a swatch) is not covered; a rest after the pointer came back shows it again. +#[trial] +fn a_press_on_a_control_hides_its_tooltip_until_the_pointer_leaves() { + let at = Rc::new(Cell::new(Rect::NOTHING)); + let seen = Rc::clone(&at); + let mut fixture = Fixture::new(move |ui| { + ui.add_space(20.0); + seen.set(Button::new("Stroke").tooltip(STROKE_TIP.to_owned()).show(ui).rect); + }); + fixture.settle(); + let shows = |fixture: &Fixture| fixture.texts().iter().any(|run| run.text == STROKE_TIP); + let center = at.get().center(); + fixture.frame(vec![moved(center)]); + fixture.advance(DELAY); + fixture.settle(); + assert!(shows(&fixture), "a rest on the button shows its tooltip"); + + fixture.click_at(center); + fixture.advance(DELAY); + fixture.settle(); + assert!(!shows(&fixture), "after a press on the button its tooltip stays hidden while the pointer rests there"); + + fixture.frame(vec![moved(at.get().right_bottom() + Vec2::splat(200.0))]); + fixture.advance(DELAY); + fixture.settle(); + fixture.frame(vec![moved(center)]); + fixture.advance(DELAY); + fixture.settle(); + assert!(shows(&fixture), "once the pointer left and came back, a rest shows the tooltip again"); +} diff --git a/crates/ui-widgets/tests/it/overlay/sampler.rs b/crates/ui-widgets/tests/it/overlay/sampler.rs index 5f7ceb7b4..b484618b8 100644 --- a/crates/ui-widgets/tests/it/overlay/sampler.rs +++ b/crates/ui-widgets/tests/it/overlay/sampler.rs @@ -422,3 +422,36 @@ fn a_press_under_a_modal_starts_nothing_on_the_canvas() { assert!(!scene.took(PointerButton::Primary), "the press stays blocked while the dialog is open"); assert!(scene.dialog.shown.get(), "the presses leave the dialog open"); } + +/// A press on the canvas a modal dialog keeps open for its gesture is the canvas's (a colour sampled): it does not +/// nudge the dialog, which stays where it is. A press beside both the dialog and the canvas still nudges it. +#[trial] +fn a_press_the_kept_canvas_takes_does_not_nudge_the_dialog() { + use craft_ui::testing::snapshot::layer_rects; + let mut frames = Frames::new(); + frames.root(craft_ui::runtime::env::RootEnv::default().motion(craft_behavior::MotionMode::Full)); + let scene = Scene::new(Arm::Gesture(CanvasGesture::PRIMARY)); + scene.open_dialog(&mut frames); + let rest = layer_rects(&frames.ctx); + assert_eq!(rest.len(), 1, "the dialog's layer shows: {rest:?}"); + let press = |pos, pressed| Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed, modifiers: Modifiers::NONE }; + // A press and release at `at`, then a few frames; whether the dialog's layer moved from its rest meanwhile. + let moved_by_press = |frames: &mut Frames, at: Pos2| { + let mut moved_away = false; + for events in [vec![moved(at)], vec![press(at, true)], vec![press(at, false)], vec![], vec![], vec![]] { + scene.frame(frames, events); + moved_away |= layer_rects(&frames.ctx) != rest; + } + scene.settle(frames, 20); + moved_away + }; + + assert!(!moved_by_press(&mut frames, CANVAS.center()), "a press the canvas takes does not nudge the dialog"); + assert!(scene.took(PointerButton::Primary), "the press reached the canvas"); + scene.assert_dialog_holds(&frames); + + let beside = Pos2::new(760.0, 560.0); + assert!(!CANVAS.contains(beside) && !OPENER.contains(beside) && !rest[0].contains(beside), "the point lies beside dialog and canvas"); + assert!(moved_by_press(&mut frames, beside), "a press beside the dialog and the canvas nudges it"); + assert!(scene.dialog.shown.get(), "the press beside leaves the dialog open"); +}