iced-yoda/examples/system_information/src/main.rs

147 lines
4.4 KiB
Rust
Raw Normal View History

use iced::system;
use iced::widget::{button, center, column, text};
use iced::{Element, Task};
2022-03-10 03:02:17 -03:00
pub fn main() -> iced::Result {
iced::application(Example::new, Example::update, Example::view).run()
2022-03-10 03:02:17 -03:00
}
#[derive(Default)]
2022-09-23 15:54:09 -05:00
#[allow(clippy::large_enum_variant)]
2022-03-10 03:02:17 -03:00
enum Example {
#[default]
2022-03-10 03:02:17 -03:00
Loading,
Loaded {
information: system::Information,
},
2022-03-10 03:02:17 -03:00
}
#[derive(Clone, Debug)]
2022-09-23 15:54:09 -05:00
#[allow(clippy::large_enum_variant)]
2022-03-10 03:02:17 -03:00
enum Message {
2022-04-27 16:18:27 -03:00
InformationReceived(system::Information),
Refresh,
2022-03-10 03:02:17 -03:00
}
2024-03-16 05:33:47 +01:00
impl Example {
fn new() -> (Self, Task<Message>) {
(
Self::Loading,
system::information().map(Message::InformationReceived),
)
}
fn update(&mut self, message: Message) -> Task<Message> {
2022-03-10 03:02:17 -03:00
match message {
Message::Refresh => {
let (state, refresh) = Self::new();
*self = state;
refresh
}
2022-03-10 03:02:17 -03:00
Message::InformationReceived(information) => {
*self = Self::Loaded { information };
Task::none()
2022-03-10 03:02:17 -03:00
}
}
}
fn view(&self) -> Element<'_, Message> {
use bytesize::ByteSize;
let content: Element<_> = match self {
Example::Loading => text("Loading...").size(40).into(),
Example::Loaded { information } => {
let system_name = text!(
2022-03-10 03:02:17 -03:00
"System name: {}",
information
.system_name
.as_ref()
.unwrap_or(&"unknown".to_string())
);
2022-03-10 03:02:17 -03:00
let system_kernel = text!(
2022-03-10 03:02:17 -03:00
"System kernel: {}",
information
.system_kernel
.as_ref()
.unwrap_or(&"unknown".to_string())
);
2022-03-10 03:02:17 -03:00
let system_version = text!(
2022-03-10 03:02:17 -03:00
"System version: {}",
information
.system_version
.as_ref()
.unwrap_or(&"unknown".to_string())
);
2022-03-10 03:02:17 -03:00
let system_short_version = text!(
2022-09-23 15:54:09 -05:00
"System short version: {}",
information
.system_short_version
.as_ref()
.unwrap_or(&"unknown".to_string())
);
2022-09-23 15:54:09 -05:00
let cpu_brand =
text!("Processor brand: {}", information.cpu_brand);
2022-03-10 03:02:17 -03:00
let cpu_cores = text!(
2022-03-10 03:02:17 -03:00
"Processor cores: {}",
information
.cpu_cores
.map_or("unknown".to_string(), |cores| cores
.to_string())
);
2022-03-10 03:02:17 -03:00
2022-03-16 19:42:40 -03:00
let memory_readable =
ByteSize::b(information.memory_total).to_string_as(true);
2022-03-16 19:42:40 -03:00
let memory_total = text!(
"Memory (total): {} bytes ({memory_readable})",
information.memory_total,
);
2022-03-10 03:02:17 -03:00
let memory_text =
if let Some(memory_used) = information.memory_used {
let memory_readable =
ByteSize::b(memory_used).to_string_as(true);
format!("{memory_used} bytes ({memory_readable})")
} else {
String::from("None")
};
let memory_used = text!("Memory (used): {memory_text}");
2024-03-18 18:24:57 -03:00
let graphics_adapter =
text!("Graphics adapter: {}", information.graphics_adapter);
2022-03-17 01:02:31 -03:00
2024-03-18 18:24:57 -03:00
let graphics_backend =
text!("Graphics backend: {}", information.graphics_backend);
2022-03-17 01:02:31 -03:00
column![
system_name.size(30),
system_kernel.size(30),
system_version.size(30),
2022-09-23 15:54:09 -05:00
system_short_version.size(30),
cpu_brand.size(30),
cpu_cores.size(30),
memory_total.size(30),
memory_used.size(30),
graphics_adapter.size(30),
graphics_backend.size(30),
button("Refresh").on_press(Message::Refresh)
]
2022-03-17 02:47:50 -03:00
.spacing(10)
2022-03-10 03:02:17 -03:00
.into()
}
};
center(content).into()
2022-03-10 03:02:17 -03:00
}
}