Skip to content

Commit

Permalink
feat(bar): floating center area for widgets
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
CtByte authored Nov 27, 2024
1 parent 6f00c52 commit 46b81e4
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
48 changes: 47 additions & 1 deletion komorebi-bar/src/bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -50,6 +53,7 @@ pub struct Komobar {
pub render_config: Rc<RefCell<RenderConfig>>,
pub komorebi_notification_state: Option<Rc<RefCell<KomorebiNotificationState>>>,
pub left_widgets: Vec<Box<dyn BarWidget>>,
pub center_widgets: Vec<Box<dyn BarWidget>>,
pub right_widgets: Vec<Box<dyn BarWidget>>,
pub rx_gui: Receiver<komorebi_client::Notification>,
pub rx_config: Receiver<KomobarConfig>,
Expand Down Expand Up @@ -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));
Expand All @@ -304,6 +318,14 @@ impl Komobar {
.map(|config| config.as_boxed_bar_widget())
.collect::<Vec<Box<dyn BarWidget>>>();

let mut center_widgets = match &config.center_widgets {
Some(center_widgets) => center_widgets
.iter()
.map(|config| config.as_boxed_bar_widget())
.collect::<Vec<Box<dyn BarWidget>>>(),
None => vec![],
};

let mut right_widgets = config
.right_widgets
.iter()
Expand All @@ -329,13 +351,15 @@ impl Komobar {
let boxed: Box<dyn BarWidget> = Box::new(widget);
match side {
Alignment::Left => left_widgets[idx] = boxed,
Alignment::Center => center_widgets[idx] = boxed,
Alignment::Right => right_widgets[idx] = boxed,
}
}

right_widgets.reverse();

self.left_widgets = left_widgets;
self.center_widgets = center_widgets;
self.right_widgets = right_widgets;

tracing::info!("widget configuration options applied");
Expand All @@ -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,
Expand Down Expand Up @@ -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);
}
});
});
});
});
}
})
})
});
Expand All @@ -505,5 +550,6 @@ impl eframe::App for Komobar {
#[derive(Copy, Clone)]
pub enum Alignment {
Left,
Center,
Right,
}
2 changes: 2 additions & 0 deletions komorebi-bar/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ pub struct KomobarConfig {
pub grouping: Option<Grouping>,
/// Left side widgets (ordered left-to-right)
pub left_widgets: Vec<WidgetConfig>,
/// Center widgets (ordered left-to-right)
pub center_widgets: Option<Vec<WidgetConfig>>,
/// Right side widgets (ordered left-to-right)
pub right_widgets: Vec<WidgetConfig>,
}
Expand Down
2 changes: 1 addition & 1 deletion komorebi-bar/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions komorebi-bar/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,15 @@ impl RenderConfig {
left: match self.alignment {
Some(align) => match align {
Alignment::Left => spacing,
Alignment::Center => spacing,
Alignment::Right => 0.0,
},
None => 0.0,
},
right: match self.alignment {
Some(align) => match align {
Alignment::Left => 0.0,
Alignment::Center => 0.0,
Alignment::Right => spacing,
},
None => 0.0,
Expand Down

0 comments on commit 46b81e4

Please sign in to comment.