Compare commits
4 changed files with 17 additions and 74 deletions
|
|
@ -8,23 +8,11 @@ const CYAN: Color = Color::from_rgb8(0, 255, 255);
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct CpuGraph {
|
pub struct CpuGraph {
|
||||||
timeline: VecDeque<f32>,
|
|
||||||
|
|
||||||
pub cache: canvas::Cache,
|
pub cache: canvas::Cache,
|
||||||
|
timeline: VecDeque<f32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CpuGraph {
|
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) {
|
pub fn update(&mut self, usage: f32) {
|
||||||
self.timeline.push_back(usage);
|
self.timeline.push_back(usage);
|
||||||
if self.timeline.len() > 1000 {
|
if self.timeline.len() > 1000 {
|
||||||
|
|
@ -43,18 +31,13 @@ impl canvas::Program<app::Message, Theme, Renderer> for CpuGraph {
|
||||||
bounds: iced::Rectangle,
|
bounds: iced::Rectangle,
|
||||||
_cursor: mouse::Cursor,
|
_cursor: mouse::Cursor,
|
||||||
) -> Vec<canvas::Geometry<Renderer>> {
|
) -> Vec<canvas::Geometry<Renderer>> {
|
||||||
let geometry = self.cache.draw(renderer, bounds.size(), |frame| {
|
let geometry = self.cache.draw_with_bounds(renderer, bounds, |frame| {
|
||||||
frame.stroke_rectangle(
|
|
||||||
Point::new(0., 0.),
|
|
||||||
bounds.size(),
|
|
||||||
canvas::Stroke::default().with_color(Color::from_rgb8(255, 0, 0)),
|
|
||||||
);
|
|
||||||
frame.stroke(
|
frame.stroke(
|
||||||
&canvas::Path::new(|builder| {
|
&canvas::Path::new(|builder| {
|
||||||
for (index, value) in self.timeline.iter().enumerate() {
|
for (index, value) in self.timeline.iter().enumerate() {
|
||||||
let x = index as f32 / self.timeline.len() as f32 * bounds.width;
|
let x = index as f32 / self.timeline.len() as f32 * bounds.width;
|
||||||
let y = bounds.height - value / 100. * bounds.height;
|
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),
|
canvas::Stroke::default().with_color(CYAN),
|
||||||
|
|
|
||||||
|
|
@ -4,24 +4,24 @@ use iced::{
|
||||||
};
|
};
|
||||||
use smol::Timer;
|
use smol::Timer;
|
||||||
|
|
||||||
use crate::{app::cpu_graph::CpuGraph, sysinfo_snapshot::SysinfoSnapshot};
|
use crate::app::cpu_graph::CpuGraph;
|
||||||
|
|
||||||
mod cpu_graph;
|
mod cpu_graph;
|
||||||
|
|
||||||
enum Message {
|
enum Message {
|
||||||
Info(SysinfoSnapshot),
|
Cpu(f32),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
|
||||||
struct State {
|
struct State {
|
||||||
total_cpu_graph: CpuGraph,
|
cpu_graph: CpuGraph,
|
||||||
cpu_graphs: Vec<CpuGraph>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl State {
|
impl State {
|
||||||
fn new() -> (Self, Task<Message>) {
|
fn new() -> (Self, Task<Message>) {
|
||||||
(
|
(
|
||||||
Self::default(),
|
Self {
|
||||||
|
cpu_graph: Default::default(),
|
||||||
|
},
|
||||||
Task::run(
|
Task::run(
|
||||||
channel(1, async |mut sender| {
|
channel(1, async |mut sender| {
|
||||||
let mut sys = sysinfo::System::new();
|
let mut sys = sysinfo::System::new();
|
||||||
|
|
@ -34,52 +34,27 @@ impl State {
|
||||||
loop {
|
loop {
|
||||||
sys.refresh_specifics(refreshes);
|
sys.refresh_specifics(refreshes);
|
||||||
Timer::after(sysinfo::MINIMUM_CPU_UPDATE_INTERVAL).await;
|
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<Message> {
|
fn update(&mut self, message: Message) -> Task<Message> {
|
||||||
match message {
|
match message {
|
||||||
Message::Info(snapshot) => {
|
Message::Cpu(value) => {
|
||||||
self.total_cpu_graph.update(snapshot.cpu_total);
|
self.cpu_graph.update(value);
|
||||||
self.total_cpu_graph.cache.clear();
|
self.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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Task::none()
|
Task::none()
|
||||||
}
|
}
|
||||||
fn view(&self) -> Element<'_, Message, Theme, Renderer> {
|
fn view(&self) -> Element<'_, Message, Theme, Renderer> {
|
||||||
widget::row([
|
widget::Canvas::new(&self.cpu_graph)
|
||||||
widget::Canvas::new(&self.total_cpu_graph)
|
|
||||||
.width(Length::Fill)
|
.width(Length::Fill)
|
||||||
.height(Length::Fill)
|
.height(Length::Fill)
|
||||||
.into(),
|
|
||||||
widget::grid(
|
|
||||||
self.cpu_graphs
|
|
||||||
.iter()
|
|
||||||
.map(|graph| widget::Canvas::new(graph).into()),
|
|
||||||
)
|
|
||||||
.columns(2)
|
|
||||||
.height(Length::Fill)
|
|
||||||
.into(),
|
|
||||||
])
|
|
||||||
.into()
|
.into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
mod app;
|
mod app;
|
||||||
mod sysinfo_snapshot;
|
|
||||||
|
|
||||||
fn main() -> Result<(), impl std::error::Error> {
|
fn main() -> Result<(), impl std::error::Error> {
|
||||||
app::run()
|
app::run()
|
||||||
|
|
|
||||||
|
|
@ -1,14 +0,0 @@
|
||||||
#[derive(Clone, Debug)]
|
|
||||||
pub struct SysinfoSnapshot {
|
|
||||||
pub cpu_total: f32,
|
|
||||||
pub cpus: Vec<f32>,
|
|
||||||
}
|
|
||||||
|
|
||||||
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(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in a new issue