diff --git a/Cargo.lock b/Cargo.lock index 48991dcc..c2999427 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3419,7 +3419,6 @@ dependencies = [ "schemars 1.2.3", "serde", "serde_json_lenient", - "starship-battery", "sysinfo 0.39.6", "systray-util", "tokio", @@ -3437,9 +3436,12 @@ name = "komorebi-bar-core" version = "0.1.42" dependencies = [ "eframe", + "egui-phosphor", "schemars 1.2.3", "serde", "serde_json_lenient", + "starship-battery", + "sysinfo 0.39.6", ] [[package]] diff --git a/crates/komorebi-bar-core/Cargo.toml b/crates/komorebi-bar-core/Cargo.toml index fff7ba05..48a081e2 100644 --- a/crates/komorebi-bar-core/Cargo.toml +++ b/crates/komorebi-bar-core/Cargo.toml @@ -6,7 +6,10 @@ edition = "2024" [dependencies] eframe = { workspace = true } +egui-phosphor = { git = "https://github.com/amPerl/egui-phosphor", rev = "d13688738478ecd12b426e3e74c59d6577a85b59" } serde = { workspace = true } +starship-battery = "0.11" +sysinfo = { workspace = true } # Optional dependencies schemars = { workspace = true, optional = true } diff --git a/komorebi-bar/src/widgets/battery.rs b/crates/komorebi-bar-core/src/widgets/battery.rs similarity index 100% rename from komorebi-bar/src/widgets/battery.rs rename to crates/komorebi-bar-core/src/widgets/battery.rs diff --git a/komorebi-bar/src/widgets/cpu.rs b/crates/komorebi-bar-core/src/widgets/cpu.rs similarity index 100% rename from komorebi-bar/src/widgets/cpu.rs rename to crates/komorebi-bar-core/src/widgets/cpu.rs diff --git a/crates/komorebi-bar-core/src/widgets/mod.rs b/crates/komorebi-bar-core/src/widgets/mod.rs index 2e7b8529..45e3fc65 100644 --- a/crates/komorebi-bar-core/src/widgets/mod.rs +++ b/crates/komorebi-bar-core/src/widgets/mod.rs @@ -1 +1,3 @@ +pub mod battery; +pub mod cpu; pub mod widget; diff --git a/komorebi-bar/Cargo.toml b/komorebi-bar/Cargo.toml index 48f83f25..f75831f4 100644 --- a/komorebi-bar/Cargo.toml +++ b/komorebi-bar/Cargo.toml @@ -35,7 +35,6 @@ reqwest = { version = "0.12", features = ["blocking"] } schemars = { workspace = true, optional = true } serde = { workspace = true } serde_json = { workspace = true } -starship-battery = "0.11" sysinfo = { workspace = true } systray-util = "0.2.0" tokio = { version = "1", features = ["rt", "sync", "time"] } diff --git a/komorebi-bar/src/widgets/mod.rs b/komorebi-bar/src/widgets/mod.rs index 9614e759..e24b53f0 100644 --- a/komorebi-bar/src/widgets/mod.rs +++ b/komorebi-bar/src/widgets/mod.rs @@ -10,8 +10,8 @@ use std::sync::LazyLock; use std::sync::RwLock; pub mod applications; -pub mod battery; -pub mod cpu; +pub use komorebi_bar_core::widgets::battery; +pub use komorebi_bar_core::widgets::cpu; pub mod date; pub mod keyboard; pub mod komorebi; diff --git a/komorebi-for-mac/Cargo.lock b/komorebi-for-mac/Cargo.lock index f02a219d..e576e8d4 100644 --- a/komorebi-for-mac/Cargo.lock +++ b/komorebi-for-mac/Cargo.lock @@ -1328,7 +1328,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e0b1fab2ae45819af2d0731d60f2afe17227ebb1a1538a236da84c93e9a60162" dependencies = [ "dispatch2", - "nix 0.31.3", + "nix", "windows-sys 0.61.2", ] @@ -3191,7 +3191,7 @@ dependencies = [ "serde_with", "shadow-rs", "strum", - "sysinfo", + "sysinfo 0.38.4", "thiserror 2.0.21", "tracing", "tracing-appender", @@ -3234,8 +3234,7 @@ dependencies = [ "schemars 1.2.3", "serde", "serde_json_lenient", - "starship-battery", - "sysinfo", + "sysinfo 0.38.4", "tracing", "tracing-subscriber", ] @@ -3245,8 +3244,11 @@ name = "komorebi-bar-core" version = "0.1.42" dependencies = [ "eframe", + "egui-phosphor", "schemars 1.2.3", "serde", + "starship-battery", + "sysinfo 0.39.6", ] [[package]] @@ -3362,7 +3364,7 @@ dependencies = [ "schemars 1.2.3", "serde_json_lenient", "shadow-rs", - "sysinfo", + "sysinfo 0.38.4", "version", ] @@ -3593,12 +3595,9 @@ checksum = "d3d25b0e0b648a86960ac23b7ad4abb9717601dec6f66c165f5b037f3f03065f" [[package]] name = "mach2" -version = "0.5.0" +version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a1b95cd5421ec55b445b5ae102f5ea0e768de1f82bd3001e11f426c269c3aea" -dependencies = [ - "libc", -] +checksum = "dae608c151f68243f2b000364e1f7b186d9c29845f7d2d85bd31b9ad77ad552b" [[package]] name = "malloc_buf" @@ -3908,18 +3907,6 @@ version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "650eef8c711430f1a879fdd01d4745a7deea475becfb90269c06775983bbf086" -[[package]] -name = "nix" -version = "0.30.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "74523f3a35e05aba87a1d978330aef40f67b0304ac79c1c00b294c9830543db6" -dependencies = [ - "bitflags 2.13.2", - "cfg-if", - "cfg_aliases", - "libc", -] - [[package]] name = "nix" version = "0.31.3" @@ -4353,7 +4340,11 @@ version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "33fafba39597d6dc1fb709123dfa8289d39406734be322956a69f0931c73bb15" dependencies = [ + "bitflags 2.13.2", + "block2 0.6.2", + "dispatch2", "libc", + "objc2 0.6.5", "objc2-core-foundation", ] @@ -4392,6 +4383,17 @@ dependencies = [ "objc2-foundation 0.2.2", ] +[[package]] +name = "objc2-open-directory" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb82bed227edf5201dfedf072bba4015a33d3d4a98519837295a90f0a23f676d" +dependencies = [ + "objc2 0.6.5", + "objc2-core-foundation", + "objc2-foundation 0.3.2", +] + [[package]] name = "objc2-quartz-core" version = "0.2.2" @@ -4803,13 +4805,13 @@ checksum = "b4596b6d070b27117e987119b4dac604f3c58cfb0b191112e24771b2faeac1a6" [[package]] name = "plist" -version = "1.8.0" +version = "1.10.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "740ebea15c5d1428f910cd1a5f52cebf8d25006245ed8ade92702f4943d91e07" +checksum = "327a4cb2ad49dc48adfdc9da89e22c64d488b508c67a044d71ba729e4521a285" dependencies = [ - "base64 0.22.1", + "base64 0.23.1", "indexmap 2.14.2", - "quick-xml 0.38.4", + "quick-xml 0.42.0", "serde", "time", ] @@ -4990,18 +4992,18 @@ dependencies = [ [[package]] name = "quick-xml" -version = "0.38.4" +version = "0.41.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b66c2058c55a409d601666cffe35f04333cf1013010882cec174a7467cd4e21c" +checksum = "e660451e55124f798a69a5af3f49ccfbefbd41910eefd25caf2393e1f3473ec1" dependencies = [ "memchr", ] [[package]] name = "quick-xml" -version = "0.41.0" +version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e660451e55124f798a69a5af3f49ccfbefbd41910eefd25caf2393e1f3473ec1" +checksum = "41b1177fdf999d2321d3fb46ff47159d9c1fb9ad66a4879f8c50a0b504615e9b" dependencies = [ "memchr", ] @@ -6035,17 +6037,18 @@ checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596" [[package]] name = "starship-battery" -version = "0.10.3" +version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd0efc2c44c92705be724265a0c758e3b7c120ea63817d2d684bab86fbeced9a" +checksum = "e919b7ecd99f2d5ed9efdc8d242ae93d91684181a782dadcbcd72079930155f9" dependencies = [ "cfg-if", - "core-foundation 0.10.1", "lazycell", "libc", "mach2", - "nix 0.30.1", + "nix", "num-traits", + "objc2-core-foundation", + "objc2-io-kit", "plist", "uom", "windows-sys 0.61.2", @@ -6169,6 +6172,21 @@ dependencies = [ "windows 0.62.2", ] +[[package]] +name = "sysinfo" +version = "0.39.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2071df9448915b71c4fe6d25deaf1c22f12bd234f01540b77312bb8e41361e6" +dependencies = [ + "libc", + "memchr", + "ntapi", + "objc2-core-foundation", + "objc2-io-kit", + "objc2-open-directory", + "windows 0.62.2", +] + [[package]] name = "system-configuration" version = "0.7.0" @@ -6694,9 +6712,9 @@ checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" [[package]] name = "uom" -version = "0.37.0" +version = "0.38.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cd5cfe7d84f6774726717f358a37f5bca8fca273bed4de40604ad129d1107b49" +checksum = "a739f83872836c82a4f2527d4e54b37007b3de68cafe7edde95fd695968bf4b9" dependencies = [ "num-traits", "typenum", diff --git a/komorebi-for-mac/komorebi-bar/Cargo.toml b/komorebi-for-mac/komorebi-bar/Cargo.toml index 02566e81..1aaa97d5 100644 --- a/komorebi-for-mac/komorebi-bar/Cargo.toml +++ b/komorebi-for-mac/komorebi-bar/Cargo.toml @@ -43,7 +43,6 @@ reqwest = { version = "0.12", features = ["blocking"] } schemars = { workspace = true, optional = true } serde = { workspace = true } serde_json = { workspace = true } -starship-battery = "0.10" sysinfo = { workspace = true } tracing = { workspace = true } tracing-subscriber = { workspace = true } diff --git a/komorebi-for-mac/komorebi-bar/src/widgets/battery.rs b/komorebi-for-mac/komorebi-bar/src/widgets/battery.rs deleted file mode 100644 index dcd8bd15..00000000 --- a/komorebi-for-mac/komorebi-bar/src/widgets/battery.rs +++ /dev/null @@ -1,191 +0,0 @@ -use crate::config::LabelPrefix; -use crate::render::RenderConfig; -use crate::selected_frame::SelectableFrame; -use crate::widgets::widget::BarWidget; -use eframe::egui::Align; -use eframe::egui::Context; -use eframe::egui::Label; -use eframe::egui::TextFormat; -use eframe::egui::Ui; -use eframe::egui::text::LayoutJob; -use serde::Deserialize; -use serde::Serialize; -use starship_battery::Manager; -use starship_battery::State; -use starship_battery::units::ratio::percent; -use std::process::Command; -use std::time::Duration; -use std::time::Instant; - -#[derive(Copy, Clone, Debug, Serialize, Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] -/// Battery widget configuration -pub struct BatteryConfig { - /// Enable the Battery widget - pub enable: bool, - /// Hide the widget if the battery is at full charge - pub hide_on_full_charge: Option, - /// Data refresh interval in seconds - #[cfg_attr(feature = "schemars", schemars(extend("default" = 10)))] - pub data_refresh_interval: Option, - /// Display label prefix - pub label_prefix: Option, - /// Select when the current percentage is under this value [[1-100]] - pub auto_select_under: Option, -} - -impl From for Battery { - fn from(value: BatteryConfig) -> Self { - let data_refresh_interval = value.data_refresh_interval.unwrap_or(10); - - Self { - enable: value.enable, - hide_on_full_charge: value.hide_on_full_charge.unwrap_or(false), - manager: Manager::new().unwrap(), - last_state: None, - data_refresh_interval, - label_prefix: value.label_prefix.unwrap_or(LabelPrefix::Icon), - auto_select_under: value.auto_select_under.map(|u| u.clamp(1, 100)), - state: BatteryState::Discharging, - last_updated: Instant::now() - .checked_sub(Duration::from_secs(data_refresh_interval)) - .unwrap(), - } - } -} - -pub enum BatteryState { - Charging, - Discharging, - High, - Medium, - Low, - Warning, -} - -#[derive(Clone, Debug)] -struct BatteryOutput { - label: String, - selected: bool, -} - -pub struct Battery { - pub enable: bool, - hide_on_full_charge: bool, - manager: Manager, - pub state: BatteryState, - data_refresh_interval: u64, - label_prefix: LabelPrefix, - auto_select_under: Option, - last_state: Option, - last_updated: Instant, -} - -impl Battery { - fn output(&mut self) -> Option { - let mut output = self.last_state.clone(); - - let now = Instant::now(); - if now.duration_since(self.last_updated) > Duration::from_secs(self.data_refresh_interval) { - output = None; - - if let Ok(mut batteries) = self.manager.batteries() - && let Some(Ok(first)) = batteries.nth(0) - { - let percentage = first.state_of_charge().get::().round() as u8; - - if percentage == 100 && self.hide_on_full_charge { - output = None - } else { - match first.state() { - State::Charging => self.state = BatteryState::Charging, - State::Discharging => { - self.state = match percentage { - p if p > 75 => BatteryState::Discharging, - p if p > 50 => BatteryState::High, - p if p > 25 => BatteryState::Medium, - p if p > 10 => BatteryState::Low, - _ => BatteryState::Warning, - } - } - _ => {} - } - - let selected = self.auto_select_under.is_some_and(|u| percentage <= u); - - output = Some(BatteryOutput { - label: match self.label_prefix { - LabelPrefix::Text | LabelPrefix::IconAndText => { - format!("BAT: {percentage}%") - } - LabelPrefix::None | LabelPrefix::Icon => { - format!("{percentage}%") - } - }, - selected, - }) - } - } - - self.last_state.clone_from(&output); - self.last_updated = now; - } - - output - } -} - -impl BarWidget for Battery { - fn render(&mut self, ctx: &Context, ui: &mut Ui, config: &mut RenderConfig) { - if self.enable { - let output = self.output(); - if let Some(output) = output { - let emoji = match self.state { - BatteryState::Charging => egui_phosphor::regular::BATTERY_CHARGING, - BatteryState::Discharging => egui_phosphor::regular::BATTERY_FULL, - BatteryState::High => egui_phosphor::regular::BATTERY_HIGH, - BatteryState::Medium => egui_phosphor::regular::BATTERY_MEDIUM, - BatteryState::Low => egui_phosphor::regular::BATTERY_LOW, - BatteryState::Warning => egui_phosphor::regular::BATTERY_WARNING, - }; - - let auto_text_color = config.auto_select_text.filter(|_| output.selected); - - let mut layout_job = LayoutJob::simple( - match self.label_prefix { - LabelPrefix::Icon | LabelPrefix::IconAndText => emoji.to_string(), - LabelPrefix::None | LabelPrefix::Text => String::new(), - }, - config.icon_font_id.clone(), - auto_text_color.unwrap_or(ctx.style().visuals.selection.stroke.color), - 100.0, - ); - - layout_job.append( - &output.label, - 10.0, - TextFormat { - font_id: config.text_font_id.clone(), - color: auto_text_color.unwrap_or(ctx.style().visuals.text_color()), - valign: Align::Center, - ..Default::default() - }, - ); - - let auto_focus_fill = config.auto_select_fill; - - config.apply_on_widget(false, ui, |ui| { - if SelectableFrame::new_auto(output.selected, auto_focus_fill) - .show(ui, |ui| ui.add(Label::new(layout_job).selectable(false))) - .clicked() - && let Err(error) = Command::new("cmd.exe") - .args(["/C", "start", "ms-settings:batterysaver"]) - .spawn() - { - eprintln!("{error}") - } - }); - } - } - } -} diff --git a/komorebi-for-mac/komorebi-bar/src/widgets/cpu.rs b/komorebi-for-mac/komorebi-bar/src/widgets/cpu.rs deleted file mode 100644 index bcf3e796..00000000 --- a/komorebi-for-mac/komorebi-bar/src/widgets/cpu.rs +++ /dev/null @@ -1,134 +0,0 @@ -use crate::config::LabelPrefix; -use crate::render::RenderConfig; -use crate::selected_frame::SelectableFrame; -use crate::widgets::widget::BarWidget; -use eframe::egui::Align; -use eframe::egui::Context; -use eframe::egui::Label; -use eframe::egui::TextFormat; -use eframe::egui::Ui; -use eframe::egui::text::LayoutJob; -use serde::Deserialize; -use serde::Serialize; -use std::process::Command; -use std::time::Duration; -use std::time::Instant; -use sysinfo::RefreshKind; -use sysinfo::System; - -#[derive(Copy, Clone, Debug, Serialize, Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] -/// CPU widget configuration -pub struct CpuConfig { - /// Enable the Cpu widget - pub enable: bool, - /// Data refresh interval in seconds - #[cfg_attr(feature = "schemars", schemars(extend("default" = 10)))] - pub data_refresh_interval: Option, - /// Display label prefix - pub label_prefix: Option, - /// Select when the current percentage is over this value [[1-100]] - pub auto_select_over: Option, -} - -impl From for Cpu { - fn from(value: CpuConfig) -> Self { - let data_refresh_interval = value.data_refresh_interval.unwrap_or(10); - - Self { - enable: value.enable, - system: System::new_with_specifics( - RefreshKind::default().without_memory().without_processes(), - ), - data_refresh_interval, - label_prefix: value.label_prefix.unwrap_or(LabelPrefix::IconAndText), - auto_select_over: value.auto_select_over.map(|o| o.clamp(1, 100)), - last_updated: Instant::now() - .checked_sub(Duration::from_secs(data_refresh_interval)) - .unwrap(), - } - } -} - -#[derive(Clone, Debug)] -struct CpuOutput { - label: String, - selected: bool, -} - -pub struct Cpu { - pub enable: bool, - system: System, - data_refresh_interval: u64, - label_prefix: LabelPrefix, - auto_select_over: Option, - last_updated: Instant, -} - -impl Cpu { - fn output(&mut self) -> CpuOutput { - let now = Instant::now(); - if now.duration_since(self.last_updated) > Duration::from_secs(self.data_refresh_interval) { - self.system.refresh_cpu_usage(); - self.last_updated = now; - } - - let used = self.system.global_cpu_usage() as u8; - let selected = self.auto_select_over.is_some_and(|o| used >= o); - - CpuOutput { - label: match self.label_prefix { - LabelPrefix::Text | LabelPrefix::IconAndText => format!("CPU: {used}%"), - LabelPrefix::None | LabelPrefix::Icon => format!("{used}%"), - }, - selected, - } - } -} - -impl BarWidget for Cpu { - fn render(&mut self, ctx: &Context, ui: &mut Ui, config: &mut RenderConfig) { - if self.enable { - let output = self.output(); - if !output.label.is_empty() { - let auto_text_color = config.auto_select_text.filter(|_| output.selected); - - 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(), - }, - config.icon_font_id.clone(), - auto_text_color.unwrap_or(ctx.style().visuals.selection.stroke.color), - 100.0, - ); - - layout_job.append( - &output.label, - 10.0, - TextFormat { - font_id: config.text_font_id.clone(), - color: auto_text_color.unwrap_or(ctx.style().visuals.text_color()), - valign: Align::Center, - ..Default::default() - }, - ); - - let auto_focus_fill = config.auto_select_fill; - - config.apply_on_widget(false, ui, |ui| { - if SelectableFrame::new_auto(output.selected, auto_focus_fill) - .show(ui, |ui| ui.add(Label::new(layout_job).selectable(false))) - .clicked() - && let Err(error) = - Command::new("cmd.exe").args(["/C", "taskmgr.exe"]).spawn() - { - eprintln!("{error}") - } - }); - } - } - } -} diff --git a/komorebi-for-mac/komorebi-bar/src/widgets/mod.rs b/komorebi-for-mac/komorebi-bar/src/widgets/mod.rs index ba03e881..950fec55 100644 --- a/komorebi-for-mac/komorebi-bar/src/widgets/mod.rs +++ b/komorebi-for-mac/komorebi-bar/src/widgets/mod.rs @@ -10,8 +10,8 @@ use std::sync::LazyLock; use std::sync::RwLock; pub mod applications; -pub mod battery; -pub mod cpu; +pub use komorebi_bar_core::widgets::battery; +pub use komorebi_bar_core::widgets::cpu; pub mod date; // pub mod keyboard; pub mod komorebi;