From b3990590f35fb566c01ef123df90ccd953da22e3 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Thu, 8 Aug 2024 18:24:25 -0700 Subject: [PATCH] feat(bar): add config struct with basic opts --- Cargo.lock | 29 ++++++------ komorebi-bar/src/main.rs | 99 +++++++++++++++++++++++++++++++--------- 2 files changed, 92 insertions(+), 36 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 537b4e381..5f81bcaa2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -805,9 +805,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.5.13" +version = "4.5.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fbb260a053428790f3de475e304ff84cdbc4face759ea7a3e64c1edd938a7fc" +checksum = "64acc1846d54c1fe936a78dc189c34e28d3f5afc348403f28ecf53660b9b8462" dependencies = [ "clap_builder", "clap_derive", @@ -815,9 +815,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.13" +version = "4.5.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "64b17d7ea74e9f833c7dbf2cbe4fb12ff26783eda4782a8975b72f895c9b4d99" +checksum = "6fb8393d67ba2e7bfaf28a23458e4e2b543cc73a99595511eb207fdb8aede942" dependencies = [ "anstream", "anstyle", @@ -828,9 +828,9 @@ dependencies = [ [[package]] name = "clap_derive" -version = "4.5.13" +version = "4.5.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "501d359d5f3dcaf6ecdeee48833ae73ec6e42723a1e52419c79abf9507eec0a0" +checksum = "2bac35c6dafb060fd4d275d9a4ffae97917c13a6327903a8be2153cd964f7085" dependencies = [ "heck", "proc-macro2", @@ -1173,9 +1173,9 @@ checksum = "75b325c5dbd37f80359721ad39aca5a29fb04c89279657cffdda8736d0c0b9d2" [[package]] name = "dunce" -version = "1.0.5" +version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "92773504d58c093f6de2459af4af33faa518c13451eb8f2b5698ed3d36e7c813" +checksum = "56ce8c6da7551ec6c462cbaf3bfbc75131ebbfa1c944aeaa9dab51ca1c5f0c3b" [[package]] name = "dyn-clone" @@ -3554,9 +3554,9 @@ dependencies = [ [[package]] name = "regex" -version = "1.10.6" +version = "1.10.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4219d74c6b67a3654a9fbebc4b419e22126d13d2f3c4a07ee0cb61ff79a79619" +checksum = "b91213439dad192326a0d7c6ee3955910425f441d7038e0d6933b0aec5c4517f" dependencies = [ "aho-corasick", "memchr", @@ -3875,12 +3875,11 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.122" +version = "1.0.120" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "784b6203951c57ff748476b126ccb5e8e2959a5c19e5c617ab1956be3dbc68da" +checksum = "4e0d21c9a8cae1235ad58a00c11cb40d4b1e5c784f1ef2c537876ed6ffd8b7c5" dependencies = [ "itoa", - "memchr", "ryu", "serde", ] @@ -5081,9 +5080,9 @@ dependencies = [ [[package]] name = "which" -version = "6.0.2" +version = "6.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3d9c5ed668ee1f17edb3b627225343d210006a90bb1e3745ce1f30b1fb115075" +checksum = "8211e4f58a2b2805adfbefbc07bab82958fc91e3836339b1ab7ae32465dce0d7" dependencies = [ "either", "home", diff --git a/komorebi-bar/src/main.rs b/komorebi-bar/src/main.rs index fac6713bb..bacdea129 100644 --- a/komorebi-bar/src/main.rs +++ b/komorebi-bar/src/main.rs @@ -15,44 +15,93 @@ use eframe::egui::CursorIcon; use eframe::egui::Layout; use eframe::egui::ViewportBuilder; use eframe::egui::Visuals; +use eframe::emath::Pos2; +use eframe::emath::Vec2; use komorebi_client::SocketMessage; use std::io::BufRead; use std::io::BufReader; use std::process::Command; +use std::sync::Arc; use std::time::Duration; use time::Time; +#[derive(Copy, Clone, Debug)] +pub struct Position { + x: f32, + y: f32, +} + +impl Into for Position { + fn into(self) -> Vec2 { + Vec2 { + x: self.x, + y: self.y, + } + } +} + +impl Into for Position { + fn into(self) -> Pos2 { + Pos2 { + x: self.x, + y: self.y, + } + } +} + +#[derive(Copy, Clone, Debug)] +pub struct Config { + inner_size: Position, + position: Position, + outer_margin: Position, + transparent: bool, + monitor_index: usize, + monitor_work_area_offset: Option, +} + fn main() -> eframe::Result<()> { + let config = Config { + inner_size: Position { x: 5120.0, y: 20.0 }, + position: Position { x: 0.0, y: 0.0 }, + outer_margin: Position { x: 10.0, y: 10.0 }, + transparent: false, + monitor_index: 0, + monitor_work_area_offset: Some(komorebi_client::Rect { + left: 0, + top: 40, + right: 0, + bottom: 40, + }), + }; + + // TODO: ensure that config.monitor_index represents a valid komorebi monitor index + let native_options = eframe::NativeOptions { viewport: ViewportBuilder::default() .with_decorations(false) - // TODO: expose via config - .with_transparent(true) - // TODO: expose via config - .with_position([0.0, 0.0]) - // TODO: expose via config - .with_inner_size([5120.0, 20.0]), + .with_transparent(config.transparent) + .with_position(config.position) + .with_inner_size(config.inner_size), ..Default::default() }; - komorebi_client::send_message(&SocketMessage::MonitorWorkAreaOffset( - 0, - komorebi_client::Rect { - left: 0, - top: 40, - right: 0, - bottom: 40, - }, - )) - .unwrap(); + if let Some(rect) = &config.monitor_work_area_offset { + komorebi_client::send_message(&SocketMessage::MonitorWorkAreaOffset( + config.monitor_index, + *rect, + )) + .unwrap(); + } let (tx_gui, rx_gui) = crossbeam_channel::unbounded(); + let config_arc = Arc::new(config); eframe::run_native( "komorebi-bar", native_options, Box::new(|cc| { let frame = cc.egui_ctx.clone(); + std::thread::spawn(move || { let listener = komorebi_client::subscribe("komorebi-bar").unwrap(); @@ -87,12 +136,13 @@ fn main() -> eframe::Result<()> { } }); - Ok(Box::new(Komobar::new(cc, rx_gui))) + Ok(Box::new(Komobar::new(cc, rx_gui, config_arc))) }), ) } struct Komobar { + config: Config, state_receiver: Receiver, selected_workspace: String, workspaces: Vec, @@ -103,13 +153,18 @@ struct Komobar { } impl Komobar { - fn new(_cc: &eframe::CreationContext<'_>, rx: Receiver) -> Self { + fn new( + _cc: &eframe::CreationContext<'_>, + rx: Receiver, + config: Arc, + ) -> Self { // Customize egui here with cc.egui_ctx.set_fonts and cc.egui_ctx.set_visuals. // Restore app state using cc.storage (requires the "persistence" feature). // Use the cc.gl (a glow::Context) to create graphics shaders and buffers that you can use // for e.g. egui::PaintCallback. Self { + config: *config.as_ref(), state_receiver: rx, selected_workspace: String::new(), workspaces: vec![], @@ -126,8 +181,7 @@ impl Komobar { if let Ok(notification) = self.state_receiver.try_recv() { self.workspaces = { let mut workspaces = vec![]; - // TODO: komobar only operates on the 0th monitor (for now) - let monitor = ¬ification.state.monitors.elements()[0]; + 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() @@ -159,7 +213,10 @@ impl eframe::App for Komobar { .frame( egui::Frame::none() // TODO: make this configurable - .outer_margin(egui::Margin::symmetric(10.0, 10.0)), + .outer_margin(egui::Margin::symmetric( + self.config.outer_margin.x, + self.config.outer_margin.y, + )), ) .show(ctx, |ui| { ui.horizontal(|ui| {