Skip to content

Commit

Permalink
feat(bar): add logging and config hotwatch
Browse files Browse the repository at this point in the history
  • Loading branch information
LGUG2Z committed Sep 9, 2024
1 parent e99138a commit d6ccf4c
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 39 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion komorebi-bar/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ komorebi-client = { path = "../komorebi-client" }

catppuccin-egui = { version = "5.1", default-features = false, features = ["egui28"] }
chrono = "0.4"
clap = { version = "4", features = ["derive", "wrap_help"] }
color-eyre = "0.6"
crossbeam-channel = "0.5"
dirs = { workspace = true }
eframe = "0.28"
egui-phosphor = "0.6.0"
font-loader = "0.11"
hotwatch = "0.5"
image = "0.25"
netdev = "0.30"
schemars = { workspace = true }
Expand All @@ -24,6 +26,8 @@ serde_json = { workspace = true }
serde_yaml = "0.8"
starship-battery = "0.10"
sysinfo = "0.31"
tracing = "0.1"
tracing-appender = "0.2"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
windows = { workspace = true }
windows-icons = "0.1"
clap = { version = "4", features = ["derive", "wrap_help"] }
90 changes: 69 additions & 21 deletions komorebi-bar/src/bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,47 +14,56 @@ use eframe::egui::FontFamily;
use eframe::egui::Frame;
use eframe::egui::Layout;
use eframe::egui::Margin;
use eframe::egui::Style;
use font_loader::system_fonts;
use font_loader::system_fonts::FontPropertyBuilder;
use std::cell::RefCell;
use std::ops::Deref;
use std::rc::Rc;
use std::sync::Arc;

pub struct Komobar {
pub config: KomobarConfig,
pub config: Arc<KomobarConfig>,
pub komorebi_notification_state: Option<Rc<RefCell<KomorebiNotificationState>>>,
pub left_widgets: Vec<Box<dyn BarWidget>>,
pub right_widgets: Vec<Box<dyn BarWidget>>,
pub rx_gui: Receiver<komorebi_client::Notification>,
pub rx_config: Receiver<KomobarConfig>,
}

impl Komobar {
pub fn new(
cc: &eframe::CreationContext<'_>,
rx_gui: Receiver<komorebi_client::Notification>,
config: Arc<KomobarConfig>,
) -> Self {
pub fn apply_config(
&mut self,
ctx: &Context,
config: &KomobarConfig,
previous_notification_state: Option<Rc<RefCell<KomorebiNotificationState>>>,
) {
if let Some(font_family) = &config.font_family {
Self::add_custom_font(&cc.egui_ctx, font_family);
tracing::info!("attempting to add custom font family: {font_family}");
Self::add_custom_font(ctx, font_family);
}

match config.theme {
Some(Theme::CatppuccinFrappe) => {
catppuccin_egui::set_theme(&cc.egui_ctx, catppuccin_egui::FRAPPE);
catppuccin_egui::set_theme(ctx, catppuccin_egui::FRAPPE);
tracing::info!("theme updated: Catppuccin Frappe");
}
Some(Theme::CatppuccinMacchiato) => {
catppuccin_egui::set_theme(&cc.egui_ctx, catppuccin_egui::MACCHIATO);
catppuccin_egui::set_theme(ctx, catppuccin_egui::MACCHIATO);
tracing::info!("theme updated: Catppuccin Macchiato");
}
Some(Theme::CatppuccinMocha) => {
catppuccin_egui::set_theme(&cc.egui_ctx, catppuccin_egui::MOCHA);
catppuccin_egui::set_theme(ctx, catppuccin_egui::MOCHA);
tracing::info!("theme updated: Catppuccin Mocha");
}
Some(Theme::Default) | None => {
ctx.set_style(Style::default());
tracing::info!("theme updated: Egui Default");
}
Some(Theme::Default) | None => {}
}

let mut komorebi_widget = None;
let mut komorebi_widget_idx = None;
let mut komorebi_notification_state = None;
let mut komorebi_notification_state = previous_notification_state;
let mut side = None;

for (idx, widget_config) in config.left_widgets.iter().enumerate() {
Expand Down Expand Up @@ -85,9 +94,22 @@ impl Komobar {
.map(|config| config.as_boxed_bar_widget())
.collect::<Vec<Box<dyn BarWidget>>>();

if let (Some(idx), Some(widget), Some(side)) = (komorebi_widget_idx, komorebi_widget, side)
if let (Some(idx), Some(mut widget), Some(side)) =
(komorebi_widget_idx, komorebi_widget, side)
{
komorebi_notification_state = Some(widget.komorebi_notification_state.clone());
match komorebi_notification_state {
None => {
komorebi_notification_state = Some(widget.komorebi_notification_state.clone());
}
Some(ref previous) => {
previous
.borrow_mut()
.update_from_config(&widget.komorebi_notification_state.borrow());

widget.komorebi_notification_state = previous.clone();
}
}

let boxed: Box<dyn BarWidget> = Box::new(widget);
match side {
Side::Left => left_widgets[idx] = boxed,
Expand All @@ -97,13 +119,31 @@ impl Komobar {

right_widgets.reverse();

Self {
config: config.deref().clone(),
komorebi_notification_state,
left_widgets,
right_widgets,
self.left_widgets = left_widgets;
self.right_widgets = right_widgets;

tracing::info!("widget configuration options applied");

self.komorebi_notification_state = komorebi_notification_state;
}
pub fn new(
cc: &eframe::CreationContext<'_>,
rx_gui: Receiver<komorebi_client::Notification>,
rx_config: Receiver<KomobarConfig>,
config: Arc<KomobarConfig>,
) -> Self {
let mut komobar = Self {
config: config.clone(),
komorebi_notification_state: None,
left_widgets: vec![],
right_widgets: vec![],
rx_gui,
}
rx_config,
};

komobar.apply_config(&cc.egui_ctx, &config, None);

komobar
}

fn add_custom_font(ctx: &Context, name: &str) {
Expand Down Expand Up @@ -144,6 +184,14 @@ impl eframe::App for Komobar {
// }

fn update(&mut self, ctx: &Context, _frame: &mut eframe::Frame) {
if let Ok(updated_config) = self.rx_config.try_recv() {
self.apply_config(
ctx,
&updated_config,
self.komorebi_notification_state.clone(),
);
}

if let Some(komorebi_notification_state) = &self.komorebi_notification_state {
komorebi_notification_state
.borrow_mut()
Expand Down
4 changes: 4 additions & 0 deletions komorebi-bar/src/komorebi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ pub struct KomorebiNotificationState {
}

impl KomorebiNotificationState {
pub fn update_from_config(&mut self, config: &Self) {
self.hide_empty_workspaces = config.hide_empty_workspaces;
}

pub fn handle_notification(
&mut self,
monitor_index: usize,
Expand Down
Loading

0 comments on commit d6ccf4c

Please sign in to comment.