Compare commits

...
Author SHA1 Message Date
ee4dc4f7b9
refactor: remove app::Message type from Graph
I wonder, given the description of canvas::Program,
would the graph be better as a true widget?

I'll have to learn how to actually implement a widget though.
2026-08-15 18:52:34 -07:00
96a972f9f0
refactor: generalize CpuGraph -> Graph 2026-08-15 18:48:45 -07:00
2 changed files with 14 additions and 16 deletions

View file

@ -2,38 +2,36 @@ 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 {
pub struct Graph {
timeline: VecDeque<f32>,
pub cache: canvas::Cache,
}
impl CpuGraph {
pub fn new(usage: f32) -> Self {
impl Graph {
pub fn new(value: f32) -> Self {
Self {
timeline: {
let mut vec = VecDeque::new();
vec.push_front(usage);
vec.push_front(value);
vec
},
..Default::default()
}
}
pub fn update(&mut self, usage: f32) {
self.timeline.push_back(usage);
pub fn update(&mut self, value: f32) {
self.timeline.push_back(value);
if self.timeline.len() > 1000 {
self.timeline.pop_front();
}
}
}
impl canvas::Program<app::Message, Theme, Renderer> for CpuGraph {
impl canvas::Program<(), Theme, Renderer> for Graph {
type State = ();
fn draw(
&self,

View file

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