1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//! `HoverCard` — a richer [`Tooltip`](crate::tooltip::Tooltip) that shows
//! arbitrary content on hover.
//!
//! ```ignore
//! let r = ui.add(sc::Link::new("@ada"));
//! sc::HoverCard::new().max_width(260.0).show(&r, |ui| {
//! ui.horizontal(|ui| {
//! ui.add(sc::Avatar::from_name("Ada Lovelace").small());
//! ui.add(sc::Label::new("Ada Lovelace").strong());
//! });
//! ui.add(sc::Label::new("First programmer.").muted());
//! });
//! ```
use egui::{Response, Ui};
pub struct HoverCard {
max_width: f32,
at_pointer: bool,
}
impl Default for HoverCard {
fn default() -> Self {
Self::new()
}
}
impl HoverCard {
pub fn new() -> Self {
Self {
max_width: 300.0,
at_pointer: false,
}
}
pub fn max_width(mut self, w: f32) -> Self {
self.max_width = w;
self
}
/// Anchor to the pointer instead of the widget.
pub fn at_pointer(mut self) -> Self {
self.at_pointer = true;
self
}
/// Show `content` while `response` is hovered. Returns the (possibly
/// updated) [`Response`].
pub fn show(self, response: &Response, content: impl FnOnce(&mut Ui)) -> Response {
let max_width = self.max_width;
let add = move |ui: &mut Ui| {
ui.set_max_width(max_width);
content(ui);
};
if self.at_pointer {
response.clone().on_hover_ui_at_pointer(add)
} else {
response.clone().on_hover_ui(add)
}
}
}