Add system_information example
This commit is contained in:
parent
c8ed318e17
commit
53538b65b1
5 changed files with 130 additions and 6 deletions
10
examples/system_information/Cargo.toml
Normal file
10
examples/system_information/Cargo.toml
Normal 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" }
|
||||
119
examples/system_information/src/main.rs
Normal file
119
examples/system_information/src/main.rs
Normal 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()
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue