mirror of
https://github.com/LGUG2Z/komorebi.git
synced 2026-03-19 16:21:17 +01:00
proper widget spacing based on alignment
This commit is contained in:
@@ -478,7 +478,7 @@ impl eframe::App for Komobar {
|
||||
ui.with_layout(Layout::left_to_right(Align::Center), |ui| {
|
||||
render_config.grouping.apply_on_alignment(ui, |ui| {
|
||||
for w in &mut self.left_widgets {
|
||||
w.render(ctx, ui, render_config_clone);
|
||||
w.render(ctx, ui, render_config_clone, Alignment::Left);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -487,7 +487,7 @@ impl eframe::App for Komobar {
|
||||
ui.with_layout(Layout::right_to_left(Align::Center), |ui| {
|
||||
render_config.grouping.apply_on_alignment(ui, |ui| {
|
||||
for w in &mut self.right_widgets {
|
||||
w.render(ctx, ui, render_config_clone);
|
||||
w.render(ctx, ui, render_config_clone, Alignment::Right);
|
||||
}
|
||||
});
|
||||
})
|
||||
@@ -498,7 +498,7 @@ impl eframe::App for Komobar {
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
enum Alignment {
|
||||
pub enum Alignment {
|
||||
Left,
|
||||
Right,
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use crate::bar::Alignment;
|
||||
use crate::config::LabelPrefix;
|
||||
use crate::widget::BarWidget;
|
||||
use crate::widget::RenderConfig;
|
||||
@@ -115,7 +116,13 @@ 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,
|
||||
mut config: RenderConfig,
|
||||
alignment: Alignment,
|
||||
) {
|
||||
if self.enable {
|
||||
let output = self.output();
|
||||
if !output.is_empty() {
|
||||
@@ -147,7 +154,7 @@ impl BarWidget for Battery {
|
||||
TextFormat::simple(font_id, ctx.style().visuals.text_color()),
|
||||
);
|
||||
|
||||
config.grouping.apply_on_widget(true, ui, |ui| {
|
||||
config.grouping.apply_on_widget(true, alignment, ui, |ui| {
|
||||
ui.add(
|
||||
Label::new(layout_job)
|
||||
.selectable(false)
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use crate::bar::Alignment;
|
||||
use crate::config::LabelPrefix;
|
||||
use crate::widget::BarWidget;
|
||||
use crate::widget::RenderConfig;
|
||||
@@ -70,7 +71,13 @@ 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,
|
||||
mut config: RenderConfig,
|
||||
alignment: Alignment,
|
||||
) {
|
||||
if self.enable {
|
||||
let output = self.output();
|
||||
if !output.is_empty() {
|
||||
@@ -99,7 +106,7 @@ impl BarWidget for Cpu {
|
||||
TextFormat::simple(font_id, ctx.style().visuals.text_color()),
|
||||
);
|
||||
|
||||
config.grouping.apply_on_widget(true, ui, |ui| {
|
||||
config.grouping.apply_on_widget(true, alignment, ui, |ui| {
|
||||
if ui
|
||||
.add(
|
||||
Label::new(layout_job)
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use crate::bar::Alignment;
|
||||
use crate::config::LabelPrefix;
|
||||
use crate::widget::BarWidget;
|
||||
use crate::widget::RenderConfig;
|
||||
@@ -86,7 +87,13 @@ 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,
|
||||
mut config: RenderConfig,
|
||||
alignment: Alignment,
|
||||
) {
|
||||
if self.enable {
|
||||
let mut output = self.output();
|
||||
if !output.is_empty() {
|
||||
@@ -119,7 +126,7 @@ impl BarWidget for Date {
|
||||
TextFormat::simple(font_id, ctx.style().visuals.text_color()),
|
||||
);
|
||||
|
||||
config.grouping.apply_on_widget(true, ui, |ui| {
|
||||
config.grouping.apply_on_widget(true, alignment, ui, |ui| {
|
||||
if ui
|
||||
.add(
|
||||
Label::new(WidgetText::LayoutJob(layout_job.clone()))
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use crate::bar::Alignment;
|
||||
use crate::config::Color32Ext;
|
||||
use crate::BACKGROUND_COLOR;
|
||||
use crate::WIDGET_SPACING;
|
||||
@@ -34,7 +35,7 @@ impl Grouping {
|
||||
add_contents: impl FnOnce(&mut Ui) -> R,
|
||||
) -> InnerResponse<R> {
|
||||
match self {
|
||||
Self::Bar(config) => Self::define_group(false, ui, add_contents, config),
|
||||
Self::Bar(config) => Self::define_group(false, None, ui, add_contents, config),
|
||||
Self::Alignment(_) => Self::no_group(ui, add_contents),
|
||||
Self::Widget(_) => Self::no_group(ui, add_contents),
|
||||
Self::None => Self::no_group(ui, add_contents),
|
||||
@@ -48,7 +49,7 @@ impl Grouping {
|
||||
) -> InnerResponse<R> {
|
||||
match self {
|
||||
Self::Bar(_) => Self::no_group(ui, add_contents),
|
||||
Self::Alignment(config) => Self::define_group(false, ui, add_contents, config),
|
||||
Self::Alignment(config) => Self::define_group(false, None, ui, add_contents, config),
|
||||
Self::Widget(_) => Self::no_group(ui, add_contents),
|
||||
Self::None => Self::no_group(ui, add_contents),
|
||||
}
|
||||
@@ -57,27 +58,31 @@ impl Grouping {
|
||||
pub fn apply_on_widget<R>(
|
||||
&mut self,
|
||||
use_spacing: bool,
|
||||
alignment: Alignment,
|
||||
ui: &mut Ui,
|
||||
add_contents: impl FnOnce(&mut Ui) -> R,
|
||||
) -> InnerResponse<R> {
|
||||
match self {
|
||||
Self::Bar(_) => Self::widget_group(use_spacing, ui, add_contents),
|
||||
Self::Alignment(_) => Self::widget_group(use_spacing, ui, add_contents),
|
||||
Self::Widget(config) => Self::define_group(use_spacing, ui, add_contents, config),
|
||||
Self::None => Self::widget_group(use_spacing, ui, add_contents),
|
||||
Self::Bar(_) => Self::widget_group(use_spacing, alignment, ui, add_contents),
|
||||
Self::Alignment(_) => Self::widget_group(use_spacing, alignment, ui, add_contents),
|
||||
Self::Widget(config) => {
|
||||
Self::define_group(use_spacing, Some(alignment), ui, add_contents, config)
|
||||
}
|
||||
Self::None => Self::widget_group(use_spacing, alignment, ui, add_contents),
|
||||
}
|
||||
}
|
||||
|
||||
fn define_group<R>(
|
||||
use_spacing: bool,
|
||||
alignment: Option<Alignment>,
|
||||
ui: &mut Ui,
|
||||
add_contents: impl FnOnce(&mut Ui) -> R,
|
||||
config: &mut GroupingConfig,
|
||||
) -> InnerResponse<R> {
|
||||
Frame::none()
|
||||
.outer_margin(Margin::same(0.0))
|
||||
.outer_margin(Self::widget_outer_margin(alignment))
|
||||
.inner_margin(match use_spacing {
|
||||
true => Margin::symmetric(WIDGET_SPACING / 2.0 + 3.0, 3.0),
|
||||
true => Margin::symmetric(5.0 + 3.0, 3.0),
|
||||
false => Margin::symmetric(3.0, 3.0),
|
||||
})
|
||||
.stroke(ui.style().visuals.widgets.noninteractive.bg_stroke)
|
||||
@@ -107,12 +112,14 @@ impl Grouping {
|
||||
|
||||
fn widget_group<R>(
|
||||
use_spacing: bool,
|
||||
alignment: Alignment,
|
||||
ui: &mut Ui,
|
||||
add_contents: impl FnOnce(&mut Ui) -> R,
|
||||
) -> InnerResponse<R> {
|
||||
Frame::none()
|
||||
.outer_margin(Self::widget_outer_margin(Some(alignment)))
|
||||
.inner_margin(match use_spacing {
|
||||
true => Margin::symmetric(WIDGET_SPACING / 2.0, 0.0),
|
||||
true => Margin::symmetric(5.0, 0.0),
|
||||
false => Margin::same(0.0),
|
||||
})
|
||||
.show(ui, add_contents)
|
||||
@@ -124,6 +131,27 @@ impl Grouping {
|
||||
response: ui.response().clone(),
|
||||
}
|
||||
}
|
||||
|
||||
fn widget_outer_margin(alignment: Option<Alignment>) -> Margin {
|
||||
Margin {
|
||||
left: match alignment {
|
||||
Some(align) => match align {
|
||||
Alignment::Left => 0.0,
|
||||
Alignment::Right => WIDGET_SPACING,
|
||||
},
|
||||
None => 0.0,
|
||||
},
|
||||
right: match alignment {
|
||||
Some(align) => match align {
|
||||
Alignment::Left => WIDGET_SPACING,
|
||||
Alignment::Right => 0.0,
|
||||
},
|
||||
None => 0.0,
|
||||
},
|
||||
top: 0.0,
|
||||
bottom: 0.0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema)]
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use crate::bar::apply_theme;
|
||||
use crate::bar::Alignment;
|
||||
use crate::config::KomobarTheme;
|
||||
use crate::ui::CustomUi;
|
||||
use crate::widget::BarWidget;
|
||||
@@ -122,14 +123,20 @@ 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,
|
||||
mut config: RenderConfig,
|
||||
alignment: Alignment,
|
||||
) {
|
||||
let mut komorebi_notification_state = self.komorebi_notification_state.borrow_mut();
|
||||
|
||||
if self.workspaces.enable {
|
||||
let mut update = None;
|
||||
|
||||
// NOTE: There should always be at least one workspace.
|
||||
config.grouping.apply_on_widget(false, ui, |ui| {
|
||||
config.grouping.apply_on_widget(false, alignment, ui, |ui| {
|
||||
for (i, (ws, should_show)) in
|
||||
komorebi_notification_state.workspaces.iter().enumerate()
|
||||
{
|
||||
@@ -196,7 +203,7 @@ impl BarWidget for Komorebi {
|
||||
|
||||
if let Some(layout) = self.layout {
|
||||
if layout.enable {
|
||||
config.grouping.apply_on_widget(true, ui, |ui| {
|
||||
config.grouping.apply_on_widget(true, alignment, ui, |ui| {
|
||||
if ui
|
||||
.add(
|
||||
Label::new(komorebi_notification_state.layout.to_string())
|
||||
@@ -247,7 +254,7 @@ impl BarWidget for Komorebi {
|
||||
for (name, location) in configuration_switcher.configurations.iter() {
|
||||
let path = PathBuf::from(location);
|
||||
if path.is_file() {
|
||||
config.grouping.apply_on_widget(true, ui,|ui|{
|
||||
config.grouping.apply_on_widget(true, alignment, ui,|ui|{
|
||||
if ui
|
||||
.add(Label::new(name).selectable(false).sense(Sense::click()))
|
||||
.clicked()
|
||||
@@ -305,7 +312,7 @@ impl BarWidget for Komorebi {
|
||||
let titles = &komorebi_notification_state.focused_container_information.0;
|
||||
|
||||
if !titles.is_empty() {
|
||||
config.grouping.apply_on_widget(true, ui, |ui| {
|
||||
config.grouping.apply_on_widget(true, alignment, ui, |ui| {
|
||||
let icons = &komorebi_notification_state.focused_container_information.1;
|
||||
let focused_window_idx =
|
||||
komorebi_notification_state.focused_container_information.2;
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use crate::bar::Alignment;
|
||||
use crate::ui::CustomUi;
|
||||
use crate::widget::BarWidget;
|
||||
use crate::widget::RenderConfig;
|
||||
@@ -78,7 +79,13 @@ 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,
|
||||
mut config: RenderConfig,
|
||||
alignment: Alignment,
|
||||
) {
|
||||
if self.enable {
|
||||
let output = self.output();
|
||||
if !output.is_empty() {
|
||||
@@ -102,7 +109,7 @@ impl BarWidget for Media {
|
||||
TextFormat::simple(font_id, ctx.style().visuals.text_color()),
|
||||
);
|
||||
|
||||
config.grouping.apply_on_widget(true, ui, |ui| {
|
||||
config.grouping.apply_on_widget(true, alignment, ui, |ui| {
|
||||
let available_height = ui.available_height();
|
||||
let mut custom_ui = CustomUi(ui);
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use crate::bar::Alignment;
|
||||
use crate::config::LabelPrefix;
|
||||
use crate::widget::BarWidget;
|
||||
use crate::widget::RenderConfig;
|
||||
@@ -73,7 +74,13 @@ 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,
|
||||
mut config: RenderConfig,
|
||||
alignment: Alignment,
|
||||
) {
|
||||
if self.enable {
|
||||
let output = self.output();
|
||||
if !output.is_empty() {
|
||||
@@ -102,7 +109,7 @@ impl BarWidget for Memory {
|
||||
TextFormat::simple(font_id, ctx.style().visuals.text_color()),
|
||||
);
|
||||
|
||||
config.grouping.apply_on_widget(true, ui, |ui| {
|
||||
config.grouping.apply_on_widget(true, alignment, ui, |ui| {
|
||||
if ui
|
||||
.add(
|
||||
Label::new(layout_job)
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use crate::bar::Alignment;
|
||||
use crate::config::LabelPrefix;
|
||||
use crate::widget::BarWidget;
|
||||
use crate::widget::RenderConfig;
|
||||
@@ -317,10 +318,16 @@ impl 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,
|
||||
alignment: Alignment,
|
||||
) {
|
||||
if self.show_total_data_transmitted {
|
||||
for output in self.total_data_transmitted() {
|
||||
config.grouping.apply_on_widget(true, ui, |ui| {
|
||||
config.grouping.apply_on_widget(true, alignment, ui, |ui| {
|
||||
ui.add(Label::new(output).selectable(false));
|
||||
});
|
||||
}
|
||||
@@ -328,7 +335,7 @@ impl BarWidget for Network {
|
||||
|
||||
if self.show_network_activity {
|
||||
for output in self.network_activity() {
|
||||
config.grouping.apply_on_widget(true, ui, |ui| {
|
||||
config.grouping.apply_on_widget(true, alignment, ui, |ui| {
|
||||
ui.add(Label::new(output).selectable(false));
|
||||
});
|
||||
}
|
||||
@@ -367,7 +374,7 @@ impl BarWidget for Network {
|
||||
TextFormat::simple(font_id, ctx.style().visuals.text_color()),
|
||||
);
|
||||
|
||||
config.grouping.apply_on_widget(true, ui, |ui| {
|
||||
config.grouping.apply_on_widget(true, alignment, ui, |ui| {
|
||||
if ui
|
||||
.add(
|
||||
Label::new(layout_job)
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use crate::bar::Alignment;
|
||||
use crate::config::LabelPrefix;
|
||||
use crate::widget::BarWidget;
|
||||
use crate::widget::RenderConfig;
|
||||
@@ -79,7 +80,13 @@ 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,
|
||||
mut config: RenderConfig,
|
||||
alignment: Alignment,
|
||||
) {
|
||||
if self.enable {
|
||||
let font_id = ctx
|
||||
.style()
|
||||
@@ -107,7 +114,7 @@ impl BarWidget for Storage {
|
||||
TextFormat::simple(font_id.clone(), ctx.style().visuals.text_color()),
|
||||
);
|
||||
|
||||
config.grouping.apply_on_widget(true, ui, |ui| {
|
||||
config.grouping.apply_on_widget(true, alignment, ui, |ui| {
|
||||
if ui
|
||||
.add(
|
||||
Label::new(layout_job)
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use crate::bar::Alignment;
|
||||
use crate::config::LabelPrefix;
|
||||
use crate::widget::BarWidget;
|
||||
use crate::widget::RenderConfig;
|
||||
@@ -77,7 +78,13 @@ 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,
|
||||
mut config: RenderConfig,
|
||||
alignment: Alignment,
|
||||
) {
|
||||
if self.enable {
|
||||
let mut output = self.output();
|
||||
if !output.is_empty() {
|
||||
@@ -110,7 +117,7 @@ impl BarWidget for Time {
|
||||
TextFormat::simple(font_id, ctx.style().visuals.text_color()),
|
||||
);
|
||||
|
||||
config.grouping.apply_on_widget(true, ui, |ui| {
|
||||
config.grouping.apply_on_widget(true, alignment, ui, |ui| {
|
||||
if ui
|
||||
.add(
|
||||
Label::new(layout_job)
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use crate::bar::Alignment;
|
||||
use crate::battery::Battery;
|
||||
use crate::battery::BatteryConfig;
|
||||
use crate::cpu::Cpu;
|
||||
@@ -24,7 +25,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: RenderConfig, alignment: Alignment);
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
|
||||
Reference in New Issue
Block a user