Add system_information example

This commit is contained in:
Richard 2022-03-10 03:02:17 -03:00
parent c8ed318e17
commit 53538b65b1
5 changed files with 130 additions and 6 deletions

View file

@ -96,6 +96,7 @@ members = [
"examples/pure/todos",
"examples/pure/tour",
"examples/websocket",
"examples/system_information"
]
[dependencies]

View file

@ -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" }

View file

@ -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()
}
}

View file

@ -9,10 +9,6 @@ pub struct Information {
pub system_version: Option<String>,
/// Contains the processor brand.
pub cpu_brand: String,
/// Contains the processor vendor id.
pub cpu_vendor: String,
/// Contains the processor name.
pub cpu_name: String,
/// Contains the number of physical cores on the processor.
pub cpu_cores: Option<usize>,
/// Contains the total RAM size in KB.

View file

@ -589,8 +589,6 @@ pub fn run_command<Message: 'static + std::fmt::Debug + Send, E: Executor>(
system_kernel: system.kernel_version(),
system_version: system.long_os_version(),
cpu_brand: cpu.brand().into(),
cpu_vendor: cpu.vendor_id().into(),
cpu_name: cpu.name().into(),
cpu_cores: system.physical_core_count(),
memory_total: system.total_memory(),
};