Compare commits

..
2 changed files with 16 additions and 14 deletions

66
src/app/cpu_graph.rs Normal file
View file

@ -0,0 +1,66 @@
use std::collections::VecDeque;
use iced::{Color, Point, Renderer, Theme, mouse, widget::canvas};
use crate::app;
const CYAN: Color = Color::from_rgb8(0, 255, 255);
#[derive(Default)]
pub struct CpuGraph {
timeline: VecDeque<f32>,
pub cache: canvas::Cache,
}
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 {
self.timeline.pop_front();
}
}
}
impl canvas::Program<app::Message, Theme, Renderer> for CpuGraph {
type State = ();
fn draw(
&self,
_state: &Self::State,
renderer: &Renderer,
_theme: &Theme,
bounds: iced::Rectangle,
_cursor: mouse::Cursor,
) -> Vec<canvas::Geometry<Renderer>> {
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)),
);
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))
}
}),
canvas::Stroke::default().with_color(CYAN),
);
});
vec![geometry]
}
}

View file

@ -4,9 +4,9 @@ use iced::{
}; };
use smol::Timer; use smol::Timer;
use crate::{app::graph::Graph, sysinfo_snapshot::SysinfoSnapshot}; use crate::{app::cpu_graph::CpuGraph, sysinfo_snapshot::SysinfoSnapshot};
mod graph; mod cpu_graph;
enum Message { enum Message {
Info(SysinfoSnapshot), Info(SysinfoSnapshot),
@ -14,8 +14,8 @@ enum Message {
#[derive(Default)] #[derive(Default)]
struct State { struct State {
total_cpu_graph: Graph, total_cpu_graph: CpuGraph,
cpu_graphs: Vec<Graph>, cpu_graphs: Vec<CpuGraph>,
} }
impl State { impl State {
@ -51,12 +51,12 @@ impl State {
self.cpu_graphs = snapshot self.cpu_graphs = snapshot
.cpus .cpus
.into_iter() .into_iter()
.map(|value| Graph::new(value)) .map(|usage| CpuGraph::new(usage))
.collect(); .collect();
} else { } else {
for (graph, value) in self.cpu_graphs.iter_mut().zip(snapshot.cpus.into_iter()) for (graph, usage) in self.cpu_graphs.iter_mut().zip(snapshot.cpus.into_iter())
{ {
graph.update(value); graph.update(usage);
graph.cache.clear(); graph.cache.clear();
} }
} }