Merge branch 'imbris/leak-fix' into 'master'

Potential fix for leak when inventory is open

See merge request veloren/veloren!2995
This commit is contained in:
Imbris 2021-11-10 22:15:25 +00:00
commit ee6b8b6828

View File

@ -50,6 +50,8 @@ pub struct ItemTooltipManager {
fade_dur: Duration, fade_dur: Duration,
// Current scaling of the ui // Current scaling of the ui
logical_scale_factor: f64, logical_scale_factor: f64,
// Ids for tooltip
tooltip_ids: widget::id::List,
} }
impl ItemTooltipManager { impl ItemTooltipManager {
@ -59,6 +61,7 @@ impl ItemTooltipManager {
hover_dur, hover_dur,
fade_dur, fade_dur,
logical_scale_factor, logical_scale_factor,
tooltip_ids: widget::id::List::new(),
} }
} }
@ -121,12 +124,14 @@ impl ItemTooltipManager {
I: Borrow<dyn ItemDesc>, I: Borrow<dyn ItemDesc>,
{ {
let mp_h = MOUSE_PAD_Y / self.logical_scale_factor; let mp_h = MOUSE_PAD_Y / self.logical_scale_factor;
let mut tooltip_id = widget::id::List::new();
for (i, item) in items.enumerate() { let tooltip_ids = &mut self.tooltip_ids; // TODO: remove with Rust 2021
tooltip_id.resize(i + 1, &mut ui.widget_id_generator()); let mut id_walker = tooltip_ids.walk();
let tooltip = |transparency, mouse_pos: [f64; 2], ui: &mut UiCell| { let tooltip = |transparency, mouse_pos: [f64; 2], ui: &mut UiCell| {
let mut prev_id = None;
for item in items {
let tooltip_id = id_walker.next(tooltip_ids, &mut ui.widget_id_generator());
// Fill in text and the potential image beforehand to get an accurate size for // Fill in text and the potential image beforehand to get an accurate size for
// spacing // spacing
let tooltip = tooltip let tooltip = tooltip
@ -153,42 +158,40 @@ impl ItemTooltipManager {
m_y - mp_h - t_h / 2.0 m_y - mp_h - t_h / 2.0
}; };
if i == 0 { if let Some(prev_id) = prev_id {
tooltip tooltip
.floating(true) .floating(true)
.transparency(transparency) .transparency(transparency)
.x_y(x, y) .up_from(prev_id, 5.0)
.set(tooltip_id[i], ui); .set(tooltip_id, ui);
} else { } else {
tooltip tooltip
.floating(true) .floating(true)
.transparency(transparency) .transparency(transparency)
.up_from(tooltip_id[i - 1], 5.0) .x_y(x, y)
.set(tooltip_id[i], ui); .set(tooltip_id, ui);
}
prev_id = Some(tooltip_id);
} }
}; };
match self.state { match self.state {
HoverState::Hovering(Hover(id, xy)) if id == src_id => tooltip(1.0, xy, ui), HoverState::Hovering(Hover(id, xy)) if id == src_id => tooltip(1.0, xy, ui),
HoverState::Fading(start, Hover(id, xy), _) if id == src_id => tooltip( HoverState::Fading(start, Hover(id, xy), _) if id == src_id => tooltip(
(0.1f32 (0.1f32 - start.elapsed().as_millis() as f32 / self.hover_dur.as_millis() as f32)
- start.elapsed().as_millis() as f32 / self.hover_dur.as_millis() as f32)
.max(0.0), .max(0.0),
xy, xy,
ui, ui,
), ),
HoverState::Start(start, id) HoverState::Start(start, id) if id == src_id && start.elapsed() > self.hover_dur => {
if id == src_id && start.elapsed() > self.hover_dur =>
{
let xy = ui.global_input().current.mouse.xy; let xy = ui.global_input().current.mouse.xy;
self.state = HoverState::Hovering(Hover(id, xy)); self.state = HoverState::Hovering(Hover(id, xy));
tooltip(1.0, xy, ui); tooltip(1.0, xy, ui);
} },
_ => (), _ => (),
} }
} }
} }
}
pub struct ItemTooltipped<'a, W, I> { pub struct ItemTooltipped<'a, W, I> {
inner: W, inner: W,