diff --git a/crates/ui-egui/src/prefs_dialog.rs b/crates/ui-egui/src/prefs_dialog.rs index d24e2da7e3..ce93cbabfe 100644 --- a/crates/ui-egui/src/prefs_dialog.rs +++ b/crates/ui-egui/src/prefs_dialog.rs @@ -332,8 +332,9 @@ fn apply(app: &mut VectorcraftApp, ctx: &egui::Context, force: bool) { app.canvas.key = None; ctx.data_mut(|d| d.insert_temp(id, Some(want))); } - let cards = - crate::toolbar::card::CardPrefs { on: app.session.prefs.show_tool_cards, animated_zoom: crate::canvas::animated_zoom(&app.session.prefs) }; + // Cards show only with Show Tool Tips on: an open card goes away as soon as tool tips are turned off. + let p = &app.session.prefs; + let cards = crate::toolbar::card::CardPrefs { on: p.show_tool_cards && p.show_tool_tips, animated_zoom: crate::canvas::animated_zoom(p) }; crate::toolbar::card::set_prefs(ctx, cards); let z = app.session.prefs.ui_scaling.clamp(0.75, 2.0) as f32; if (ctx.zoom_factor() - z).abs() > 1e-3 { diff --git a/crates/ui-egui/src/toolbar/card.rs b/crates/ui-egui/src/toolbar/card.rs index 6ff3ef0318..7a911b0773 100644 --- a/crates/ui-egui/src/toolbar/card.rs +++ b/crates/ui-egui/src/toolbar/card.rs @@ -72,11 +72,13 @@ pub(crate) fn hover(resp: Response, tool: &ToolInfo, side: Side, tip: Option bool { /// modifier key is held, or a press, key, modifier, scroll or pinch came since it arrived. fn quiet(ctx: &Context, resp: &Response) -> bool { let quiet_for: Option = ctx.data(|d| d.get_temp(quiet_id())); - let over = resp.hovered() && ctx.input(|i| i.pointer.hover_pos()).is_some_and(|p| resp.rect.contains(p)); - if !over { - if quiet_for == Some(resp.id) { - ctx.data_mut(|d| d.remove::(quiet_id())); - } + if !pointer_on(ctx, resp) { + forget_quiet(ctx, resp); return true; } let busy = ctx.input(|i| { @@ -176,6 +175,18 @@ fn quiet(ctx: &Context, resp: &Response) -> bool { quiet_for == Some(resp.id) } +/// Whether the pointer rests on the button `resp`. +fn pointer_on(ctx: &Context, resp: &Response) -> bool { + resp.hovered() && ctx.input(|i| i.pointer.hover_pos()).is_some_and(|p| resp.rect.contains(p)) +} + +/// Drops the quiet mark of `resp`, once the pointer has left it. +fn forget_quiet(ctx: &Context, resp: &Response) { + if ctx.data(|d| d.get_temp::(quiet_id())) == Some(resp.id) { + ctx.data_mut(|d| d.remove::(quiet_id())); + } +} + /// The card: icon, name and shortcut; what the tool does; one line per action of its hint. fn card(ui: &mut Ui, tool: &ToolInfo, description: &str, animated_zoom: bool) { let t = Tokens::get(ui.ctx()); diff --git a/crates/ui-egui/src/toolbar/tests.rs b/crates/ui-egui/src/toolbar/tests.rs index 4d07cb975b..156b0714cf 100644 --- a/crates/ui-egui/src/toolbar/tests.rs +++ b/crates/ui-egui/src/toolbar/tests.rs @@ -1127,6 +1127,44 @@ fn tool_cards_are_off_by_default_and_fall_back_to_the_tool_tip() { assert!(!none.contains(&described("selection")) && !none.contains(&selection_tip), "tool tips off: {none:?}"); } +/// A card that is showing goes away when tool tips are turned off, while the pointer stays on its +/// button. +#[test] +fn a_shown_tool_card_goes_away_when_tool_tips_are_turned_off() { + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + app.run("file.new", json!({"width": 300, "height": 300})).unwrap(); + app.run("prefs.set", json!({"key": "showToolCards", "value": true})).unwrap(); + let ctx = egui::Context::default(); + let selection = frame(&mut app, &ctx, 0.0, vec![])[0].center(); + let (time, _) = until_card(&mut app, &ctx, 1.0, selection, "selection"); + app.run("prefs.set", json!({"key": "showToolTips", "value": false})).unwrap(); + let after: Vec = (1..5).flat_map(|i| painted_texts(&mut app, &ctx, time + f64::from(i) * 0.1, vec![])).collect(); + assert!(!after.contains(&described("selection")), "{after:?}"); + + // A card a click put away stays away while the pointer rests on its button, and shows again on + // the next rest when the pointer left and came back while tool tips were off. + app.run("prefs.set", json!({"key": "showToolTips", "value": true})).unwrap(); + let (t, _) = until_card(&mut app, &ctx, 10.0, selection, "selection"); + let b = |pressed| Event::PointerButton { pos: selection, button: PointerButton::Primary, pressed, modifiers: Default::default() }; + painted_texts(&mut app, &ctx, t + 0.1, vec![b(true)]); + painted_texts(&mut app, &ctx, t + 0.15, vec![b(false)]); + // Tool tips off and on again while the pointer stays: the card stays away. + app.run("prefs.set", json!({"key": "showToolTips", "value": false})).unwrap(); + painted_texts(&mut app, &ctx, t + 0.2, vec![]); + app.run("prefs.set", json!({"key": "showToolTips", "value": true})).unwrap(); + let stayed: Vec = (3..30).flat_map(|i| painted_texts(&mut app, &ctx, t + f64::from(i) * 0.1, vec![])).collect(); + assert!(!stayed.contains(&described("selection")), "{stayed:?}"); + let t = t + 3.0; + app.run("prefs.set", json!({"key": "showToolTips", "value": false})).unwrap(); + painted_texts(&mut app, &ctx, t + 0.2, vec![Event::PointerGone]); + // Back onto the button. + painted_texts(&mut app, &ctx, t + 0.3, vec![Event::PointerMoved(selection + vec2(2.0, 2.0))]); + painted_texts(&mut app, &ctx, t + 0.35, vec![Event::PointerMoved(selection)]); + app.run("prefs.set", json!({"key": "showToolTips", "value": true})).unwrap(); + let again: Vec = (4..30).flat_map(|i| painted_texts(&mut app, &ctx, t + f64::from(i) * 0.1, vec![])).collect(); + assert!(again.contains(&described("selection")), "{again:?}"); +} + /// A card never gets in the way: a press with it showing picks the tool and puts the card away at /// once, it stays away until the pointer leaves, a key (one the shortcuts take first too), a /// modifier key alone and a scroll put it away, a long press with it showing still opens a group's flyout on time, while that flyout is