Skip to content

Commit

Permalink
feat(bar): opt to hide battery widget when charged
Browse files Browse the repository at this point in the history
  • Loading branch information
CtByte authored and LGUG2Z committed Jan 9, 2025
1 parent 996a556 commit b1db417
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 16 deletions.
48 changes: 32 additions & 16 deletions komorebi-bar/src/battery.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::config::LabelPrefix;
use crate::render::RenderConfig;
use crate::selected_frame::SelectableFrame;
use crate::widget::BarWidget;
use eframe::egui::text::LayoutJob;
use eframe::egui::Align;
use eframe::egui::Context;
use eframe::egui::Label;
use eframe::egui::Sense;
use eframe::egui::TextFormat;
use eframe::egui::Ui;
use schemars::JsonSchema;
Expand All @@ -14,13 +14,16 @@ use serde::Serialize;
use starship_battery::units::ratio::percent;
use starship_battery::Manager;
use starship_battery::State;
use std::process::Command;
use std::time::Duration;
use std::time::Instant;

#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema)]
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<bool>,
/// Data refresh interval (default: 10 seconds)
pub data_refresh_interval: Option<u64>,
/// Display label prefix
Expand All @@ -33,6 +36,7 @@ impl From<BatteryConfig> for Battery {

Self {
enable: value.enable,
hide_on_full_charge: value.hide_on_full_charge.unwrap_or(false),
manager: Manager::new().unwrap(),
last_state: String::new(),
data_refresh_interval,
Expand All @@ -52,6 +56,7 @@ pub enum BatteryState {

pub struct Battery {
pub enable: bool,
hide_on_full_charge: bool,
manager: Manager,
pub state: BatteryState,
data_refresh_interval: u64,
Expand All @@ -71,17 +76,22 @@ impl Battery {
if let Ok(mut batteries) = self.manager.batteries() {
if let Some(Ok(first)) = batteries.nth(0) {
let percentage = first.state_of_charge().get::<percent>();
match first.state() {
State::Charging => self.state = BatteryState::Charging,
State::Discharging => self.state = BatteryState::Discharging,
_ => {}
}

output = match self.label_prefix {
LabelPrefix::Text | LabelPrefix::IconAndText => {
format!("BAT: {percentage:.0}%")
if percentage == 100.0 && self.hide_on_full_charge {
output = String::new()
} else {
match first.state() {
State::Charging => self.state = BatteryState::Charging,
State::Discharging => self.state = BatteryState::Discharging,
_ => {}
}

output = match self.label_prefix {
LabelPrefix::Text | LabelPrefix::IconAndText => {
format!("BAT: {percentage:.0}%")
}
LabelPrefix::None | LabelPrefix::Icon => format!("{percentage:.0}%"),
}
LabelPrefix::None | LabelPrefix::Icon => format!("{percentage:.0}%"),
}
}
}
Expand Down Expand Up @@ -125,12 +135,18 @@ impl BarWidget for Battery {
},
);

config.apply_on_widget(true, ui, |ui| {
ui.add(
Label::new(layout_job)
.selectable(false)
.sense(Sense::click()),
);
config.apply_on_widget(false, ui, |ui| {
if SelectableFrame::new(false)
.show(ui, |ui| ui.add(Label::new(layout_job).selectable(false)))
.clicked()
{
if let Err(error) = Command::new("cmd.exe")
.args(["/C", "start", "ms-settings:batterysaver"])
.spawn()
{
eprintln!("{}", error)
}
}
});
}
}
Expand Down
12 changes: 12 additions & 0 deletions schema.bar.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
"description": "Enable the Battery widget",
"type": "boolean"
},
"hide_on_full_charge": {
"description": "Hide the widget if the battery is at full charge",
"type": "boolean"
},
"label_prefix": {
"description": "Display label prefix",
"oneOf": [
Expand Down Expand Up @@ -1214,6 +1218,10 @@
"description": "Enable the Battery widget",
"type": "boolean"
},
"hide_on_full_charge": {
"description": "Hide the widget if the battery is at full charge",
"type": "boolean"
},
"label_prefix": {
"description": "Display label prefix",
"oneOf": [
Expand Down Expand Up @@ -2145,6 +2153,10 @@
"description": "Enable the Battery widget",
"type": "boolean"
},
"hide_on_full_charge": {
"description": "Hide the widget if the battery is at full charge",
"type": "boolean"
},
"label_prefix": {
"description": "Display label prefix",
"oneOf": [
Expand Down

0 comments on commit b1db417

Please sign in to comment.