feat: basic cpu graph

This commit is contained in:
electria 2026-08-15 14:11:29 -07:00
commit 99c60658fa
Signed by: electria
SSH key fingerprint: SHA256:8LlB3ucPbBHqozqkhsNbaV5oG3SlzzqUj8FZDL6IPQs
4 changed files with 237 additions and 13 deletions

View file

@ -1,23 +1,68 @@
use iced::{Color, Element, Renderer, Task, Theme, application, theme, widget};
use std::time::Instant;
enum Message {}
use iced::{
Color, Element, Length, Renderer, Subscription, Task, Theme, application, theme, widget, window,
};
struct State {}
use crate::app::cpu_graph::CpuGraph;
mod cpu_graph;
enum Message {
Frame(Instant),
}
struct State {
sys: sysinfo::System,
refreshes: sysinfo::RefreshKind,
last_refresh: Instant,
cpu_graph: CpuGraph,
}
impl State {
fn new() -> Self {
Self {}
Self {
sys: sysinfo::System::new(),
refreshes: sysinfo::RefreshKind::nothing()
.with_cpu(sysinfo::CpuRefreshKind::nothing().with_cpu_usage()),
cpu_graph: Default::default(),
last_refresh: Instant::now(),
}
}
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;
}
}
}
Task::none()
}
fn view(&self) -> Element<'_, Message, Theme, Renderer> {
widget::text("hiiiiiii").into()
widget::Canvas::new(&self.cpu_graph)
.width(Length::Fill)
.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 {