diff --git a/Cargo.lock b/Cargo.lock index 8fbf03ef..48991dcc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3437,6 +3437,9 @@ name = "komorebi-bar-core" version = "0.1.42" dependencies = [ "eframe", + "schemars 1.2.3", + "serde", + "serde_json_lenient", ] [[package]] diff --git a/crates/komorebi-bar-core/Cargo.toml b/crates/komorebi-bar-core/Cargo.toml index 796901a9..fff7ba05 100644 --- a/crates/komorebi-bar-core/Cargo.toml +++ b/crates/komorebi-bar-core/Cargo.toml @@ -6,3 +6,14 @@ edition = "2024" [dependencies] eframe = { workspace = true } +serde = { workspace = true } + +# Optional dependencies +schemars = { workspace = true, optional = true } + +[dev-dependencies] +serde_json = { workspace = true } + +[features] +default = ["schemars"] +schemars = ["dep:schemars"] diff --git a/crates/komorebi-bar-core/src/config/mod.rs b/crates/komorebi-bar-core/src/config/mod.rs new file mode 100644 index 00000000..cd408564 --- /dev/null +++ b/crates/komorebi-bar-core/src/config/mod.rs @@ -0,0 +1 @@ +pub mod types; diff --git a/crates/komorebi-bar-core/src/config/types.rs b/crates/komorebi-bar-core/src/config/types.rs new file mode 100644 index 00000000..cd29b8b3 --- /dev/null +++ b/crates/komorebi-bar-core/src/config/types.rs @@ -0,0 +1,173 @@ +use eframe::egui::Pos2; +use eframe::egui::Vec2; +use serde::Deserialize; +use serde::Serialize; + +#[derive(Copy, Clone, Debug, Serialize, Deserialize)] +#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +/// Position +pub struct Position { + /// X coordinate + pub x: f32, + /// Y coordinate + pub y: f32, +} + +impl From for Vec2 { + fn from(value: Position) -> Self { + Self { + x: value.x, + y: value.y, + } + } +} + +impl From for Pos2 { + fn from(value: Position) -> Self { + Self { + x: value.x, + y: value.y, + } + } +} + +#[derive(Copy, Clone, Debug, Serialize, Deserialize)] +#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +/// Label prefix +pub enum LabelPrefix { + /// Show no prefix + None, + /// Show an icon + Icon, + /// Show text + Text, + /// Show an icon and text + IconAndText, +} + +#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq)] +#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +/// Display format +pub enum DisplayFormat { + /// Show only icon + Icon, + /// Show only text + Text, + /// Show an icon and text for the selected element, and text on the rest + TextAndIconOnSelected, + /// Show both icon and text + IconAndText, + /// Show an icon and text for the selected element, and icons on the rest + IconAndTextOnSelected, +} + +macro_rules! extend_enum { + ($(#[$type_meta:meta])* $existing_enum:ident, $new_enum:ident, { $($(#[$meta:meta])* $variant:ident),* $(,)? }) => { + #[derive(Copy, Clone, Debug, serde::Serialize, serde::Deserialize, PartialEq)] + #[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] + $(#[$type_meta])* + pub enum $new_enum { + // Add new variants + $( + $(#[$meta])* + $variant, + )* + // Include a variant that wraps the existing enum and flatten it when deserializing + #[serde(untagged)] + Existing($existing_enum), + } + + // Implement From for the existing enum + impl From<$existing_enum> for $new_enum { + fn from(value: $existing_enum) -> Self { + $new_enum::Existing(value) + } + } + }; +} + +extend_enum!( + /// Workspaces display format + DisplayFormat, WorkspacesDisplayFormat, { + /// Show all icons only + AllIcons, + /// Show both all icons and text + AllIconsAndText, + /// Show all icons and text for the selected element, and all icons on the rest + AllIconsAndTextOnSelected, +}); + +#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq)] +#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +/// Media widget display format +pub enum MediaDisplayFormat { + /// Show only the media info icon + Icon, + /// Show only the media info text (artist - title) + Text, + /// Show both icon and text + IconAndText, + /// Show only the control buttons (previous, play/pause, next) + ControlsOnly, + /// Show icon with control buttons + IconAndControls, + /// Show text with control buttons + TextAndControls, + /// Show icon, text, and control buttons + Full, +} + +#[cfg(test)] +mod tests { + use serde::Deserialize; + use serde::Serialize; + use serde_json::json; + + #[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq)] + #[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] + pub enum OriginalDisplayFormat { + /// Show None Of The Things + NoneOfTheThings, + } + + extend_enum!(OriginalDisplayFormat, ExtendedDisplayFormat, { + /// Show Some Of The Things + SomeOfTheThings, + }); + + #[derive(serde::Deserialize)] + struct ExampleConfig { + #[allow(unused)] + format: ExtendedDisplayFormat, + } + + #[test] + pub fn extend_new_variant() { + let raw = json!({ + "format": "SomeOfTheThings", + }) + .to_string(); + + assert!(serde_json::from_str::(&raw).is_ok()) + } + + #[test] + pub fn extend_existing_variant() { + let raw = json!({ + "format": "NoneOfTheThings", + }) + .to_string(); + + assert!(serde_json::from_str::(&raw).is_ok()) + } + + #[test] + pub fn extend_invalid_variant() { + let raw = json!({ + "format": "ALLOFTHETHINGS", + }) + .to_string(); + + assert!(serde_json::from_str::(&raw).is_err()) + } +} diff --git a/crates/komorebi-bar-core/src/lib.rs b/crates/komorebi-bar-core/src/lib.rs index 7a081016..1ebe0cfe 100644 --- a/crates/komorebi-bar-core/src/lib.rs +++ b/crates/komorebi-bar-core/src/lib.rs @@ -10,3 +10,5 @@ pub mod ui; pub mod alignment; pub mod statics; + +pub mod config; diff --git a/komorebi-bar/Cargo.toml b/komorebi-bar/Cargo.toml index 803957e0..48f83f25 100644 --- a/komorebi-bar/Cargo.toml +++ b/komorebi-bar/Cargo.toml @@ -6,7 +6,7 @@ edition = "2024" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -komorebi-bar-core = { path = "../crates/komorebi-bar-core" } +komorebi-bar-core = { path = "../crates/komorebi-bar-core", default-features = false } komorebi-client = { path = "../komorebi-client", default-features = false } komorebi-themes = { path = "../komorebi-themes", default-features = false } @@ -51,6 +51,7 @@ windows-icons-fallback = { package = "windows-icons", git = "https://github.com/ default = ["schemars"] schemars = [ "dep:schemars", + "komorebi-bar-core/schemars", "komorebi-client/default", "komorebi-themes/default", ] diff --git a/komorebi-bar/src/config.rs b/komorebi-bar/src/config.rs index d4c84679..7633f870 100644 --- a/komorebi-bar/src/config.rs +++ b/komorebi-bar/src/config.rs @@ -2,9 +2,7 @@ use crate::DEFAULT_PADDING; use crate::bar::exec_powershell; use crate::render::Grouping; use crate::widgets::widget::WidgetConfig; -use eframe::egui::Pos2; use eframe::egui::TextBuffer; -use eframe::egui::Vec2; use komorebi_client::PathExt; use komorebi_client::Rect; use komorebi_client::SocketMessage; @@ -525,173 +523,5 @@ impl KomobarConfig { } } -#[derive(Copy, Clone, Debug, Serialize, Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] -/// Position -pub struct Position { - /// X coordinate - pub x: f32, - /// Y coordinate - pub y: f32, -} - -impl From for Vec2 { - fn from(value: Position) -> Self { - Self { - x: value.x, - y: value.y, - } - } -} - -impl From for Pos2 { - fn from(value: Position) -> Self { - Self { - x: value.x, - y: value.y, - } - } -} - +pub use komorebi_bar_core::config::types::*; pub use komorebi_themes::KomobarTheme; - -#[derive(Copy, Clone, Debug, Serialize, Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] -/// Label prefix -pub enum LabelPrefix { - /// Show no prefix - None, - /// Show an icon - Icon, - /// Show text - Text, - /// Show an icon and text - IconAndText, -} - -#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] -/// Display format -pub enum DisplayFormat { - /// Show only icon - Icon, - /// Show only text - Text, - /// Show an icon and text for the selected element, and text on the rest - TextAndIconOnSelected, - /// Show both icon and text - IconAndText, - /// Show an icon and text for the selected element, and icons on the rest - IconAndTextOnSelected, -} - -macro_rules! extend_enum { - ($(#[$type_meta:meta])* $existing_enum:ident, $new_enum:ident, { $($(#[$meta:meta])* $variant:ident),* $(,)? }) => { - #[derive(Copy, Clone, Debug, serde::Serialize, serde::Deserialize, PartialEq)] - #[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] - $(#[$type_meta])* - pub enum $new_enum { - // Add new variants - $( - $(#[$meta])* - $variant, - )* - // Include a variant that wraps the existing enum and flatten it when deserializing - #[serde(untagged)] - Existing($existing_enum), - } - - // Implement From for the existing enum - impl From<$existing_enum> for $new_enum { - fn from(value: $existing_enum) -> Self { - $new_enum::Existing(value) - } - } - }; -} - -extend_enum!( - /// Workspaces display format - DisplayFormat, WorkspacesDisplayFormat, { - /// Show all icons only - AllIcons, - /// Show both all icons and text - AllIconsAndText, - /// Show all icons and text for the selected element, and all icons on the rest - AllIconsAndTextOnSelected, -}); - -#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] -/// Media widget display format -pub enum MediaDisplayFormat { - /// Show only the media info icon - Icon, - /// Show only the media info text (artist - title) - Text, - /// Show both icon and text - IconAndText, - /// Show only the control buttons (previous, play/pause, next) - ControlsOnly, - /// Show icon with control buttons - IconAndControls, - /// Show text with control buttons - TextAndControls, - /// Show icon, text, and control buttons - Full, -} - -#[cfg(test)] -mod tests { - use serde::Deserialize; - use serde::Serialize; - use serde_json::json; - - #[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq)] - #[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] - pub enum OriginalDisplayFormat { - /// Show None Of The Things - NoneOfTheThings, - } - - extend_enum!(OriginalDisplayFormat, ExtendedDisplayFormat, { - /// Show Some Of The Things - SomeOfTheThings, - }); - - #[derive(serde::Deserialize)] - struct ExampleConfig { - #[allow(unused)] - format: ExtendedDisplayFormat, - } - - #[test] - pub fn extend_new_variant() { - let raw = json!({ - "format": "SomeOfTheThings", - }) - .to_string(); - - assert!(serde_json::from_str::(&raw).is_ok()) - } - - #[test] - pub fn extend_existing_variant() { - let raw = json!({ - "format": "NoneOfTheThings", - }) - .to_string(); - - assert!(serde_json::from_str::(&raw).is_ok()) - } - - #[test] - pub fn extend_invalid_variant() { - let raw = json!({ - "format": "ALLOFTHETHINGS", - }) - .to_string(); - - assert!(serde_json::from_str::(&raw).is_err()) - } -} diff --git a/komorebi-for-mac/Cargo.lock b/komorebi-for-mac/Cargo.lock index 9fb7e5e2..f02a219d 100644 --- a/komorebi-for-mac/Cargo.lock +++ b/komorebi-for-mac/Cargo.lock @@ -3245,6 +3245,8 @@ name = "komorebi-bar-core" version = "0.1.42" dependencies = [ "eframe", + "schemars 1.2.3", + "serde", ] [[package]] diff --git a/komorebi-for-mac/komorebi-bar/Cargo.toml b/komorebi-for-mac/komorebi-bar/Cargo.toml index 0a95de41..02566e81 100644 --- a/komorebi-for-mac/komorebi-bar/Cargo.toml +++ b/komorebi-for-mac/komorebi-bar/Cargo.toml @@ -6,7 +6,7 @@ edition = "2024" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -komorebi-bar-core = { path = "../../crates/komorebi-bar-core" } +komorebi-bar-core = { path = "../../crates/komorebi-bar-core", default-features = false } komorebi-client = { path = "../komorebi-client", default-features = false } komorebi-themes = { workspace = true } @@ -52,6 +52,7 @@ tracing-subscriber = { workspace = true } default = ["schemars"] schemars = [ "dep:schemars", + "komorebi-bar-core/schemars", "komorebi-client/default", "komorebi-themes/default", ] diff --git a/komorebi-for-mac/komorebi-bar/src/config.rs b/komorebi-for-mac/komorebi-bar/src/config.rs index 129449d3..a52b26d4 100644 --- a/komorebi-for-mac/komorebi-bar/src/config.rs +++ b/komorebi-for-mac/komorebi-bar/src/config.rs @@ -3,9 +3,7 @@ use crate::DEFAULT_PADDING; use crate::render::Grouping; use crate::widgets::widget::WidgetConfig; use color_eyre::eyre; -use eframe::egui::Pos2; use eframe::egui::TextBuffer; -use eframe::egui::Vec2; use komorebi_client::Rect; use komorebi_themes::KomobarTheme; use serde::Deserialize; @@ -518,151 +516,4 @@ impl KomobarConfig { } } -#[derive(Copy, Clone, Debug, Serialize, Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] -/// Position -pub struct Position { - /// X coordinate - pub x: f32, - /// Y coordinate - pub y: f32, -} - -impl From for Vec2 { - fn from(value: Position) -> Self { - Self { - x: value.x, - y: value.y, - } - } -} - -impl From for Pos2 { - fn from(value: Position) -> Self { - Self { - x: value.x, - y: value.y, - } - } -} - -#[derive(Copy, Clone, Debug, Serialize, Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] -/// Label prefix -pub enum LabelPrefix { - /// Show no prefix - None, - /// Show an icon - Icon, - /// Show text - Text, - /// Show an icon and text - IconAndText, -} - -#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] -/// Display format -pub enum DisplayFormat { - /// Show only icon - Icon, - /// Show only text - Text, - /// Show an icon and text for the selected element, and text on the rest - TextAndIconOnSelected, - /// Show both icon and text - IconAndText, - /// Show an icon and text for the selected element, and icons on the rest - IconAndTextOnSelected, -} - -macro_rules! extend_enum { - ($(#[$type_meta:meta])* $existing_enum:ident, $new_enum:ident, { $($(#[$meta:meta])* $variant:ident),* $(,)? }) => { - #[derive(Copy, Clone, Debug, serde::Serialize, serde::Deserialize, PartialEq)] - #[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] - $(#[$type_meta])* - pub enum $new_enum { - // Add new variants - $( - $(#[$meta])* - $variant, - )* - // Include a variant that wraps the existing enum and flatten it when deserializing - #[serde(untagged)] - Existing($existing_enum), - } - - // Implement From for the existing enum - impl From<$existing_enum> for $new_enum { - fn from(value: $existing_enum) -> Self { - $new_enum::Existing(value) - } - } - }; -} - -extend_enum!( - /// Workspaces display format - DisplayFormat, WorkspacesDisplayFormat, { - /// Show all icons only - AllIcons, - /// Show both all icons and text - AllIconsAndText, - /// Show all icons and text for the selected element, and all icons on the rest - AllIconsAndTextOnSelected, -}); - -#[cfg(test)] -mod tests { - use serde::Deserialize; - use serde::Serialize; - use serde_json::json; - - #[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq)] - #[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] - pub enum OriginalDisplayFormat { - /// Show None Of The Things - NoneOfTheThings, - } - - extend_enum!(OriginalDisplayFormat, ExtendedDisplayFormat, { - /// Show Some Of The Things - SomeOfTheThings, - }); - - #[derive(serde::Deserialize)] - struct ExampleConfig { - #[allow(unused)] - format: ExtendedDisplayFormat, - } - - #[test] - pub fn extend_new_variant() { - let raw = json!({ - "format": "SomeOfTheThings", - }) - .to_string(); - - assert!(serde_json::from_str::(&raw).is_ok()) - } - - #[test] - pub fn extend_existing_variant() { - let raw = json!({ - "format": "NoneOfTheThings", - }) - .to_string(); - - assert!(serde_json::from_str::(&raw).is_ok()) - } - - #[test] - pub fn extend_invalid_variant() { - let raw = json!({ - "format": "ALLOFTHETHINGS", - }) - .to_string(); - - assert!(serde_json::from_str::(&raw).is_err()) - } -} +pub use komorebi_bar_core::config::types::*;