Skip to content

Commit

Permalink
feat(bar): add widget grouping options
Browse files Browse the repository at this point in the history
This commit adds various widget grouping and transparency options to
komorebi-bar, and is comprised of the individual commits listed below,
worked on in PR #1108, squashed into one.

e8f5952
* adding RenderConfig, and some test frames on widgets

0a5e0a4
* no clone

a5a7d69
* comment

6a91dd4
* ignore unused

80f0214
* Group enum, Copy RenderConfig

fbe5e2c
* Group -> Grouping

ce49b43
* GroupingConfig

f446a6a
* "fmt --check" fix (thanks VS)

d188222
* added widget grouping and group module

1008ec2
* rounding from settings, and apply_on_side

7fff6d2
* dereferencing

655e8ce
* AlphaColour, transparency, bar background, more grouping config options

cba0fcd
* added RoundingConfig

ec5f7dc
* handling grouping edge case for komorebi focus window

12117b8
* changed default values

645c46b
* background color using theme color, AlphaColour.to_color32_or, updating json format for Grouping and RoundingConfig

10d2ab2
* hot-reload on grouping

d887743
* grouping correction on init

2cd237f
* added shadow to grouping, optional width on grouping stroke

4f4b617
* grouping on bar, converting AlphaColour from_rgba_unmultiplied, simplified grouping

3808fce
* widget rounding based on grouping, atomic background color, simplified config, style on grouping

be45d14
* renamed Side to Alignment, group spacing

be45d14
* proper widget spacing based on alignment

b43a5bd
* added widget_spacing to config

c18e5f4
* test commit

cba2b2f
* refactoring of render and grouping, widget spacing WIP

9311cb0
* simplify no_spacing

36c2672
* correct spacing on komorebi and network widgets (WIP)

85a41bf
* correct widget spacing on all widgets

50b49cc
* refactoring widget spacing

9ec67ad
* account for ui item_spacing when setting the widget_spacing

e88a2fd
* format
  • Loading branch information
CtByte authored and LGUG2Z committed Nov 19, 2024
1 parent e4e94fd commit 219fa8e
Show file tree
Hide file tree
Showing 15 changed files with 719 additions and 363 deletions.
91 changes: 69 additions & 22 deletions komorebi-bar/src/bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ use crate::config::PositionConfig;
use crate::komorebi::Komorebi;
use crate::komorebi::KomorebiNotificationState;
use crate::process_hwnd;
use crate::render::Color32Ext;
use crate::render::Grouping;
use crate::render::RenderConfig;
use crate::render::RenderExt;
use crate::widget::BarWidget;
use crate::widget::WidgetConfig;
use crate::BAR_HEIGHT;
Expand All @@ -24,8 +28,10 @@ use eframe::egui::FontId;
use eframe::egui::Frame;
use eframe::egui::Layout;
use eframe::egui::Margin;
use eframe::egui::Rgba;
use eframe::egui::Style;
use eframe::egui::TextStyle;
use eframe::egui::Visuals;
use font_loader::system_fonts;
use font_loader::system_fonts::FontPropertyBuilder;
use komorebi_client::KomorebiTheme;
Expand All @@ -41,6 +47,7 @@ use std::sync::Arc;

pub struct Komobar {
pub config: Arc<KomobarConfig>,
pub render_config: Rc<RefCell<RenderConfig>>,
pub komorebi_notification_state: Option<Rc<RefCell<KomorebiNotificationState>>>,
pub left_widgets: Vec<Box<dyn BarWidget>>,
pub right_widgets: Vec<Box<dyn BarWidget>>,
Expand Down Expand Up @@ -237,6 +244,30 @@ impl Komobar {
}
}

// apply rounding to the widgets
if let Some(
Grouping::Bar(config) | Grouping::Alignment(config) | Grouping::Widget(config),
) = &config.grouping
{
if let Some(rounding) = config.rounding {
ctx.style_mut(|style| {
style.visuals.widgets.noninteractive.rounding = rounding.into();
style.visuals.widgets.inactive.rounding = rounding.into();
style.visuals.widgets.hovered.rounding = rounding.into();
style.visuals.widgets.active.rounding = rounding.into();
style.visuals.widgets.open.rounding = rounding.into();
});
}
}

let theme_color = *self.bg_color.borrow();

self.render_config
.replace(config.new_renderconfig(theme_color));

self.bg_color
.replace(theme_color.try_apply_alpha(self.config.transparency_alpha));

