feat: update the cpu graph async

This commit is contained in:
electria 2026-08-15 14:38:06 -07:00
commit 2314f67349
Signed by: electria
SSH key fingerprint: SHA256:8LlB3ucPbBHqozqkhsNbaV5oG3SlzzqUj8FZDL6IPQs
3 changed files with 72 additions and 32 deletions

41
Cargo.lock generated
View file

@ -139,6 +139,17 @@ dependencies = [
"slab",
]
[[package]]
name = "async-fs"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8034a681df4aed8b8edbd7fbe472401ecf009251c8b40556b304567052e294c5"
dependencies = [
"async-lock",
"blocking",
"futures-lite",
]
[[package]]
name = "async-io"
version = "2.6.0"
@ -168,6 +179,17 @@ dependencies = [
"pin-project-lite",
]
[[package]]
name = "async-net"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b948000fad4873c1c9339d60f2623323a0cfd3816e5181033c6a5cb68b2accf7"
dependencies = [
"async-io",
"blocking",
"futures-lite",
]
[[package]]
name = "async-process"
version = "2.5.0"
@ -1211,6 +1233,7 @@ dependencies = [
"iced_core",
"log",
"rustc-hash 2.1.3",
"smol",
"wasm-bindgen-futures",
"wasmtimer",
]
@ -1357,6 +1380,7 @@ name = "itop"
version = "0.1.0"
dependencies = [
"iced",
"smol",
"sysinfo",
]
@ -2740,6 +2764,23 @@ dependencies = [
"wayland-backend",
]
[[package]]
name = "smol"
version = "2.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a33bd3e260892199c3ccfc487c88b2da2265080acb316cd920da72fdfd7c599f"
dependencies = [
"async-channel",
"async-executor",
"async-fs",
"async-io",
"async-lock",
"async-net",
"async-process",
"blocking",
"futures-lite",
]
[[package]]
name = "smol_str"
version = "0.2.2"

View file

@ -5,8 +5,9 @@ edition = "2024"
license = "AGPL-3.0-or-later"
[dependencies]
smol = "2.0.2"
sysinfo = "0.39.6"
[dependencies.iced]
version = "0.14.0"
features = [ "canvas" ]
features = [ "canvas", "smol" ]

View file

@ -1,48 +1,51 @@
use std::time::Instant;
use iced::{
Color, Element, Length, Renderer, Subscription, Task, Theme, application, theme, widget, window,
Color, Element, Length, Renderer, Task, Theme, application, futures::SinkExt, stream::channel,
theme, widget,
};
use smol::Timer;
use crate::app::cpu_graph::CpuGraph;
mod cpu_graph;
enum Message {
Frame(Instant),
Cpu(f32),
}
struct State {
sys: sysinfo::System,
refreshes: sysinfo::RefreshKind,
last_refresh: Instant,
cpu_graph: CpuGraph,
}
impl State {
fn new() -> Self {
Self {
sys: sysinfo::System::new(),
refreshes: sysinfo::RefreshKind::nothing()
.with_cpu(sysinfo::CpuRefreshKind::nothing().with_cpu_usage()),
fn new() -> (Self, Task<Message>) {
(
Self {
cpu_graph: Default::default(),
},
Task::run(
channel(1, async |mut sender| {
let mut sys = sysinfo::System::new();
let refreshes = sysinfo::RefreshKind::nothing()
.with_cpu(sysinfo::CpuRefreshKind::nothing().with_cpu_usage());
cpu_graph: Default::default(),
last_refresh: Instant::now(),
}
sys.refresh_specifics(refreshes);
Timer::after(sysinfo::MINIMUM_CPU_UPDATE_INTERVAL).await;
loop {
sys.refresh_specifics(refreshes);
Timer::after(sysinfo::MINIMUM_CPU_UPDATE_INTERVAL).await;
sender.send(sys.global_cpu_usage()).await.unwrap();
}
}),
Message::Cpu,
),
)
}
fn update(&mut self, message: Message) -> Task<Message> {
match message {
Message::Frame(instant) => {
if instant.duration_since(self.last_refresh) >= sysinfo::MINIMUM_CPU_UPDATE_INTERVAL
{
self.sys.refresh_specifics(self.refreshes);
self.cpu_graph.update(self.sys.global_cpu_usage());
self.cpu_graph.cache.clear();
self.last_refresh = instant;
}
Message::Cpu(value) => {
self.cpu_graph.update(value);
self.cpu_graph.cache.clear();
}
}
@ -54,15 +57,10 @@ impl State {
.height(Length::Fill)
.into()
}
fn subscription(&self) -> Subscription<Message> {
window::frames().map(Message::Frame)
}
}
pub fn run() -> Result<(), impl std::error::Error> {
application(State::new, State::update, State::view)
.subscription(State::subscription)
.theme(theme::Theme::custom(
"high-contrast-dark",
theme::Palette {