From ec5f7dc82db1f52f8c401586e3232860d76b536c Mon Sep 17 00:00:00 2001 From: Csaba Date: Sat, 9 Nov 2024 01:02:44 +0100 Subject: [PATCH] handling grouping edge case for komorebi focus window --- komorebi-bar/src/komorebi.rs | 192 ++++++++++++++++++----------------- 1 file changed, 99 insertions(+), 93 deletions(-) diff --git a/komorebi-bar/src/komorebi.rs b/komorebi-bar/src/komorebi.rs index adcfcc08..4b8235ca 100644 --- a/komorebi-bar/src/komorebi.rs +++ b/komorebi-bar/src/komorebi.rs @@ -129,6 +129,7 @@ impl BarWidget for Komorebi { if self.workspaces.enable { let mut update = None; + // NOTE: There should always be at least one workspace. config.grouping.apply_on_widget(ui, |ui| { for (i, (ws, should_show)) in komorebi_notification_state.workspaces.iter().enumerate() @@ -308,115 +309,120 @@ impl BarWidget for Komorebi { if let Some(focused_window) = self.focused_window { if focused_window.enable { - config.grouping.apply_on_widget(ui, |ui| { - let titles = &komorebi_notification_state.focused_container_information.0; - let icons = &komorebi_notification_state.focused_container_information.1; - let focused_window_idx = - komorebi_notification_state.focused_container_information.2; + let titles = &komorebi_notification_state.focused_container_information.0; - let iter = titles.iter().zip(icons.iter()); + if !titles.is_empty() { + config.grouping.apply_on_widget(ui, |ui| { + let icons = &komorebi_notification_state.focused_container_information.1; + let focused_window_idx = + komorebi_notification_state.focused_container_information.2; - for (i, (title, icon)) in iter.enumerate() { - if focused_window.show_icon { - if let Some(img) = icon { - ui.add( - Image::from(&img_to_texture(ctx, img)) - .maintain_aspect_ratio(true) - .max_height(15.0), - ); + let iter = titles.iter().zip(icons.iter()); + + for (i, (title, icon)) in iter.enumerate() { + if focused_window.show_icon { + if let Some(img) = icon { + ui.add( + Image::from(&img_to_texture(ctx, img)) + .maintain_aspect_ratio(true) + .max_height(15.0), + ); + } } - } - if i == focused_window_idx { - let font_id = ctx - .style() - .text_styles - .get(&TextStyle::Body) - .cloned() - .unwrap_or_else(FontId::default); + if i == focused_window_idx { + let font_id = ctx + .style() + .text_styles + .get(&TextStyle::Body) + .cloned() + .unwrap_or_else(FontId::default); - let layout_job = LayoutJob::simple( - title.to_string(), - font_id.clone(), - komorebi_notification_state - .stack_accent - .unwrap_or(ctx.style().visuals.selection.stroke.color), - 100.0, - ); - - if titles.len() > 1 { - let available_height = ui.available_height(); - let mut custom_ui = CustomUi(ui); - custom_ui.add_sized_left_to_right( - Vec2::new( - MAX_LABEL_WIDTH.load(Ordering::SeqCst) as f32, - available_height, - ), - Label::new(layout_job).selectable(false).truncate(), + let layout_job = LayoutJob::simple( + title.to_string(), + font_id.clone(), + komorebi_notification_state + .stack_accent + .unwrap_or(ctx.style().visuals.selection.stroke.color), + 100.0, ); + + if titles.len() > 1 { + let available_height = ui.available_height(); + let mut custom_ui = CustomUi(ui); + custom_ui.add_sized_left_to_right( + Vec2::new( + MAX_LABEL_WIDTH.load(Ordering::SeqCst) as f32, + available_height, + ), + Label::new(layout_job).selectable(false).truncate(), + ); + } else { + let available_height = ui.available_height(); + let mut custom_ui = CustomUi(ui); + custom_ui.add_sized_left_to_right( + Vec2::new( + MAX_LABEL_WIDTH.load(Ordering::SeqCst) as f32, + available_height, + ), + Label::new(title).selectable(false).truncate(), + ); + } } else { let available_height = ui.available_height(); let mut custom_ui = CustomUi(ui); - custom_ui.add_sized_left_to_right( - Vec2::new( - MAX_LABEL_WIDTH.load(Ordering::SeqCst) as f32, - available_height, - ), - Label::new(title).selectable(false).truncate(), - ); - } - } else { - let available_height = ui.available_height(); - let mut custom_ui = CustomUi(ui); - if custom_ui - .add_sized_left_to_right( - Vec2::new( - MAX_LABEL_WIDTH.load(Ordering::SeqCst) as f32, - available_height, - ), - Label::new(title) - .selectable(false) - .sense(Sense::click()) - .truncate(), - ) - .clicked() - { - if komorebi_client::send_message(&SocketMessage::MouseFollowsFocus( - false, - )) - .is_err() + if custom_ui + .add_sized_left_to_right( + Vec2::new( + MAX_LABEL_WIDTH.load(Ordering::SeqCst) as f32, + available_height, + ), + Label::new(title) + .selectable(false) + .sense(Sense::click()) + .truncate(), + ) + .clicked() { - tracing::error!( - "could not send message to komorebi: MouseFollowsFocus" - ); - } + if komorebi_client::send_message( + &SocketMessage::MouseFollowsFocus(false), + ) + .is_err() + { + tracing::error!( + "could not send message to komorebi: MouseFollowsFocus" + ); + } - if komorebi_client::send_message(&SocketMessage::FocusStackWindow( - i, - )) - .is_err() - { - tracing::error!( - "could not send message to komorebi: FocusStackWindow" - ); - } + if komorebi_client::send_message( + &SocketMessage::FocusStackWindow(i), + ) + .is_err() + { + tracing::error!( + "could not send message to komorebi: FocusStackWindow" + ); + } - if komorebi_client::send_message(&SocketMessage::MouseFollowsFocus( - komorebi_notification_state.mouse_follows_focus, - )) - .is_err() - { - tracing::error!( - "could not send message to komorebi: MouseFollowsFocus" - ); + if komorebi_client::send_message( + &SocketMessage::MouseFollowsFocus( + komorebi_notification_state.mouse_follows_focus, + ), + ) + .is_err() + { + tracing::error!( + "could not send message to komorebi: MouseFollowsFocus" + ); + } } } + + ui.add_space(WIDGET_SPACING); } - - ui.add_space(WIDGET_SPACING); - } - }); + }); + } } ui.add_space(WIDGET_SPACING);