summaryrefslogtreecommitdiffstats
path: root/examples/system_information/src/main.rs
blob: 95f57ac02c451732fd421b358cd855e5ec26a5f0 (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
124
125
use iced::{
    executor, system, Application, Column, Command, Container, Element, Length,
    Settings, Text,
};

use bytesize::ByteSize;

pub fn main() -> iced::Result {
    Example::run(Settings::default())
}

enum Example {
    Loading,
    Loaded { information: system::Information },
    Unsupported,
}

#[derive(Debug)]
enum Message {
    InformationReceived(Option<system::Information>),
}

impl Application for Example {
    type Message = Message;
    type Executor = executor::Default;
    type Flags = ();

    fn new(_flags: ()) -> (Self, Command<Message>) {
        (
            Self::Loading,
            system::information(Message::InformationReceived),
        )
    }

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

    fn update(&mut self, message: Message) -> Command<Message> {
        match message {
            Message::InformationReceived(information) => {
                if let Some(information) = information {
                    *self = Self::Loaded { information };
                } else {
                    *self = Self::Unsupported;
                }
            }
        }

        Command::none()
    }

    fn view(&mut self) -> Element<Message> {
        let content: Element<Message> = match self {
            Example::Loading => Text::new("Loading...").size(40).into(),
            Example::Loaded { information } => {
                let system_name = Text::new(format!(
                    "System name: {}",
                    information
                        .system_name
                        .as_ref()
                        .unwrap_or(&"unknown".to_string())
                ));

                let system_kernel = Text::new(format!(
                    "System kernel: {}",
                    information
                        .system_kernel
                        .as_ref()
                        .unwrap_or(&"unknown".to_string())
                ));

                let system_version = Text::new(format!(
                    "System version: {}",
                    information
                        .system_version
                        .as_ref()
                        .unwrap_or(&"unknown".to_string())
                ));

                let cpu_brand = Text::new(format!(
                    "Processor brand: {}",
                    information.cpu_brand
                ));

                let cpu_cores = Text::new(format!(
                    "Processor cores: {}",
                    information
                        .cpu_cores
                        .map_or("unknown".to_string(), |cores| cores
                            .to_string())
                ));

                let memory_readable =
                    ByteSize::kb(information.memory_total).to_string();

                let memory_total = Text::new(format!(
                    "Memory (total): {}",
                    format!(
                        "{} kb ({})",
                        information.memory_total, memory_readable
                    )
                ));

                Column::with_children(vec![
                    system_name.into(),
                    system_kernel.into(),
                    system_version.into(),
                    cpu_brand.into(),
                    cpu_cores.into(),
                    memory_total.into(),
                ])
                .into()
            }
            Example::Unsupported => Text::new("Unsupported!").size(20).into(),
        };

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