diff --git a/komorebi-bar/src/bar.rs b/komorebi-bar/src/bar.rs index 0f70d63b..e01e709c 100644 --- a/komorebi-bar/src/bar.rs +++ b/komorebi-bar/src/bar.rs @@ -315,12 +315,14 @@ impl Komobar { let mut left_widgets = config .left_widgets .iter() + .filter(|config| config.enabled()) .map(|config| config.as_boxed_bar_widget()) .collect::>>(); let mut center_widgets = match &config.center_widgets { Some(center_widgets) => center_widgets .iter() + .filter(|config| config.enabled()) .map(|config| config.as_boxed_bar_widget()) .collect::>>(), None => vec![], @@ -329,6 +331,7 @@ impl Komobar { let mut right_widgets = config .right_widgets .iter() + .filter(|config| config.enabled()) .map(|config| config.as_boxed_bar_widget()) .collect::>>(); diff --git a/komorebi-bar/src/battery.rs b/komorebi-bar/src/battery.rs index 1aac1e6c..7307eb25 100644 --- a/komorebi-bar/src/battery.rs +++ b/komorebi-bar/src/battery.rs @@ -30,37 +30,18 @@ pub struct BatteryConfig { impl From for Battery { fn from(value: BatteryConfig) -> Self { - let manager = Manager::new().unwrap(); - let mut last_state = String::new(); - let mut state = None; - let prefix = value.label_prefix.unwrap_or(LabelPrefix::Icon); - - if let Ok(mut batteries) = manager.batteries() { - if let Some(Ok(first)) = batteries.nth(0) { - let percentage = first.state_of_charge().get::(); - match first.state() { - State::Charging => state = Some(BatteryState::Charging), - State::Discharging => state = Some(BatteryState::Discharging), - _ => {} - } - - last_state = match prefix { - LabelPrefix::Text | LabelPrefix::IconAndText => { - format!("BAT: {percentage:.0}%") - } - LabelPrefix::None | LabelPrefix::Icon => format!("{percentage:.0}%"), - } - } - } + let data_refresh_interval = value.data_refresh_interval.unwrap_or(10); Self { enable: value.enable, - manager, - last_state, - data_refresh_interval: value.data_refresh_interval.unwrap_or(10), - label_prefix: prefix, - state: state.unwrap_or(BatteryState::Discharging), - last_updated: Instant::now(), + manager: Manager::new().unwrap(), + last_state: String::new(), + data_refresh_interval, + label_prefix: value.label_prefix.unwrap_or(LabelPrefix::Icon), + state: BatteryState::Discharging, + last_updated: Instant::now() + .checked_sub(Duration::from_secs(data_refresh_interval)) + .unwrap(), } } } diff --git a/komorebi-bar/src/cpu.rs b/komorebi-bar/src/cpu.rs index 9666cc07..3dab1ea6 100644 --- a/komorebi-bar/src/cpu.rs +++ b/komorebi-bar/src/cpu.rs @@ -30,17 +30,18 @@ pub struct CpuConfig { impl From for Cpu { fn from(value: CpuConfig) -> Self { - let mut system = - System::new_with_specifics(RefreshKind::default().without_memory().without_processes()); - - system.refresh_cpu_usage(); + let data_refresh_interval = value.data_refresh_interval.unwrap_or(10); Self { enable: value.enable, - system, - data_refresh_interval: value.data_refresh_interval.unwrap_or(10), + system: System::new_with_specifics( + RefreshKind::default().without_memory().without_processes(), + ), + data_refresh_interval, label_prefix: value.label_prefix.unwrap_or(LabelPrefix::IconAndText), - last_updated: Instant::now(), + last_updated: Instant::now() + .checked_sub(Duration::from_secs(data_refresh_interval)) + .unwrap(), } } } diff --git a/komorebi-bar/src/memory.rs b/komorebi-bar/src/memory.rs index cb4834ee..b98403e3 100644 --- a/komorebi-bar/src/memory.rs +++ b/komorebi-bar/src/memory.rs @@ -30,17 +30,18 @@ pub struct MemoryConfig { impl From for Memory { fn from(value: MemoryConfig) -> Self { - let mut system = - System::new_with_specifics(RefreshKind::default().without_cpu().without_processes()); - - system.refresh_memory(); + let data_refresh_interval = value.data_refresh_interval.unwrap_or(10); Self { enable: value.enable, - system, - data_refresh_interval: value.data_refresh_interval.unwrap_or(10), + system: System::new_with_specifics( + RefreshKind::default().without_cpu().without_processes(), + ), + data_refresh_interval, label_prefix: value.label_prefix.unwrap_or(LabelPrefix::IconAndText), - last_updated: Instant::now(), + last_updated: Instant::now() + .checked_sub(Duration::from_secs(data_refresh_interval)) + .unwrap(), } } } diff --git a/komorebi-bar/src/widget.rs b/komorebi-bar/src/widget.rs index aecf6a08..ae0c6fc9 100644 --- a/komorebi-bar/src/widget.rs +++ b/komorebi-bar/src/widget.rs @@ -54,4 +54,35 @@ impl WidgetConfig { WidgetConfig::Time(config) => Box::new(Time::from(config.clone())), } } + + pub fn enabled(&self) -> bool { + match self { + WidgetConfig::Battery(config) => config.enable, + WidgetConfig::Cpu(config) => config.enable, + WidgetConfig::Date(config) => config.enable, + WidgetConfig::Komorebi(config) => { + config.workspaces.enable + || (if let Some(layout) = &config.layout { + layout.enable + } else { + false + }) + || (if let Some(focused_window) = &config.focused_window { + focused_window.enable + } else { + false + }) + || (if let Some(configuration_switcher) = &config.configuration_switcher { + configuration_switcher.enable + } else { + false + }) + } + WidgetConfig::Media(config) => config.enable, + WidgetConfig::Memory(config) => config.enable, + WidgetConfig::Network(config) => config.enable, + WidgetConfig::Storage(config) => config.enable, + WidgetConfig::Time(config) => config.enable, + } + } }