feat(volume): Adding a volume widget in the UI

This commit is contained in:
Patrick Marie 2024-05-05 20:03:41 +02:00
parent bf9a6e45c1
commit ff7d5bcdf2
No known key found for this signature in database
GPG key ID: BB519E5CD8E7BFA7
5 changed files with 48 additions and 24 deletions

View file

@ -87,6 +87,7 @@ variant | struct, tuple or unit | fields (separated by comma) | description
`Styled(styles, texts)` | tuple | list of [`Style`](#Style), [`Texts`](#Texts) | styled text
`Parts(parts)` | tuple | list of [`Texts`](#Texts) | concatenate multiple parts of texts
`If(condition, lhs, rhs)` or `If(condition, lhs)` | tuple | [`Condition`](#Condition), [`Texts`](#Texts), optional [`Texts`](#Texts) | if `condition` then `lhs` (else `rhs`)
`CurrentVolume` | unit | | current volume level in %
### Style
@ -167,6 +168,7 @@ variant | struct, tuple or unit | fields (separated by comma) | description
`And(lhs, rhs)` | tuple | [`Condition`](#Condition), [`Condition`](#Condition) | logical and
`Or(lhs, rhs)` | tuple | [`Condition`](#Condition), [`Condition`](#Condition) | logical or
`Xor(lhs, rhs)` | tuple | [`Condition`](#Condition), [`Condition`](#Condition) | logical exclusive or
`VolumeMuted` | unit | | true if current volume is 0
### Column

View file

@ -59,29 +59,43 @@ Config(
Styled([Fg(Indexed(185))], Query),
Styled([Fg(Indexed(185))], Text("⎸")),
]),
If(Not(Stopped), Parts([
Styled([Fg(Indexed(113))], Parts([
If(Playing, Text("[playing: "), Text("[paused: ")),
CurrentElapsed,
Text("/"),
CurrentDuration,
Text("] "),
])),
If(TitleExist,
Parts([
Styled([Fg(Indexed(149))], CurrentTitle),
If(ArtistExist, Parts([
Styled([Fg(Indexed(216))], Text(" ◆ ")),
Styled([Fg(Indexed(185))], CurrentArtist),
If(AlbumExist, Parts([
Parts([
Parts([
Styled([Fg(Indexed(113))], Text("[")),
If(VolumeMuted,
Styled([Fg(Indexed(113))], Text("muted")),
Parts([
Styled([Fg(Indexed(113))], Text("vol: ")),
Styled([Fg(Indexed(113))], CurrentVolume),
Styled([Fg(Indexed(113))], Text("%")),
]),
),
Styled([Fg(Indexed(113))], Text("]")),
]),
If(Not(Stopped), Parts([
Styled([Fg(Indexed(113))], Parts([
If(Playing, Text("[playing: "), Text("[paused: ")),
CurrentElapsed,
Text("/"),
CurrentDuration,
Text("] "),
])),
If(TitleExist,
Parts([
Styled([Fg(Indexed(149))], CurrentTitle),
If(ArtistExist, Parts([
Styled([Fg(Indexed(216))], Text(" ◆ ")),
Styled([Fg(Indexed(221))], CurrentAlbum),
Styled([Fg(Indexed(185))], CurrentArtist),
If(AlbumExist, Parts([
Styled([Fg(Indexed(216))], Text(" ◆ ")),
Styled([Fg(Indexed(221))], CurrentAlbum),
])),
])),
])),
]),
Styled([Fg(Indexed(185))], CurrentFile),
),
])),
]),
Styled([Fg(Indexed(185))], CurrentFile),
),
])),
])
)))),
Fixed(7, TextboxR(Styled([Fg(Indexed(81))], Parts([
Text("["),

View file

@ -80,6 +80,7 @@ pub enum Texts {
Styled(Vec<AddStyle>, Box<Texts>),
Parts(Vec<Texts>),
If(Condition, Box<Texts>, Option<Box<Texts>>),
CurrentVolume,
}
#[derive(Deserialize)]
@ -128,6 +129,7 @@ pub enum Condition {
And(Box<Condition>, Box<Condition>),
Or(Box<Condition>, Box<Condition>),
Xor(Box<Condition>, Box<Condition>),
VolumeMuted,
}
#[derive(Deserialize)]
@ -172,6 +174,7 @@ impl<'de> Deserialize<'de> for Texts {
Styled,
Parts,
If,
CurrentVolume,
}
struct StyledVisitor;
@ -245,6 +248,7 @@ impl<'de> Deserialize<'de> for Texts {
Variant::Styled => va.tuple_variant(2, StyledVisitor),
Variant::Parts => Ok(Texts::Parts(va.newtype_variant()?)),
Variant::If => va.tuple_variant(3, IfVisitor),
Variant::CurrentVolume => unit_variant!(CurrentVolume),
}
}
}

View file

@ -432,6 +432,9 @@ fn _flatten<'a>(spans: &mut Vec<Span<'a>>, xs: &'a Texts, s: &FlattenState<'a, '
_flatten(spans, xs, s);
}
}
Texts::CurrentVolume => {
spans.push(Span::styled(format!("{}", s.status.volume), *s.style));
}
}
}
@ -532,5 +535,6 @@ fn eval_cond(cond: &Condition, s: &ConditionState) -> bool {
Condition::And(x, y) => eval_cond(x, s) && eval_cond(y, s),
Condition::Or(x, y) => eval_cond(x, s) || eval_cond(y, s),
Condition::Xor(x, y) => eval_cond(x, s) ^ eval_cond(y, s),
Condition::VolumeMuted => s.status.volume == 0,
}
}

View file

@ -512,13 +512,13 @@ async fn run() -> Result<()> {
cl.command(b"volume +1")
.await
.context("Failed to increase volume")?;
0b100
0b101
}
Command::VolumeDecrease => {
cl.command(b"volume -1")
.await
.context("Failed to decrease volume")?;
0b100
0b101
}
Command::VolumeToggle => {
let current_volume = s.status.volume;
@ -533,7 +533,7 @@ async fn run() -> Result<()> {
.context("Failed to mute/unmute volume")?;
last_volume = current_volume;
0b100
0b101
}
}) | updates.swap(0b000, Ordering::SeqCst)
} else {