diff --git a/crates/komorebi-bar-core/src/lib.rs b/crates/komorebi-bar-core/src/lib.rs index 1ebe0cfe..96d08e22 100644 --- a/crates/komorebi-bar-core/src/lib.rs +++ b/crates/komorebi-bar-core/src/lib.rs @@ -12,3 +12,5 @@ pub mod alignment; pub mod statics; pub mod config; + +pub mod render; diff --git a/komorebi-bar/src/render.rs b/crates/komorebi-bar-core/src/render.rs similarity index 99% rename from komorebi-bar/src/render.rs rename to crates/komorebi-bar-core/src/render.rs index 5546aa0b..a8fdadb9 100644 --- a/komorebi-bar/src/render.rs +++ b/crates/komorebi-bar-core/src/render.rs @@ -1,4 +1,4 @@ -use crate::bar::Alignment; +use crate::alignment::Alignment; use eframe::egui::Color32; use eframe::egui::Context; use eframe::egui::CornerRadius; @@ -82,6 +82,7 @@ impl RenderConfig { SHOW_KOMOREBI_LAYOUT_OPTIONS.store(show as usize, Ordering::SeqCst); } + #[allow(clippy::new_without_default)] pub fn new() -> Self { Self { monitor_idx: 0, diff --git a/komorebi-bar/src/main.rs b/komorebi-bar/src/main.rs index 328c6df8..4ca843f5 100644 --- a/komorebi-bar/src/main.rs +++ b/komorebi-bar/src/main.rs @@ -1,6 +1,6 @@ mod bar; mod config; -mod render; +pub use komorebi_bar_core::render; pub use komorebi_bar_core::selected_frame; pub use komorebi_bar_core::ui; mod widgets; diff --git a/komorebi-for-mac/komorebi-bar/src/main.rs b/komorebi-for-mac/komorebi-bar/src/main.rs index fcd1e080..857092c2 100644 --- a/komorebi-for-mac/komorebi-bar/src/main.rs +++ b/komorebi-for-mac/komorebi-bar/src/main.rs @@ -1,6 +1,6 @@ mod bar; mod config; -mod render; +pub use komorebi_bar_core::render; pub use komorebi_bar_core::selected_frame; pub use komorebi_bar_core::ui; mod widgets; diff --git a/komorebi-for-mac/komorebi-bar/src/render.rs b/komorebi-for-mac/komorebi-bar/src/render.rs deleted file mode 100644 index 5546aa0b..00000000 --- a/komorebi-for-mac/komorebi-bar/src/render.rs +++ /dev/null @@ -1,382 +0,0 @@ -use crate::bar::Alignment; -use eframe::egui::Color32; -use eframe::egui::Context; -use eframe::egui::CornerRadius; -use eframe::egui::FontId; -use eframe::egui::Frame; -use eframe::egui::InnerResponse; -use eframe::egui::Margin; -use eframe::egui::Shadow; -use eframe::egui::Ui; -use serde::Deserialize; -use serde::Serialize; -use std::sync::Arc; -use std::sync::atomic::AtomicUsize; -use std::sync::atomic::Ordering; - -static SHOW_KOMOREBI_LAYOUT_OPTIONS: AtomicUsize = AtomicUsize::new(0); - -#[derive(Copy, Clone, Debug, Serialize, Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] -#[serde(tag = "kind")] -/// Grouping -pub enum Grouping { - /// No grouping is applied - #[cfg_attr(feature = "schemars", schemars(title = "None"))] - None, - /// Widgets are grouped as a whole - #[cfg_attr(feature = "schemars", schemars(title = "Bar"))] - Bar(GroupingConfig), - /// Widgets are grouped by alignment - #[cfg_attr(feature = "schemars", schemars(title = "Alignment"))] - Alignment(GroupingConfig), - /// Widgets are grouped individually - #[cfg_attr(feature = "schemars", schemars(title = "Widget"))] - Widget(GroupingConfig), -} - -#[derive(Clone)] -/// Render configuration -pub struct RenderConfig { - /// Komorebi monitor index of the monitor on which to render the bar - pub monitor_idx: usize, - /// Spacing between widgets - pub spacing: f32, - /// Sets how widgets are grouped - pub grouping: Grouping, - /// Background color - pub background_color: Color32, - /// Alignment of the widgets - pub alignment: Option, - /// Add more inner margin when adding a widget group - pub more_inner_margin: bool, - /// Set to true after the first time the apply_on_widget was called on an alignment - pub applied_on_widget: bool, - /// FontId for text - pub text_font_id: FontId, - /// FontId for icon (based on scaling the text font id) - pub icon_font_id: FontId, - /// Show all icons on the workspace section of the Komorebi widget - pub show_all_icons: bool, - /// Background color of the selected frame - pub auto_select_fill: Option, - /// Text color of the selected frame - pub auto_select_text: Option, -} - -pub trait RenderExt { - fn new_renderconfig( - &self, - ctx: &Context, - background_color: Color32, - icon_scale: Option, - ) -> RenderConfig; -} - -impl RenderConfig { - pub fn load_show_komorebi_layout_options() -> bool { - SHOW_KOMOREBI_LAYOUT_OPTIONS.load(Ordering::SeqCst) != 0 - } - - pub fn store_show_komorebi_layout_options(show: bool) { - SHOW_KOMOREBI_LAYOUT_OPTIONS.store(show as usize, Ordering::SeqCst); - } - - pub fn new() -> Self { - Self { - monitor_idx: 0, - spacing: 0.0, - grouping: Grouping::None, - background_color: Color32::BLACK, - alignment: None, - more_inner_margin: false, - applied_on_widget: false, - text_font_id: FontId::default(), - icon_font_id: FontId::default(), - show_all_icons: false, - auto_select_fill: None, - auto_select_text: None, - } - } - - pub fn change_frame_on_bar( - &mut self, - frame: Frame, - ui_style: &Arc, - ) -> Frame { - self.alignment = None; - - if let Grouping::Bar(config) = self.grouping { - return self.define_group_frame( - //TODO: this outer margin can be a config - Some(Margin { - left: 10, - right: 10, - top: 6, - bottom: 6, - }), - config, - ui_style, - ); - } - - frame - } - - pub fn apply_on_alignment( - &mut self, - ui: &mut Ui, - add_contents: impl FnOnce(&mut Ui) -> R, - ) -> InnerResponse { - self.alignment = None; - - if let Grouping::Alignment(config) = self.grouping { - return self.define_group(None, config, ui, add_contents); - } - - Self::fallback_group(ui, add_contents) - } - - pub fn apply_on_widget( - &mut self, - more_inner_margin: bool, - ui: &mut Ui, - add_contents: impl FnOnce(&mut Ui) -> R, - ) -> InnerResponse { - self.more_inner_margin = more_inner_margin; - let outer_margin = self.widget_outer_margin(ui); - - if let Grouping::Widget(config) = self.grouping { - return self.define_group(Some(outer_margin), config, ui, add_contents); - } - - self.fallback_widget_group(Some(outer_margin), ui, add_contents) - } - - fn fallback_group(ui: &mut Ui, add_contents: impl FnOnce(&mut Ui) -> R) -> InnerResponse { - InnerResponse { - inner: add_contents(ui), - response: ui.response().clone(), - } - } - - fn fallback_widget_group( - &mut self, - outer_margin: Option, - ui: &mut Ui, - add_contents: impl FnOnce(&mut Ui) -> R, - ) -> InnerResponse { - Frame::NONE - .outer_margin(outer_margin.unwrap_or(Margin::ZERO)) - .inner_margin(match self.more_inner_margin { - true => Margin::symmetric(5, 0), - false => Margin::same(0), - }) - .show(ui, add_contents) - } - - fn define_group( - &mut self, - outer_margin: Option, - config: GroupingConfig, - ui: &mut Ui, - add_contents: impl FnOnce(&mut Ui) -> R, - ) -> InnerResponse { - self.define_group_frame(outer_margin, config, ui.style()) - .show(ui, add_contents) - } - - pub fn define_group_frame( - &mut self, - outer_margin: Option, - config: GroupingConfig, - ui_style: &Arc, - ) -> Frame { - Frame::group(ui_style) - .outer_margin(outer_margin.unwrap_or(Margin::ZERO)) - .inner_margin(match self.more_inner_margin { - true => Margin::symmetric(6, 1), - false => Margin::symmetric(1, 1), - }) - .stroke(ui_style.visuals.widgets.noninteractive.bg_stroke) - .corner_radius(match config.rounding { - Some(rounding) => rounding.into(), - None => ui_style.visuals.widgets.noninteractive.corner_radius, - }) - .fill( - self.background_color - .try_apply_alpha(config.transparency_alpha), - ) - .shadow(match config.style { - Some(style) => match style { - // new styles can be added if needed here - GroupingStyle::Default => Shadow::NONE, - GroupingStyle::DefaultWithShadowB4O1S3 => Shadow { - blur: 4, - offset: [1, 1], - spread: 3, - color: Color32::BLACK.try_apply_alpha(config.transparency_alpha), - }, - GroupingStyle::DefaultWithShadowB4O0S3 => Shadow { - blur: 4, - offset: [0, 0], - spread: 3, - color: Color32::BLACK.try_apply_alpha(config.transparency_alpha), - }, - GroupingStyle::DefaultWithShadowB0O1S3 => Shadow { - blur: 0, - offset: [1, 1], - spread: 3, - color: Color32::BLACK.try_apply_alpha(config.transparency_alpha), - }, - GroupingStyle::DefaultWithGlowB3O1S2 => Shadow { - blur: 3, - offset: [1, 1], - spread: 2, - color: ui_style - .visuals - .selection - .stroke - .color - .try_apply_alpha(config.transparency_alpha), - }, - GroupingStyle::DefaultWithGlowB3O0S2 => Shadow { - blur: 3, - offset: [0, 0], - spread: 2, - color: ui_style - .visuals - .selection - .stroke - .color - .try_apply_alpha(config.transparency_alpha), - }, - GroupingStyle::DefaultWithGlowB0O1S2 => Shadow { - blur: 0, - offset: [1, 1], - spread: 2, - color: ui_style - .visuals - .selection - .stroke - .color - .try_apply_alpha(config.transparency_alpha), - }, - }, - None => Shadow::NONE, - }) - } - - fn widget_outer_margin(&mut self, ui: &mut Ui) -> Margin { - let spacing = if self.applied_on_widget { - // Remove the default item spacing from the margin - (self.spacing - ui.spacing().item_spacing.x) as i8 - } else { - 0 - }; - - if !self.applied_on_widget { - self.applied_on_widget = true; - } - - Margin { - left: match self.alignment { - Some(align) => match align { - Alignment::Left => spacing, - Alignment::Center => spacing, - Alignment::Right => 0, - }, - None => 0, - }, - right: match self.alignment { - Some(align) => match align { - Alignment::Left => 0, - Alignment::Center => 0, - Alignment::Right => spacing, - }, - None => 0, - }, - top: 0, - bottom: 0, - } - } -} - -#[derive(Copy, Clone, Debug, Serialize, Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] -/// Grouping configuration -pub struct GroupingConfig { - /// Styles for the grouping - pub style: Option, - /// Alpha value for the color transparency [[0-255]] - #[cfg_attr(feature = "schemars", schemars(extend("default" = 200)))] - pub transparency_alpha: Option, - /// Rounding values for the 4 corners. Can be a single or 4 values. - pub rounding: Option, -} - -#[derive(Copy, Clone, Debug, Serialize, Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] -/// Grouping Style -pub enum GroupingStyle { - /// Default - #[serde(alias = "CtByte")] - Default, - /// A shadow is added under the default group. (blur: 4, offset: x-1 y-1, spread: 3) - #[serde(alias = "CtByteWithShadow")] - #[serde(alias = "DefaultWithShadow")] - DefaultWithShadowB4O1S3, - /// A shadow is added under the default group. (blur: 4, offset: x-0 y-0, spread: 3) - DefaultWithShadowB4O0S3, - /// A shadow is added under the default group. (blur: 0, offset: x-1 y-1, spread: 3) - DefaultWithShadowB0O1S3, - /// A glow is added under the default group. (blur: 3, offset: x-1 y-1, spread: 2) - DefaultWithGlowB3O1S2, - /// A glow is added under the default group. (blur: 3, offset: x-0 y-0, spread: 2) - DefaultWithGlowB3O0S2, - /// A glow is added under the default group. (blur: 0, offset: x-1 y-1, spread: 2) - DefaultWithGlowB0O1S2, -} - -#[derive(Copy, Clone, Debug, Serialize, Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] -#[serde(untagged)] -/// Rounding configuration -pub enum RoundingConfig { - /// All 4 corners are the same - Same(f32), - /// All 4 corners are custom. Order: NW, NE, SW, SE - Individual([f32; 4]), -} - -impl From for CornerRadius { - fn from(value: RoundingConfig) -> Self { - match value { - RoundingConfig::Same(value) => Self::same(value as u8), - RoundingConfig::Individual(values) => { - let values = values.map(|f| f as u8); - Self { - nw: values[0], - ne: values[1], - sw: values[2], - se: values[3], - } - } - } - } -} - -pub trait Color32Ext { - fn try_apply_alpha(self, transparency_alpha: Option) -> Self; -} - -impl Color32Ext for Color32 { - /// Tries to apply the alpha value to the Color32 - fn try_apply_alpha(self, transparency_alpha: Option) -> Self { - if let Some(alpha) = transparency_alpha { - return Color32::from_rgba_unmultiplied(self.r(), self.g(), self.b(), alpha); - } - - self - } -}