diff --git a/src/main.rs b/src/main.rs index a4c2f30..89f2cca 100644 --- a/src/main.rs +++ b/src/main.rs @@ -96,6 +96,8 @@ pub fn data_dir() -> crate::Result { Ok(dir) } +/// Simply creates and runs the player, so that the [`Result`] of both operations +/// can be easily handled by the [`main`] function. async fn player(args: Args, environment: ui::Environment) -> crate::Result<()> { let stream = audio::stream()?; let mut player = Player::init(args, environment, stream.mixer()).await?; diff --git a/src/ui.rs b/src/ui.rs index 297e307..675d921 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -1,4 +1,4 @@ -use std::{env, sync::Arc}; +use std::sync::Arc; use crate::{ player::Current, @@ -119,6 +119,7 @@ struct Tasks { } impl Tasks { + /// Actually takes care of spawning the tasks for the [`ui`]. pub fn spawn( tx: Sender, updater: broadcast::Receiver, @@ -196,17 +197,15 @@ impl Handle { state: State, args: &Args, ) -> Result { - let disabled = env::var("LOWFI_DISABLE_UI").is_ok_and(|x| x == "1"); - if disabled && !cfg!(feature = "mpris") { - return Err(Error::RejectedDisable); - } + let params = interface::Params::try_from(args)?; Ok(Self { #[cfg(feature = "mpris")] mpris: mpris::Server::new(state.clone(), tx.clone(), updater.resubscribe()).await?, environment, - _tasks: (!disabled) - .then(|| Tasks::spawn(tx, updater, state, interface::Params::from(args))), + _tasks: params + .enabled + .then(|| Tasks::spawn(tx, updater, state, params)), }) } } diff --git a/src/ui/interface.rs b/src/ui/interface.rs index 6114e4f..4e7bdef 100644 --- a/src/ui/interface.rs +++ b/src/ui/interface.rs @@ -1,4 +1,4 @@ -use std::time::Duration; +use std::{env, time::Duration}; use crate::{ ui::{self, components, window::Window}, @@ -9,19 +9,28 @@ use crate::{ pub struct Params { pub borderless: bool, pub minimalist: bool, + pub enabled: bool, pub delta: Duration, } -impl From<&Args> for Params { - fn from(args: &Args) -> Self { +impl TryFrom<&Args> for Params { + type Error = ui::Error; + + fn try_from(args: &Args) -> ui::Result { let delta = 1.0 / f32::from(args.fps); let delta = Duration::from_secs_f32(delta); - Self { + let disabled = env::var("LOWFI_DISABLE_UI").is_ok_and(|x| x == "1"); + if disabled && !cfg!(feature = "mpris") { + return Err(ui::Error::RejectedDisable); + } + + Ok(Self { delta, + enabled: !disabled, minimalist: args.minimalist, borderless: args.borderless, - } + }) } }