Skip to content

Commit

Permalink
feat(bar): scaled icon size relying on the font size
Browse files Browse the repository at this point in the history
  • Loading branch information
CtByte authored and LGUG2Z committed Dec 17, 2024
1 parent 804faef commit 81f3eb2
Show file tree
Hide file tree
Showing 13 changed files with 124 additions and 134 deletions.
12 changes: 6 additions & 6 deletions komorebi-bar/src/bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,6 @@ impl Komobar {

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(config.transparency_alpha));

Expand All @@ -281,6 +278,9 @@ impl Komobar {
Self::set_font_size(ctx, *font_size);
}

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

let mut komorebi_widget = None;
let mut komorebi_widget_idx = None;
let mut komorebi_notification_state = previous_notification_state;
Expand Down Expand Up @@ -503,7 +503,7 @@ impl eframe::App for Komobar {
ui.horizontal_centered(|ui| {
// Left-aligned widgets layout
ui.with_layout(Layout::left_to_right(Align::Center), |ui| {
let mut render_conf = *render_config;
let mut render_conf = render_config.clone();
render_conf.alignment = Some(Alignment::Left);

render_config.apply_on_alignment(ui, |ui| {
Expand All @@ -515,7 +515,7 @@ impl eframe::App for Komobar {

// Right-aligned widgets layout
ui.with_layout(Layout::right_to_left(Align::Center), |ui| {
let mut render_conf = *render_config;
let mut render_conf = render_config.clone();
render_conf.alignment = Some(Alignment::Right);

render_config.apply_on_alignment(ui, |ui| {
Expand All @@ -532,7 +532,7 @@ impl eframe::App for Komobar {
.show(ctx, |ui| {
Frame::none().show(ui, |ui| {
ui.horizontal_centered(|ui| {
let mut render_conf = *render_config;
let mut render_conf = render_config.clone();
render_conf.alignment = Some(Alignment::Center);

render_config.apply_on_alignment(ui, |ui| {
Expand Down
19 changes: 8 additions & 11 deletions komorebi-bar/src/battery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ use crate::config::LabelPrefix;
use crate::render::RenderConfig;
use crate::widget::BarWidget;
use eframe::egui::text::LayoutJob;
use eframe::egui::Align;
use eframe::egui::Context;
use eframe::egui::FontId;
use eframe::egui::Label;
use eframe::egui::Sense;
use eframe::egui::TextFormat;
use eframe::egui::TextStyle;
use eframe::egui::Ui;
use schemars::JsonSchema;
use serde::Deserialize;
Expand Down Expand Up @@ -105,27 +104,25 @@ impl BarWidget for Battery {
BatteryState::Discharging => egui_phosphor::regular::BATTERY_FULL,
};

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 => emoji.to_string(),
LabelPrefix::None | LabelPrefix::Text => String::new(),
},
font_id.clone(),
config.icon_font_id.clone(),
ctx.style().visuals.selection.stroke.color,
100.0,
);

layout_job.append(
&output,
10.0,
TextFormat::simple(font_id, ctx.style().visuals.text_color()),
TextFormat {
font_id: config.text_font_id.clone(),
color: ctx.style().visuals.text_color(),
valign: Align::Center,
..Default::default()
},
);

config.apply_on_widget(true, ui, |ui| {
Expand Down
19 changes: 8 additions & 11 deletions komorebi-bar/src/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ use crate::render::RenderConfig;
use crate::selected_frame::SelectableFrame;
use crate::widget::BarWidget;
use eframe::egui::text::LayoutJob;
use eframe::egui::Align;
use eframe::egui::Context;
use eframe::egui::FontId;
use eframe::egui::Label;
use eframe::egui::TextFormat;
use eframe::egui::TextStyle;
use eframe::egui::Ui;
use schemars::JsonSchema;
use serde::Deserialize;
Expand Down Expand Up @@ -75,29 +74,27 @@ impl BarWidget for Cpu {
if self.enable {
let output = self.output();
if !output.is_empty() {
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::CPU.to_string()
}
LabelPrefix::None | LabelPrefix::Text => String::new(),
},
font_id.clone(),
config.icon_font_id.clone(),
ctx.style().visuals.selection.stroke.color,
100.0,
);

layout_job.append(
&output,
10.0,
TextFormat::simple(font_id, ctx.style().visuals.text_color()),
TextFormat {
font_id: config.text_font_id.clone(),
color: ctx.style().visuals.text_color(),
valign: Align::Center,
..Default::default()
},
);

config.apply_on_widget(false, ui, |ui| {
Expand Down
19 changes: 8 additions & 11 deletions komorebi-bar/src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ use crate::render::RenderConfig;
use crate::selected_frame::SelectableFrame;
use crate::widget::BarWidget;
use eframe::egui::text::LayoutJob;
use eframe::egui::Align;
use eframe::egui::Context;
use eframe::egui::FontId;
use eframe::egui::Label;
use eframe::egui::TextFormat;
use eframe::egui::TextStyle;
use eframe::egui::Ui;
use eframe::egui::WidgetText;
use schemars::JsonSchema;
Expand Down Expand Up @@ -90,21 +89,14 @@ impl BarWidget for Date {
if self.enable {
let mut output = self.output();
if !output.is_empty() {
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::CALENDAR_DOTS.to_string()
}
LabelPrefix::None | LabelPrefix::Text => String::new(),
},
font_id.clone(),
config.icon_font_id.clone(),
ctx.style().visuals.selection.stroke.color,
100.0,
);
Expand All @@ -116,7 +108,12 @@ impl BarWidget for Date {
layout_job.append(
&output,
10.0,
TextFormat::simple(font_id, ctx.style().visuals.text_color()),
TextFormat {
font_id: config.text_font_id.clone(),
color: ctx.style().visuals.text_color(),
valign: Align::Center,
..Default::default()
},
);

config.apply_on_widget(false, ui, |ui| {
Expand Down
16 changes: 4 additions & 12 deletions komorebi-bar/src/komorebi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@ use eframe::egui::vec2;
use eframe::egui::Color32;
use eframe::egui::ColorImage;
use eframe::egui::Context;
use eframe::egui::FontId;
use eframe::egui::Frame;
use eframe::egui::Image;
use eframe::egui::Label;
use eframe::egui::Margin;
use eframe::egui::Rounding;
use eframe::egui::Sense;
use eframe::egui::Stroke;
use eframe::egui::TextStyle;
use eframe::egui::TextureHandle;
use eframe::egui::TextureOptions;
use eframe::egui::Ui;
Expand Down Expand Up @@ -141,6 +139,7 @@ pub struct Komorebi {
impl BarWidget for Komorebi {
fn render(&mut self, ctx: &Context, ui: &mut Ui, config: &mut RenderConfig) {
let mut komorebi_notification_state = self.komorebi_notification_state.borrow_mut();
let icon_size = Vec2::splat(config.icon_font_id.size);

if self.workspaces.enable {
let mut update = None;
Expand Down Expand Up @@ -172,7 +171,7 @@ impl BarWidget for Komorebi {
ui.add(
Image::from(&img_to_texture(ctx, icon))
.maintain_aspect_ratio(true)
.shrink_to_fit(),
.fit_to_exact_size(icon_size),
);

if !has_icon {
Expand All @@ -188,15 +187,8 @@ impl BarWidget for Komorebi {
DisplayFormat::Icon => !has_icon,
_ => false,
} {
let font_id = ctx
.style()
.text_styles
.get(&TextStyle::Body)
.cloned()
.unwrap_or_else(FontId::default);

let (response, painter) =
ui.allocate_painter(Vec2::splat(font_id.size), Sense::hover());
ui.allocate_painter(icon_size, Sense::hover());
let stroke =
Stroke::new(1.0, ctx.style().visuals.selection.stroke.color);
let mut rect = response.rect;
Expand Down Expand Up @@ -388,7 +380,7 @@ impl BarWidget for Komorebi {
let response = ui.add(
Image::from(&img_to_texture(ctx, img))
.maintain_aspect_ratio(true)
.shrink_to_fit(),
.fit_to_exact_size(icon_size),
);

if let DisplayFormat::Icon = format {
Expand Down
9 changes: 1 addition & 8 deletions komorebi-bar/src/komorebi_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use eframe::egui::Label;
use eframe::egui::Rounding;
use eframe::egui::Sense;
use eframe::egui::Stroke;
use eframe::egui::TextStyle;
use eframe::egui::Ui;
use eframe::egui::Vec2;
use komorebi_client::SocketMessage;
Expand Down Expand Up @@ -225,13 +224,7 @@ impl KomorebiLayout {
workspace_idx: Option<usize>,
) {
let monitor_idx = render_config.monitor_idx;
let font_id = ctx
.style()
.text_styles
.get(&TextStyle::Body)
.cloned()
.unwrap_or_else(FontId::default);

let font_id = render_config.icon_font_id.clone();
let mut show_options = RenderConfig::load_show_komorebi_layout_options();
let format = layout_config.display.unwrap_or(DisplayFormat::IconAndText);

Expand Down
19 changes: 8 additions & 11 deletions komorebi-bar/src/media.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ use crate::ui::CustomUi;
use crate::widget::BarWidget;
use crate::MAX_LABEL_WIDTH;
use eframe::egui::text::LayoutJob;
use eframe::egui::Align;
use eframe::egui::Context;
use eframe::egui::FontId;
use eframe::egui::Label;
use eframe::egui::TextFormat;
use eframe::egui::TextStyle;
use eframe::egui::Ui;
use eframe::egui::Vec2;
use schemars::JsonSchema;
Expand Down Expand Up @@ -82,24 +81,22 @@ impl BarWidget for Media {
if self.enable {
let output = self.output();
if !output.is_empty() {
let font_id = ctx
.style()
.text_styles
.get(&TextStyle::Body)
.cloned()
.unwrap_or_else(FontId::default);

let mut layout_job = LayoutJob::simple(
egui_phosphor::regular::HEADPHONES.to_string(),
font_id.clone(),
config.icon_font_id.clone(),
ctx.style().visuals.selection.stroke.color,
100.0,
);

layout_job.append(
&output,
10.0,
TextFormat::simple(font_id, ctx.style().visuals.text_color()),
TextFormat {
font_id: config.text_font_id.clone(),
color: ctx.style().visuals.text_color(),
valign: Align::Center,
..Default::default()
},
);

config.apply_on_widget(false, ui, |ui| {
Expand Down
19 changes: 8 additions & 11 deletions komorebi-bar/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ use crate::render::RenderConfig;
use crate::selected_frame::SelectableFrame;
use crate::widget::BarWidget;
use eframe::egui::text::LayoutJob;
use eframe::egui::Align;
use eframe::egui::Context;
use eframe::egui::FontId;
use eframe::egui::Label;
use eframe::egui::TextFormat;
use eframe::egui::TextStyle;
use eframe::egui::Ui;
use schemars::JsonSchema;
use serde::Deserialize;
Expand Down Expand Up @@ -78,29 +77,27 @@ impl BarWidget for Memory {
if self.enable {
let output = self.output();
if !output.is_empty() {
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::MEMORY.to_string()
}
LabelPrefix::None | LabelPrefix::Text => String::new(),
},
font_id.clone(),
config.icon_font_id.clone(),
ctx.style().visuals.selection.stroke.color,
100.0,
);

layout_job.append(
&output,
10.0,
TextFormat::simple(font_id, ctx.style().visuals.text_color()),
TextFormat {
font_id: config.text_font_id.clone(),
color: ctx.style().visuals.text_color(),
valign: Align::Center,
..Default::default()
},
);

config.apply_on_widget(false, ui, |ui| {
Expand Down
Loading

0 comments on commit 81f3eb2

Please sign in to comment.