diff --git a/komorebi-bar/src/date.rs b/komorebi-bar/src/date.rs index efbdea62..1e9eb358 100644 --- a/komorebi-bar/src/date.rs +++ b/komorebi-bar/src/date.rs @@ -1,5 +1,6 @@ use crate::widget::BarWidget; +#[derive(Copy, Clone, Debug)] pub enum DateFormat { MonthDateYear, YearMonthDate, @@ -27,15 +28,15 @@ impl DateFormat { } } +#[derive(Copy, Clone, Debug)] pub struct Date { + pub enable: bool, pub format: DateFormat, } -impl Default for Date { - fn default() -> Self { - Self { - format: DateFormat::MonthDateYear, - } +impl Date { + pub fn new(enable: bool, format: DateFormat) -> Self { + Self { enable, format } } } diff --git a/komorebi-bar/src/main.rs b/komorebi-bar/src/main.rs index d5c9bc38..45f01f08 100644 --- a/komorebi-bar/src/main.rs +++ b/komorebi-bar/src/main.rs @@ -5,8 +5,12 @@ mod time; mod widget; use crate::date::Date; +use crate::date::DateFormat; use crate::memory::Memory; +use crate::memory::MemoryConfig; use crate::storage::Storage; +use crate::storage::StorageConfig; +use crate::time::TimeFormat; use crate::widget::BarWidget; use crossbeam_channel::Receiver; use eframe::egui; @@ -31,20 +35,20 @@ pub struct Position { y: f32, } -impl Into for Position { - fn into(self) -> Vec2 { - Vec2 { - x: self.x, - y: self.y, +impl From for Vec2 { + fn from(value: Position) -> Self { + Self { + x: value.x, + y: value.y, } } } -impl Into for Position { - fn into(self) -> Pos2 { - Pos2 { - x: self.x, - y: self.y, +impl From for Pos2 { + fn from(value: Position) -> Self { + Self { + x: value.x, + y: value.y, } } } @@ -57,6 +61,10 @@ pub struct Config { transparent: bool, monitor_index: usize, monitor_work_area_offset: Option, + time: Time, + date: Date, + storage: StorageConfig, + memory: MemoryConfig, } fn main() -> eframe::Result<()> { @@ -72,6 +80,10 @@ fn main() -> eframe::Result<()> { right: 0, bottom: 40, }), + time: Time::new(true, TimeFormat::TwentyFourHour), + date: Date::new(true, DateFormat::DayDateMonthYear), + storage: StorageConfig { enable: true }, + memory: MemoryConfig { enable: true }, }; // TODO: ensure that config.monitor_index represents a valid komorebi monitor index @@ -165,6 +177,8 @@ struct Komobar { config: Config, state_receiver: Receiver, selected_workspace: String, + focused_window_title: String, + workspace_layout: String, workspaces: Vec, time: Time, date: Date, @@ -184,14 +198,16 @@ impl Komobar { // for e.g. egui::PaintCallback. Self { - config: *config.as_ref(), + config: *config, state_receiver: rx, selected_workspace: String::new(), + focused_window_title: String::new(), + workspace_layout: String::new(), workspaces: vec![], - time: Time::default(), - date: Date::default(), - memory: Memory::default(), - storage: Storage::default(), + time: config.time, + date: config.date, + memory: Memory::from(config.memory), + storage: Storage::from(config.storage), } } } @@ -199,20 +215,32 @@ impl Komobar { impl Komobar { fn handle_komorebi_notification(&mut self) { if let Ok(notification) = self.state_receiver.try_recv() { - self.workspaces = { - let mut workspaces = vec![]; - let monitor = ¬ification.state.monitors.elements()[self.config.monitor_index]; - let focused_workspace_idx = monitor.focused_workspace_idx(); - self.selected_workspace = monitor.workspaces()[focused_workspace_idx] - .name() - .to_owned() - .unwrap_or_else(|| format!("{}", focused_workspace_idx + 1)); + let monitor = ¬ification.state.monitors.elements()[self.config.monitor_index]; + let focused_workspace_idx = monitor.focused_workspace_idx(); - for (i, ws) in monitor.workspaces().iter().enumerate() { - workspaces.push(ws.name().to_owned().unwrap_or_else(|| format!("{}", i + 1))); + let mut workspaces = vec![]; + self.selected_workspace = monitor.workspaces()[focused_workspace_idx] + .name() + .to_owned() + .unwrap_or_else(|| format!("{}", focused_workspace_idx + 1)); + + for (i, ws) in monitor.workspaces().iter().enumerate() { + workspaces.push(ws.name().to_owned().unwrap_or_else(|| format!("{}", i + 1))); + } + + self.workspaces = workspaces; + self.workspace_layout = match monitor.workspaces()[focused_workspace_idx].layout() { + komorebi_client::Layout::Default(layout) => layout.to_string(), + komorebi_client::Layout::Custom(_) => String::from("Custom"), + }; + + if let Some(container) = monitor.workspaces()[focused_workspace_idx].focused_container() + { + if let Some(window) = container.focused_window() { + if let Ok(title) = window.title() { + self.focused_window_title.clone_from(&title); + } } - - workspaces } } } @@ -267,78 +295,91 @@ impl eframe::App for Komobar { komorebi_client::send_message(&SocketMessage::Retile).unwrap(); } } + + ui.label(&self.workspace_layout); + + ui.add_space(10.0); + + ui.label(&self.focused_window_title); + + ui.add_space(10.0); }); // TODO: make the order configurable - // TODO: make each widget optional?? ui.with_layout(Layout::right_to_left(Align::Center), |ui| { - for time in self.time.output() { - ctx.request_repaint(); - if ui - .label(format!("🕐 {}", time)) - .on_hover_cursor(CursorIcon::default()) - .clicked() - { - // TODO: make default format configurable - self.time.format.toggle() + if self.time.enable { + for time in self.time.output() { + ctx.request_repaint(); + if ui + .label(format!("🕐 {}", time)) + .on_hover_cursor(CursorIcon::default()) + .clicked() + { + self.time.format.toggle() + } } + + // TODO: make spacing configurable + ui.add_space(10.0); } - // TODO: make spacing configurable - ui.add_space(10.0); - - for date in self.date.output() { - if ui - .label(format!("📅 {}", date)) - .on_hover_cursor(CursorIcon::default()) - .clicked() - { - // TODO: make default format configurable - self.date.format.next() - }; - } - - // TODO: make spacing configurable - ui.add_space(10.0); - - for ram in self.memory.output() { - if ui - // TODO: make label configurable?? - .label(format!("🐏 {}", ram)) - .on_hover_cursor(CursorIcon::default()) - .clicked() - { - if let Err(error) = - Command::new("cmd.exe").args(["/C", "taskmgr.exe"]).output() + if self.date.enable { + for date in self.date.output() { + if ui + .label(format!("📅 {}", date)) + .on_hover_cursor(CursorIcon::default()) + .clicked() { - eprintln!("{}", error) - } - }; + self.date.format.next() + }; + } + + // TODO: make spacing configurable + ui.add_space(10.0); } - ui.add_space(10.0); - - for disk in self.storage.output() { - if ui - // TODO: Make emoji configurable?? - .label(format!("🖴 {}", disk)) - .on_hover_cursor(CursorIcon::default()) - .clicked() - { - if let Err(error) = Command::new("cmd.exe") - .args([ - "/C", - "explorer.exe", - disk.split(' ').collect::>()[0], - ]) - .output() + if self.memory.enable { + for ram in self.memory.output() { + if ui + // TODO: make label configurable?? + .label(format!("🐏 {}", ram)) + .on_hover_cursor(CursorIcon::default()) + .clicked() { - eprintln!("{}", error) - } - }; + if let Err(error) = + Command::new("cmd.exe").args(["/C", "taskmgr.exe"]).output() + { + eprintln!("{}", error) + } + }; + } ui.add_space(10.0); } + + if self.storage.enable { + for disk in self.storage.output() { + if ui + // TODO: Make emoji configurable?? + .label(format!("🖴 {}", disk)) + .on_hover_cursor(CursorIcon::default()) + .clicked() + { + if let Err(error) = Command::new("cmd.exe") + .args([ + "/C", + "explorer.exe", + disk.split(' ').collect::>()[0], + ]) + .output() + { + eprintln!("{}", error) + } + }; + + ui.add_space(10.0); + } + } }) }) }); diff --git a/komorebi-bar/src/memory.rs b/komorebi-bar/src/memory.rs index b25fe7dd..9b8c1189 100644 --- a/komorebi-bar/src/memory.rs +++ b/komorebi-bar/src/memory.rs @@ -3,12 +3,19 @@ use sysinfo::RefreshKind; use sysinfo::System; pub struct Memory { + pub enable: bool, system: System, } -impl Default for Memory { - fn default() -> Self { +#[derive(Copy, Clone, Debug)] +pub struct MemoryConfig { + pub enable: bool, +} + +impl From for Memory { + fn from(value: MemoryConfig) -> Self { Self { + enable: value.enable, system: System::new_with_specifics( RefreshKind::default().without_cpu().without_processes(), ), diff --git a/komorebi-bar/src/storage.rs b/komorebi-bar/src/storage.rs index 4769d708..47aa21b4 100644 --- a/komorebi-bar/src/storage.rs +++ b/komorebi-bar/src/storage.rs @@ -1,18 +1,25 @@ use crate::widget::BarWidget; use sysinfo::Disks; -pub struct Storage { - disks: Disks, +#[derive(Copy, Clone, Debug)] +pub struct StorageConfig { + pub enable: bool, } -impl Default for Storage { - fn default() -> Self { +impl From for Storage { + fn from(value: StorageConfig) -> Self { Self { + enable: value.enable, disks: Disks::new_with_refreshed_list(), } } } +pub struct Storage { + pub enable: bool, + disks: Disks, +} + impl BarWidget for Storage { fn output(&mut self) -> Vec { self.disks.refresh(); diff --git a/komorebi-bar/src/time.rs b/komorebi-bar/src/time.rs index 74017ad1..9f44d587 100644 --- a/komorebi-bar/src/time.rs +++ b/komorebi-bar/src/time.rs @@ -1,5 +1,6 @@ use crate::widget::BarWidget; +#[derive(Copy, Clone, Debug)] pub enum TimeFormat { TwelveHour, TwentyFourHour, @@ -21,15 +22,15 @@ impl TimeFormat { } } +#[derive(Copy, Clone, Debug)] pub struct Time { + pub enable: bool, pub format: TimeFormat, } -impl Default for Time { - fn default() -> Self { - Self { - format: TimeFormat::TwelveHour, - } +impl Time { + pub fn new(enable: bool, format: TimeFormat) -> Self { + Self { enable, format } } }