From 645c46beb86793225ed58db0ed417ad13dae6802 Mon Sep 17 00:00:00 2001 From: Csaba Date: Sat, 9 Nov 2024 17:38:30 +0100 Subject: [PATCH] background color using theme color, AlphaColour.to_color32_or, updating json format for Grouping and RoundingConfig --- komorebi-bar/src/bar.rs | 17 +++++++++-------- komorebi-bar/src/config.rs | 14 +++++++++----- komorebi-bar/src/group.rs | 6 ++++-- komorebi-bar/src/widget.rs | 2 -- 4 files changed, 22 insertions(+), 17 deletions(-) diff --git a/komorebi-bar/src/bar.rs b/komorebi-bar/src/bar.rs index 6a2d8f06..eaab81d6 100644 --- a/komorebi-bar/src/bar.rs +++ b/komorebi-bar/src/bar.rs @@ -326,7 +326,6 @@ impl Komobar { Some(grouping) => grouping, None => Grouping::None, }, - background_color: config.background.map(|value| value.into()), })), komorebi_notification_state: None, left_widgets: vec![], @@ -432,26 +431,28 @@ impl eframe::App for Komobar { ); } + let theme_color = *self.bg_color.borrow(); + + if let Some(background) = self.config.background { + self.bg_color + .replace(background.to_color32_or(Some(theme_color))); + } + let frame = if let Some(frame) = &self.config.frame { Frame::none() .inner_margin(Margin::symmetric( frame.inner_margin.x, frame.inner_margin.y, )) - .fill(*self.bg_color.borrow()) + .fill(theme_color) } else { - Frame::none().fill(*self.bg_color.borrow()) + Frame::none().fill(theme_color) }; // NOTE: is there a better way? let mut render_config = self.render_config.borrow_mut(); let render_config_clone = *render_config; - // prefer the custom background from the config over the theme background - if let Some(bg_color) = render_config.background_color { - self.bg_color.replace(bg_color); - }; - CentralPanel::default().frame(frame).show(ctx, |ui| { ui.horizontal_centered(|ui| { ui.with_layout(Layout::left_to_right(Align::Center), |ui| { diff --git a/komorebi-bar/src/config.rs b/komorebi-bar/src/config.rs index 63782916..72817788 100644 --- a/komorebi-bar/src/config.rs +++ b/komorebi-bar/src/config.rs @@ -194,14 +194,18 @@ pub struct AlphaColour { pub transparency_alpha: Option, } -impl From for Color32 { - fn from(value: AlphaColour) -> Self { - let color = match value.color { +impl AlphaColour { + /// Returns an Rgb or Rgba color using the alpha, and default_color or Rgb(0,0,0) + pub fn to_color32_or(self, default_color: Option) -> Color32 { + let color = match self.color { Some(color) => color.into(), - None => Color32::from_rgb(0, 0, 0), + None => match default_color { + Some(color) => color, + None => Color32::from_rgb(0, 0, 0), + }, }; - if let Some(alpha) = value.transparency_alpha { + if let Some(alpha) = self.transparency_alpha { return Color32::from_rgba_premultiplied(color.r(), color.g(), color.b(), alpha); } diff --git a/komorebi-bar/src/group.rs b/komorebi-bar/src/group.rs index 120bcb22..264ea5ed 100644 --- a/komorebi-bar/src/group.rs +++ b/komorebi-bar/src/group.rs @@ -13,6 +13,7 @@ use serde::Deserialize; use serde::Serialize; #[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema)] +#[serde(tag = "kind")] pub enum Grouping { /// No grouping is applied None, @@ -31,7 +32,7 @@ impl Grouping { match self { Self::Widget(config) => Frame::none() .fill(match config.fill { - Some(color) => color.into(), + Some(color) => color.to_color32_or(None), None => Color32::TRANSPARENT, }) .outer_margin(match config.outer_margin { @@ -74,7 +75,7 @@ impl Grouping { }, Self::Side(config) => Frame::none() .fill(match config.fill { - Some(color) => color.into(), + Some(color) => color.to_color32_or(None), None => Color32::TRANSPARENT, }) .outer_margin(match config.outer_margin { @@ -139,6 +140,7 @@ impl From for Stroke { } #[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema)] +#[serde(untagged)] pub enum RoundingConfig { /// All 4 corners are the same Same(f32), diff --git a/komorebi-bar/src/widget.rs b/komorebi-bar/src/widget.rs index 027db543..5f9f9584 100644 --- a/komorebi-bar/src/widget.rs +++ b/komorebi-bar/src/widget.rs @@ -17,7 +17,6 @@ use crate::storage::Storage; use crate::storage::StorageConfig; use crate::time::Time; use crate::time::TimeConfig; -use eframe::egui::Color32; use eframe::egui::Context; use eframe::egui::Ui; use schemars::JsonSchema; @@ -32,7 +31,6 @@ pub trait BarWidget { pub struct RenderConfig { /// Sets how widgets are grouped pub grouping: Grouping, - pub background_color: Option, } #[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]