summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Richard <richardsoncusto@gmail.com>2022-04-14 02:11:43 -0300
committerLibravatar Richard <richardsoncusto@gmail.com>2022-04-26 18:59:08 -0300
commitc9ea1f11dec96df04ade463ea9f33062a85c9219 (patch)
treefcdf29f469d18dfdc3b3ab40d7e6b2491a416194
parent5bfe887e3d4acb2c48cca22c73aa52f4cc4856ad (diff)
downloadiced-c9ea1f11dec96df04ade463ea9f33062a85c9219.tar.gz
iced-c9ea1f11dec96df04ade463ea9f33062a85c9219.tar.bz2
iced-c9ea1f11dec96df04ade463ea9f33062a85c9219.zip
Add memory usage to `Information` struct
-rw-r--r--examples/system_information/src/main.rs43
-rw-r--r--native/src/system/information.rs4
-rw-r--r--winit/src/application.rs12
3 files changed, 51 insertions, 8 deletions
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<system::Information>),
+ Refresh,
}
impl Application for Example {
@@ -38,9 +42,16 @@ impl Application for Example {
fn update(&mut self, message: Message) -> Command<Message> {
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<Message> {
let content: Element<Message> = 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()
diff --git a/native/src/system/information.rs b/native/src/system/information.rs
index bf0b100c..fa4a835b 100644
--- a/native/src/system/information.rs
+++ b/native/src/system/information.rs
@@ -1,5 +1,5 @@
/// Contains informations about the system (e.g. system name, processor, memory, graphics adapter).
-#[derive(Debug)]
+#[derive(Clone, Debug)]
pub struct Information {
/// Contains the system name.
pub system_name: Option<String>,
@@ -13,6 +13,8 @@ pub struct Information {
pub cpu_cores: Option<usize>,
/// Contains the total RAM size in KB.
pub memory_total: u64,
+ /// Contains the system used RAM size in KB.
+ pub memory_used: Option<u64>,
/// Contains the graphics backend.
pub graphics_backend: String,
/// Contains the graphics adapter.
diff --git a/winit/src/application.rs b/winit/src/application.rs
index 9d65d769..1865f344 100644
--- a/winit/src/application.rs
+++ b/winit/src/application.rs
@@ -584,12 +584,21 @@ pub fn run_command<Message: 'static + std::fmt::Debug + Send, E: Executor>(
system::Action::QueryInformation(tag) => {
#[cfg(feature = "sysinfo")]
let information = {
- use sysinfo::{ProcessorExt, System, SystemExt};
+ use sysinfo::{
+ ProcessExt, ProcessorExt, System, SystemExt,
+ };
let mut system = System::new_all();
system.refresh_all();
let cpu = system.global_processor_info();
+ let memory_used = sysinfo::get_current_pid()
+ .and_then(|pid| {
+ system.process(pid).ok_or("Process not found")
+ })
+ .and_then(|process| Ok(process.memory()))
+ .ok();
+
let information = system::Information {
system_name: system.name(),
system_kernel: system.kernel_version(),
@@ -597,6 +606,7 @@ pub fn run_command<Message: 'static + std::fmt::Debug + Send, E: Executor>(
cpu_brand: cpu.brand().into(),
cpu_cores: system.physical_core_count(),
memory_total: system.total_memory(),
+ memory_used,
graphics_adapter: graphics_info.adapter.clone(),
graphics_backend: graphics_info.backend.clone(),
};