style: improve a few names

This commit is contained in:
talwat 2025-12-17 22:44:34 +01:00
parent 287a61ca80
commit 8ff0f477ac
7 changed files with 30 additions and 33 deletions

View File

@ -61,7 +61,7 @@ pub struct Player {
rx: Receiver<crate::Message>,
/// Broadcast channel used to send UI updates.
broadcast: broadcast::Sender<ui::Update>,
updater: broadcast::Sender<ui::Update>,
/// Current playback state (loading or track).
current: Current,
@ -106,7 +106,7 @@ impl Player {
/// Sends a `ui::Update` to the broadcast channel.
pub fn update(&mut self, update: ui::Update) -> crate::Result<()> {
self.broadcast.send(update)?;
self.updater.send(update)?;
Ok(())
}
@ -147,7 +147,7 @@ impl Player {
waiter: waiter::Handle::new(Arc::clone(&sink), tx),
bookmarks: Bookmarks::load().await?,
current: Current::default(),
broadcast: utx,
updater: utx,
rx,
sink,
})

View File

@ -156,7 +156,7 @@ mod interface {
sink.set_volume(0.5);
let mut state = State::initial(sink, String::from("test"));
state.timer = Some(Instant::now());
state.volume_timer = Some(Instant::now());
let menu = Interface::default().menu(&mut state);
assert_eq!(menu[0], "loading ");

View File

@ -29,11 +29,11 @@ pub enum Error {
#[error("unable to write output: {0}")]
Write(#[from] std::io::Error),
#[error("sending message to backend from ui failed: {0}")]
CrateSend(#[from] tokio::sync::mpsc::error::SendError<crate::Message>),
#[error("sending signal message to backend from ui failed: {0}")]
SignalSend(#[from] tokio::sync::mpsc::error::SendError<crate::Message>),
#[error("sharing state between backend and frontend failed: {0}")]
Send(#[from] tokio::sync::broadcast::error::SendError<Update>),
StateSend(#[from] tokio::sync::broadcast::error::SendError<Update>),
#[error("you can't disable the UI without MPRIS!")]
RejectedDisable,
@ -64,11 +64,11 @@ pub struct State {
pub bookmarked: bool,
/// The timer, which is used when the user changes volume to briefly display it.
pub(crate) timer: Option<Instant>,
pub(crate) volume_timer: Option<Instant>,
/// The name of the playing tracklist, for MPRIS.
/// The name of the playing tracklist, mainly for MPRIS.
#[allow(dead_code)]
list: String,
tracklist: String,
}
impl State {
@ -76,10 +76,10 @@ impl State {
pub fn initial(sink: Arc<rodio::Sink>, list: String) -> Self {
Self {
sink,
list,
tracklist: list,
current: Current::default(),
bookmarked: false,
timer: None,
volume_timer: None,
}
}
}
@ -155,18 +155,18 @@ impl Handle {
/// `rx` is the receiver for state updates, `state` the initial state,
/// and `params` specifies aesthetic options that are specified by the user.
async fn ui(
mut rx: broadcast::Receiver<Update>,
mut updater: broadcast::Receiver<Update>,
mut state: State,
params: interface::Params,
) -> Result<()> {
let mut interface = Interface::new(params);
loop {
if let Ok(message) = rx.try_recv() {
if let Ok(message) = updater.try_recv() {
match message {
Update::Track(track) => state.current = track,
Update::Bookmarked(bookmarked) => state.bookmarked = bookmarked,
Update::Volume => state.timer = Some(Instant::now()),
Update::Volume => state.volume_timer = Some(Instant::now()),
Update::Quit => break,
}
}

View File

@ -121,12 +121,12 @@ impl Interface {
pub(crate) fn menu(&self, state: &mut State) -> Vec<String> {
let action = components::action(state, self.params.width);
let middle = match state.timer {
let middle = match state.volume_timer {
Some(timer) => {
let volume = state.sink.volume();
let percentage = format!("{}%", (volume * 100.0).round().abs());
if timer.elapsed() > Duration::from_secs(1) {
state.timer = None;
state.volume_timer = None;
}
components::audio_bar(self.params.width - 17, volume, &percentage)

View File

@ -1,7 +1,7 @@
use std::fmt::Display;
use unicode_segmentation::UnicodeSegmentation;
/// The titlebar, which is essentially the entire top of the window.
/// The titlebar, which is the entire top row of the window.
///
/// The struct offers a basic API for displaying messages to it.
pub struct TitleBar {

View File

@ -331,7 +331,7 @@ impl Server {
let suffix = if env::var("LOWFI_FIXED_MPRIS_NAME").is_ok_and(|x| x == "1") {
String::from("lowfi")
} else {
format!("lowfi.{}.instance{}", state.list, process::id())
format!("lowfi.{}.instance{}", state.tracklist, process::id())
};
let server = mpris_server::Server::new(
@ -340,7 +340,7 @@ impl Server {
sender: Sender::new(sender),
sink: state.sink,
current: ArcSwap::new(Arc::new(state.current)),
list: state.list,
list: state.tracklist,
},
)
.await?;

View File

@ -12,7 +12,7 @@ type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("couldn't find config directory")]
Directory,
DirectoryNotFound,
#[error("io error: {0}")]
Io(#[from] std::io::Error),
@ -33,17 +33,17 @@ pub struct PersistentVolume {
}
impl PersistentVolume {
/// Retrieves the config directory, creating it if necessary.
async fn config() -> Result<PathBuf> {
/// Retrieves the config file path, creating it if necessary.
async fn path() -> Result<PathBuf> {
let config = dirs::config_dir()
.ok_or(Error::Directory)?
.ok_or(Error::DirectoryNotFound)?
.join(PathBuf::from("lowfi"));
if !config.exists() {
fs::create_dir_all(&config).await?;
}
Ok(config)
Ok(config.join(PathBuf::from("volume.txt")))
}
/// Returns the volume as a normalized float in the range 0.0..=1.0.
@ -56,17 +56,16 @@ impl PersistentVolume {
/// If the file does not exist a default of `100` is written and
/// returned.
pub async fn load() -> Result<Self> {
let config = Self::config().await?;
let volume = config.join(PathBuf::from("volume.txt"));
let path = Self::path().await?;
// Basically just read from the volume file if it exists, otherwise return 100.
let volume = if volume.exists() {
let contents = fs::read_to_string(volume).await?;
let volume = if path.exists() {
let contents = fs::read_to_string(path).await?;
let trimmed = contents.trim();
let stripped = trimmed.strip_suffix("%").unwrap_or(trimmed);
stripped.parse()?
} else {
fs::write(&volume, "100").await?;
fs::write(&path, "100").await?;
100u16
};
@ -75,10 +74,8 @@ impl PersistentVolume {
/// Saves `volume` (0.0..=1.0) to `volume.txt` as an integer percent.
pub async fn save(volume: f32) -> Result<()> {
let config = Self::config().await?;
let path = config.join(PathBuf::from("volume.txt"));
let percentage = (volume * 100.0).abs().round() as u16;
fs::write(path, percentage.to_string()).await?;
fs::write(Self::path().await?, percentage.to_string()).await?;
Ok(())
}