Skip to content

Commit

Permalink
Defer icon lookup until formatting
Browse files Browse the repository at this point in the history
Fixes #1774
  • Loading branch information
bim9262 committed Jul 30, 2023
1 parent 521ea25 commit 2720972
Show file tree
Hide file tree
Showing 40 changed files with 158 additions and 153 deletions.
24 changes: 0 additions & 24 deletions src/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ use std::sync::Arc;
use std::time::Duration;

use crate::click::MouseButton;
use crate::config::SharedConfig;
use crate::errors::*;
use crate::widget::Widget;
use crate::{Request, RequestCmd};
Expand Down Expand Up @@ -194,7 +193,6 @@ pub type BlockAction = Cow<'static, str>;
#[derive(Clone)]
pub struct CommonApi {
pub(crate) id: usize,
pub(crate) shared_config: SharedConfig,
pub(crate) update_request: Arc<Notify>,
pub(crate) request_sender: mpsc::Sender<Request>,
pub(crate) error_interval: Duration,
Expand Down Expand Up @@ -262,26 +260,4 @@ impl CommonApi {
pub async fn wait_for_update_request(&self) {
self.update_request.notified().await;
}

pub fn get_icon(&self, icon: &str) -> Result<String> {
self.shared_config
.get_icon(icon, None)
.or_error(|| format!("Icon '{icon}' not found"))
}

pub fn get_icon_in_progression(&self, icon: &str, value: f64) -> Result<String> {
self.shared_config
.get_icon(icon, Some(value))
.or_error(|| format!("Icon '{icon}' not found"))
}

pub fn get_icon_in_progression_bound(
&self,
icon: &str,
value: f64,
low: f64,
high: f64,
) -> Result<String> {
self.get_icon_in_progression(icon, (value.clamp(low, high) - low) / (high - low))
}
}
2 changes: 1 addition & 1 deletion src/blocks/amd_gpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
let info = read_gpu_info(&config.device).await?;

widget.set_values(map! {
"icon" => Value::icon(api.get_icon("gpu")?),
"icon" => Value::icon("gpu"),
"utilization" => Value::percents(info.utilization_percents),
"vram_total" => Value::bytes(info.vram_total_bytes),
"vram_used" => Value::bytes(info.vram_used_bytes),
Expand Down
2 changes: 1 addition & 1 deletion src/blocks/apt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
});
widget.set_values(map!(
"count" => Value::number(count),
"icon" => Value::icon(api.get_icon("update")?)
"icon" => Value::icon("update"),
));

let warning = warning_updates_regex
Expand Down
2 changes: 1 addition & 1 deletion src/blocks/backlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
icon_value = 1.0 - icon_value;
}
widget.set_values(map! {
"icon" => Value::icon(api.get_icon_in_progression("backlight", icon_value)?),
"icon" => Value::icon_progression("backlight", icon_value),
"brightness" => Value::percents((brightness * 100.0).round())
});
api.set_widget(widget).await?;
Expand Down
4 changes: 2 additions & 2 deletions src/blocks/battery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {

values.insert(
"icon".into(),
Value::icon(api.get_icon_in_progression(icon_name, icon_value)?),
Value::icon_progression(icon_name, icon_value),
);

widget.set_values(values);
Expand All @@ -213,7 +213,7 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
let mut widget = Widget::new()
.with_format(missing_format.clone())
.with_state(State::Critical);
widget.set_values(map!("icon" => Value::icon(api.get_icon("bat_not_available")?)));
widget.set_values(map!("icon" => Value::icon("bat_not_available")));
api.set_widget(widget).await?;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/blocks/bluetooth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
let mut widget = Widget::new();

let values = map! {
"icon" => Value::icon(api.get_icon(device.icon)?),
"icon" => Value::icon(device.icon),
"name" => Value::text(device.name),
"available" => Value::flag(),
[if let Some(p) = device.battery_percentage] "percentage" => Value::percents(p),
[if let Some(p) = device.battery_percentage]
"battery_icon" => Value::icon(api.get_icon_in_progression("bat", p as f64 / 100.0)?),
"battery_icon" => Value::icon_progression("bat", p as f64 / 100.0),
};

if device.connected {
Expand All @@ -135,7 +135,7 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
None => {
debug!("Showing device as unavailable");
let mut widget = Widget::new().with_format(disconnected_format.clone());
widget.set_values(map!("icon" => Value::icon(api.get_icon("bluetooth")?)));
widget.set_values(map!("icon" => Value::icon("bluetooth")));
api.set_widget(widget).await?;
}
}
Expand Down
9 changes: 3 additions & 6 deletions src/blocks/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,6 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
None => None,
};

