use std::{f32::consts::SQRT_2, sync::Arc, time::Duration};
use parking_lot::Mutex;
use super::{
TooltipSide,
part_style::{Length, TransitionDuration},
recipe::{Name, Recipe, ResolvedPart},
};
use crate::{
app::{
ctx::{Ctx, Placement},
theme::Theme,
},
components::{Column, Row, Stack},
core::{ElementRect, ElementRef, Signal},
layout::{
Alignment, StackAlignment,
layout_kind::{Justify, LayoutKind},
},
node::{Element, SpacingValue, border::Border, dimension::Dimension, transform::Transform2D},
};
pub(crate) fn length(duration: TransitionDuration, theme: &Theme) -> Duration {
match duration {
TransitionDuration::Role(role) => theme.durations().try_get(role).unwrap_or_default(),
TransitionDuration::Set(duration) => duration,
}
}
pub(crate) fn with_offset(rules: Recipe, root: impl Into<Name>, side: TooltipSide, offset: SpacingValue) -> Recipe {
rules.base(root, |s| match side {
TooltipSide::Top => s.padding_bottom(offset),
TooltipSide::Bottom => s.padding_top(offset),
TooltipSide::Left => s.padding_end(offset),
TooltipSide::Right => s.padding_start(offset),
})
}
pub(crate) fn side_of(placement: Placement) -> TooltipSide {
match placement {
Placement::TopStart | Placement::Top | Placement::TopEnd => TooltipSide::Top,
Placement::BottomStart | Placement::Bottom | Placement::BottomEnd => TooltipSide::Bottom,
Placement::LeftStart | Placement::Left | Placement::LeftEnd => TooltipSide::Left,
Placement::RightStart | Placement::Right | Placement::RightEnd => TooltipSide::Right,
}
}
fn vertical(side: TooltipSide) -> bool {
matches!(side, TooltipSide::Top | TooltipSide::Bottom)
}
fn opposite(side: TooltipSide) -> TooltipSide {
match side {
TooltipSide::Top => TooltipSide::Bottom,
TooltipSide::Bottom => TooltipSide::Top,
TooltipSide::Left => TooltipSide::Right,
TooltipSide::Right => TooltipSide::Left,
}
}
pub(crate) fn put<T: crate::core::SignalValue + PartialEq + Clone + Send + Sync + 'static>(
signal: &Signal<T>,
value: T,
) {
if signal.get_untracked() != value {
signal.set(value);
}
}
pub(crate) fn holds_children(element: Element) -> Element {
match element.node.layout_kind() {
LayoutKind::Row { .. } | LayoutKind::Column { .. } | LayoutKind::Stack { .. } | LayoutKind::Grid(_) => element,
_ => Row::new().child(element).into(),
}
}
#[derive(Clone)]
pub(crate) struct Placed {
flipped: Signal<bool>,
aim: Signal<f32>,
placing: Arc<Mutex<Placing>>,
}
struct Placing {
side: TooltipSide,
anchor: ElementRef,
along: f32,
}
impl Placed {
pub(crate) fn new(ctx: &mut Ctx, shown: &ElementRef) -> Self {
let placed = Self {
flipped: ctx.signal(false),
aim: ctx.signal(0.0_f32),
placing: Arc::new(Mutex::new(Placing {
side: TooltipSide::Bottom,
anchor: ElementRef::new(),
along: 0.0,
})),
};
let reader = placed.clone();
shown.observe_rect(move |rect| reader.read(rect));
placed
}
pub(crate) fn side(&self, placement: Placement) -> TooltipSide {
let side = side_of(placement);
if self.flipped.get() { opposite(side) } else { side }
}
pub(crate) fn aim(&self) -> f32 {
self.aim.get()
}
pub(crate) fn note(&self, placement: Placement, anchor: &ElementRef, along: f32) {
let mut placing = self.placing.lock();
placing.side = side_of(placement);
placing.anchor = anchor.clone();
placing.along = along;
}
fn read(&self, rect: ElementRect) {
let (side, anchor, along) = {
let placing = self.placing.lock();
(placing.side, placing.anchor.clone(), placing.along)
};
let ((x, y), (anchor_x, anchor_y)) = (rect.center(), anchor.bounds().center());
let other = match side {
TooltipSide::Top => y > anchor_y,
TooltipSide::Bottom => y < anchor_y,
TooltipSide::Left => x > anchor_x,
TooltipSide::Right => x < anchor_x,
};
put(&self.flipped, other);
let (off, length) = if vertical(side) {
(anchor_x - x, rect.width)
} else {
(anchor_y - y, rect.height)
};
let limit = (length / 2.0 - along).max(0.0);
let off = (off.clamp(-limit, limit) * 2.0).round() / 2.0;
if (self.aim.get_untracked() - off).abs() > 0.25 {
self.aim.set(off);
}
}
}
pub(crate) fn arrow_size(arrow: &ResolvedPart, side: TooltipSide) -> Option<(f32, f32)> {
let theme = arrow.theme();
let px = |length: Option<Length>| match length?.resolve(theme)? {
Dimension::Px(px) => Some(px),
_ => None,
};
let rest = arrow.rest();
let (width, height) = (px(rest.width)?, px(rest.height)?);
Some(if vertical(side) {
(width, height)
} else {
(height, width)
})
}
pub(crate) fn arrow(arrow: &ResolvedPart, panel: &ResolvedPart, side: TooltipSide, aim: f32) -> Option<Element> {
let (along, away) = arrow_size(arrow, side)?;
let rest = panel.rest();
let fill = rest.background?;
let edge = rest.border.map(|border| {
let width = border.width.resolve(&panel.theme().border_sizes());
(width, Border::inside(width, border.color))
});
let over = edge.as_ref().map_or(0.0, |(width, _)| *width);
let side_length = along / SQRT_2;
let (shift, over) = match side {
TooltipSide::Top | TooltipSide::Left => (-away / 2.0, -over),
TooltipSide::Bottom | TooltipSide::Right => (away / 2.0, over),
};
let (dx, dy) = if vertical(side) { (0.0, shift) } else { (shift, 0.0) };
let mut tip = Row::new()
.width(side_length)
.height(side_length)
.flex_shrink(0.0)
.background(fill)
.offset(dx, dy)
.transform(Transform2D::rotate_deg(45.0));
if let Some((_, border)) = edge {
tip = tip.border(border);
}
Some(if vertical(side) {
Row::new()
.justify(Justify::Center)
.align_items(Alignment::Center)
.flex_shrink(0.0)
.part(arrow)
.offset(aim, -over)
.child(tip)
.into()
} else {
Column::new()
.justify(Justify::Center)
.align_items(Alignment::Center)
.flex_shrink(0.0)
.part(arrow)
.offset(-over, aim)
.child(tip)
.into()
})
}
pub(crate) fn frame(
root: &ResolvedPart,
panel: Element,
arrow: Option<Element>,
away: f32,
side: TooltipSide,
) -> Stack {
let (align, padded) = match side {
TooltipSide::Top => (StackAlignment::BottomCenter, Column::new().padding_bottom(away)),
TooltipSide::Bottom => (StackAlignment::TopCenter, Column::new().padding_top(away)),
TooltipSide::Left => (StackAlignment::CenterEnd, Column::new().padding_right(away)),
TooltipSide::Right => (StackAlignment::CenterStart, Column::new().padding_left(away)),
};
let away_room = if arrow.is_some() { padded } else { Column::new() };
let mut stack = Stack::new().stack_align(align).part(root).overflow_visible().child(
away_room
.align_items(Alignment::Stretch)
.overflow_visible()
.child(panel),
);
if let Some(arrow) = arrow {
stack = stack.child(arrow);
}
stack
}