diff --git a/komorebi-bar/src/bar.rs b/komorebi-bar/src/bar.rs index b62ff072..079aa59f 100644 --- a/komorebi-bar/src/bar.rs +++ b/komorebi-bar/src/bar.rs @@ -347,13 +347,7 @@ impl Komobar { ) -> Self { let mut komobar = Self { config: config.clone(), - render_config: Rc::new(RefCell::new(RenderConfig { - spacing: 0.0, - grouping: Grouping::None, - background_color: Color32::BLACK, - alignment: None, - no_spacing: false, - })), + render_config: Rc::new(RefCell::new(RenderConfig::new())), komorebi_notification_state: None, left_widgets: vec![], right_widgets: vec![], @@ -486,7 +480,7 @@ impl eframe::App for Komobar { w.render(ctx, ui, render_conf); } - render_conf.no_spacing = true; + render_conf.is_last_in_alignment = true; last.render(ctx, ui, render_conf); } }); @@ -503,7 +497,7 @@ impl eframe::App for Komobar { w.render(ctx, ui, render_conf); } - render_conf.no_spacing = true; + render_conf.is_last_in_alignment = true; last.render(ctx, ui, render_conf); } }); diff --git a/komorebi-bar/src/komorebi.rs b/komorebi-bar/src/komorebi.rs index 34b5324b..fd42f447 100644 --- a/komorebi-bar/src/komorebi.rs +++ b/komorebi-bar/src/komorebi.rs @@ -124,30 +124,30 @@ pub struct Komorebi { impl BarWidget for Komorebi { fn render(&mut self, ctx: &Context, ui: &mut Ui, mut config: RenderConfig) { let mut komorebi_notification_state = self.komorebi_notification_state.borrow_mut(); - let mut enable_widget: [bool; 4] = [self.workspaces.enable, false, false, false]; + let mut add_to_ui: [bool; 4] = [self.workspaces.enable, false, false, false]; if let Some(layout) = self.layout { - enable_widget[1] = layout.enable; + add_to_ui[1] = layout.enable; } if let Some(configuration_switcher) = &self.configuration_switcher { - enable_widget[2] = configuration_switcher.enable; + add_to_ui[2] = configuration_switcher.enable; } if let Some(focused_window) = self.focused_window { if focused_window.enable { let titles = &komorebi_notification_state.focused_container_information.0; - enable_widget[3] = !titles.is_empty(); + add_to_ui[3] = !titles.is_empty(); } } - let last_enabled_widget_index = enable_widget.iter().rposition(|&x| x); + let last_add_to_ui = add_to_ui.iter().rposition(|&x| x); - if enable_widget[0] { + if add_to_ui[0] { let mut update = None; // NOTE: There should always be at least one workspace. - config.apply_on_widget(false, last_enabled_widget_index == Some(0), ui, |ui| { + config.apply_on_widget(false, last_add_to_ui == Some(0), ui, |ui| { for (i, (ws, should_show)) in komorebi_notification_state.workspaces.iter().enumerate() { @@ -212,8 +212,8 @@ impl BarWidget for Komorebi { } } - if enable_widget[1] { - config.apply_on_widget(true, last_enabled_widget_index == Some(1), ui, |ui| { + if add_to_ui[1] { + config.apply_on_widget(true, last_add_to_ui == Some(1), ui, |ui| { if ui .add( Label::new(komorebi_notification_state.layout.to_string()) @@ -249,12 +249,12 @@ impl BarWidget for Komorebi { }); } - if enable_widget[2] { + if add_to_ui[2] { let configuration_switcher = self.configuration_switcher.as_ref().unwrap(); for (name, location) in configuration_switcher.configurations.iter() { let path = PathBuf::from(location); if path.is_file() { - config.apply_on_widget(true, last_enabled_widget_index == Some(2), ui,|ui|{ + config.apply_on_widget(true, last_add_to_ui == Some(2), ui,|ui|{ if ui .add(Label::new(name).selectable(false).sense(Sense::click())) .clicked() @@ -306,10 +306,10 @@ impl BarWidget for Komorebi { } } - if enable_widget[3] { + if add_to_ui[3] { let focused_window = self.focused_window.unwrap(); let titles = &komorebi_notification_state.focused_container_information.0; - config.apply_on_widget(true, last_enabled_widget_index == Some(4), ui, |ui| { + config.apply_on_widget(true, last_add_to_ui == Some(3), ui, |ui| { let icons = &komorebi_notification_state.focused_container_information.1; let focused_window_idx = komorebi_notification_state.focused_container_information.2; diff --git a/komorebi-bar/src/network.rs b/komorebi-bar/src/network.rs index 991ab2eb..e70ce2b1 100644 --- a/komorebi-bar/src/network.rs +++ b/komorebi-bar/src/network.rs @@ -318,7 +318,7 @@ impl Network { impl BarWidget for Network { fn render(&mut self, ctx: &Context, ui: &mut Ui, mut config: RenderConfig) { - let mut enable_widget: [bool; 3] = [ + let mut add_to_ui: [bool; 3] = [ self.show_total_data_transmitted, self.show_network_activity, false, @@ -328,29 +328,29 @@ impl BarWidget for Network { self.default_interface(); if !self.default_interface.is_empty() { - enable_widget[2] = true; + add_to_ui[2] = true; } } - let last_enabled_widget_index = enable_widget.iter().rposition(|&x| x); + let last_add_to_ui = add_to_ui.iter().rposition(|&x| x); - if enable_widget[0] { + if add_to_ui[0] { for output in self.total_data_transmitted() { - config.apply_on_widget(true, last_enabled_widget_index == Some(0), ui, |ui| { + config.apply_on_widget(true, last_add_to_ui == Some(0), ui, |ui| { ui.add(Label::new(output).selectable(false)); }); } } - if enable_widget[1] { + if add_to_ui[1] { for output in self.network_activity() { - config.apply_on_widget(true, last_enabled_widget_index == Some(1), ui, |ui| { + config.apply_on_widget(true, last_add_to_ui == Some(1), ui, |ui| { ui.add(Label::new(output).selectable(false)); }); } } - if enable_widget[2] { + if add_to_ui[2] { let font_id = ctx .style() .text_styles @@ -380,7 +380,7 @@ impl BarWidget for Network { TextFormat::simple(font_id, ctx.style().visuals.text_color()), ); - config.apply_on_widget(true, last_enabled_widget_index == Some(2), ui, |ui| { + config.apply_on_widget(true, last_add_to_ui == Some(2), ui, |ui| { if ui .add( Label::new(layout_job) diff --git a/komorebi-bar/src/render.rs b/komorebi-bar/src/render.rs index c2608351..5ae0e2a7 100644 --- a/komorebi-bar/src/render.rs +++ b/komorebi-bar/src/render.rs @@ -35,7 +35,11 @@ pub struct RenderConfig { pub background_color: Color32, /// Alignment of the widgets pub alignment: Option, - /// Remove spacing if true + /// True if this config belongs to the last widget in the alignment + pub is_last_in_alignment: bool, + /// Add more inner margin when adding a widget group + pub more_inner_margin: bool, + /// Add no widget spacing pub no_spacing: bool, } @@ -50,12 +54,26 @@ impl RenderExt for &KomobarConfig { grouping: self.grouping.unwrap_or(Grouping::None), background_color, alignment: None, + is_last_in_alignment: false, + more_inner_margin: false, no_spacing: false, } } } impl RenderConfig { + pub fn new() -> Self { + Self { + spacing: 0.0, + grouping: Grouping::None, + background_color: Color32::BLACK, + alignment: None, + is_last_in_alignment: false, + more_inner_margin: false, + no_spacing: false, + } + } + pub fn apply_on_bar( &mut self, ui: &mut Ui, @@ -64,7 +82,7 @@ impl RenderConfig { self.alignment = None; if let Grouping::Bar(config) = self.grouping { - return self.define_group(false, config, ui, add_contents); + return self.define_group(config, ui, add_contents); } Self::fallback_group(ui, add_contents) @@ -78,7 +96,7 @@ impl RenderConfig { self.alignment = None; if let Grouping::Alignment(config) = self.grouping { - return self.define_group(false, config, ui, add_contents); + return self.define_group(config, ui, add_contents); } Self::fallback_group(ui, add_contents) @@ -87,19 +105,20 @@ impl RenderConfig { pub fn apply_on_widget( &mut self, more_inner_margin: bool, - is_last_widget: bool, + is_last_add_to_ui: bool, ui: &mut Ui, add_contents: impl FnOnce(&mut Ui) -> R, ) -> InnerResponse { // since a widget can call this multiple times, it is necessary to know the last time // in order to add widget spacing correctly - self.no_spacing = self.no_spacing && is_last_widget; + self.more_inner_margin = more_inner_margin; + self.no_spacing = self.is_last_in_alignment && is_last_add_to_ui; if let Grouping::Widget(config) = self.grouping { - return self.define_group(more_inner_margin, config, ui, add_contents); + return self.define_group(config, ui, add_contents); } - self.fallback_widget_group(more_inner_margin, ui, add_contents) + self.fallback_widget_group(ui, add_contents) } fn fallback_group(ui: &mut Ui, add_contents: impl FnOnce(&mut Ui) -> R) -> InnerResponse { @@ -111,13 +130,12 @@ impl RenderConfig { fn fallback_widget_group( &mut self, - more_inner_margin: bool, ui: &mut Ui, add_contents: impl FnOnce(&mut Ui) -> R, ) -> InnerResponse { Frame::none() .outer_margin(self.widget_outer_margin()) - .inner_margin(match more_inner_margin { + .inner_margin(match self.more_inner_margin { true => Margin::symmetric(5.0, 0.0), false => Margin::same(0.0), }) @@ -126,14 +144,13 @@ impl RenderConfig { fn define_group( &mut self, - more_inner_margin: bool, config: GroupingConfig, ui: &mut Ui, add_contents: impl FnOnce(&mut Ui) -> R, ) -> InnerResponse { Frame::none() .outer_margin(self.widget_outer_margin()) - .inner_margin(match more_inner_margin { + .inner_margin(match self.more_inner_margin { true => Margin::symmetric(8.0, 3.0), false => Margin::symmetric(3.0, 3.0), }) diff --git a/komorebi-bar/src/storage.rs b/komorebi-bar/src/storage.rs index 045f8832..3fac40c8 100644 --- a/komorebi-bar/src/storage.rs +++ b/komorebi-bar/src/storage.rs @@ -88,7 +88,9 @@ impl BarWidget for Storage { .cloned() .unwrap_or_else(FontId::default); - for output in self.output() { + let last_index = self.output().len() - 1; + + for (index, output) in self.output().iter().enumerate() { let mut layout_job = LayoutJob::simple( match self.label_prefix { LabelPrefix::Icon | LabelPrefix::IconAndText => { @@ -102,13 +104,12 @@ impl BarWidget for Storage { ); layout_job.append( - &output, + output, 10.0, TextFormat::simple(font_id.clone(), ctx.style().visuals.text_color()), ); - // TODO: WIP - config.apply_on_widget(true, false, ui, |ui| { + config.apply_on_widget(true, index == last_index, ui, |ui| { if ui .add( Label::new(layout_job)