fix tooltips not disappearing, clippy

This commit is contained in:
Aleksander
2025-11-26 22:01:19 +01:00
parent d5c5d06b3a
commit 85eab33c94
10 changed files with 131 additions and 74 deletions

View File

@@ -1,6 +1,6 @@
use crate::{
animation::{Animation, AnimationEasing},
components::{self, tooltip::ComponentTooltip, Component, ComponentBase, ComponentTrait, RefreshData},
components::{self, Component, ComponentBase, ComponentTrait, RefreshData, tooltip::ComponentTooltip},
drawing::{self, Boundary, Color},
event::{CallbackDataCommon, EventListenerCollection, EventListenerID, EventListenerKind},
i18n::Translation,
@@ -10,15 +10,15 @@ use crate::{
util::centered_matrix,
},
widget::{
self, ConstructEssentials, EventResult, WidgetData,
label::{WidgetLabel, WidgetLabelParams},
rectangle::{WidgetRectangle, WidgetRectangleParams},
util::WLength,
ConstructEssentials, EventResult, WidgetData,
},
};
use glam::{Mat4, Vec3};
use std::{cell::RefCell, rc::Rc};
use taffy::{prelude::length, AlignItems, JustifyContent};
use taffy::{AlignItems, JustifyContent, prelude::length};
pub struct Params {
pub text: Option<Translation>, // if unset, label will not be populated
@@ -91,8 +91,19 @@ impl ComponentTrait for ComponentButton {
&mut self.base
}
fn refresh(&self, _data: &mut RefreshData) {
fn refresh(&self, data: &mut RefreshData) {
// nothing to do
let mut state = self.state.borrow_mut();
if state.active_tooltip.is_some() {
if let Some(node_id) = data.common.state.nodes.get(self.base.get_id()) {
if !widget::is_node_visible(&data.common.state.tree, *node_id) {
state.active_tooltip = None; // destroy the tooltip, this button is now hidden
}
} else {
debug_assert!(false);
}
}
}
}
@@ -472,6 +483,6 @@ pub fn construct(ess: &mut ConstructEssentials, params: Params) -> anyhow::Resul
let button = Rc::new(ComponentButton { base, data, state });
ess.layout.defer_component_refresh(Component(button.clone()));
ess.layout.register_component_refresh(Component(button.clone()));
Ok((root, button))
}

View File

@@ -1,4 +1,5 @@
use std::rc::Rc;
use std::hash::Hash;
use std::{hash::Hasher, rc::Rc};
use crate::{
any::AnyTrait,
@@ -54,3 +55,21 @@ impl Component {
unsafe { Ok(Rc::from_raw(Rc::into_raw(self.0.clone()).cast())) }
}
}
// these hash/eq impls are required in case we want to do something like HashSet<Component> for convenience reasons.
// hash by address
impl Hash for Component {
fn hash<H: Hasher>(&self, state: &mut H) {
std::ptr::hash(&raw const self.0, state);
}
}
// match by address
impl PartialEq for Component {
fn eq(&self, other: &Self) -> bool {
std::ptr::eq(&raw const self.0, &raw const other.0)
}
}
impl Eq for Component {}

View File

