From 53538b65b1c557015c2900fc28b8916cf719a10b Mon Sep 17 00:00:00 2001 From: Richard Date: Thu, 10 Mar 2022 03:02:17 -0300 Subject: Add `system_information` example --- examples/system_information/src/main.rs | 119 ++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 examples/system_information/src/main.rs (limited to 'examples/system_information/src/main.rs') 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), +} + +impl Application for Example { + type Message = Message; + type Executor = executor::Default; + type Flags = (); + + fn new(_flags: ()) -> (Self, Command) { + ( + Self::Loading, + system::information(Message::InformationReceived), + ) + } + + fn title(&self) -> String { + String::from("System Information - Iced") + } + + fn update(&mut self, message: Message) -> Command { + 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 { + let content: Element = 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() + } +} -- cgit From d9853165e78bffc1d2d4b1fc4ef71fe91b1bd38d Mon Sep 17 00:00:00 2001 From: Richard Date: Wed, 16 Mar 2022 19:42:40 -0300 Subject: Add unformated memory total to example --- examples/system_information/src/main.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'examples/system_information/src/main.rs') diff --git a/examples/system_information/src/main.rs b/examples/system_information/src/main.rs index f8ae1aaf..95f57ac0 100644 --- a/examples/system_information/src/main.rs +++ b/examples/system_information/src/main.rs @@ -91,9 +91,15 @@ impl Application for Example { .to_string()) )); + let memory_readable = + ByteSize::kb(information.memory_total).to_string(); + let memory_total = Text::new(format!( "Memory (total): {}", - ByteSize::kb(information.memory_total).to_string() + format!( + "{} kb ({})", + information.memory_total, memory_readable + ) )); Column::with_children(vec![ -- cgit From 56065fe95968459c9b507715e87971c4c13ce2d5 Mon Sep 17 00:00:00 2001 From: Richard Date: Thu, 17 Mar 2022 01:02:31 -0300 Subject: Add graphics information to example --- examples/system_information/src/main.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'examples/system_information/src/main.rs') diff --git a/examples/system_information/src/main.rs b/examples/system_information/src/main.rs index 95f57ac0..eac76212 100644 --- a/examples/system_information/src/main.rs +++ b/examples/system_information/src/main.rs @@ -102,6 +102,16 @@ impl Application for Example { ) )); + let graphics_adapter = Text::new(format!( + "Graphics adapter: {}", + information.graphics_adapter + )); + + let graphics_backend = Text::new(format!( + "Graphics backend: {}", + information.graphics_backend + )); + Column::with_children(vec![ system_name.into(), system_kernel.into(), @@ -109,6 +119,8 @@ impl Application for Example { cpu_brand.into(), cpu_cores.into(), memory_total.into(), + graphics_adapter.into(), + graphics_backend.into(), ]) .into() } -- cgit From 5bfe887e3d4acb2c48cca22c73aa52f4cc4856ad Mon Sep 17 00:00:00 2001 From: Richard Date: Thu, 17 Mar 2022 02:47:50 -0300 Subject: Improve example readability --- examples/system_information/src/main.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'examples/system_information/src/main.rs') diff --git a/examples/system_information/src/main.rs b/examples/system_information/src/main.rs index eac76212..e0e7db09 100644 --- a/examples/system_information/src/main.rs +++ b/examples/system_information/src/main.rs @@ -113,15 +113,16 @@ impl Application for Example { )); Column::with_children(vec![ - system_name.into(), - system_kernel.into(), - system_version.into(), - cpu_brand.into(), - cpu_cores.into(), - memory_total.into(), - graphics_adapter.into(), - graphics_backend.into(), + system_name.size(30).into(), + system_kernel.size(30).into(), + system_version.size(30).into(), + cpu_brand.size(30).into(), + cpu_cores.size(30).into(), + memory_total.size(30).into(), + graphics_adapter.size(30).into(), + graphics_backend.size(30).into(), ]) + .spacing(10) .into() } Example::Unsupported => Text::new("Unsupported!").size(20).into(), -- cgit From c9ea1f11dec96df04ade463ea9f33062a85c9219 Mon Sep 17 00:00:00 2001 From: Richard Date: Thu, 14 Apr 2022 02:11:43 -0300 Subject: Add memory usage to `Information` struct --- examples/system_information/src/main.rs | 43 ++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 6 deletions(-) (limited to 'examples/system_information/src/main.rs') diff --git a/examples/system_information/src/main.rs b/examples/system_information/src/main.rs index e0e7db09..79033e4c 100644 --- a/examples/system_information/src/main.rs +++ b/examples/system_information/src/main.rs @@ -1,6 +1,6 @@ use iced::{ - executor, system, Application, Column, Command, Container, Element, Length, - Settings, Text, + button, executor, system, Application, Button, Column, Command, Container, + Element, Length, Settings, Text, }; use bytesize::ByteSize; @@ -11,13 +11,17 @@ pub fn main() -> iced::Result { enum Example { Loading, - Loaded { information: system::Information }, + Loaded { + information: system::Information, + refresh_button: button::State, + }, Unsupported, } -#[derive(Debug)] +#[derive(Clone, Debug)] enum Message { InformationReceived(Option), + Refresh, } impl Application for Example { @@ -38,9 +42,16 @@ impl Application for Example { fn update(&mut self, message: Message) -> Command { match message { + Message::Refresh => { + return system::information(Message::InformationReceived); + } Message::InformationReceived(information) => { if let Some(information) = information { - *self = Self::Loaded { information }; + let refresh_button = button::State::new(); + *self = Self::Loaded { + information, + refresh_button, + }; } else { *self = Self::Unsupported; } @@ -53,7 +64,10 @@ impl Application for Example { fn view(&mut self) -> Element { let content: Element = match self { Example::Loading => Text::new("Loading...").size(40).into(), - Example::Loaded { information } => { + Example::Loaded { + information, + refresh_button, + } => { let system_name = Text::new(format!( "System name: {}", information @@ -102,6 +116,19 @@ impl Application for Example { ) )); + let memory_text = if let Some(memory_used) = + information.memory_used + { + let memory_readable = ByteSize::kb(memory_used).to_string(); + + format!("{} kb ({})", memory_used, memory_readable) + } else { + String::from("None") + }; + + let memory_used = + Text::new(format!("Memory (used): {}", memory_text)); + let graphics_adapter = Text::new(format!( "Graphics adapter: {}", information.graphics_adapter @@ -119,8 +146,12 @@ impl Application for Example { cpu_brand.size(30).into(), cpu_cores.size(30).into(), memory_total.size(30).into(), + memory_used.size(30).into(), graphics_adapter.size(30).into(), graphics_backend.size(30).into(), + Button::new(refresh_button, Text::new("Refresh")) + .on_press(Message::Refresh) + .into(), ]) .spacing(10) .into() -- cgit From 8643fbef90fc16371e27ae0142eb7e4c6b432d29 Mon Sep 17 00:00:00 2001 From: Richard Date: Tue, 26 Apr 2022 19:35:43 -0300 Subject: Rename `system::information` to `fetch_information` --- examples/system_information/src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'examples/system_information/src/main.rs') diff --git a/examples/system_information/src/main.rs b/examples/system_information/src/main.rs index 79033e4c..1e61480f 100644 --- a/examples/system_information/src/main.rs +++ b/examples/system_information/src/main.rs @@ -32,7 +32,7 @@ impl Application for Example { fn new(_flags: ()) -> (Self, Command) { ( Self::Loading, - system::information(Message::InformationReceived), + system::fetch_information(Message::InformationReceived), ) } @@ -43,7 +43,7 @@ impl Application for Example { fn update(&mut self, message: Message) -> Command { match message { Message::Refresh => { - return system::information(Message::InformationReceived); + return system::fetch_information(Message::InformationReceived); } Message::InformationReceived(information) => { if let Some(information) = information { -- cgit From 5eefa5d4ead9ebfac7dab1db9aebf9797d2dad38 Mon Sep 17 00:00:00 2001 From: Richard Date: Wed, 27 Apr 2022 16:18:27 -0300 Subject: Simplify the `QueryInformation` Action --- examples/system_information/src/main.rs | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) (limited to 'examples/system_information/src/main.rs') diff --git a/examples/system_information/src/main.rs b/examples/system_information/src/main.rs index 1e61480f..704f5f4d 100644 --- a/examples/system_information/src/main.rs +++ b/examples/system_information/src/main.rs @@ -15,12 +15,11 @@ enum Example { information: system::Information, refresh_button: button::State, }, - Unsupported, } #[derive(Clone, Debug)] enum Message { - InformationReceived(Option), + InformationReceived(system::Information), Refresh, } @@ -46,15 +45,11 @@ impl Application for Example { return system::fetch_information(Message::InformationReceived); } Message::InformationReceived(information) => { - if let Some(information) = information { - let refresh_button = button::State::new(); - *self = Self::Loaded { - information, - refresh_button, - }; - } else { - *self = Self::Unsupported; - } + let refresh_button = button::State::new(); + *self = Self::Loaded { + information, + refresh_button, + }; } } @@ -156,7 +151,6 @@ impl Application for Example { .spacing(10) .into() } - Example::Unsupported => Text::new("Unsupported!").size(20).into(), }; Container::new(content) -- cgit From 1aeb8b8340400e28219c17d7d2ee2a441ae1cd71 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 4 May 2022 14:25:44 +0200 Subject: Show `Loading` screen when refreshing in `system_information` example --- examples/system_information/src/main.rs | 2 ++ 1 file changed, 2 insertions(+) (limited to 'examples/system_information/src/main.rs') diff --git a/examples/system_information/src/main.rs b/examples/system_information/src/main.rs index 704f5f4d..560220b8 100644 --- a/examples/system_information/src/main.rs +++ b/examples/system_information/src/main.rs @@ -42,6 +42,8 @@ impl Application for Example { fn update(&mut self, message: Message) -> Command { match message { Message::Refresh => { + *self = Self::Loading; + return system::fetch_information(Message::InformationReceived); } Message::InformationReceived(information) => { -- cgit