diff --git a/src/app/cpu_graph.rs b/src/app/cpu_graph.rs index 9fe439c..38c76ba 100644 --- a/src/app/cpu_graph.rs +++ b/src/app/cpu_graph.rs @@ -8,23 +8,11 @@ const CYAN: Color = Color::from_rgb8(0, 255, 255); #[derive(Default)] pub struct CpuGraph { - timeline: VecDeque, - pub cache: canvas::Cache, + timeline: VecDeque, } impl CpuGraph { - pub fn new(usage: f32) -> Self { - Self { - timeline: { - let mut vec = VecDeque::new(); - vec.push_front(usage); - vec - }, - ..Default::default() - } - } - pub fn update(&mut self, usage: f32) { self.timeline.push_back(usage); if self.timeline.len() > 1000 { @@ -43,18 +31,13 @@ impl canvas::Program for CpuGraph { bounds: iced::Rectangle, _cursor: mouse::Cursor, ) -> Vec> { - let geometry = self.cache.draw(renderer, bounds.size(), |frame| { - frame.stroke_rectangle( - Point::new(0., 0.), - bounds.size(), - canvas::Stroke::default().with_color(Color::from_rgb8(255, 0, 0)), - ); + let geometry = self.cache.draw_with_bounds(renderer, bounds, |frame| { frame.stroke( &canvas::Path::new(|builder| { for (index, value) in self.timeline.iter().enumerate() { let x = index as f32 / self.timeline.len() as f32 * bounds.width; let y = bounds.height - value / 100. * bounds.height; - builder.line_to(Point::new(x, y)) + builder.line_to(Point { x, y }) } }), canvas::Stroke::default().with_color(CYAN), diff --git a/src/app/mod.rs b/src/app/mod.rs index acc8959..1e0262a 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -4,24 +4,24 @@ use iced::{ }; use smol::Timer; -use crate::{app::cpu_graph::CpuGraph, sysinfo_snapshot::SysinfoSnapshot}; +use crate::app::cpu_graph::CpuGraph; mod cpu_graph; enum Message { - Info(SysinfoSnapshot), + Cpu(f32), } -#[derive(Default)] struct State { - total_cpu_graph: CpuGraph, - cpu_graphs: Vec, + cpu_graph: CpuGraph, } impl State { fn new() -> (Self, Task) { ( - Self::default(), + Self { + cpu_graph: Default::default(), + }, Task::run( channel(1, async |mut sender| { let mut sys = sysinfo::System::new(); @@ -34,53 +34,28 @@ impl State { loop { sys.refresh_specifics(refreshes); Timer::after(sysinfo::MINIMUM_CPU_UPDATE_INTERVAL).await; - sender.send(SysinfoSnapshot::from(&sys)).await.unwrap(); + sender.send(sys.global_cpu_usage()).await.unwrap(); } }), - Message::Info, + Message::Cpu, ), ) } fn update(&mut self, message: Message) -> Task { match message { - Message::Info(snapshot) => { - self.total_cpu_graph.update(snapshot.cpu_total); - self.total_cpu_graph.cache.clear(); - - if self.cpu_graphs.len() != snapshot.cpus.len() { - self.cpu_graphs = snapshot - .cpus - .into_iter() - .map(|usage| CpuGraph::new(usage)) - .collect(); - } else { - for (graph, usage) in self.cpu_graphs.iter_mut().zip(snapshot.cpus.into_iter()) - { - graph.update(usage); - graph.cache.clear(); - } - } + Message::Cpu(value) => { + self.cpu_graph.update(value); + self.cpu_graph.cache.clear(); } } Task::none() } fn view(&self) -> Element<'_, Message, Theme, Renderer> { - widget::row([ - widget::Canvas::new(&self.total_cpu_graph) - .width(Length::Fill) - .height(Length::Fill) - .into(), - widget::grid( - self.cpu_graphs - .iter() - .map(|graph| widget::Canvas::new(graph).into()), - ) - .columns(2) + widget::Canvas::new(&self.cpu_graph) + .width(Length::Fill) .height(Length::Fill) - .into(), - ]) - .into() + .into() } } diff --git a/src/main.rs b/src/main.rs index fa3e73b..2e13470 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,4 @@ mod app; -mod sysinfo_snapshot; fn main() -> Result<(), impl std::error::Error> { app::run() diff --git a/src/sysinfo_snapshot.rs b/src/sysinfo_snapshot.rs deleted file mode 100644 index cb55f06..0000000 --- a/src/sysinfo_snapshot.rs +++ /dev/null @@ -1,14 +0,0 @@ -#[derive(Clone, Debug)] -pub struct SysinfoSnapshot { - pub cpu_total: f32, - pub cpus: Vec, -} - -impl From<&sysinfo::System> for SysinfoSnapshot { - fn from(value: &sysinfo::System) -> Self { - Self { - cpu_total: value.global_cpu_usage(), - cpus: value.cpus().iter().map(|cpu| cpu.cpu_usage()).collect(), - } - } -}