@@ -1,6 +1,6 @@
use std::{
cell::{RefCell, RefMut},
collections::{HashMap, VecDeque},
collections::{HashMap, HashSet, VecDeque},
io::Write,
rc::{Rc, Weak},
};
@@ -139,7 +139,7 @@ pub struct Layout {
pub tasks: LayoutTasks,
components_to_refresh_once: Vec<Component>,
components_to_refresh_once: HashSet<Component>,
registered_components_to_refresh: HashMap<taffy::NodeId, Component>,
pub widgets_to_tick: Vec<WidgetID>,
@@ -320,10 +320,6 @@ impl Layout {
alterables,
};
/* todo
let widget_id = comp.0.base().get_id();
*/
comp.0.refresh(&mut RefreshData { common: &mut common });
}
self.components_to_refresh_once.clear();
@@ -340,12 +336,12 @@ impl Layout {
self.widgets_to_tick.clear();
}
// call ComponentTrait::refresh() once
// call ComponentTrait::refresh() *once* in the next tick
pub fn defer_component_refresh(&mut self, component: Component) {
self.components_to_refresh_once.push(component);
self.components_to_refresh_once.insert(component);
}
// call ComponentTrait::refresh() every time the layout is dirty
// call ComponentTrait::refresh() *every time time* the layout is dirty
pub fn register_component_refresh(&mut self, component: Component) {
let widget_id = component.0.base().get_id();
let Some(node_id) = self.state.nodes.get(widget_id) else {
@@ -571,7 +567,7 @@ impl Layout {
needs_redraw: true,
haptics_triggered: false,
animations: Animations::default(),
components_to_refresh_once: Vec::new(),
components_to_refresh_once: HashSet::new(),
registered_components_to_refresh: HashMap::new(),
widgets_to_tick: Vec::new(),
tasks: LayoutTasks::new(),
@@ -607,9 +603,9 @@ impl Layout {
self.refresh_recursively(self.tree_root_node, &mut to_refresh);
if !to_refresh.is_empty() {
log::debug!("refreshing {} registered widgets", to_refresh.len());
log::debug!("refreshing {} registered components", to_refresh.len());
for c in &to_refresh {
self.components_to_refresh_once.push(c.clone());
self.components_to_refresh_once.insert(c.clone());
}
}
@@ -725,11 +721,20 @@ impl Layout {
}
}
for (widget_id, style) in alterables.style_set_requests {
if let Some(node_id) = self.state.nodes.get(widget_id)
&& let Err(e) = self.state.tree.set_style(*node_id, style)
{
log::error!("failed to set style for taffy widget ID {node_id:?}: {e:?}");
for (widget_id, new_style) in alterables.style_set_requests {
if let Some(node_id) = self.state.nodes.get(widget_id) {
let old_style = self.state.tree.style(*node_id).unwrap() /* always safe */;
// refresh the component in case if visibility/display mode has changed
if old_style.display != new_style.display
&& let Some(component) = self.registered_components_to_refresh.get(node_id)
{
self.components_to_refresh_once.insert(component.clone());
}
if let Err(e) = self.state.tree.set_style(*node_id, new_style) {
log::error!("failed to set style for taffy widget ID {node_id:?}: {e:?}");
}
}
}

View File

@@ -1,4 +1,5 @@
use glam::Vec2;
use taffy::{NodeId, TaffyTree};
use super::drawing::RenderPrimitive;
@@ -392,25 +393,37 @@ impl WidgetState {
let step_pixels = 64.0;
if info.handle_size.x < 1.0 && wheel.pos.x != 0.0 {
// Horizontal scrolling
let mult = (1.0 / (l.content_box_width() - info.content_size.x)) * step_pixels;
let new_scroll = (self.data.scrolling_target.x + wheel.delta.x * mult).clamp(0.0, 1.0);
if self.data.scrolling_target.x != new_scroll {
self.data.scrolling_target.x = new_scroll;
params.alterables.mark_tick(self.obj.get_id());
}
}
let mut handle_scroll =
|scrolling_target: &mut f32, wheel_delta: f32, handle_size: f32, content_length: f32, content_box_length: f32| {
if handle_size >= 1.0 || wheel_delta == 0.0 {
return;
}
if info.handle_size.y < 1.0 && wheel.pos.y != 0.0 {
// Vertical scrolling
let mult = (1.0 / (l.content_box_height() - info.content_size.y)) * step_pixels;
let new_scroll = (self.data.scrolling_target.y + wheel.delta.y * mult).clamp(0.0, 1.0);
if self.data.scrolling_target.y != new_scroll {
self.data.scrolling_target.y = new_scroll;
let mult = (1.0 / (content_box_length - content_length)) * step_pixels;
let new_scroll = (*scrolling_target + wheel_delta * mult).clamp(0.0, 1.0);
if *scrolling_target == new_scroll {
return;
}
*scrolling_target = new_scroll;
params.alterables.mark_tick(self.obj.get_id());
}
}
};
handle_scroll(
&mut self.data.scrolling_target.x,
wheel.delta.x,
info.handle_size.x,
info.content_size.x,
l.content_box_width(),
);
handle_scroll(
&mut self.data.scrolling_target.y,
wheel.delta.y,
info.handle_size.y,
info.content_size.y,
l.content_box_height(),
);
true
}
@@ -507,3 +520,20 @@ pub struct ConstructEssentials<'a> {
pub layout: &'a mut Layout,
pub parent: WidgetID,
}
/// Determines whether a given node is visible within the layout tree.
///
/// Traversal is definitely a little bit more expensive than just checking the value, but
/// Taffy doesn't calculate that for us, so here it is.
pub fn is_node_visible(tree: &TaffyTree<WidgetID>, node_id: NodeId) -> bool {
let mut cur = Some(node_id);
while let Some(node_id) = cur {
if let Ok(style) = tree.style(node_id)
&& style.display == taffy::Display::None
{
return false;
}
cur = tree.parent(node_id);
}
true
}