aboutsummaryrefslogtreecommitdiff
path: root/examples/rich/src/main.rs
blob: 9ee2ea9a2cfe0c484d800d9cecccb2360fe1073b (plain)
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
use iced::widget::{column, container, responsive};
use iced::{Center, Element, Fill, Font, color, font, padding};
use iced_selection::{rich_text, span};

fn main() -> iced::Result {
    iced::run(State::update, State::view)
}

#[derive(Default)]
struct State {
    clicked: Option<String>,
    hovered: Option<String>,
}

#[derive(Debug, Clone)]
enum Message {
    LinkClicked(String),
    LinkHovered(String),
    HoverLost,
}

impl State {
    fn update(&mut self, message: Message) {
        match message {
            Message::LinkClicked(link) => {
                let _ = open::that_in_background(&link);
                self.clicked = Some(link);
            }
            Message::LinkHovered(link) => {
                self.hovered = Some(link);
            }
            Message::HoverLost => self.hovered = None,
        };
    }

    fn view(&self) -> Element<'_, Message> {
        responsive(|size| {
            container(
                column![
                    rich_text![
                        span("iced")
                            .color(color!(0x2b79a2))
                            .link("https://iced.rs"),
                        " is a cross-platform GUI library for ",
                        span("Rust")
                            .color(color!(0x2b79a2))
                            .link("https://rust-lang.org"),
                        ".\nIt is inspired by ",
                        span("Elm")
                            .color(color!(0x2b79a2))
                            .link("https://elm-lang.org"),
                        ", a delightful functional language for building web applications.\n\n",
                        "As a GUI library, iced helps you build ",
                        span("graphical user interfaces")
                            .color(color!(0x2b79a2))
                            .link("https://en.wikipedia.org/wiki/Graphical_user_interface")
                            .font(Font {
                                style:font::Style::Italic,
                                ..Font::DEFAULT
                            }),
                        " for your Rust applications."
                    ]
                    .align_x(Center)
                    .on_link_click(Message::LinkClicked)
                    .on_link_hover(Message::LinkHovered)
                    .on_hover_lost(Message::HoverLost),
                    self.clicked.as_deref().map(|link| rich_text![
                        "Last clicked link: ",
                        span(link).color(color!(0x2b79a2)).link(link)
                    ]
                    .on_link_click(Message::LinkClicked)
                    .on_link_hover(Message::LinkHovered)
                    .on_hover_lost(Message::HoverLost)),
                    self.hovered.as_deref().map(|link| rich_text![
                        "Currently hovered link: ",
                        span(link).color(color!(0x2b79a2)).link(link)
                    ]
                    .on_link_click(Message::LinkClicked)),
                ]
                .spacing(10)
                .align_x(Center)
                .max_width(size.width * 0.8),
            )
            .padding(padding::top(size.height * 0.4))
            .center_x(Fill)
            .into()
        })
        .into()
    }
}