refactoring widget spacing

This commit is contained in:
Csaba
2024-11-16 16:45:27 +01:00
parent 85a41bf5b2
commit 50b49ccf69
12 changed files with 267 additions and 293 deletions

View File

@@ -475,13 +475,8 @@ impl eframe::App for Komobar {
render_conf.alignment = Some(Alignment::Left);
render_config.apply_on_alignment(ui, |ui| {
if let Some((last, rest)) = self.left_widgets.split_last_mut() {
for w in rest.iter_mut() {
w.render(ctx, ui, render_conf);
}
render_conf.is_last_in_alignment = true;
last.render(ctx, ui, render_conf);
for w in &mut self.left_widgets {
w.render(ctx, ui, &mut render_conf);
}
});
});
@@ -492,13 +487,8 @@ impl eframe::App for Komobar {
render_conf.alignment = Some(Alignment::Right);
render_config.apply_on_alignment(ui, |ui| {
if let Some((last, rest)) = self.right_widgets.split_last_mut() {
for w in rest.iter_mut() {
w.render(ctx, ui, render_conf);
}
render_conf.is_last_in_alignment = true;
last.render(ctx, ui, render_conf);
for w in &mut self.right_widgets {
w.render(ctx, ui, &mut render_conf);
}
});
})

View File