let boost_icon_on = api.get_icon("cpu_boost_on")?;
let boost_icon_off = api.get_icon("cpu_boost_off")?;

// Store previous /proc/stat state
let mut cputime = read_proc_stat().await?;
let cores = cputime.1.len();
Expand Down Expand Up @@ -106,12 +103,12 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {

// Read boost state on intel CPUs
let boost = boost_status().await.map(|status| match status {
true => boost_icon_on.clone(),
false => boost_icon_off.clone(),
true => "cpu_boost_on",
false => "cpu_boost_off",
});

let mut values = map!(
"icon" => Value::icon(api.get_icon_in_progression("cpu", utilization_avg)?),
"icon" => Value::icon_progression("cpu", utilization_avg),
"barchart" => Value::text(barchart),
"utilization" => Value::percents(utilization_avg * 100.),
[if !freqs.is_empty()] "frequency" => Value::hertz(freqs.iter().sum::<f64>() / (freqs.len() as f64)),
Expand Down
2 changes: 1 addition & 1 deletion src/blocks/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ async fn update_bar(
text_empty = input.text.is_empty();
widget.set_values(map! {
"text" => Value::text(input.text),
[if !input.icon.is_empty()] "icon" => Value::icon(api.get_icon(&input.icon)?),
[if !input.icon.is_empty()] "icon" => Value::icon(input.icon),
[if let Some(t) = input.short_text] "short_text" => Value::text(t)
});
widget.state = input.state;
Expand Down
12 changes: 6 additions & 6 deletions src/blocks/custom_dbus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ struct Block {
short_text: Option<String>,
}

fn block_values(block: &Block, api: &CommonApi) -> Result<HashMap<Cow<'static, str>, Value>> {
Ok(map! {
[if let Some(icon) = &block.icon] "icon" => Value::icon(api.get_icon(icon)?),
fn block_values(block: &Block) -> HashMap<Cow<'static, str>, Value> {
map! {
[if let Some(icon) = &block.icon] "icon" => Value::icon(icon.to_string()),
[if let Some(text) = &block.text] "text" => Value::text(text.to_string()),
[if let Some(short_text) = &block.short_text] "short_text" => Value::text(short_text.to_string()),
})
}
}

#[dbus_interface(name = "rs.i3status.custom")]
Expand All @@ -94,15 +94,15 @@ impl Block {
} else {
Some(icon.to_string())
};
self.widget.set_values(block_values(self, &self.api)?);
self.widget.set_values(block_values(self));
self.api.set_widget(self.widget.clone()).await?;
Ok(())
}

async fn set_text(&mut self, full: String, short: String) -> fdo::Result<()> {
self.text = Some(full);
self.short_text = Some(short);
self.widget.set_values(block_values(self, &self.api)?);
self.widget.set_values(block_values(self));
self.api.set_widget(self.widget.clone()).await?;
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion src/blocks/disk_space.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {

let percentage = result / (total as f64) * 100.;
widget.set_values(map! {
"icon" => Value::icon(api.get_icon("disk_drive")?),
"icon" => Value::icon("disk_drive"),
"path" => Value::text(path.to_string()),
"percentage" => Value::percents(percentage),
"total" => Value::bytes(total as f64),
Expand Down
2 changes: 1 addition & 1 deletion src/blocks/dnf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
_ => format.clone(),
});
widget.set_values(map!(
"icon" => Value::icon(api.get_icon("update")?),
"icon" => Value::icon("update"),
"count" => Value::number(count)
));

Expand Down
2 changes: 1 addition & 1 deletion src/blocks/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {

let mut widget = Widget::new().with_format(format.clone());
widget.set_values(map! {
"icon" => Value::icon(api.get_icon("docker")?),
"icon" => Value::icon("docker"),
"total" => Value::number(status.total),
"running" => Value::number(status.running),
"paused" => Value::number(status.paused),
Expand Down
2 changes: 1 addition & 1 deletion src/blocks/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
.into_iter()
.map(|(k, v)| (k.into(), Value::number(v)))
.collect();
values.insert("icon".into(), Value::icon(api.get_icon("github")?));
values.insert("icon".into(), Value::icon("github"));
widget.set_values(values);

api.set_widget(widget).await?;
Expand Down
20 changes: 7 additions & 13 deletions src/blocks/kdeconnect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
}

if connected {
values.insert("icon".into(), Value::icon(api.get_icon("phone")?));
values.insert("icon".into(), Value::icon("phone"));
values.insert("connected".into(), Value::flag());
let (
(level, charging),
Expand All @@ -123,10 +123,10 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
values.insert("bat_charge".into(), Value::percents(level));
values.insert(
"bat_icon".into(),
Value::icon(api.get_icon_in_progression(
Value::icon_progression(
if charging { "bat_charging" } else { "bat" },
level as f64 / 100.0,
)?),
),
);
if battery_state {
widget.state = if charging {
Expand All @@ -150,10 +150,10 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
let cell_network_percent = (cellular_network_strength.clamp(0, 4) * 25) as f64;
values.insert(
"network_icon".into(),
Value::icon(api.get_icon_in_progression(
Value::icon_progression(
"net_cellular",
(cellular_network_strength + 1).clamp(0, 5) as f64 / 5.0,
)?),
),
);
values.insert(
"network_strength".into(),
Expand All @@ -170,10 +170,7 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {

if notif_count > 0 {
values.insert("notif_count".into(), Value::number(notif_count));
values.insert(
"notif_icon".into(),
Value::icon(api.get_icon("notification")?),
);
values.insert("notif_icon".into(), Value::icon("notification"));
}
if !battery_state {
widget.state = if notif_count == 0 {
Expand All @@ -183,10 +180,7 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
};
}
} else {
values.insert(
"icon".into(),
Value::icon(api.get_icon("phone_disconnected")?),
);
values.insert("icon".into(), Value::icon("phone_disconnected"));
}

widget.set_values(values);
Expand Down
2 changes: 1 addition & 1 deletion src/blocks/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
_ => State::Idle,
};
widget.set_values(map! {
"icon" => Value::icon(api.get_icon("cogs")?),
"icon" => Value::icon("cogs"),
"1m" => Value::number(m1),
"5m" => Value::number(m5),
"15m" => Value::number(m15),
Expand Down
2 changes: 1 addition & 1 deletion src/blocks/maildir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
State::Idle
};
widget.set_values(map!(
"icon" => Value::icon(api.get_icon("mail")?),
"icon" => Value::icon("mail"),
"status" => Value::number(newmails)
));
api.set_widget(widget).await?;
Expand Down
4 changes: 2 additions & 2 deletions src/blocks/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {

let mut widget = Widget::new().with_format(format.clone());
widget.set_values(map! {
"icon" => Value::icon(api.get_icon("memory_mem")?),
"icon_swap" => Value::icon(api.get_icon("memory_swap")?),
"icon" => Value::icon("memory_mem"),
"icon_swap" => Value::icon("memory_swap"),
"mem_total" => Value::bytes(mem_total),
"mem_free" => Value::bytes(mem_free),
"mem_free_percents" => Value::percents(mem_free / mem_total * 100.),
Expand Down
16 changes: 8 additions & 8 deletions src/blocks/music.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,14 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {

let volume_step = config.volume_step.clamp(0.0, 50.0) / 100.0;

let new_btn = |icon: &str, instance: &'static str, api: &CommonApi| -> Result<Value> {
Ok(Value::icon(api.get_icon(icon)?).with_instance(instance))
let new_btn = |icon: &str, instance: &'static str| -> Result<Value> {
Ok(Value::icon(icon.to_string()).with_instance(instance))
};

let values = map! {
"icon" => Value::icon(api.get_icon("music")?),
"next" => new_btn("music_next", NEXT_BTN, api)?,
"prev" => new_btn("music_prev", PREV_BTN, api)?,
"icon" => Value::icon("music"),
"next" => new_btn("music_next", NEXT_BTN)?,
"prev" => new_btn("music_prev", PREV_BTN)?,
};

let preferred_players = match config.player.clone() {
Expand Down Expand Up @@ -292,7 +292,7 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
Some(PlaybackStatus::Playing) => (State::Info, "music_pause"),
_ => (State::Idle, "music_play"),
};
values.insert("play".into(), new_btn(play_icon, PLAY_PAUSE_BTN, api)?);
values.insert("play".into(), new_btn(play_icon, PLAY_PAUSE_BTN)?);
if let Some(url) = &player.metadata.url {
values.insert("url".into(), Value::text(url.clone()));
}
Expand Down Expand Up @@ -325,7 +325,7 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
if let Some(volume) = player.volume {
values.insert(
"volume_icon".into(),
Value::icon(api.get_icon_in_progression("volume", volume)?),
Value::icon_progression("volume", volume),
);
values.insert("volume".into(), Value::percents(volume * 100.0));
}
Expand All @@ -336,7 +336,7 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
}
None => {
let mut widget = Widget::new().with_format(format.clone());
widget.set_values(map!("icon" => Value::icon(api.get_icon("music")?)));
widget.set_values(map!("icon" => Value::icon("music")));
api.set_widget(widget).await?;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/blocks/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
push_to_hist(&mut tx_hist, speed_up);

let icon = if let Some(signal) = device.signal() {
Value::icon(api.get_icon_in_progression(device.icon, signal / 100.0)?)
Value::icon_progression(device.icon, signal / 100.0)
} else {
Value::icon(api.get_icon(device.icon)?)
Value::icon(device.icon)
};

widget.set_values(map! {
Expand Down
2 changes: 1 addition & 1 deletion src/blocks/notify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {

let mut widget = Widget::new().with_format(format.clone());
widget.set_values(map!(
"icon" => Value::icon(api.get_icon(if is_paused { ICON_OFF } else { ICON_ON })?),
"icon" => Value::icon(if is_paused { ICON_OFF } else { ICON_ON }),
[if notification_count != 0] "notification_count" => Value::number(notification_count),
[if is_paused] "paused" => Value::flag(),
));
Expand Down
2 changes: 1 addition & 1 deletion src/blocks/notmuch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
let mut widget = Widget::new().with_format(format.clone());

widget.set_values(map! {
"icon" => Value::icon(api.get_icon("mail")?),
"icon" => Value::icon("mail"),
"count" => Value::number(count)
});

Expand Down
2 changes: 1 addition & 1 deletion src/blocks/nvidia_gpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
};

widget.set_values(map! {
"icon" => Value::icon(api.get_icon("gpu")?),
"icon" => Value::icon("gpu"),
"name" => Value::text(info.name.clone()),
"utilization" => Value::percents(info.utilization),
"memory" => Value::bytes(if show_mem_total {info.mem_total} else {info.mem_used}).with_instance(MEM_BTN),
Expand Down
Loading

0 comments on commit 2720972

Please sign in to comment.