diff --git a/crates/ui-contract/src/contract/steps/update.rs b/crates/ui-contract/src/contract/steps/update.rs index fb6866c34a..c61cac44e5 100644 --- a/crates/ui-contract/src/contract/steps/update.rs +++ b/crates/ui-contract/src/contract/steps/update.rs @@ -116,6 +116,7 @@ fn double_click(core: &CxCore<'_>) -> Option> { pub(crate) fn secondary_click(core: &CxCore<'_>) -> bool { crate::runtime::pointer::frame_raw(&core.ctx).release == Some(craft_behavior::PointerButton::Secondary) && (core.response.hovered() + || (!core.response.enabled() && crate::runtime::pointer::hovers(&core.ctx, core.id)) || core.texts.as_ref().is_some_and(|(key, _)| { core.ctx.read_response(crate::primitives::text_adapter::text_id(core.id, *key)).is_some_and(|text| text.hovered()) })) diff --git a/crates/ui-contract/src/contract/tooltip.rs b/crates/ui-contract/src/contract/tooltip.rs index 53464e1f29..085c005879 100644 --- a/crates/ui-contract/src/contract/tooltip.rs +++ b/crates/ui-contract/src/contract/tooltip.rs @@ -185,7 +185,14 @@ pub(crate) fn run(core: &super::CxCore<'_>, input: TipIn<'_>) { let keyboard = core.state.flags.contains(Flags::FOCUS_VISIBLE); let (spec, rect, hovered, subject) = match &part { Some(part) => (Some(part.spec), part.rect, part.hovered || keyboard, Some(part.part)), - None => (core.resolved.tooltip.map(Tooltip::spec), core.rect, core.response.hovered() || keyboard, None), + // (a disabled root reports itself hovered by nothing, as egui's rule says, but it still explains itself: the + // pointer over it counts for the tooltip, by the arena's hover of the root) + None => ( + core.resolved.tooltip.map(Tooltip::spec), + core.rect, + core.response.hovered() || (!core.response.enabled() && crate::runtime::pointer::hovers(&core.ctx, core.id)) || keyboard, + None, + ), }; let (shows_label, canvas_control, message) = (input.shows_label, input.canvas_control, input.message.as_deref()); let text = || match &part { diff --git a/crates/ui-contract/src/primitives/sensing.rs b/crates/ui-contract/src/primitives/sensing.rs index e25eaaeffe..2197f80ff4 100644 --- a/crates/ui-contract/src/primitives/sensing.rs +++ b/crates/ui-contract/src/primitives/sensing.rs @@ -792,6 +792,7 @@ pub(crate) fn sense_root(ui: &Ui, spec: RootSpec<'_>) -> RootResult { std::iter::once(background).chain(own_shapes(spec.own, spec.id, read.target())), ); let mut response = response; + response::restrict_enabled(&mut response, spec.enabled); let outcome = outcome_in(ctx, &read, arena, layer, spec.enabled); // The root is hovered while a shape of its arena shows hover (`pointer::Under`): its background, a part or a member; // it contains the pointer while any shape of the arena lies under it. diff --git a/crates/ui-contract/src/runtime/egui/response.rs b/crates/ui-contract/src/runtime/egui/response.rs index 2179582534..707bf63e2b 100644 --- a/crates/ui-contract/src/runtime/egui/response.rs +++ b/crates/ui-contract/src/runtime/egui/response.rs @@ -126,12 +126,25 @@ fn member_on(ctx: &egui::Context, layer_id: LayerId, clip: Rect, id: Id, rect: R response } +/// Clears a root's `Response` enabled flag when its component is disabled or inert, so the returned response tells the +/// truth: `enabled()` is false and egui's disabled-hover hooks apply. The root's own egui widget stays registered as +/// enabled, so a disabled button with a reason can still take keyboard focus and explain itself. Calling egui's +/// `Response::interact` on this response is not supported: it registers the id again from this flag, as disabled, and +/// the root loses that focus. Add senses through the component instead. +pub(crate) fn restrict_enabled(response: &mut Response, enabled: bool) { + if !enabled { + response.flags.remove(Flags::ENABLED); + } +} + /// Sets the hover of a root's `Response` from what lies under the pointer (`pointer::Under`), in place of egui's: egui /// reports a widget that senses a click hovered only where it is egui's click winner, and a root's widget senses the /// clicks of its parts and members. `hovered`: a shape of the root's arena shows hover; `contains`: one lies under the /// pointer, on top or not. pub(crate) fn hover_flags(response: &mut Response, hovered: bool, contains: bool) { - response.flags.set(Flags::HOVERED, hovered); + // egui's own rule: a disabled response is never hovered, whatever lies under it. Containment still reports, and the + // arena's own hover fact is untouched: this is only what the returned `Response` says. + response.flags.set(Flags::HOVERED, hovered && response.enabled()); response.flags.set(Flags::CONTAINS_POINTER, contains); } diff --git a/crates/ui-widgets/tests/it/disabled_response.rs b/crates/ui-widgets/tests/it/disabled_response.rs new file mode 100644 index 0000000000..5e87aee77f --- /dev/null +++ b/crates/ui-widgets/tests/it/disabled_response.rs @@ -0,0 +1,116 @@ +//! A disabled component reports its effective state on the response it returns, without disabling its native parent. + +use craft_ui::buttons::Button; +use craft_ui::contract::{Bind, Props as _, Role, Show as _, ShowContainer as _}; +use craft_ui::display::Text; +use craft_ui::popover::Popover; +use craft_ui::runtime::overlay::CloseReason; +use craft_ui::testing::{Fixture, pointer, trial}; +use egui::{Pos2, Rect}; +use std::cell::{Cell, RefCell}; +use std::rc::Rc; + +#[trial] +fn a_button_reports_explicit_and_inherited_disabled_state_on_its_response() { + let enabled = Rc::new(Cell::new(false)); + let parent_enabled = Rc::new(Cell::new(true)); + let clicks = Rc::new(Cell::new(0usize)); + let observed = Rc::new(RefCell::new(None)); + let (shown_enabled, shown_parent, seen_clicks, seen) = (enabled.clone(), parent_enabled.clone(), clicks.clone(), observed.clone()); + let mut fixture = Fixture::new(move |ui| { + ui.add_enabled_ui(shown_parent.get(), |ui| { + let parent = ui.is_enabled(); + let out = Button::new("Use Library Folder").enabled(shown_enabled.get()).show(ui); + if out.clicked() { + seen_clicks.set(seen_clicks.get() + 1); + } + *seen.borrow_mut() = Some((out.response.id, out.response.enabled(), parent)); + }); + }); + fixture.settle(); + let (id, reported, parent) = observed.borrow().as_ref().copied().expect("the button's output"); + assert!(parent, "the native parent is enabled"); + assert_eq!(fixture.tree().role(id), Some(Role::BUTTON), "the response addresses the button's own node"); + assert_eq!(fixture.tree().name(id), Some("Use Library Folder")); + assert!(fixture.tree().is_disabled(id)); + assert!(!reported, "Button::enabled(false) reports false on its response under an enabled parent"); + + enabled.set(true); + fixture.idle(2); + let (current, reported, parent) = observed.borrow().as_ref().copied().expect("the enabled output"); + assert_eq!(current, id); + assert!(parent && reported && !fixture.tree().is_disabled(id), "an enabled button reports enabled"); + + parent_enabled.set(false); + fixture.idle(2); + let (current, reported, parent) = observed.borrow().as_ref().copied().expect("the inherited-disabled output"); + assert_eq!(current, id); + assert!(!parent && !reported && fixture.tree().is_disabled(id), "a disabled parent disables the button"); + + parent_enabled.set(true); + enabled.set(false); + fixture.idle(2); + let (current, reported, parent) = observed.borrow().as_ref().copied().expect("the disabled output again"); + assert_eq!(current, id); + assert!(parent && !reported && fixture.tree().is_disabled(id)); + let target = *fixture.by_role(Role::BUTTON).first().expect("the disabled button"); + fixture.click(&target).settle(); + assert_eq!(clicks.get(), 0, "a disabled button does not activate"); + + enabled.set(true); + fixture.idle(2); + let target = *fixture.by_role(Role::BUTTON).first().expect("the re-enabled button"); + fixture.click(&target).settle(); + assert_eq!(clicks.get(), 1, "the re-enabled button activates once"); +} + +#[trial] +fn a_disabled_opener_closes_its_popover_by_the_anchor_gone_rule() { + let enabled = Rc::new(Cell::new(true)); + let closes = Rc::new(RefCell::new(Vec::new())); + let opens = Rc::new(Cell::new(false)); + let (opener_enabled, seen_closes, seen_open) = (enabled.clone(), closes.clone(), opens.clone()); + let mut fixture = Fixture::new(move |ui| { + let opener = Button::new("Options").enabled(opener_enabled.get()).show(ui); + let mut is_open = seen_open.get(); + let shown = Popover::new("Options card", Bind::bound(&mut is_open)).opened_by(&opener.response).show(ui, |ui, _| { + Text::new("Body").show(ui); + }); + seen_open.set(is_open); + if let Some(why) = shown.output.closed { + seen_closes.borrow_mut().push(why); + } + }); + fixture.settle(); + opens.set(true); + fixture.idle(3); + assert!(opens.get(), "the popover stays open while its opener is enabled"); + assert!(closes.borrow().is_empty(), "nothing closes it: {:?}", closes.borrow()); + enabled.set(false); + fixture.idle(3); + assert_eq!(closes.borrow().first(), Some(&CloseReason::AnchorGone), "the disabled opener's popover closes: {:?}", closes.borrow()); +} + +const FIELD: Rect = Rect { min: Pos2::new(20.0, 140.0), max: Pos2::new(200.0, 180.0) }; + +#[trial] +fn a_disabled_root_contains_the_pointer_without_reporting_hover() { + let enabled = Rc::new(Cell::new(false)); + let seen = Rc::new(RefCell::new(None)); + let (shown, out) = (enabled.clone(), seen.clone()); + let mut fixture = Fixture::new(move |ui| { + let shown = Button::new("Use Library Folder").enabled(shown.get()).show_at(ui, FIELD); + *out.borrow_mut() = Some((shown.response.enabled(), shown.response.contains_pointer(), shown.response.hovered())); + }); + fixture.frame(vec![pointer::moved(FIELD.center())]); + fixture.frame(Vec::new()); + let (is_enabled, contains, hovered) = seen.borrow().expect("the button ran"); + assert!(!is_enabled, "the button reports itself disabled"); + assert!(contains, "a disabled root still reports that it contains the pointer"); + assert!(!hovered, "egui's rule: a disabled response is not hovered, though the pointer is over it"); + enabled.set(true); + fixture.frame(vec![pointer::moved(FIELD.center())]); + fixture.frame(Vec::new()); + let (is_enabled, contains, hovered) = seen.borrow().expect("the button ran"); + assert!(is_enabled && contains && hovered, "an enabled root is hovered under the pointer"); +} diff --git a/crates/ui-widgets/tests/it/label/tooltip.rs b/crates/ui-widgets/tests/it/label/tooltip.rs index b27928077a..e5c51646b6 100644 --- a/crates/ui-widgets/tests/it/label/tooltip.rs +++ b/crates/ui-widgets/tests/it/label/tooltip.rs @@ -337,6 +337,25 @@ fn a_tooltip_of_a_control_in_an_inspector_cell_keeps_its_text_width() { assert!(inspector < room / 4.0, "the tooltip does not span the window: {inspector} of {room}"); } +/// The lower button contains the pointer but cannot start a tooltip through the control on top. +#[trial] +fn a_covered_enabled_button_does_not_show_its_tooltip() { + let rect = Rect::from_min_size(Pos2::new(30.0, 50.0), Vec2::new(120.0, 36.0)); + let hit = Rc::new(Cell::new((false, false, false))); + let seen = Rc::clone(&hit); + let mut fixture = Fixture::new(move |ui| { + let under = Button::new("Under").id_salt("under").tooltip(STROKE_TIP.to_owned()).show_at(ui, rect); + let over = Button::new("Over").id_salt("over").show_at(ui, rect); + seen.set((under.response.contains_pointer(), under.response.hovered(), over.response.hovered())); + }); + fixture.settle(); + fixture.frame(vec![moved(rect.center())]); + fixture.advance(DELAY); + fixture.settle(); + assert_eq!(hit.get(), (true, false, true), "the pointer is inside both buttons but only the upper one wins hover"); + assert!(!fixture.texts().iter().any(|run| run.text == STROKE_TIP), "the covered button's tooltip stays hidden"); +} + /// 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] diff --git a/crates/ui-widgets/tests/it/main.rs b/crates/ui-widgets/tests/it/main.rs index 82d153f715..4908163a89 100644 --- a/crates/ui-widgets/tests/it/main.rs +++ b/crates/ui-widgets/tests/it/main.rs @@ -11,6 +11,7 @@ pub(crate) mod checkbox_sweep; pub(crate) mod commands; pub(crate) mod contract; pub(crate) mod disabled_fade; +pub(crate) mod disabled_response; pub(crate) mod field_text_centred; pub(crate) mod focus_ring; pub(crate) mod form;