summaryrefslogtreecommitdiffstats
path: root/examples/svg/src/main.rs
blob: 14d8f16448ff777b51a0d5f0e49d8c9e49414580 (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
use iced::widget::{center, center_x, checkbox, column, svg};
use iced::{Element, Fill, color};

pub fn main() -> iced::Result {
    iced::run("SVG - Iced", Tiger::update, Tiger::view)
}

#[derive(Debug, Default)]
struct Tiger {
    apply_color_filter: bool,
}

#[derive(Debug, Clone, Copy)]
pub enum Message {
    ToggleColorFilter(bool),
}

impl Tiger {
    fn update(&mut self, message: Message) {
        match message {
            Message::ToggleColorFilter(apply_color_filter) => {
                self.apply_color_filter = apply_color_filter;
            }
        }
    }

    fn view(&self) -> Element<Message> {
        let handle = svg::Handle::from_path(format!(
            "{}/resources/tiger.svg",
            env!("CARGO_MANIFEST_DIR")
        ));

        let svg =
            svg(handle)
                .width(Fill)
                .height(Fill)
                .style(|_theme, _status| svg::Style {
                    color: if self.apply_color_filter {
                        Some(color!(0x0000ff))
                    } else {
                        None
                    },
                });

        let apply_color_filter =
            checkbox("Apply a color filter", self.apply_color_filter)
                .on_toggle(Message::ToggleColorFilter);

        center(column![svg, center_x(apply_color_filter)].spacing(20))
            .padding(20)
            .into()
    }
}