diff options
author | 2022-03-10 03:02:17 -0300 | |
---|---|---|
committer | 2022-04-26 18:57:00 -0300 | |
commit | 53538b65b1c557015c2900fc28b8916cf719a10b (patch) | |
tree | d0e9b72d60275d3bd3654c3f4dbbc870717d5790 /examples/system_information | |
parent | c8ed318e17c8a7673e4260e5d4e12c44c9faa987 (diff) | |
download | iced-53538b65b1c557015c2900fc28b8916cf719a10b.tar.gz iced-53538b65b1c557015c2900fc28b8916cf719a10b.tar.bz2 iced-53538b65b1c557015c2900fc28b8916cf719a10b.zip |
Add `system_information` example
Diffstat (limited to 'examples/system_information')
-rw-r--r-- | examples/system_information/Cargo.toml | 10 | ||||
-rw-r--r-- | examples/system_information/src/main.rs | 119 |
2 files changed, 129 insertions, 0 deletions
diff --git a/examples/system_information/Cargo.toml b/examples/system_information/Cargo.toml new file mode 100644 index 00000000..d3d76182 --- /dev/null +++ b/examples/system_information/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "system_information" +version = "0.1.0" +authors = ["Richard <richardsoncusto@gmail.com>"] +edition = "2021" +publish = false + +[dependencies] +iced = { path = "../..", features = ["sysinfo"] } +bytesize = { version = "1.1.0" }
\ No newline at end of file diff --git a/examples/system_information/src/main.rs b/examples/system_information/src/main.rs new file mode 100644 index 00000000..f8ae1aaf --- /dev/null +++ b/examples/system_information/src/main.rs @@ -0,0 +1,119 @@ +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_total = Text::new(format!( + "Memory (total): {}", + ByteSize::kb(information.memory_total).to_string() + )); + + 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() + } +} |