correct spacing on komorebi and network widgets (WIP)

This commit is contained in:
Csaba
2024-11-14 23:17:39 +01:00
parent 9311cb00ec
commit 36c267246b
10 changed files with 242 additions and 230 deletions
+1 -1
View File
@@ -147,7 +147,7 @@ impl BarWidget for Battery {
TextFormat::simple(font_id, ctx.style().visuals.text_color()), TextFormat::simple(font_id, ctx.style().visuals.text_color()),
); );
config.apply_on_widget(true, ui, |ui| { config.apply_on_widget(true, true, ui, |ui| {
ui.add( ui.add(
Label::new(layout_job) Label::new(layout_job)
.selectable(false) .selectable(false)
+1 -1
View File
@@ -99,7 +99,7 @@ impl BarWidget for Cpu {
TextFormat::simple(font_id, ctx.style().visuals.text_color()), TextFormat::simple(font_id, ctx.style().visuals.text_color()),
); );
config.apply_on_widget(true, ui, |ui| { config.apply_on_widget(true, true, ui, |ui| {
if ui if ui
.add( .add(
Label::new(layout_job) Label::new(layout_job)
+1 -1
View File
@@ -119,7 +119,7 @@ impl BarWidget for Date {
TextFormat::simple(font_id, ctx.style().visuals.text_color()), TextFormat::simple(font_id, ctx.style().visuals.text_color()),
); );
config.apply_on_widget(true, ui, |ui| { config.apply_on_widget(true, true, ui, |ui| {
if ui if ui
.add( .add(
Label::new(WidgetText::LayoutJob(layout_job.clone())) Label::new(WidgetText::LayoutJob(layout_job.clone()))
+39 -41
View File
@@ -124,12 +124,30 @@ pub struct Komorebi {
impl BarWidget for Komorebi { impl BarWidget for Komorebi {
fn render(&mut self, ctx: &Context, ui: &mut Ui, mut config: RenderConfig) { 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 komorebi_notification_state = self.komorebi_notification_state.borrow_mut();
let mut enable_widget: [bool; 4] = [self.workspaces.enable, false, false, false];
if self.workspaces.enable { if let Some(layout) = self.layout {
enable_widget[1] = layout.enable;
}
if let Some(configuration_switcher) = &self.configuration_switcher {
enable_widget[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();
}
}
let last_enabled_widget_index = enable_widget.iter().rposition(|&x| x);
if enable_widget[0] {
let mut update = None; let mut update = None;
// NOTE: There should always be at least one workspace. // NOTE: There should always be at least one workspace.
config.apply_on_widget(false, ui, |ui| { config.apply_on_widget(false, last_enabled_widget_index == Some(0), ui, |ui| {
for (i, (ws, should_show)) in for (i, (ws, should_show)) in
komorebi_notification_state.workspaces.iter().enumerate() komorebi_notification_state.workspaces.iter().enumerate()
{ {
@@ -194,9 +212,8 @@ impl BarWidget for Komorebi {
} }
} }
if let Some(layout) = self.layout { if enable_widget[1] {
if layout.enable { config.apply_on_widget(true, last_enabled_widget_index == Some(1), ui, |ui| {
config.apply_on_widget(true, ui, |ui| {
if ui if ui
.add( .add(
Label::new(komorebi_notification_state.layout.to_string()) Label::new(komorebi_notification_state.layout.to_string())
@@ -212,27 +229,18 @@ impl BarWidget for Komorebi {
)) ))
.is_err() .is_err()
{ {
tracing::error!( tracing::error!("could not send message to komorebi: CycleLayout");
"could not send message to komorebi: CycleLayout"
);
} }
} }
KomorebiLayout::Floating => { KomorebiLayout::Floating => {
if komorebi_client::send_message(&SocketMessage::ToggleTiling) if komorebi_client::send_message(&SocketMessage::ToggleTiling).is_err()
.is_err()
{ {
tracing::error!( tracing::error!("could not send message to komorebi: ToggleTiling");
"could not send message to komorebi: ToggleTiling"
);
} }
} }
KomorebiLayout::Paused => { KomorebiLayout::Paused => {
if komorebi_client::send_message(&SocketMessage::TogglePause) if komorebi_client::send_message(&SocketMessage::TogglePause).is_err() {
.is_err() tracing::error!("could not send message to komorebi: TogglePause");
{
tracing::error!(
"could not send message to komorebi: TogglePause"
);
} }
} }
KomorebiLayout::Custom => {} KomorebiLayout::Custom => {}
@@ -240,14 +248,13 @@ impl BarWidget for Komorebi {
} }
}); });
} }
}
if let Some(configuration_switcher) = &self.configuration_switcher { if enable_widget[2] {
if configuration_switcher.enable { let configuration_switcher = self.configuration_switcher.as_ref().unwrap();
for (name, location) in configuration_switcher.configurations.iter() { for (name, location) in configuration_switcher.configurations.iter() {
let path = PathBuf::from(location); let path = PathBuf::from(location);
if path.is_file() { if path.is_file() {
config.apply_on_widget(true, ui,|ui|{ config.apply_on_widget(true, last_enabled_widget_index == Some(2), ui,|ui|{
if ui if ui
.add(Label::new(name).selectable(false).sense(Sense::click())) .add(Label::new(name).selectable(false).sense(Sense::click()))
.clicked() .clicked()
@@ -298,14 +305,11 @@ impl BarWidget for Komorebi {
} }
} }
} }
}
if let Some(focused_window) = self.focused_window { if enable_widget[3] {
if focused_window.enable { let focused_window = self.focused_window.unwrap();
let titles = &komorebi_notification_state.focused_container_information.0; let titles = &komorebi_notification_state.focused_container_information.0;
config.apply_on_widget(true, last_enabled_widget_index == Some(4), ui, |ui| {
if !titles.is_empty() {
config.apply_on_widget(true, ui, |ui| {
let icons = &komorebi_notification_state.focused_container_information.1; let icons = &komorebi_notification_state.focused_container_information.1;
let focused_window_idx = let focused_window_idx =
komorebi_notification_state.focused_container_information.2; komorebi_notification_state.focused_container_information.2;
@@ -378,9 +382,9 @@ impl BarWidget for Komorebi {
) )
.clicked() .clicked()
{ {
if komorebi_client::send_message( if komorebi_client::send_message(&SocketMessage::MouseFollowsFocus(
&SocketMessage::MouseFollowsFocus(false), false,
) ))
.is_err() .is_err()
{ {
tracing::error!( tracing::error!(
@@ -388,9 +392,7 @@ impl BarWidget for Komorebi {
); );
} }
if komorebi_client::send_message( if komorebi_client::send_message(&SocketMessage::FocusStackWindow(i))
&SocketMessage::FocusStackWindow(i),
)
.is_err() .is_err()
{ {
tracing::error!( tracing::error!(
@@ -398,11 +400,9 @@ impl BarWidget for Komorebi {
); );
} }
if komorebi_client::send_message( if komorebi_client::send_message(&SocketMessage::MouseFollowsFocus(
&SocketMessage::MouseFollowsFocus(
komorebi_notification_state.mouse_follows_focus, komorebi_notification_state.mouse_follows_focus,
), ))
)
.is_err() .is_err()
{ {
tracing::error!( tracing::error!(
@@ -415,8 +415,6 @@ impl BarWidget for Komorebi {
}); });
} }
} }
}
}
} }
fn img_to_texture(ctx: &Context, rgba_image: &RgbaImage) -> TextureHandle { fn img_to_texture(ctx: &Context, rgba_image: &RgbaImage) -> TextureHandle {
+1 -1
View File
@@ -102,7 +102,7 @@ impl BarWidget for Media {
TextFormat::simple(font_id, ctx.style().visuals.text_color()), TextFormat::simple(font_id, ctx.style().visuals.text_color()),
); );
config.apply_on_widget(true, ui, |ui| { config.apply_on_widget(true, true, ui, |ui| {
let available_height = ui.available_height(); let available_height = ui.available_height();
let mut custom_ui = CustomUi(ui); let mut custom_ui = CustomUi(ui);
+1 -1
View File
@@ -102,7 +102,7 @@ impl BarWidget for Memory {
TextFormat::simple(font_id, ctx.style().visuals.text_color()), TextFormat::simple(font_id, ctx.style().visuals.text_color()),
); );
config.apply_on_widget(true, ui, |ui| { config.apply_on_widget(true, true, ui, |ui| {
if ui if ui
.add( .add(
Label::new(layout_job) Label::new(layout_job)
+29 -17
View File
@@ -318,26 +318,39 @@ impl Network {
impl BarWidget for Network { impl BarWidget for Network {
fn render(&mut self, ctx: &Context, ui: &mut Ui, mut config: RenderConfig) { fn render(&mut self, ctx: &Context, ui: &mut Ui, mut config: RenderConfig) {
if self.show_total_data_transmitted { let mut enable_widget: [bool; 3] = [
for output in self.total_data_transmitted() { self.show_total_data_transmitted,
config.apply_on_widget(true, ui, |ui| { self.show_network_activity,
ui.add(Label::new(output).selectable(false)); false,
}); ];
}
}
if self.show_network_activity {
for output in self.network_activity() {
config.apply_on_widget(true, ui, |ui| {
ui.add(Label::new(output).selectable(false));
});
}
}
if self.enable { if self.enable {
self.default_interface(); self.default_interface();
if !self.default_interface.is_empty() { if !self.default_interface.is_empty() {
enable_widget[2] = true;
}
}
let last_enabled_widget_index = enable_widget.iter().rposition(|&x| x);
if enable_widget[0] {
for output in self.total_data_transmitted() {
config.apply_on_widget(true, last_enabled_widget_index == Some(0), ui, |ui| {
ui.add(Label::new(output).selectable(false));
});
}
}
if enable_widget[1] {
for output in self.network_activity() {
config.apply_on_widget(true, last_enabled_widget_index == Some(1), ui, |ui| {
ui.add(Label::new(output).selectable(false));
});
}
}
if enable_widget[2] {
let font_id = ctx let font_id = ctx
.style() .style()
.text_styles .text_styles
@@ -367,7 +380,7 @@ impl BarWidget for Network {
TextFormat::simple(font_id, ctx.style().visuals.text_color()), TextFormat::simple(font_id, ctx.style().visuals.text_color()),
); );
config.apply_on_widget(true, ui, |ui| { config.apply_on_widget(true, last_enabled_widget_index == Some(2), ui, |ui| {
if ui if ui
.add( .add(
Label::new(layout_job) Label::new(layout_job)
@@ -383,7 +396,6 @@ impl BarWidget for Network {
}); });
} }
} }
}
} }
#[derive(Debug, FromPrimitive)] #[derive(Debug, FromPrimitive)]
+12 -11
View File
@@ -86,19 +86,20 @@ impl RenderConfig {
pub fn apply_on_widget<R>( pub fn apply_on_widget<R>(
&mut self, &mut self,
use_spacing: bool, more_inner_margin: bool,
// TODO: this should remove the margin on the last widget on the left side and the first widget on the right side is_last_widget: bool,
// This is complex, since the last/first widget can have multiple "sections", like komorebi, network, ...
// This and the same setting on RenderConfig needs to be combined.
//no_spacing: Option<bool>,
ui: &mut Ui, ui: &mut Ui,
add_contents: impl FnOnce(&mut Ui) -> R, add_contents: impl FnOnce(&mut Ui) -> R,
) -> InnerResponse<R> { ) -> InnerResponse<R> {
// 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;
if let Grouping::Widget(config) = self.grouping { if let Grouping::Widget(config) = self.grouping {
return self.define_group(use_spacing, config, ui, add_contents); return self.define_group(more_inner_margin, config, ui, add_contents);
} }
self.fallback_widget_group(use_spacing, ui, add_contents) self.fallback_widget_group(more_inner_margin, ui, add_contents)
} }
fn fallback_group<R>(ui: &mut Ui, add_contents: impl FnOnce(&mut Ui) -> R) -> InnerResponse<R> { fn fallback_group<R>(ui: &mut Ui, add_contents: impl FnOnce(&mut Ui) -> R) -> InnerResponse<R> {
@@ -110,13 +111,13 @@ impl RenderConfig {
fn fallback_widget_group<R>( fn fallback_widget_group<R>(
&mut self, &mut self,
use_spacing: bool, more_inner_margin: bool,
ui: &mut Ui, ui: &mut Ui,
add_contents: impl FnOnce(&mut Ui) -> R, add_contents: impl FnOnce(&mut Ui) -> R,
) -> InnerResponse<R> { ) -> InnerResponse<R> {
Frame::none() Frame::none()
.outer_margin(self.widget_outer_margin()) .outer_margin(self.widget_outer_margin())
.inner_margin(match use_spacing { .inner_margin(match more_inner_margin {
true => Margin::symmetric(5.0, 0.0), true => Margin::symmetric(5.0, 0.0),
false => Margin::same(0.0), false => Margin::same(0.0),
}) })
@@ -125,14 +126,14 @@ impl RenderConfig {
fn define_group<R>( fn define_group<R>(
&mut self, &mut self,
use_spacing: bool, more_inner_margin: bool,
config: GroupingConfig, config: GroupingConfig,
ui: &mut Ui, ui: &mut Ui,
add_contents: impl FnOnce(&mut Ui) -> R, add_contents: impl FnOnce(&mut Ui) -> R,
) -> InnerResponse<R> { ) -> InnerResponse<R> {
Frame::none() Frame::none()
.outer_margin(self.widget_outer_margin()) .outer_margin(self.widget_outer_margin())
.inner_margin(match use_spacing { .inner_margin(match more_inner_margin {
true => Margin::symmetric(8.0, 3.0), true => Margin::symmetric(8.0, 3.0),
false => Margin::symmetric(3.0, 3.0), false => Margin::symmetric(3.0, 3.0),
}) })
+2 -1
View File
@@ -107,7 +107,8 @@ impl BarWidget for Storage {
TextFormat::simple(font_id.clone(), ctx.style().visuals.text_color()), TextFormat::simple(font_id.clone(), ctx.style().visuals.text_color()),
); );
config.apply_on_widget(true, ui, |ui| { // TODO: WIP
config.apply_on_widget(true, false, ui, |ui| {
if ui if ui
.add( .add(
Label::new(layout_job) Label::new(layout_job)
+1 -1
View File
@@ -110,7 +110,7 @@ impl BarWidget for Time {
TextFormat::simple(font_id, ctx.style().visuals.text_color()), TextFormat::simple(font_id, ctx.style().visuals.text_color()),
); );
config.apply_on_widget(true, ui, |ui| { config.apply_on_widget(true, true, ui, |ui| {
if ui if ui
.add( .add(
Label::new(layout_job) Label::new(layout_job)