Skip to content

Commit

Permalink
feat(bar): only collect enabled widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
CtByte authored and LGUG2Z committed Dec 9, 2024
1 parent 830da89 commit 9d6173e
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 42 deletions.
3 changes: 3 additions & 0 deletions komorebi-bar/src/bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<Box<dyn BarWidget>>>();

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::<Vec<Box<dyn BarWidget>>>(),
None => vec![],
Expand All @@ -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::<Vec<Box<dyn BarWidget>>>();

Expand Down
37 changes: 9 additions & 28 deletions komorebi-bar/src/battery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,37 +30,18 @@ pub struct BatteryConfig {

impl From<BatteryConfig> 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::<percent>();
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(),
}
}
}
Expand Down
15 changes: 8 additions & 7 deletions komorebi-bar/src/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,18 @@ pub struct CpuConfig {

impl From<CpuConfig> 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(),
}
}
}
Expand Down
15 changes: 8 additions & 7 deletions komorebi-bar/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,18 @@ pub struct MemoryConfig {

impl From<MemoryConfig> 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(),
}
}
}
Expand Down
31 changes: 31 additions & 0 deletions komorebi-bar/src/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
}

0 comments on commit 9d6173e

Please sign in to comment.