@@ -115,7 +115,7 @@ impl Battery {
}
impl BarWidget for Battery {
fn render(&mut self, ctx: &Context, ui: &mut Ui, mut config: RenderConfig) {
fn render(&mut self, ctx: &Context, ui: &mut Ui, config: &mut RenderConfig) {
if self.enable {
let output = self.output();
if !output.is_empty() {
@@ -147,7 +147,7 @@ impl BarWidget for Battery {
TextFormat::simple(font_id, ctx.style().visuals.text_color()),
);
config.apply_on_widget(true, true, ui, |ui| {
config.apply_on_widget(true, ui, |ui| {
ui.add(
Label::new(layout_job)
.selectable(false)

View File

@@ -70,7 +70,7 @@ impl Cpu {
}
impl BarWidget for Cpu {
fn render(&mut self, ctx: &Context, ui: &mut Ui, mut config: RenderConfig) {
fn render(&mut self, ctx: &Context, ui: &mut Ui, config: &mut RenderConfig) {
if self.enable {
let output = self.output();
if !output.is_empty() {
@@ -99,7 +99,7 @@ impl BarWidget for Cpu {
TextFormat::simple(font_id, ctx.style().visuals.text_color()),
);
config.apply_on_widget(true, true, ui, |ui| {
config.apply_on_widget(true, ui, |ui| {
if ui
.add(
Label::new(layout_job)

View File

@@ -86,7 +86,7 @@ impl Date {
}
impl BarWidget for Date {
fn render(&mut self, ctx: &Context, ui: &mut Ui, mut config: RenderConfig) {
fn render(&mut self, ctx: &Context, ui: &mut Ui, config: &mut RenderConfig) {
if self.enable {
let mut output = self.output();
if !output.is_empty() {
@@ -119,7 +119,7 @@ impl BarWidget for Date {
TextFormat::simple(font_id, ctx.style().visuals.text_color()),
);
config.apply_on_widget(true, true, ui, |ui| {
config.apply_on_widget(true, ui, |ui| {
if ui
.add(
Label::new(WidgetText::LayoutJob(layout_job.clone()))

View File

@@ -122,32 +122,14 @@ pub struct 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, config: &mut RenderConfig) {
let mut komorebi_notification_state = self.komorebi_notification_state.borrow_mut();
let mut add_to_ui: [bool; 4] = [self.workspaces.enable, false, false, false];
if let Some(layout) = self.layout {
add_to_ui[1] = layout.enable;
}
if let Some(configuration_switcher) = &self.configuration_switcher {
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;
add_to_ui[3] = !titles.is_empty();
}
}
let last_add_to_ui = add_to_ui.iter().rposition(|&x| x);
if add_to_ui[0] {
if self.workspaces.enable {
let mut update = None;
// NOTE: There should always be at least one workspace.
config.apply_on_widget(false, last_add_to_ui == Some(0), ui, |ui| {
// NOTE: There should always be at least one workspace if the bar is connected to komorebi.
config.apply_on_widget(false, ui, |ui| {
for (i, (ws, should_show)) in
komorebi_notification_state.workspaces.iter().enumerate()
{
@@ -212,49 +194,60 @@ impl BarWidget for Komorebi {
}
}
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())
.selectable(false)
.sense(Sense::click()),
)
.clicked()
{
match komorebi_notification_state.layout {
KomorebiLayout::Default(_) => {
if komorebi_client::send_message(&SocketMessage::CycleLayout(
CycleDirection::Next,
))
.is_err()
{
tracing::error!("could not send message to komorebi: CycleLayout");
if let Some(layout) = self.layout {
if layout.enable {
config.apply_on_widget(true, ui, |ui| {
if ui
.add(
Label::new(komorebi_notification_state.layout.to_string())
.selectable(false)
.sense(Sense::click()),
)
.clicked()
{
match komorebi_notification_state.layout {
KomorebiLayout::Default(_) => {
if komorebi_client::send_message(&SocketMessage::CycleLayout(
CycleDirection::Next,
))
.is_err()
{
tracing::error!(
"could not send message to komorebi: CycleLayout"
);
}
}
}
KomorebiLayout::Floating => {
if komorebi_client::send_message(&SocketMessage::ToggleTiling).is_err()
{
tracing::error!("could not send message to komorebi: ToggleTiling");
KomorebiLayout::Floating => {
if komorebi_client::send_message(&SocketMessage::ToggleTiling)
.is_err()
{
tracing::error!(
"could not send message to komorebi: ToggleTiling"
);
}
}
}
KomorebiLayout::Paused => {
if komorebi_client::send_message(&SocketMessage::TogglePause).is_err() {
tracing::error!("could not send message to komorebi: TogglePause");
KomorebiLayout::Paused => {
if komorebi_client::send_message(&SocketMessage::TogglePause)
.is_err()
{
tracing::error!(
"could not send message to komorebi: TogglePause"
);
}
}
KomorebiLayout::Custom => {}
}
KomorebiLayout::Custom => {}
}
}
});
});
}
}
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_add_to_ui == Some(2), ui,|ui|{
if let Some(configuration_switcher) = &self.configuration_switcher {
if configuration_switcher.enable {
for (name, location) in configuration_switcher.configurations.iter() {
let path = PathBuf::from(location);
if path.is_file() {
config.apply_on_widget(true, ui,|ui|{
if ui
.add(Label::new(name).selectable(false).sense(Sense::click()))
.clicked()
@@ -302,117 +295,125 @@ impl BarWidget for Komorebi {
}
}
}});
}
}
}
}
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_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;
if let Some(focused_window) = self.focused_window {
if focused_window.enable {
let titles = &komorebi_notification_state.focused_container_information.0;
if !titles.is_empty() {
config.apply_on_widget(true, ui, |ui| {
let icons = &komorebi_notification_state.focused_container_information.1;
let focused_window_idx =
komorebi_notification_state.focused_container_information.2;
let iter = titles.iter().zip(icons.iter());
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);
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);
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()
{
tracing::error!(
"could not send message to komorebi: MouseFollowsFocus"
);
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 komorebi_client::send_message(&SocketMessage::FocusStackWindow(i))
.is_err()
{
tracing::error!(
"could not send message to komorebi: FocusStackWindow"
);
}
if i == focused_window_idx {
let font_id = ctx
.style()
.text_styles
.get(&TextStyle::Body)
.cloned()
.unwrap_or_else(FontId::default);
if komorebi_client::send_message(&SocketMessage::MouseFollowsFocus(
komorebi_notification_state.mouse_follows_focus,
))
.is_err()
{
tracing::error!(
"could not send message to komorebi: MouseFollowsFocus"
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);
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()
{
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::MouseFollowsFocus(
komorebi_notification_state.mouse_follows_focus,
),
)
.is_err()
{
tracing::error!(
"could not send message to komorebi: MouseFollowsFocus"
);
}
}
}
}
}
});
}
});
}
}
}
}

View File

@@ -78,7 +78,7 @@ impl Media {
}
impl BarWidget for Media {
fn render(&mut self, ctx: &Context, ui: &mut Ui, mut config: RenderConfig) {
fn render(&mut self, ctx: &Context, ui: &mut Ui, config: &mut RenderConfig) {
if self.enable {
let output = self.output();
if !output.is_empty() {
@@ -102,7 +102,7 @@ impl BarWidget for Media {
TextFormat::simple(font_id, ctx.style().visuals.text_color()),
);
config.apply_on_widget(true, true, ui, |ui| {
config.apply_on_widget(true, ui, |ui| {
let available_height = ui.available_height();
let mut custom_ui = CustomUi(ui);

View File

@@ -73,7 +73,7 @@ impl Memory {
}
impl BarWidget for Memory {
fn render(&mut self, ctx: &Context, ui: &mut Ui, mut config: RenderConfig) {
fn render(&mut self, ctx: &Context, ui: &mut Ui, config: &mut RenderConfig) {
if self.enable {
let output = self.output();
if !output.is_empty() {
@@ -102,7 +102,7 @@ impl BarWidget for Memory {
TextFormat::simple(font_id, ctx.style().visuals.text_color()),
);
config.apply_on_widget(true, true, ui, |ui| {
config.apply_on_widget(true, ui, |ui| {
if ui
.add(
Label::new(layout_job)

View File

@@ -317,83 +317,71 @@ impl Network {
}
impl BarWidget for Network {
fn render(&mut self, ctx: &Context, ui: &mut Ui, mut config: RenderConfig) {
let mut add_to_ui: [bool; 3] = [
self.show_total_data_transmitted,
self.show_network_activity,
false,
];
fn render(&mut self, ctx: &Context, ui: &mut Ui, config: &mut RenderConfig) {
if self.show_total_data_transmitted {
for output in self.total_data_transmitted() {
config.apply_on_widget(true, ui, |ui| {
ui.add(Label::new(output).selectable(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 {
self.default_interface();
if !self.default_interface.is_empty() {
add_to_ui[2] = true;
}
}
let font_id = ctx
.style()
.text_styles
.get(&TextStyle::Body)
.cloned()
.unwrap_or_else(FontId::default);
let last_add_to_ui = add_to_ui.iter().rposition(|&x| x);
let mut layout_job = LayoutJob::simple(
match self.label_prefix {
LabelPrefix::Icon | LabelPrefix::IconAndText => {
egui_phosphor::regular::WIFI_HIGH.to_string()
}
LabelPrefix::None | LabelPrefix::Text => String::new(),
},
font_id.clone(),
ctx.style().visuals.selection.stroke.color,
100.0,
);
if add_to_ui[0] {
for output in self.total_data_transmitted() {
config.apply_on_widget(true, last_add_to_ui == Some(0), ui, |ui| {
ui.add(Label::new(output).selectable(false));
});
}
}
if add_to_ui[1] {
for output in self.network_activity() {
config.apply_on_widget(true, last_add_to_ui == Some(1), ui, |ui| {
ui.add(Label::new(output).selectable(false));
});
}
}
if add_to_ui[2] {
let font_id = ctx
.style()
.text_styles
.get(&TextStyle::Body)
.cloned()
.unwrap_or_else(FontId::default);
let mut layout_job = LayoutJob::simple(
match self.label_prefix {
LabelPrefix::Icon | LabelPrefix::IconAndText => {
egui_phosphor::regular::WIFI_HIGH.to_string()
}
LabelPrefix::None | LabelPrefix::Text => String::new(),
},
font_id.clone(),
ctx.style().visuals.selection.stroke.color,
100.0,
);
if let LabelPrefix::Text | LabelPrefix::IconAndText = self.label_prefix {
self.default_interface.insert_str(0, "NET: ");
}
layout_job.append(
&self.default_interface,
10.0,
TextFormat::simple(font_id, ctx.style().visuals.text_color()),
);
config.apply_on_widget(true, last_add_to_ui == Some(2), ui, |ui| {
if ui
.add(
Label::new(layout_job)
.selectable(false)
.sense(Sense::click()),
)
.clicked()
{
if let Err(error) = Command::new("cmd.exe").args(["/C", "ncpa"]).spawn() {
eprintln!("{}", error)
}
if let LabelPrefix::Text | LabelPrefix::IconAndText = self.label_prefix {
self.default_interface.insert_str(0, "NET: ");
}
});
layout_job.append(
&self.default_interface,
10.0,
TextFormat::simple(font_id, ctx.style().visuals.text_color()),
);
config.apply_on_widget(true, ui, |ui| {
if ui
.add(
Label::new(layout_job)
.selectable(false)
.sense(Sense::click()),
)
.clicked()
{
if let Err(error) = Command::new("cmd.exe").args(["/C", "ncpa"]).spawn() {
eprintln!("{}", error)
}
}
});
}
}
}
}

View File

@@ -35,12 +35,10 @@ pub struct RenderConfig {
pub background_color: Color32,
/// Alignment of the widgets
pub alignment: Option<Alignment>,
/// 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,
/// Set to true after the first time the apply_on_widget was called on an alignment
pub applied_on_widget: bool,
}
pub trait RenderExt {
@@ -54,9 +52,8 @@ 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,
applied_on_widget: false,
}
}
}
@@ -68,9 +65,8 @@ impl RenderConfig {
grouping: Grouping::None,
background_color: Color32::BLACK,
alignment: None,
is_last_in_alignment: false,
more_inner_margin: false,
no_spacing: false,
applied_on_widget: false,
}
}
@@ -82,7 +78,7 @@ impl RenderConfig {
self.alignment = None;
if let Grouping::Bar(config) = self.grouping {
return self.define_group(config, ui, add_contents);
return self.define_group(None, config, ui, add_contents);
}
Self::fallback_group(ui, add_contents)
@@ -96,7 +92,7 @@ impl RenderConfig {
self.alignment = None;
if let Grouping::Alignment(config) = self.grouping {
return self.define_group(config, ui, add_contents);
return self.define_group(None, config, ui, add_contents);
}
Self::fallback_group(ui, add_contents)
@@ -105,20 +101,17 @@ impl RenderConfig {
pub fn apply_on_widget<R>(
&mut self,
more_inner_margin: bool,
is_last_add_to_ui: bool,
ui: &mut Ui,
add_contents: impl FnOnce(&mut Ui) -> 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.more_inner_margin = more_inner_margin;
self.no_spacing = self.is_last_in_alignment && is_last_add_to_ui;
let outer_margin = self.widget_outer_margin();
if let Grouping::Widget(config) = self.grouping {
return self.define_group(config, ui, add_contents);
return self.define_group(Some(outer_margin), config, ui, add_contents);
}
self.fallback_widget_group(ui, add_contents)
self.fallback_widget_group(Some(outer_margin), ui, add_contents)
}
fn fallback_group<R>(ui: &mut Ui, add_contents: impl FnOnce(&mut Ui) -> R) -> InnerResponse<R> {
@@ -130,11 +123,12 @@ impl RenderConfig {
fn fallback_widget_group<R>(
&mut self,
outer_margin: Option<Margin>,
ui: &mut Ui,
add_contents: impl FnOnce(&mut Ui) -> R,
) -> InnerResponse<R> {
Frame::none()
.outer_margin(self.widget_outer_margin())
.outer_margin(outer_margin.unwrap_or(Margin::ZERO))
.inner_margin(match self.more_inner_margin {
true => Margin::symmetric(5.0, 0.0),
false => Margin::same(0.0),
@@ -144,12 +138,13 @@ impl RenderConfig {
fn define_group<R>(
&mut self,
outer_margin: Option<Margin>,
config: GroupingConfig,
ui: &mut Ui,
add_contents: impl FnOnce(&mut Ui) -> R,
) -> InnerResponse<R> {
Frame::none()
.outer_margin(self.widget_outer_margin())
.outer_margin(outer_margin.unwrap_or(Margin::ZERO))
.inner_margin(match self.more_inner_margin {
true => Margin::symmetric(8.0, 3.0),
false => Margin::symmetric(3.0, 3.0),
@@ -179,31 +174,33 @@ impl RenderConfig {
.show(ui, add_contents)
}
fn widget_outer_margin(&self) -> Margin {
fn set_spacing(&mut self) {
self.applied_on_widget = true;
}
fn widget_outer_margin(&mut self) -> Margin {
let spacing = if self.applied_on_widget {
self.spacing
} else {
0.0
};
if !self.applied_on_widget {
self.set_spacing();
}
Margin {
left: match self.alignment {
Some(align) => match align {
Alignment::Left => 0.0,
Alignment::Right => {
if self.no_spacing {
0.0
} else {
self.spacing
}
}
Alignment::Left => spacing,
Alignment::Right => 0.0,
},
None => 0.0,
},
right: match self.alignment {
Some(align) => match align {
Alignment::Left => {
if self.no_spacing {
0.0
} else {
self.spacing
}
}
Alignment::Right => 0.0,
Alignment::Left => 0.0,
Alignment::Right => spacing,
},
None => 0.0,
},

View File

@@ -79,7 +79,7 @@ impl Storage {
}
impl BarWidget for Storage {
fn render(&mut self, ctx: &Context, ui: &mut Ui, mut config: RenderConfig) {
fn render(&mut self, ctx: &Context, ui: &mut Ui, config: &mut RenderConfig) {
if self.enable {
let font_id = ctx
.style()
@@ -88,9 +88,7 @@ impl BarWidget for Storage {
.cloned()
.unwrap_or_else(FontId::default);
let last_index = self.output().len() - 1;
for (index, output) in self.output().iter().enumerate() {
for output in self.output() {
let mut layout_job = LayoutJob::simple(
match self.label_prefix {
LabelPrefix::Icon | LabelPrefix::IconAndText => {
@@ -104,12 +102,12 @@ impl BarWidget for Storage {
);
layout_job.append(
output,
&output,
10.0,
TextFormat::simple(font_id.clone(), ctx.style().visuals.text_color()),
);
config.apply_on_widget(true, index == last_index, ui, |ui| {
config.apply_on_widget(true, ui, |ui| {
if ui
.add(
Label::new(layout_job)

View File

@@ -77,7 +77,7 @@ impl Time {
}
impl BarWidget for Time {
fn render(&mut self, ctx: &Context, ui: &mut Ui, mut config: RenderConfig) {
fn render(&mut self, ctx: &Context, ui: &mut Ui, config: &mut RenderConfig) {
if self.enable {
let mut output = self.output();
if !output.is_empty() {
@@ -110,7 +110,7 @@ impl BarWidget for Time {
TextFormat::simple(font_id, ctx.style().visuals.text_color()),
);
config.apply_on_widget(true, true, ui, |ui| {
config.apply_on_widget(true, ui, |ui| {
if ui
.add(
Label::new(layout_job)

View File

@@ -24,7 +24,7 @@ use serde::Deserialize;
use serde::Serialize;
pub trait BarWidget {
fn render(&mut self, ctx: &Context, ui: &mut Ui, config: RenderConfig);
fn render(&mut self, ctx: &Context, ui: &mut Ui, config: &mut RenderConfig);
}
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]