use colours from sway config while interpolating

This commit is contained in:
Alex Janka 2024-07-24 16:05:53 +10:00
parent bc538f27ea
commit b5faff6871

View file

@ -63,6 +63,8 @@ fn lab_de<'a, D: serde::Deserializer<'a>>(deserializer: D) -> Result<Lab, D::Err
static CONFIG: tokio::sync::OnceCell<Config> = tokio::sync::OnceCell::const_new();
static DEFAULT_COLOURS: tokio::sync::OnceCell<String> = tokio::sync::OnceCell::const_new();
static DEFAULT_COLOURS_NO_INDICATOR: tokio::sync::OnceCell<String> =
tokio::sync::OnceCell::const_new();
static DEFAULT_BORDER: tokio::sync::OnceCell<Lab> = tokio::sync::OnceCell::const_new();
#[tokio::main]
@ -153,6 +155,10 @@ async fn get_sway_config(
DEFAULT_BORDER.set(parse_hex(default_border)?)?;
DEFAULT_COLOURS.set(default_colours.to_string())?;
let mut split = default_colours.split_whitespace().collect::<Vec<_>>();
split.pop();
DEFAULT_COLOURS_NO_INDICATOR.set(split.join(" "))?;
Ok(())
}
@ -177,7 +183,10 @@ async fn interpolate_task() -> Result<(), Box<dyn std::error::Error>> {
);
let mut c = config.flash_colour;
connection.run_command(set_command(&c)).await?;
{
let command = set_command(&c)?;
connection.run_command(command).await?;
}
let dur_per_frame = std::time::Duration::from_secs_f64(1.0 / config.refresh_rate as f64);
tokio::time::sleep(dur_per_frame * config.frames_delay).await;
@ -185,7 +194,10 @@ async fn interpolate_task() -> Result<(), Box<dyn std::error::Error>> {
c.l += d_l;
c.a += d_a;
c.b += d_b;
connection.run_command(set_command(&c)).await?;
{
let command = set_command(&c)?;
connection.run_command(command).await?;
}
tokio::time::sleep(dur_per_frame).await;
}
connection
@ -194,7 +206,10 @@ async fn interpolate_task() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}
fn set_command(colour: &Lab) -> String {
fn set_command(colour: &Lab) -> Result<String, Box<dyn std::error::Error>> {
let [r, g, b] = colour.to_rgb();
format!("client.focused #000000 #88c0d0 #000000 #{r:02x}{g:02x}{b:02x}")
Ok(format!(
"{} #{r:02x}{g:02x}{b:02x}",
DEFAULT_COLOURS_NO_INDICATOR.get().ok_or(NoMatchingConfig)?
))
}