* claim: #10481 * Restart tooltip delay per menu row and place tips beside the drop-down (#10481) * Format --------- Co-authored-by: Dahak Builds <dahakbuilds@users.noreply.github.com>
This commit is contained in:
1 parent
24b91bd453
commit
f25a8e6abc
1 file changed
+141
-9
@@ -2297,6 +2297,126 @@ fn dropdown_width(app: &SolveApp, ui: &egui::Ui, cmds: &[&'static solidcraft_eng
|
||||
.fold(200.0, f32::max)
|
||||
}
|
||||
|
||||
/// True once the pointer has rested on this drop-down row long enough for its tip. egui's
|
||||
/// shared tooltip grace time lets every row after the first show its tip the moment the
|
||||
/// pointer reaches it; timing each row's own hover restarts the delay for each newly hovered
|
||||
/// row instead (#10481).
|
||||
fn dropdown_tip_ready(ui: &egui::Ui, resp: &egui::Response) -> bool {
|
||||
let ctx = ui.ctx();
|
||||
let key = resp.id.with("sc_dropdown_tip");
|
||||
if !resp.hovered() {
|
||||
ctx.data_mut(|d| d.remove::<(f64, u64)>(key));
|
||||
return false;
|
||||
}
|
||||
let pass = ctx.cumulative_pass_nr();
|
||||
let now = ctx.input(|i| i.time);
|
||||
let (mut began, seen) = ctx.data(|d| d.get_temp::<(f64, u64)>(key)).unwrap_or((now, 0));
|
||||
// A drop-down that closed and opened again under the pointer starts the delay afresh.
|
||||
if seen + 1 < pass {
|
||||
began = now;
|
||||
}
|
||||
ctx.data_mut(|d| d.insert_temp(key, (began, pass)));
|
||||
let delay = f64::from(ui.style().interaction.tooltip_delay);
|
||||
if now - began < delay {
|
||||
ctx.request_repaint_after_secs((delay - (now - began)) as f32);
|
||||
return false;
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
/// Show `add` as `resp`'s tip beside the drop-down rather than under the pointer: to the
|
||||
/// right of the menu, or to its left when the right has no room, so the tip never covers
|
||||
/// the menu's rows (#10481). Returns the tip's rect when it was shown.
|
||||
fn dropdown_tip(ui: &egui::Ui, menu: Rect, resp: &egui::Response, add: impl FnOnce(&mut egui::Ui)) -> Option<Rect> {
|
||||
if !dropdown_tip_ready(ui, resp) {
|
||||
return None;
|
||||
}
|
||||
let ctx = ui.ctx();
|
||||
let tip_id = resp.id.with("sc_dropdown_tip_area");
|
||||
// Last pass's size; the first pass assumes the tip fits on the right.
|
||||
let size = ctx.read_response(tip_id).map(|r| r.rect.size()).unwrap_or(egui::Vec2::ZERO);
|
||||
let screen = ctx.content_rect();
|
||||
let gap = 6.0;
|
||||
let x = if menu.right() + gap + size.x <= screen.right() { menu.right() + gap } else { (menu.left() - gap - size.x).max(screen.left()) };
|
||||
let y = resp.rect.top().min((screen.bottom() - size.y).max(screen.top()));
|
||||
let area = Container::new(ctx, Surface::Popup)
|
||||
.area(tip_id)
|
||||
.order(egui::Order::Tooltip)
|
||||
.interactable(false)
|
||||
.fixed_pos(pos2(x, y))
|
||||
.show(ctx, |ui| Container::new(ctx, Surface::Popup).show_frame(ui, Container::menu_frame(), ui.max_rect(), add));
|
||||
Some(area.inner.response.rect)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod dropdown_tip_tests {
|
||||
use super::*;
|
||||
use egui::{Event, RawInput};
|
||||
|
||||
/// One pass of a fake two-row drop-down at `menu`; returns the tips shown this pass.
|
||||
fn tip_pass(ctx: &egui::Context, menu: Rect, time: f64, at: egui::Pos2) -> Vec<(usize, Rect)> {
|
||||
let raw = RawInput { events: vec![Event::PointerMoved(at)], time: Some(time), ..Default::default() };
|
||||
let mut shown: Vec<(usize, Rect)> = Vec::new();
|
||||
crate::test_support::frame(ctx, raw, |ui| {
|
||||
let ctx = ui.ctx();
|
||||
egui::Area::new(egui::Id::new("test_dropdown")).fixed_pos(menu.min).show(ctx, |ui| {
|
||||
for row in 0..2 {
|
||||
let rect = Rect::from_min_size(menu.min + vec2(0.0, row as f32 * 32.0), vec2(menu.width(), 32.0));
|
||||
let resp = ui.interact(rect, egui::Id::new(("test_row", row)), Sense::hover());
|
||||
let tip = if row == 0 {
|
||||
dropdown_tip(ui, menu, &resp, |ui| {
|
||||
ui.label("first row");
|
||||
})
|
||||
} else {
|
||||
dropdown_tip(ui, menu, &resp, |ui| {
|
||||
ui.label("second row");
|
||||
})
|
||||
};
|
||||
if let Some(rect) = tip {
|
||||
shown.push((row, rect));
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
shown
|
||||
}
|
||||
|
||||
/// egui registers hover a pass or two after the pointer arrives (measured), while leaving
|
||||
/// a row drops hover the same pass. Timings below keep a margin around that lag.
|
||||
#[test]
|
||||
fn each_newly_hovered_row_waits_the_full_tip_delay() {
|
||||
let ctx = crate::fonts::test_context();
|
||||
let menu = Rect::from_min_size(pos2(20.0, 20.0), vec2(220.0, 64.0));
|
||||
let a = menu.min + vec2(10.0, 16.0);
|
||||
let b = menu.min + vec2(10.0, 48.0);
|
||||
let mut tips = Vec::new();
|
||||
for (time, at, expect) in [
|
||||
(0.0, a, None),
|
||||
(0.1, a, None),
|
||||
(0.3, a, None), // hover registers around here; the first row's delay starts
|
||||
(0.5, a, None), // still inside the delay
|
||||
(0.9, a, Some(0)),
|
||||
(1.0, b, None), // the first row lost hover the moment the pointer left it
|
||||
(1.2, b, None), // the second row must not inherit the open tip's grace (#10481)
|
||||
(1.4, b, None), // its own delay cannot have elapsed yet, whenever hover registered
|
||||
(2.2, b, Some(1)), // after its own full delay, with margin for late registration
|
||||
] {
|
||||
let got = tip_pass(&ctx, menu, time, at);
|
||||
match expect {
|
||||
Some(row) => {
|
||||
assert_eq!(got.len(), 1, "exactly row {row}'s tip at t={time}, got {got:?}");
|
||||
assert_eq!(got[0].0, row, "wrong row's tip at t={time}");
|
||||
let tip = got[0].1;
|
||||
assert!(tip.min.x >= menu.max.x, "the tip sits beside the menu, never over its rows: {tip:?} vs {menu:?}");
|
||||
tips.push((row, tip));
|
||||
}
|
||||
None => assert!(got.is_empty(), "no tip expected at t={time}, got {got:?}"),
|
||||
}
|
||||
}
|
||||
assert_eq!(tips.len(), 2, "both rows showed a tip after their own delay");
|
||||
}
|
||||
}
|
||||
|
||||
/// One command row of a panel drop-down: icon, label and shortcut, with the hover background
|
||||
/// under the pointer or keyboard focus (like every menu).
|
||||
/// `revealed` is the pulse strength when the command search points this row out (#7287).
|
||||
@@ -2363,19 +2483,31 @@ fn dropdown_row(
|
||||
crate::scenario::publish_handle(&format!("dropdown_pin:{}", c.id), pr.center());
|
||||
}
|
||||
let tip = if pinned { "Remove from Toolbar" } else { "Pin to Toolbar" };
|
||||
if pin.on_hover_text(tip).clicked() {
|
||||
// The drop-down's rect from the last pass: tips sit beside it, never over its rows (#10481).
|
||||
let menu = egui::containers::AreaState::load(ui.ctx(), ui.layer_id().id).map(|state| state.rect()).unwrap_or(rect);
|
||||
if pin.clicked() {
|
||||
return RowAction::TogglePin;
|
||||
}
|
||||
dropdown_tip(ui, menu, &pin, |ui| {
|
||||
ui.label(crate::i18n::tr_label(tip));
|
||||
});
|
||||
// A disabled row says why; a documented command also shows its help (#2758).
|
||||
let reason = info.disabled_reason.as_deref().filter(|_| !info.enabled).map(|reason| app.tr_label(reason));
|
||||
let resp = if let Some(hint) = persistent_hint(app, c, info.enabled) {
|
||||
resp.on_hover_text(hint)
|
||||
} else {
|
||||
match crate::command_help::row_tip(c.id, &label, Some(shortcut.as_str()).filter(|k| !k.is_empty()), reason) {
|
||||
Some(tip) => crate::command_help::tooltip(resp, c.id, tip),
|
||||
None => resp,
|
||||
}
|
||||
};
|
||||
if let Some(hint) = persistent_hint(app, c, info.enabled) {
|
||||
dropdown_tip(ui, menu, &resp, move |ui| {
|
||||
ui.label(hint);
|
||||
});
|
||||
} else if let Some(tip) = crate::command_help::row_tip(c.id, &label, Some(shortcut.as_str()).filter(|k| !k.is_empty()), reason) {
|
||||
let help = crate::command_help::help(c.id);
|
||||
dropdown_tip(ui, menu, &resp, move |ui| {
|
||||
ui.set_max_width(300.0);
|
||||
ui.label(tip);
|
||||
if let Some(help) = help {
|
||||
ui.add_space(2.0);
|
||||
ui.label(egui::RichText::new(help).weak());
|
||||
}
|
||||
});
|
||||
}
|
||||
if resp.secondary_clicked() {
|
||||
return RowAction::Menu(resp.interact_pointer_pos().unwrap_or(rect.left_bottom()));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user