From e310ca570b0aeab24f8836aa6cf6c444b1016c5e Mon Sep 17 00:00:00 2001 From: electria Date: Sun, 16 Aug 2026 13:56:18 -0700 Subject: [PATCH] feat: keybinds for toggling displays --- src/app/mod.rs | 103 +++++++++++++++++++++++++++++++++++------------ src/app/utils.rs | 14 +++++++ src/main.rs | 1 + src/utils.rs | 9 +++++ 4 files changed, 101 insertions(+), 26 deletions(-) create mode 100644 src/app/utils.rs create mode 100644 src/utils.rs diff --git a/src/app/mod.rs b/src/app/mod.rs index 0f038bd..ce3a1d0 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -1,13 +1,28 @@ use std::time::Duration; -use iced::{Color, Element, Error, Length, Renderer, Task, Theme, application, theme, widget}; +use iced::{ + Color, Error, Event, Length, Renderer, Subscription, Task, Theme, application, event, + keyboard::{self, Key}, + theme, widget, +}; -use crate::{app::graph::Graph, sysinfo_snapshot::SysinfoSnapshot}; +use crate::{ + app::{graph::Graph, utils::maybe_widget}, + sysinfo_snapshot::SysinfoSnapshot, + utils::Toggle, +}; mod graph; mod info_stream; +mod utils; + +type Element<'a> = iced::Element<'a, Message, Theme, Renderer>; enum Message { + ToggleMain, + ToggleCpu, + ToggleMemory, + Info(SysinfoSnapshot), } @@ -15,18 +30,33 @@ enum Message { struct State { uptime: Duration, + is_main_section_shown: bool, total_cpu_graph: Graph, + + is_cpu_section_shown: bool, cpu_graphs: Vec, + + is_memory_section_shown: bool, memory_graph: Graph, swap_graph: Graph, } impl State { fn new() -> (Self, Task) { - (Self::default(), info_stream::create()) + ( + Self { + is_main_section_shown: true, + ..Default::default() + }, + info_stream::create(), + ) } fn update(&mut self, message: Message) -> Task { match message { + Message::ToggleMain => self.is_main_section_shown.toggle(), + Message::ToggleCpu => self.is_cpu_section_shown.toggle(), + Message::ToggleMemory => self.is_memory_section_shown.toggle(), + Message::Info(snapshot) => { self.uptime = snapshot.uptime(); @@ -55,38 +85,58 @@ impl State { Task::none() } - fn view(&self) -> Element<'_, Message, Theme, Renderer> { + fn view(&self) -> Element<'_> { widget::column([ widget::row([ - widget::canvas(&self.total_cpu_graph) - .width(Length::Fill) + maybe_widget(self.is_main_section_shown, || { + widget::canvas(&self.total_cpu_graph) + .width(Length::Fill) + .height(Length::Fill) + .into() + }), + maybe_widget(self.is_cpu_section_shown, || { + widget::grid( + self.cpu_graphs + .iter() + .map(|graph| widget::Canvas::new(graph).into()), + ) + .columns(2) .height(Length::Fill) - .into(), - widget::grid( - self.cpu_graphs - .iter() - .map(|graph| widget::Canvas::new(graph).into()), - ) - .columns(2) - .height(Length::Fill) - .into(), - ]) - .into(), - widget::row([ - widget::canvas(&self.memory_graph) - .width(Length::Fill) - .height(Length::Fill) - .into(), - widget::canvas(&self.swap_graph) - .width(Length::Fill) - .height(Length::Fill) - .into(), + .into() + }), ]) .into(), + maybe_widget(self.is_memory_section_shown, || { + widget::row([ + widget::canvas(&self.memory_graph) + .width(Length::Fill) + .height(Length::Fill) + .into(), + widget::canvas(&self.swap_graph) + .width(Length::Fill) + .height(Length::Fill) + .into(), + ]) + .into() + }), ]) .into() } + fn subscription(&self) -> Subscription { + event::listen().filter_map(|event| match event { + Event::Keyboard(keyboard::Event::KeyPressed { key, modifiers, .. }) => { + match key.as_ref() { + Key::Character("m" | "1") if modifiers.shift() => Some(Message::ToggleMain), + Key::Character("c" | "2") => Some(Message::ToggleCpu), + Key::Character("m" | "3") => Some(Message::ToggleMemory), + _ => None, + } + } + _ => None, + }) + } + fn title(&self) -> String { format!("itop {:?}", self.uptime) } @@ -94,6 +144,7 @@ impl State { pub fn run() -> Result<(), Error> { application(State::new, State::update, State::view) + .subscription(State::subscription) .title(State::title) .theme(theme::Theme::custom( "high-contrast-dark", diff --git a/src/app/utils.rs b/src/app/utils.rs new file mode 100644 index 0000000..ceef158 --- /dev/null +++ b/src/app/utils.rs @@ -0,0 +1,14 @@ +use iced::widget; + +use crate::app; + +pub fn maybe_widget<'a>( + condition: bool, + widget: impl FnOnce() -> app::Element<'a>, +) -> app::Element<'a> { + if condition { + widget() + } else { + widget::space().into() + } +} diff --git a/src/main.rs b/src/main.rs index c6dcb49..d8e6e6e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ use std::env; mod app; mod server; mod sysinfo_snapshot; +mod utils; fn main() { match env::args().nth(1).as_deref() { diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..3556bf9 --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,9 @@ +pub trait Toggle { + fn toggle(&mut self); +} + +impl Toggle for bool { + fn toggle(&mut self) { + *self = !*self + } +}