Skip to content

Commit

Permalink
tooltip improvements (#499)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmoulton committed Jun 26, 2024
1 parent 80f3a3a commit 02205d8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/views/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,11 @@ impl Label {

let padding_left = match style.padding_left() {
PxPct::Px(padding) => padding as f32,
PxPct::Pct(pct) => pct as f32 * layout.size.width,
PxPct::Pct(pct) => (pct / 100.) as f32 * layout.size.width,
};
let padding_top = match style.padding_top() {
PxPct::Px(padding) => padding as f32,
PxPct::Pct(pct) => pct as f32 * layout.size.width,
PxPct::Pct(pct) => (pct / 100.) as f32 * layout.size.width,
};
self.text_layout.as_ref().unwrap().hit(
point.x as f32 - padding_left,
Expand Down Expand Up @@ -436,11 +436,11 @@ impl View for Label {
let style = view_state.combined_style.builtin();
let padding_left = match style.padding_left() {
PxPct::Px(padding) => padding as f32,
PxPct::Pct(pct) => pct as f32 * layout.size.width,
PxPct::Pct(pct) => (pct / 100.) as f32 * layout.size.width,
};
let padding_right = match style.padding_right() {
PxPct::Px(padding) => padding as f32,
PxPct::Pct(pct) => pct as f32 * layout.size.width,
PxPct::Pct(pct) => (pct / 100.) as f32 * layout.size.width,
};
let text_overflow = style.text_overflow();
(text_overflow, padding_left + padding_right)
Expand Down
42 changes: 37 additions & 5 deletions src/views/tooltip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use peniko::kurbo::Point;
use std::cell::RefCell;
use std::{rc::Rc, time::Duration};

use crate::style::{Style, StyleClass as _};
use crate::views::Decorators;
use crate::{
action::{add_overlay, exec_after, remove_overlay, TimerToken},
Expand All @@ -12,9 +13,8 @@ use crate::{
view::{default_compute_layout, IntoView, View},
};

use super::container;

style_class!(pub TooltipClass);
style_class!(pub TooltipContainerClass);

prop!(pub Delay: f64 {} = 0.6);

Expand All @@ -31,6 +31,8 @@ pub struct Tooltip {
overlay: Rc<RefCell<Option<ViewId>>>,
tip: Rc<dyn Fn() -> Box<dyn View>>,
style: TooltipStyle,
tip_style: Style,
scale: f64,
window_origin: Option<Point>,
}

Expand All @@ -45,12 +47,15 @@ pub fn tooltip<V: IntoView + 'static, T: IntoView + 'static>(
let overlay = Rc::new(RefCell::new(None));
Tooltip {
id,
tip: Rc::new(move || container(tip()).class(TooltipClass).into_any()),
tip: Rc::new(move || tip().into_any()),
hover: None,
overlay: overlay.clone(),
style: Default::default(),
tip_style: Default::default(),
scale: 1.0,
window_origin: None,
}
.class(TooltipContainerClass)
.on_cleanup(move || {
if let Some(overlay_id) = overlay.borrow_mut().take() {
remove_overlay(overlay_id);
Expand All @@ -68,16 +73,33 @@ impl View for Tooltip {
if let Some(window_origin) = self.window_origin {
if self.hover.map(|(_, t)| t) == Some(*token) {
let tip = self.tip.clone();

let tip_style = self.tip_style.clone();
let overlay_id = add_overlay(
window_origin + self.hover.unwrap().0.to_vec2() + (10., 10.),
move |_| tip(),
window_origin
+ self.hover.unwrap().0.to_vec2()
+ (10. / self.scale, 10. / self.scale),
move |_| tip().style(move |_| tip_style.clone()),
);
// overlay_id.request_all();
*self.overlay.borrow_mut() = Some(overlay_id);
}
}
}
}

fn style_pass(&mut self, cx: &mut crate::context::StyleCx<'_>) {
self.style.read(cx);
self.scale = cx.app_state.scale;

self.tip_style =
Style::new().apply_classes_from_context(&[TooltipClass::class_ref()], &cx.current);

for child in self.id.children() {
cx.style_view(child);
}
}

fn event_before_children(&mut self, cx: &mut EventCx, event: &Event) -> EventPropagation {
match &event {
Event::PointerMove(e) => {
Expand Down Expand Up @@ -114,3 +136,13 @@ impl View for Tooltip {
default_compute_layout(self.id, cx)
}
}

pub trait TooltipTrait {
fn tooltip<V: IntoView + 'static>(self, tip: impl Fn() -> V + 'static) -> Tooltip;
}

impl<T: View + 'static> TooltipTrait for T {
fn tooltip<V: IntoView + 'static>(self, tip: impl Fn() -> V + 'static) -> Tooltip {
tooltip(self, tip)
}
}

0 comments on commit 02205d8

Please sign in to comment.