mirror of
https://github.com/LGUG2Z/komorebi.git
synced 2026-05-04 01:44:17 +02:00
correct widget spacing on all widgets
This commit is contained in:
@@ -347,13 +347,7 @@ impl Komobar {
|
|||||||
) -> Self {
|
) -> Self {
|
||||||
let mut komobar = Self {
|
let mut komobar = Self {
|
||||||
config: config.clone(),
|
config: config.clone(),
|
||||||
render_config: Rc::new(RefCell::new(RenderConfig {
|
render_config: Rc::new(RefCell::new(RenderConfig::new())),
|
||||||
spacing: 0.0,
|
|
||||||
grouping: Grouping::None,
|
|
||||||
background_color: Color32::BLACK,
|
|
||||||
alignment: None,
|
|
||||||
no_spacing: false,
|
|
||||||
})),
|
|
||||||
komorebi_notification_state: None,
|
komorebi_notification_state: None,
|
||||||
left_widgets: vec![],
|
left_widgets: vec![],
|
||||||
right_widgets: vec![],
|
right_widgets: vec![],
|
||||||
@@ -486,7 +480,7 @@ impl eframe::App for Komobar {
|
|||||||
w.render(ctx, ui, render_conf);
|
w.render(ctx, ui, render_conf);
|
||||||
}
|
}
|
||||||
|
|
||||||
render_conf.no_spacing = true;
|
render_conf.is_last_in_alignment = true;
|
||||||
last.render(ctx, ui, render_conf);
|
last.render(ctx, ui, render_conf);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -503,7 +497,7 @@ impl eframe::App for Komobar {
|
|||||||
w.render(ctx, ui, render_conf);
|
w.render(ctx, ui, render_conf);
|
||||||
}
|
}
|
||||||
|
|
||||||
render_conf.no_spacing = true;
|
render_conf.is_last_in_alignment = true;
|
||||||
last.render(ctx, ui, render_conf);
|
last.render(ctx, ui, render_conf);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -124,30 +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];
|
let mut add_to_ui: [bool; 4] = [self.workspaces.enable, false, false, false];
|
||||||
|
|
||||||
if let Some(layout) = self.layout {
|
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 {
|
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 let Some(focused_window) = self.focused_window {
|
||||||
if focused_window.enable {
|
if focused_window.enable {
|
||||||
let titles = &komorebi_notification_state.focused_container_information.0;
|
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;
|
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, 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
|
for (i, (ws, should_show)) in
|
||||||
komorebi_notification_state.workspaces.iter().enumerate()
|
komorebi_notification_state.workspaces.iter().enumerate()
|
||||||
{
|
{
|
||||||
@@ -212,8 +212,8 @@ impl BarWidget for Komorebi {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if enable_widget[1] {
|
if add_to_ui[1] {
|
||||||
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| {
|
||||||
if ui
|
if ui
|
||||||
.add(
|
.add(
|
||||||
Label::new(komorebi_notification_state.layout.to_string())
|
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();
|
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, last_enabled_widget_index == Some(2), ui,|ui|{
|
config.apply_on_widget(true, last_add_to_ui == 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()
|
||||||
@@ -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 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| {
|
config.apply_on_widget(true, last_add_to_ui == Some(3), 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;
|
||||||
|
|||||||
@@ -318,7 +318,7 @@ 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) {
|
||||||
let mut enable_widget: [bool; 3] = [
|
let mut add_to_ui: [bool; 3] = [
|
||||||
self.show_total_data_transmitted,
|
self.show_total_data_transmitted,
|
||||||
self.show_network_activity,
|
self.show_network_activity,
|
||||||
false,
|
false,
|
||||||
@@ -328,29 +328,29 @@ impl BarWidget for Network {
|
|||||||
self.default_interface();
|
self.default_interface();
|
||||||
|
|
||||||
if !self.default_interface.is_empty() {
|
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() {
|
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));
|
ui.add(Label::new(output).selectable(false));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if enable_widget[1] {
|
if add_to_ui[1] {
|
||||||
for output in self.network_activity() {
|
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));
|
ui.add(Label::new(output).selectable(false));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if enable_widget[2] {
|
if add_to_ui[2] {
|
||||||
let font_id = ctx
|
let font_id = ctx
|
||||||
.style()
|
.style()
|
||||||
.text_styles
|
.text_styles
|
||||||
@@ -380,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, last_enabled_widget_index == Some(2), ui, |ui| {
|
config.apply_on_widget(true, last_add_to_ui == Some(2), ui, |ui| {
|
||||||
if ui
|
if ui
|
||||||
.add(
|
.add(
|
||||||
Label::new(layout_job)
|
Label::new(layout_job)
|
||||||
|
|||||||
@@ -35,7 +35,11 @@ pub struct RenderConfig {
|
|||||||
pub background_color: Color32,
|
pub background_color: Color32,
|
||||||
/// Alignment of the widgets
|
/// Alignment of the widgets
|
||||||
pub alignment: Option<Alignment>,
|
pub alignment: Option<Alignment>,
|
||||||
/// 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,
|
pub no_spacing: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,12 +54,26 @@ impl RenderExt for &KomobarConfig {
|
|||||||
grouping: self.grouping.unwrap_or(Grouping::None),
|
grouping: self.grouping.unwrap_or(Grouping::None),
|
||||||
background_color,
|
background_color,
|
||||||
alignment: None,
|
alignment: None,
|
||||||
|
is_last_in_alignment: false,
|
||||||
|
more_inner_margin: false,
|
||||||
no_spacing: false,
|
no_spacing: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RenderConfig {
|
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<R>(
|
pub fn apply_on_bar<R>(
|
||||||
&mut self,
|
&mut self,
|
||||||
ui: &mut Ui,
|
ui: &mut Ui,
|
||||||
@@ -64,7 +82,7 @@ impl RenderConfig {
|
|||||||
self.alignment = None;
|
self.alignment = None;
|
||||||
|
|
||||||
if let Grouping::Bar(config) = self.grouping {
|
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)
|
Self::fallback_group(ui, add_contents)
|
||||||
@@ -78,7 +96,7 @@ impl RenderConfig {
|
|||||||
self.alignment = None;
|
self.alignment = None;
|
||||||
|
|
||||||
if let Grouping::Alignment(config) = self.grouping {
|
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)
|
Self::fallback_group(ui, add_contents)
|
||||||
@@ -87,19 +105,20 @@ impl RenderConfig {
|
|||||||
pub fn apply_on_widget<R>(
|
pub fn apply_on_widget<R>(
|
||||||
&mut self,
|
&mut self,
|
||||||
more_inner_margin: bool,
|
more_inner_margin: bool,
|
||||||
is_last_widget: bool,
|
is_last_add_to_ui: 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
|
// since a widget can call this multiple times, it is necessary to know the last time
|
||||||
// in order to add widget spacing correctly
|
// 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 {
|
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<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> {
|
||||||
@@ -111,13 +130,12 @@ impl RenderConfig {
|
|||||||
|
|
||||||
fn fallback_widget_group<R>(
|
fn fallback_widget_group<R>(
|
||||||
&mut self,
|
&mut self,
|
||||||
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 more_inner_margin {
|
.inner_margin(match self.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),
|
||||||
})
|
})
|
||||||
@@ -126,14 +144,13 @@ impl RenderConfig {
|
|||||||
|
|
||||||
fn define_group<R>(
|
fn define_group<R>(
|
||||||
&mut self,
|
&mut self,
|
||||||
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 more_inner_margin {
|
.inner_margin(match self.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),
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -88,7 +88,9 @@ impl BarWidget for Storage {
|
|||||||
.cloned()
|
.cloned()
|
||||||
.unwrap_or_else(FontId::default);
|
.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(
|
let mut layout_job = LayoutJob::simple(
|
||||||
match self.label_prefix {
|
match self.label_prefix {
|
||||||
LabelPrefix::Icon | LabelPrefix::IconAndText => {
|
LabelPrefix::Icon | LabelPrefix::IconAndText => {
|
||||||
@@ -102,13 +104,12 @@ impl BarWidget for Storage {
|
|||||||
);
|
);
|
||||||
|
|
||||||
layout_job.append(
|
layout_job.append(
|
||||||
&output,
|
output,
|
||||||
10.0,
|
10.0,
|
||||||
TextFormat::simple(font_id.clone(), ctx.style().visuals.text_color()),
|
TextFormat::simple(font_id.clone(), ctx.style().visuals.text_color()),
|
||||||
);
|
);
|
||||||
|
|
||||||
// TODO: WIP
|
config.apply_on_widget(true, index == last_index, ui, |ui| {
|
||||||
config.apply_on_widget(true, false, ui, |ui| {
|
|
||||||
if ui
|
if ui
|
||||||
.add(
|
.add(
|
||||||
Label::new(layout_job)
|
Label::new(layout_job)
|
||||||
|
|||||||
Reference in New Issue
Block a user