From 46b81e4372cacdf5accdbe732776f234d98d0d08 Mon Sep 17 00:00:00 2001 From: CtByte <165908630+CtByte@users.noreply.github.com> Date: Wed, 27 Nov 2024 05:14:01 +0100 Subject: [PATCH] feat(bar): floating center area for widgets * Added a new floating area at the center of the bar * Optional center widgets config, fixed spacing on the center widget * Turning transparency on by default --- komorebi-bar/src/bar.rs | 48 +++++++++++++++++++++++++++++++++++++- komorebi-bar/src/config.rs | 2 ++ komorebi-bar/src/main.rs | 2 +- komorebi-bar/src/render.rs | 2 ++ 4 files changed, 52 insertions(+), 2 deletions(-) diff --git a/komorebi-bar/src/bar.rs b/komorebi-bar/src/bar.rs index 07eb72e4d..0f70d63ba 100644 --- a/komorebi-bar/src/bar.rs +++ b/komorebi-bar/src/bar.rs @@ -18,6 +18,8 @@ use crate::MONITOR_RIGHT; use crate::MONITOR_TOP; use crossbeam_channel::Receiver; use eframe::egui::Align; +use eframe::egui::Align2; +use eframe::egui::Area; use eframe::egui::CentralPanel; use eframe::egui::Color32; use eframe::egui::Context; @@ -26,6 +28,7 @@ use eframe::egui::FontDefinitions; use eframe::egui::FontFamily; use eframe::egui::FontId; use eframe::egui::Frame; +use eframe::egui::Id; use eframe::egui::Layout; use eframe::egui::Margin; use eframe::egui::Rgba; @@ -50,6 +53,7 @@ pub struct Komobar { pub render_config: Rc>, pub komorebi_notification_state: Option>>, pub left_widgets: Vec>, + pub center_widgets: Vec>, pub right_widgets: Vec>, pub rx_gui: Receiver, pub rx_config: Receiver, @@ -290,6 +294,16 @@ impl Komobar { } } + if let Some(center_widgets) = &config.center_widgets { + for (idx, widget_config) in center_widgets.iter().enumerate() { + if let WidgetConfig::Komorebi(config) = widget_config { + komorebi_widget = Some(Komorebi::from(config)); + komorebi_widget_idx = Some(idx); + side = Some(Alignment::Center); + } + } + } + for (idx, widget_config) in config.right_widgets.iter().enumerate() { if let WidgetConfig::Komorebi(config) = widget_config { komorebi_widget = Some(Komorebi::from(config)); @@ -304,6 +318,14 @@ impl Komobar { .map(|config| config.as_boxed_bar_widget()) .collect::>>(); + let mut center_widgets = match &config.center_widgets { + Some(center_widgets) => center_widgets + .iter() + .map(|config| config.as_boxed_bar_widget()) + .collect::>>(), + None => vec![], + }; + let mut right_widgets = config .right_widgets .iter() @@ -329,6 +351,7 @@ impl Komobar { let boxed: Box = Box::new(widget); match side { Alignment::Left => left_widgets[idx] = boxed, + Alignment::Center => center_widgets[idx] = boxed, Alignment::Right => right_widgets[idx] = boxed, } } @@ -336,6 +359,7 @@ impl Komobar { right_widgets.reverse(); self.left_widgets = left_widgets; + self.center_widgets = center_widgets; self.right_widgets = right_widgets; tracing::info!("widget configuration options applied"); @@ -354,6 +378,7 @@ impl Komobar { render_config: Rc::new(RefCell::new(RenderConfig::new())), komorebi_notification_state: None, left_widgets: vec![], + center_widgets: vec![], right_widgets: vec![], rx_gui, rx_config, @@ -495,7 +520,27 @@ impl eframe::App for Komobar { w.render(ctx, ui, &mut render_conf); } }); - }) + }); + + if !self.center_widgets.is_empty() { + // Floating center widgets + Area::new(Id::new("center_panel")) + .anchor(Align2::CENTER_CENTER, [0.0, 0.0]) // Align in the center of the window + .show(ctx, |ui| { + Frame::none().show(ui, |ui| { + ui.horizontal_centered(|ui| { + let mut render_conf = *render_config; + render_conf.alignment = Some(Alignment::Center); + + render_config.apply_on_alignment(ui, |ui| { + for w in &mut self.center_widgets { + w.render(ctx, ui, &mut render_conf); + } + }); + }); + }); + }); + } }) }) }); @@ -505,5 +550,6 @@ impl eframe::App for Komobar { #[derive(Copy, Clone)] pub enum Alignment { Left, + Center, Right, } diff --git a/komorebi-bar/src/config.rs b/komorebi-bar/src/config.rs index fe8355b4e..1a6970d57 100644 --- a/komorebi-bar/src/config.rs +++ b/komorebi-bar/src/config.rs @@ -37,6 +37,8 @@ pub struct KomobarConfig { pub grouping: Option, /// Left side widgets (ordered left-to-right) pub left_widgets: Vec, + /// Center widgets (ordered left-to-right) + pub center_widgets: Option>, /// Right side widgets (ordered left-to-right) pub right_widgets: Vec, } diff --git a/komorebi-bar/src/main.rs b/komorebi-bar/src/main.rs index 771589c13..242af9bea 100644 --- a/komorebi-bar/src/main.rs +++ b/komorebi-bar/src/main.rs @@ -269,7 +269,7 @@ fn main() -> color_eyre::Result<()> { let viewport_builder = ViewportBuilder::default() .with_decorations(false) - .with_transparent(config.transparency_alpha.is_some()) + .with_transparent(true) .with_taskbar(false); let native_options = eframe::NativeOptions { diff --git a/komorebi-bar/src/render.rs b/komorebi-bar/src/render.rs index 0cfe56411..c8d8c4076 100644 --- a/komorebi-bar/src/render.rs +++ b/komorebi-bar/src/render.rs @@ -190,6 +190,7 @@ impl RenderConfig { left: match self.alignment { Some(align) => match align { Alignment::Left => spacing, + Alignment::Center => spacing, Alignment::Right => 0.0, }, None => 0.0, @@ -197,6 +198,7 @@ impl RenderConfig { right: match self.alignment { Some(align) => match align { Alignment::Left => 0.0, + Alignment::Center => 0.0, Alignment::Right => spacing, }, None => 0.0,