use std::{collections::HashMap, sync::Arc, time::Duration};
use parking_lot::Mutex;
use super::apply_column_box;
use crate::{
app::{
component::Component,
ctx::{CollisionStrategy, Ctx, Overlay, Placement, Timeout},
theme::{MarkdownBlockStyle, MarkdownInlineStyle},
},
components::{Column, Text},
core::{ElementRef, Signal},
layout::{Alignment, text_style::TextStyle},
node::{Element, HitTestBehavior},
};
const TOOLTIP_GAP: f32 = 4.0;
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub(super) struct TooltipKey {
destination: Arc<str>,
occurrence: usize,
}
impl TooltipKey {
pub(super) fn new(destination: Arc<str>, occurrence: usize) -> Self {
Self {
destination,
occurrence,
}
}
}
#[cfg(feature = "devtools")]
impl crate::app::component::DevtoolsInspectable for TooltipKey {}
#[derive(Clone)]
pub(super) struct TooltipHover {
shown: Signal<Option<TooltipKey>>,
pending: Arc<Mutex<Option<TooltipKey>>>,
timer: Timeout,
delay: Arc<Mutex<Duration>>,
anchors: Arc<Mutex<HashMap<TooltipKey, ElementRef>>>,
}
impl TooltipHover {
pub(super) fn new(create_timeout: impl FnOnce(Box<dyn Fn() + Send + Sync>) -> Timeout) -> Self {
let shown = Signal::new(None);
let pending = Arc::new(Mutex::new(None::<TooltipKey>));
let timer = {
let (shown, pending) = (shown.clone(), pending.clone());
create_timeout(Box::new(move || shown.set(pending.lock().clone())))
};
Self {
shown,
pending,
timer,
delay: Arc::new(Mutex::new(Duration::ZERO)),
anchors: Arc::new(Mutex::new(HashMap::new())),
}
}
pub(super) fn anchor(&self, key: &TooltipKey) -> ElementRef {
self.anchors.lock().entry(key.clone()).or_default().clone()
}
pub(super) fn keep_anchors(&self, tooltips: &[LinkTooltip]) {
self
.anchors
.lock()
.retain(|key, _| tooltips.iter().any(|tooltip| tooltip.key == *key));
}
pub(super) fn shown(&self) -> Signal<Option<TooltipKey>> {
self.shown.clone()
}
pub(super) fn set_delay(&self, delay: Duration) {
*self.delay.lock() = delay;
self.timer.set_duration(delay);
}
pub(super) fn enter(&self, key: &TooltipKey) {
if self.delay.lock().is_zero() {
self.shown.set(Some(key.clone()));
return;
}
*self.pending.lock() = Some(key.clone());
self.timer.restart();
}
pub(super) fn leave(&self, key: &TooltipKey) {
{
let mut pending = self.pending.lock();
if pending.as_ref() == Some(key) {
*pending = None;
self.timer.cancel();
}
}
if self.shown.get_untracked().as_ref() == Some(key) {
self.shown.set(None);
}
}
}
#[derive(Clone)]
pub(super) struct LinkTooltip {
key: TooltipKey,
anchor: ElementRef,
text: Arc<str>,
}
impl LinkTooltip {
pub(super) fn new(key: TooltipKey, anchor: ElementRef, text: Arc<str>) -> Self {
Self { key, anchor, text }
}
}
#[derive(Clone)]
pub(super) struct LinkTooltipsProps {
pub(super) tooltips: Arc<[LinkTooltip]>,
pub(super) shown: Signal<Option<TooltipKey>>,
pub(super) style: TextStyle,
pub(super) text: MarkdownInlineStyle,
pub(super) panel: MarkdownBlockStyle,
}
impl PartialEq for LinkTooltipsProps {
fn eq(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.tooltips, &other.tooltips)
&& self.style == other.style
&& self.text == other.text
&& self.panel == other.panel
}
}
#[cfg(feature = "devtools")]
impl crate::app::component::DevtoolsInspectable for LinkTooltipsProps {}
pub(super) struct LinkTooltips;
impl Component for LinkTooltips {
type Props = LinkTooltipsProps;
fn create(_ctx: &mut Ctx) -> Self {
Self
}
fn render(&self, ctx: &mut Ctx) -> impl Into<Element> {
let props = ctx.props::<Self::Props>().clone();
let hovered = props
.shown
.get()
.and_then(|key| props.tooltips.iter().find(|tooltip| tooltip.key == key));
let focused = props.tooltips.iter().find(|tooltip| tooltip.anchor.focus_visible());
let Some(tooltip) = hovered.or(focused) else {
return Element::from(Overlay::new(Element::from_node(crate::node::Node::new())).open_when(false));
};
let words = Text::styled(&tooltip.text, props.text.apply(&props.style));
let panel = apply_column_box(
Column::new().align_items(Alignment::Center).child(words),
&props.panel,
true,
);
Element::from(
Overlay::new(panel)
.anchor(tooltip.anchor.clone())
.placement(Placement::BottomStart)
.offset(0.0, TOOLTIP_GAP)
.collision(CollisionStrategy::FlipThenClamp)
.hit_test(HitTestBehavior::None),
)
}
}