Skip to content

Commit

Permalink
feat(upower): add new formatting properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Disr0 authored and JakeStanger committed Feb 12, 2024
1 parent d2adecb commit 76a6816
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 16 deletions.
11 changes: 11 additions & 0 deletions docs/modules/Upower.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ end:

</details>

### Formatting Tokens

The following tokens can be used in the `format` config option,
and will be replaced with values from the current battery state:

| Token | Description |
|----------------|------------------------------------------|
| `{percentage}` | The battery charge percentage. |
| `{state}` | The current battery (dis)charging state. |
| `{remaining}` | The ETA to battery empty or full. |

## Styling

| Selector | Description |
Expand Down
53 changes: 37 additions & 16 deletions src/modules/upower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::modules::PopupButton;
use crate::modules::{
Module, ModuleInfo, ModuleParts, ModulePopup, ModuleUpdateEvent, WidgetContext,
};
use crate::{error, glib_recv, send_async, spawn, try_send};
use crate::{glib_recv, send_async, spawn, try_send};

const DAY: i64 = 24 * 60 * 60;
const HOUR: i64 = 60 * 60;
Expand Down Expand Up @@ -45,7 +45,7 @@ const fn default_icon_size() -> i32 {
pub struct UpowerProperties {
percentage: f64,
icon_name: String,
state: u32,
state: BatteryState,
time_to_full: i64,
time_to_empty: i64,
}
Expand Down Expand Up @@ -84,9 +84,12 @@ impl Module<gtk::Button> for UpowerModule {
.downcast_ref::<str>()
.expect("expected IconName: str in HashMap of all properties")
.to_string();
let state = *properties["State"]
.downcast_ref::<u32>()
.expect("expected State: u32 in HashMap of all properties");
let state = u32_to_battery_state(
*properties["State"]
.downcast_ref::<u32>()
.expect("expected State: u32 in HashMap of all properties"),
)
.unwrap_or(BatteryState::Unknown);
let time_to_full = *properties["TimeToFull"]
.downcast_ref::<i64>()
.expect("expected TimeToFull: i64 in HashMap of all properties");
Expand Down Expand Up @@ -123,9 +126,9 @@ impl Module<gtk::Button> for UpowerModule {
.to_string();
}
"State" => {
properties.state = changed_value
.downcast::<u32>()
.expect("expected State to be u32");
properties.state =
u32_to_battery_state(changed_value.downcast::<u32>().unwrap_or(0))
.expect("expected State to be BatteryState");
}
"TimeToFull" => {
properties.time_to_full = changed_value
Expand Down Expand Up @@ -185,7 +188,17 @@ impl Module<gtk::Button> for UpowerModule {

let rx = context.subscribe();
glib_recv!(rx, properties => {
let format = format.replace("{percentage}", &properties.percentage.to_string());
let state = properties.state;
let is_charging = state == BatteryState::Charging || state == BatteryState::PendingCharge;
let time_remaining = if is_charging {
seconds_to_string(properties.time_to_full)
}
else {
seconds_to_string(properties.time_to_empty)
};
let format = format.replace("{percentage}", &properties.percentage.to_string())
.replace("{time_remaining}", &time_remaining)
.replace("{state}", battery_state_to_string(state));
let icon_name = String::from("icon:") + &properties.icon_name;

ImageProvider::parse(&icon_name, &icon_theme, false, self.icon_size)
Expand Down Expand Up @@ -220,28 +233,24 @@ impl Module<gtk::Button> for UpowerModule {
container.add(&label);

glib_recv!(rx, properties => {
let state = u32_to_battery_state(properties.state);
let state = properties.state;
let format = match state {
Ok(BatteryState::Charging | BatteryState::PendingCharge) => {
BatteryState::Charging | BatteryState::PendingCharge => {
let ttf = properties.time_to_full;
if ttf > 0 {
format!("Full in {}", seconds_to_string(ttf))
} else {
String::new()
}
}
Ok(BatteryState::Discharging | BatteryState::PendingDischarge) => {
BatteryState::Discharging | BatteryState::PendingDischarge => {
let tte = properties.time_to_empty;
if tte > 0 {
format!("Empty in {}", seconds_to_string(tte))
} else {
String::new()
}
}
Err(state) => {
error!("Invalid battery state: {state}");
String::new()
}
_ => String::new(),
};

Expand Down Expand Up @@ -290,3 +299,15 @@ const fn u32_to_battery_state(number: u32) -> Result<BatteryState, u32> {
Err(number)
}
}

fn battery_state_to_string(state: BatteryState) -> &'static str {
match state {
BatteryState::Unknown => "Unknown",
BatteryState::Charging => "Charging",
BatteryState::Discharging => "Discharging",
BatteryState::Empty => "Empty",
BatteryState::FullyCharged => "Fully charged",
BatteryState::PendingCharge => "Pending charge",
BatteryState::PendingDischarge => "Pending discharge",
}
}

0 comments on commit 76a6816

Please sign in to comment.