summaryrefslogtreecommitdiffstats
path: root/examples/panes/src/main.rs
blob: c1bf991aa7c05e2b4e6e4caf5acda226fdb6591f (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
use iced::{
    panes, Application, Command, Element, Panes, Settings, Subscription,
};

use clock::{self, Clock};
use stopwatch::{self, Stopwatch};

pub fn main() {
    Launcher::run(Settings {
        antialiasing: true,
        ..Settings::default()
    })
}

struct Launcher {
    panes: panes::State<Example>,
}

#[derive(Debug)]
enum Example {
    Clock(Clock),
    Stopwatch(Stopwatch),
}

#[derive(Debug, Clone)]
enum Message {
    Clock(panes::Pane, clock::Message),
    Stopwatch(panes::Pane, stopwatch::Message),
}

impl Application for Launcher {
    type Executor = iced::executor::Default;
    type Message = Message;

    fn new() -> (Self, Command<Message>) {
        let (clock, _) = Clock::new();
        let (panes, _) = panes::State::new(Example::Clock(clock));

        dbg!(&panes);

        (Self { panes }, Command::none())
    }

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

    fn update(&mut self, message: Message) -> Command<Message> {
        match message {
            Message::Clock(pane, message) => {
                if let Some(Example::Clock(clock)) = self.panes.get_mut(&pane) {
                    let _ = clock.update(message);
                }
            }
            Message::Stopwatch(pane, message) => {
                if let Some(Example::Stopwatch(stopwatch)) =
                    self.panes.get_mut(&pane)
                {
                    let _ = stopwatch.update(message);
                }
            }
        }

        Command::none()
    }

    fn subscription(&self) -> Subscription<Message> {
        Subscription::batch(self.panes.iter().map(|(pane, example)| {
            match example {
                Example::Clock(clock) => clock
                    .subscription()
                    .map(move |message| Message::Clock(pane, message)),

                Example::Stopwatch(stopwatch) => stopwatch
                    .subscription()
                    .map(move |message| Message::Stopwatch(pane, message)),
            }
        }))
    }

    fn view(&mut self) -> Element<Message> {
        let Self { panes } = self;

        Panes::new(panes, |pane, example| match example {
            Example::Clock(clock) => clock
                .view()
                .map(move |message| Message::Clock(pane, message)),

            Example::Stopwatch(stopwatch) => stopwatch
                .view()
                .map(move |message| Message::Stopwatch(pane, message)),
        })
        .into()
    }
}