summaryrefslogtreecommitdiffstats
path: root/examples/tooltip/src/main.rs
blob: 6e2c4dd4181558c7f67137110ae248207551c0dc (plain) (blame)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
use iced::{
    button, tooltip::TooltipPosition, Button, Column, Container, Element,
    Length, Row, Sandbox, Settings, Text, Tooltip,
};

pub fn main() {
    Example::run(Settings::default()).unwrap()
}

#[derive(Default)]
struct Example {
    tooltip_top_button_state: button::State,
    tooltip_bottom_button_state: button::State,
    tooltip_right_button_state: button::State,
    tooltip_left_button_state: button::State,
    tooltip_cursor_button_state: button::State,
}

#[derive(Debug, Clone, Copy)]
struct Message;

impl Sandbox for Example {
    type Message = Message;

    fn new() -> Self {
        Self::default()
    }

    fn title(&self) -> String {
        String::from("Tooltip - Iced")
    }

    fn update(&mut self, _message: Message) {}

    fn view(&mut self) -> Element<Message> {
        let tooltip_top = tooltip_builder(
            "Tooltip at top",
            &mut self.tooltip_top_button_state,
            TooltipPosition::Top,
        );
        let tooltip_bottom = tooltip_builder(
            "Tooltip at bottom",
            &mut self.tooltip_bottom_button_state,
            TooltipPosition::Bottom,
        );
        let tooltip_right = tooltip_builder(
            "Tooltip at right",
            &mut self.tooltip_right_button_state,
            TooltipPosition::Right,
        );
        let tooltip_left = tooltip_builder(
            "Tooltip at left",
            &mut self.tooltip_left_button_state,
            TooltipPosition::Left,
        );

        let fixed_tooltips = Row::with_children(vec![
            tooltip_top.into(),
            tooltip_bottom.into(),
            tooltip_left.into(),
            tooltip_right.into(),
        ])
        .width(Length::Fill)
        .height(Length::Fill)
        .align_items(iced::Align::Center)
        .spacing(120);

        let cursor_tooltip_area = Tooltip::new(
            Button::new(
                &mut self.tooltip_cursor_button_state,
                Container::new(Text::new("Tooltip follows cursor").size(40))
                    .center_y()
                    .center_x()
                    .width(Length::Fill)
                    .height(Length::Fill),
            )
            .on_press(Message)
            .width(Length::Fill)
            .height(Length::Fill),
            tooltip(),
            TooltipPosition::FollowCursor,
        );

        let content = Column::with_children(vec![
            Container::new(fixed_tooltips)
                .width(Length::Fill)
                .height(Length::Fill)
                .center_x()
                .center_y()
                .into(),
            cursor_tooltip_area.into(),
        ])
        .width(Length::Fill)
        .height(Length::Fill);

        Container::new(content)
            .width(Length::Fill)
            .height(Length::Fill)
            .center_x()
            .center_y()
            .into()
    }
}

fn tooltip_builder<'a>(
    label: &str,
    button_state: &'a mut button::State,
    position: TooltipPosition,
) -> Container<'a, Message> {
    Container::new(Tooltip::new(
        Button::new(button_state, Text::new(label).size(40)).on_press(Message),
        tooltip(),
        position,
    ))
    .center_x()
    .center_y()
    .width(Length::Fill)
    .height(Length::Fill)
}

fn tooltip() -> Text {
    Text::new("Tooltip").size(20)
}