if let Some(font_size) = &config.font_size {
tracing::info!("attempting to set custom font size: {font_size}");
Self::set_font_size(ctx, *font_size);
Expand All @@ -251,15 +282,15 @@ impl Komobar {
if let WidgetConfig::Komorebi(config) = widget_config {
komorebi_widget = Some(Komorebi::from(config));
komorebi_widget_idx = Some(idx);
side = Some(Side::Left);
side = Some(Alignment::Left);
}
}

for (idx, widget_config) in config.right_widgets.iter().enumerate() {
if let WidgetConfig::Komorebi(config) = widget_config {
komorebi_widget = Some(Komorebi::from(config));
komorebi_widget_idx = Some(idx);
side = Some(Side::Right);
side = Some(Alignment::Right);
}
}

Expand Down Expand Up @@ -293,8 +324,8 @@ impl Komobar {

let boxed: Box<dyn BarWidget> = Box::new(widget);
match side {
Side::Left => left_widgets[idx] = boxed,
Side::Right => right_widgets[idx] = boxed,
Alignment::Left => left_widgets[idx] = boxed,
Alignment::Right => right_widgets[idx] = boxed,
}
}

Expand All @@ -307,6 +338,7 @@ impl Komobar {

self.komorebi_notification_state = komorebi_notification_state;
}

pub fn new(
cc: &eframe::CreationContext<'_>,
rx_gui: Receiver<komorebi_client::Notification>,
Expand All @@ -315,6 +347,7 @@ impl Komobar {
) -> Self {
let mut komobar = Self {
config: config.clone(),
render_config: Rc::new(RefCell::new(RenderConfig::new())),
komorebi_notification_state: None,
left_widgets: vec![],
right_widgets: vec![],
Expand Down Expand Up @@ -385,13 +418,10 @@ impl Komobar {
}
}
impl eframe::App for Komobar {
// TODO: I think this is needed for transparency??
// fn clear_color(&self, _visuals: &Visuals) -> [f32; 4] {
// egui::Rgba::TRANSPARENT.to_array()
// let mut background = Color32::from_gray(18).to_normalized_gamma_f32();
// background[3] = 0.9;
// background
// }
// Needed for transparency
fn clear_color(&self, _visuals: &Visuals) -> [f32; 4] {
Rgba::TRANSPARENT.to_array()
}

fn update(&mut self, ctx: &Context, _frame: &mut eframe::Frame) {
if self.scale_factor != ctx.native_pixels_per_point().unwrap_or(1.0) {
Expand Down Expand Up @@ -433,26 +463,43 @@ impl eframe::App for Komobar {
Frame::none().fill(*self.bg_color.borrow())
};

let mut render_config = self.render_config.borrow_mut();

CentralPanel::default().frame(frame).show(ctx, |ui| {
ui.horizontal_centered(|ui| {
ui.with_layout(Layout::left_to_right(Align::Center), |ui| {
for w in &mut self.left_widgets {
w.render(ctx, ui);
}
});
// Apply grouping logic for the bar as a whole
render_config.clone().apply_on_bar(ui, |ui| {
ui.horizontal_centered(|ui| {
// Left-aligned widgets layout
ui.with_layout(Layout::left_to_right(Align::Center), |ui| {
let mut render_conf = *render_config;
render_conf.alignment = Some(Alignment::Left);

render_config.apply_on_alignment(ui, |ui| {
for w in &mut self.left_widgets {
w.render(ctx, ui, &mut render_conf);
}
});
});

ui.with_layout(Layout::right_to_left(Align::Center), |ui| {
for w in &mut self.right_widgets {
w.render(ctx, ui);
}
// Right-aligned widgets layout
ui.with_layout(Layout::right_to_left(Align::Center), |ui| {
let mut render_conf = *render_config;
render_conf.alignment = Some(Alignment::Right);

render_config.apply_on_alignment(ui, |ui| {
for w in &mut self.right_widgets {
w.render(ctx, ui, &mut render_conf);
}
});
})
})
})
});
}
}

#[derive(Copy, Clone)]
enum Side {
pub enum Alignment {
Left,
Right,
}
18 changes: 9 additions & 9 deletions komorebi-bar/src/battery.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::config::LabelPrefix;
use crate::render::RenderConfig;
use crate::widget::BarWidget;
use crate::WIDGET_SPACING;
use eframe::egui::text::LayoutJob;
use eframe::egui::Context;
use eframe::egui::FontId;
Expand Down Expand Up @@ -115,7 +115,7 @@ impl Battery {
}

impl BarWidget for Battery {
fn render(&mut self, ctx: &Context, ui: &mut Ui) {
fn render(&mut self, ctx: &Context, ui: &mut Ui, config: &mut RenderConfig) {
if self.enable {
let output = self.output();
if !output.is_empty() {
Expand Down Expand Up @@ -147,14 +147,14 @@ impl BarWidget for Battery {
TextFormat::simple(font_id, ctx.style().visuals.text_color()),
);

ui.add(
Label::new(layout_job)
.selectable(false)
.sense(Sense::click()),
);
config.apply_on_widget(true, ui, |ui| {
ui.add(
Label::new(layout_job)
.selectable(false)
.sense(Sense::click()),
);
});
}

ui.add_space(WIDGET_SPACING);
}
}
}
7 changes: 7 additions & 0 deletions komorebi-bar/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::render::Grouping;
use crate::widget::WidgetConfig;
use eframe::egui::Pos2;
use eframe::egui::TextBuffer;
Expand Down Expand Up @@ -28,6 +29,12 @@ pub struct KomobarConfig {
pub max_label_width: Option<f32>,
/// Theme
pub theme: Option<KomobarTheme>,
/// Alpha value for the color transparency [[0-255]] (default: 200)
pub transparency_alpha: Option<u8>,
/// Spacing between widgets (default: 10.0)
pub widget_spacing: Option<f32>,
/// Visual grouping for widgets
pub grouping: Option<Grouping>,
/// Left side widgets (ordered left-to-right)
pub left_widgets: Vec<WidgetConfig>,
/// Right side widgets (ordered left-to-right)
Expand Down
31 changes: 16 additions & 15 deletions komorebi-bar/src/cpu.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::config::LabelPrefix;
use crate::render::RenderConfig;
use crate::widget::BarWidget;
use crate::WIDGET_SPACING;
use eframe::egui::text::LayoutJob;
use eframe::egui::Context;
use eframe::egui::FontId;
Expand Down Expand Up @@ -70,7 +70,7 @@ impl Cpu {
}

impl BarWidget for Cpu {
fn render(&mut self, ctx: &Context, ui: &mut Ui) {
fn render(&mut self, ctx: &Context, ui: &mut Ui, config: &mut RenderConfig) {
if self.enable {
let output = self.output();
if !output.is_empty() {
Expand Down Expand Up @@ -99,22 +99,23 @@ impl BarWidget for Cpu {
TextFormat::simple(font_id, ctx.style().visuals.text_color()),
);

if ui
.add(
Label::new(layout_job)
.selectable(false)
.sense(Sense::click()),
)
.clicked()
{
if let Err(error) = Command::new("cmd.exe").args(["/C", "taskmgr.exe"]).spawn()
config.apply_on_widget(true, ui, |ui| {
if ui
.add(
Label::new(layout_job)
.selectable(false)
.sense(Sense::click()),
)
.clicked()
{
eprintln!("{}", error)
if let Err(error) =
Command::new("cmd.exe").args(["/C", "taskmgr.exe"]).spawn()
{
eprintln!("{}", error)
}
}
}
});
}

ui.add_space(WIDGET_SPACING);
}
}
}
28 changes: 14 additions & 14 deletions komorebi-bar/src/date.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::config::LabelPrefix;
use crate::render::RenderConfig;
use crate::widget::BarWidget;
use crate::WIDGET_SPACING;
use eframe::egui::text::LayoutJob;
use eframe::egui::Context;
use eframe::egui::FontId;
Expand Down Expand Up @@ -86,7 +86,7 @@ impl Date {
}

impl BarWidget for Date {
fn render(&mut self, ctx: &Context, ui: &mut Ui) {
fn render(&mut self, ctx: &Context, ui: &mut Ui, config: &mut RenderConfig) {
if self.enable {
let mut output = self.output();
if !output.is_empty() {
Expand Down Expand Up @@ -119,19 +119,19 @@ impl BarWidget for Date {
TextFormat::simple(font_id, ctx.style().visuals.text_color()),
);

if ui
.add(
Label::new(WidgetText::LayoutJob(layout_job.clone()))
.selectable(false)
.sense(Sense::click()),
)
.clicked()
{
self.format.next()
}
config.apply_on_widget(true, ui, |ui| {
if ui
.add(
Label::new(WidgetText::LayoutJob(layout_job.clone()))
.selectable(false)
.sense(Sense::click()),
)
.clicked()
{
self.format.next()
}
});
}

ui.add_space(WIDGET_SPACING);
}
}
}
Loading

0 comments on commit 219fa8e

Please sign in to comment.