Skip to content

Commit

Permalink
feat(bar): hotloading for viewport inner_size
Browse files Browse the repository at this point in the history
This commit adds hot reloading for changes made to viewport.inner_size
in the configuration file. I still don't understand how the scaling
works with egui, but at least for the time being there are some rough
heuristics I've thrown together.

The transformation of y still seems a little off, but the transformation
of x seems pretty accurate when dividing by native_pixels_per_point.
  • Loading branch information
LGUG2Z committed Sep 15, 2024
1 parent b69db86 commit de3d4d0
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions komorebi-bar/src/bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ use eframe::egui::Frame;
use eframe::egui::Layout;
use eframe::egui::Margin;
use eframe::egui::Style;
use eframe::egui::Vec2;
use eframe::egui::ViewportCommand;
use font_loader::system_fonts;
use font_loader::system_fonts::FontPropertyBuilder;
use komorebi_themes::catppuccin_egui;
Expand All @@ -33,6 +35,7 @@ pub struct Komobar {
pub rx_gui: Receiver<komorebi_client::Notification>,
pub rx_config: Receiver<KomobarConfig>,
pub bg_color: Rc<RefCell<Color32>>,
pub scale_factor: f32,
}

pub fn apply_theme(ctx: &Context, theme: KomobarTheme, bg_color: Rc<RefCell<Color32>>) {
Expand Down Expand Up @@ -129,6 +132,17 @@ impl Komobar {
Self::add_custom_font(ctx, font_family);
}

if let Some(viewport) = &config.viewport {
if let Some(inner_size) = viewport.inner_size {
let mut vec2 = Vec2::new(inner_size.x, inner_size.y * 2.0);
if self.scale_factor != 1.0 {
vec2 = Vec2::new(inner_size.x / self.scale_factor, inner_size.y * 2.0);
}

ctx.send_viewport_cmd(ViewportCommand::InnerSize(vec2));
}
}

match config.theme {
Some(theme) => {
apply_theme(ctx, theme, self.bg_color.clone());
Expand Down Expand Up @@ -241,6 +255,7 @@ impl Komobar {
rx_gui,
rx_config,
bg_color: Rc::new(RefCell::new(Style::default().visuals.panel_fill)),
scale_factor: cc.egui_ctx.native_pixels_per_point().unwrap_or(1.0),
};

komobar.apply_config(&cc.egui_ctx, &config, None);
Expand Down Expand Up @@ -286,6 +301,15 @@ impl eframe::App for Komobar {
// }

fn update(&mut self, ctx: &Context, _frame: &mut eframe::Frame) {
if self.scale_factor != ctx.native_pixels_per_point().unwrap_or(1.0) {
self.scale_factor = ctx.native_pixels_per_point().unwrap_or(1.0);
self.apply_config(
ctx,
&self.config.clone(),
self.komorebi_notification_state.clone(),
);
}

if let Ok(updated_config) = self.rx_config.try_recv() {
self.apply_config(
ctx,
Expand Down

0 comments on commit de3d4d0

Please sign in to comment.