ailloli_ui_widgets/overlay/tooltip.rs
1//! Immediate-mode tooltip bubble drawing for an overlay layer.
2
3use ailloli_ui_core::{Color, FontId, Rect, TextStyle};
4use ailloli_ui_runtime::{DrawCmd, DrawRRect, DrawText};
5use ailloli_ui_text::{TextLayoutParams, TextSystem, WrapMode};
6
7#[derive(Debug, Clone, Copy, PartialEq)]
8/// Tooltip palette, padding, typography, and wrap bound.
9///
10/// All floating dimensions are logical pixels. Values pass through without
11/// clamping; callers should provide finite non-negative padding, radius, and
12/// maximum width. `font_px` is an integer logical-pixel size.
13///
14/// # Examples
15///
16/// ```
17/// use ailloli_ui_widgets::overlay::TooltipStyle;
18/// let style = TooltipStyle::default();
19/// assert_eq!(style.font_px, 12);
20/// assert_eq!(style.max_width, 280.0);
21/// ```
22pub struct TooltipStyle {
23 /// Bubble fill.
24 pub bg: Color,
25 /// Glyph fill.
26 pub fg: Color,
27 /// Bubble corner radius in logical pixels.
28 pub radius: f32,
29 /// Horizontal text inset in logical pixels.
30 pub pad_x: f32,
31 /// Vertical text inset in logical pixels.
32 pub pad_y: f32,
33 /// Font size in integer logical pixels.
34 pub font_px: u16,
35 /// Maximum shaped line width in logical pixels.
36 pub max_width: f32,
37}
38
39/// Supplies the dark tooltip defaults.
40impl Default for TooltipStyle {
41 fn default() -> Self {
42 Self {
43 bg: Color::rgba(17, 24, 40, 0.95),
44 fg: Color::rgba(243, 246, 251, 1.0),
45 radius: 6.0,
46 pad_x: 10.0,
47 pad_y: 6.0,
48 font_px: 12,
49 max_width: 280.0,
50 }
51 }
52}
53
54/// Tooltip bubble (place in an **overlay** layer for correct z-order).
55///
56/// `card` is caller-resolved geometry; the helper does not measure or resize it.
57/// It returns exactly two commands: background, then word-wrapped text. Text y
58/// is baseline-positioned at `card.y + pad_y + font_px`.
59///
60/// # Examples
61///
62/// ```
63/// use ailloli_ui_core::Rect;
64/// use ailloli_ui_text::TextSystem;
65/// use ailloli_ui_widgets::overlay::{draw_tooltip, TooltipStyle};
66/// let mut text_system = TextSystem::new();
67/// let commands = draw_tooltip(Rect::new(0.0, 0.0, 120.0, 32.0), "Details", TooltipStyle::default(), &mut text_system);
68/// assert_eq!(commands.len(), 2);
69/// ```
70pub fn draw_tooltip(
71 card: Rect,
72 text: &str,
73 style: TooltipStyle,
74 text_system: &mut TextSystem,
75) -> Vec<DrawCmd> {
76 vec![
77 DrawCmd::RRect(DrawRRect {
78 rect: card,
79 radius: style.radius,
80 color: style.bg,
81 }),
82 DrawCmd::Text(DrawText {
83 pos: [
84 card.x + style.pad_x,
85 card.y + style.pad_y + style.font_px as f32,
86 ],
87 color: style.fg,
88 decoration: ailloli_ui_core::TextDecoration::None,
89 layout: text_system.layout_cached(TextLayoutParams {
90 text,
91 style: TextStyle::new(FontId::Ui, style.font_px, style.fg),
92 max_width: Some(style.max_width),
93 wrap_mode: WrapMode::Word,
94 }),
95 }),
96 ]
97}