Compare commits

..
2 changed files with 16 additions and 14 deletions

View file

@ -2,36 +2,38 @@ use std::collections::VecDeque;
use iced::{Color, Point, Renderer, Theme, mouse, widget::canvas}; use iced::{Color, Point, Renderer, Theme, mouse, widget::canvas};
use crate::app;
const CYAN: Color = Color::from_rgb8(0, 255, 255); const CYAN: Color = Color::from_rgb8(0, 255, 255);
#[derive(Default)] #[derive(Default)]
pub struct Graph { pub struct CpuGraph {
timeline: VecDeque<f32>, timeline: VecDeque<f32>,
pub cache: canvas::Cache, pub cache: canvas::Cache,
} }
impl Graph { impl CpuGraph {
pub fn new(value: f32) -> Self { pub fn new(usage: f32) -> Self {
Self { Self {
timeline: { timeline: {
let mut vec = VecDeque::new(); let mut vec = VecDeque::new();
vec.push_front(value); vec.push_front(usage);
vec vec
}, },
..Default::default() ..Default::default()
} }
} }
pub fn update(&mut self, value: f32) { pub fn update(&mut self, usage: f32) {
self.timeline.push_back(value); self.timeline.push_back(usage);
if self.timeline.len() > 1000 { if self.timeline.len() > 1000 {
self.timeline.pop_front(); self.timeline.pop_front();
} }
} }
} }
impl canvas::Program<(), Theme, Renderer> for Graph { impl canvas::Program<app::Message, Theme, Renderer> for CpuGraph {
type State = (); type State = ();
fn draw( fn draw(
&self, &self,

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();
} }
} }