use std::any::TypeId;
use std::sync::Arc;
use accesskit::{Node, Role};
use tracing::{Span, trace_span};
use crate::core::{
AccessCtx, AccessEvent, ChildrenIds, EventCtx, LayoutCtx, MeasureCtx, NewWidget, NoAction,
PaintCtx, PointerEvent, PropertiesMut, PropertiesRef, RegisterCtx, Subwindow, TextEvent,
Update, UpdateCtx, Widget, WidgetId, WidgetMut, WidgetPod,
};
use crate::imaging::Painter;
use crate::kurbo::{Axis, Size};
use crate::layout::{LayoutSize, LenReq, Length, SizeDef};
use crate::widgets::Label;
pub struct Tooltip {
child: WidgetPod<dyn Widget>,
}
impl Tooltip {
pub fn new(child: NewWidget<impl Widget + ?Sized>) -> Self {
Self {
child: child.with_class("#tooltip").erased().to_pod(),
}
}
pub fn with_text(text: impl Into<Arc<str>>) -> Self {
Self::new(Label::new(text).prepare())
}
}
impl Tooltip {
pub fn set_child(this: &mut WidgetMut<'_, Self>, child: NewWidget<impl Widget + ?Sized>) {
this.ctx.remove_child(std::mem::replace(
&mut this.widget.child,
child.erased().to_pod(),
));
}
pub fn child_mut<'t>(this: &'t mut WidgetMut<'_, Self>) -> WidgetMut<'t, dyn Widget> {
this.ctx.get_mut(&mut this.widget.child)
}
}
impl Widget for Tooltip {
type Action = NoAction;
fn on_pointer_event(
&mut self,
_ctx: &mut EventCtx<'_>,
_props: &mut PropertiesMut<'_>,
_event: &PointerEvent,
) {
}
fn on_text_event(
&mut self,
_ctx: &mut EventCtx<'_>,
_props: &mut PropertiesMut<'_>,
_event: &TextEvent,
) {
}
fn on_access_event(
&mut self,
_ctx: &mut EventCtx<'_>,
_props: &mut PropertiesMut<'_>,
_event: &AccessEvent,
) {
}
fn update(
&mut self,
_ctx: &mut UpdateCtx<'_>,
_props: &mut PropertiesMut<'_>,
_event: &Update,
) {
}
fn register_children(&mut self, ctx: &mut RegisterCtx<'_>) {
ctx.register_child(&mut self.child);
}
fn property_changed(&mut self, _ctx: &mut UpdateCtx<'_>, _property_type: TypeId) {}
fn measure(
&mut self,
ctx: &mut MeasureCtx<'_>,
_props: &PropertiesRef<'_>,
axis: Axis,
len_req: LenReq,
cross_length: Option<Length>,
) -> Length {
let auto_length = len_req.into();
let context_size = LayoutSize::maybe(axis.cross(), cross_length);
ctx.compute_length(
&mut self.child,
auto_length,
context_size,
axis,
cross_length,
)
}
fn layout(&mut self, ctx: &mut LayoutCtx<'_>, _props: &PropertiesRef<'_>, size: Size) {
let child_size = ctx.compute_size(&mut self.child, SizeDef::fit(size), size.into());
ctx.run_layout(&mut self.child, child_size);
let child_origin = ((size - child_size).to_vec2() * 0.5).to_point();
ctx.place_child(&mut self.child, child_origin);
ctx.derive_baselines(&self.child);
}
fn paint(
&mut self,
_ctx: &mut PaintCtx<'_>,
_props: &PropertiesRef<'_>,
_painter: &mut Painter<'_>,
) {
}
fn accessibility_role(&self) -> Role {
Role::Tooltip
}
fn accessibility(
&mut self,
_ctx: &mut AccessCtx<'_>,
_props: &PropertiesRef<'_>,
_node: &mut Node,
) {
}
fn children_ids(&self) -> ChildrenIds {
ChildrenIds::from_slice(&[self.child.id()])
}
fn as_subwindow(&mut self) -> Option<&mut dyn Subwindow> {
Some(self)
}
fn accepts_pointer_interaction(&self) -> bool {
false
}
fn make_trace_span(&self, id: WidgetId) -> Span {
trace_span!("Tooltip", id = id.trace())
}
}
impl Subwindow for Tooltip {
fn capture_pointer_event(
&mut self,
ctx: &mut EventCtx<'_>,
_props: &mut PropertiesMut<'_>,
event: &PointerEvent,
) {
let remove_tooltip = matches!(
event,
PointerEvent::Down(_)
| PointerEvent::Up(_)
| PointerEvent::Move(_)
| PointerEvent::Leave(_)
);
if remove_tooltip {
ctx.remove_subwindow(ctx.subwindow_id().unwrap());
}
}
}