Improve the artict

This commit is contained in:
Anas Elgarhy 2023-02-20 23:04:38 +02:00
parent 9a5a93450a
commit d004461209
No known key found for this signature in database
GPG Key ID: 0501802A1D496528
4 changed files with 164 additions and 88 deletions

View File

@ -1,5 +1,8 @@
use crate::cmus::player_settings::{AAAMode, PlayerSettings, Shuffle};
use crate::cmus::{Track, TrackStatus};
use crate::notification::Action;
use crate::process_template_placeholders;
use crate::settings::Settings;
#[derive(Debug, PartialEq)]
pub enum CmusEvent {
@ -11,3 +14,142 @@ pub enum CmusEvent {
RepeatChanged(Track, PlayerSettings),
AAAMode(Track, PlayerSettings),
}
impl CmusEvent {
pub fn build_notification(
&self,
settings: &Settings,
notification: &mut notify_rust::Notification,
) -> Action {
use CmusEvent::*;
match self {
StatusChanged(_, _) | TrackChanged(_, _) => {
let (body, body_action) = self.build_notification_body(settings);
let (summary, summary_action) = self.build_notification_summary(settings);
notification.body(&body).summary(&summary);
body_action.max(summary_action)
}
VolumeChanged(_, _)
| PositionChanged(_, _)
| ShuffleChanged(_, _)
| RepeatChanged(_, _)
| AAAMode(_, _)
if settings.show_player_notifications =>
{
let (body, body_action) = self.build_notification_body(settings);
let (summary, summary_action) = self.build_notification_summary(settings);
notification.body(&body).summary(&summary);
body_action.max(summary_action)
}
_ => Action::None,
}
}
#[inline]
fn build_notification_body(&self, settings: &Settings) -> (String, Action) {
use CmusEvent::*;
match self {
StatusChanged(track, player_settings) => (
process_template_placeholders(
&settings.status_notification_body,
track,
player_settings,
),
Action::Show,
),
TrackChanged(track, player_settings) => (
process_template_placeholders(&settings.body, track, player_settings),
Action::Show,
),
VolumeChanged(track, player_settings) => (
process_template_placeholders(
&settings.volume_notification_body,
track,
player_settings,
),
Action::Show,
),
PositionChanged(_track, _player_settings) => {
(String::new(), Action::None) // TODO: Implement this
}
ShuffleChanged(track, player_settings) => (
process_template_placeholders(
&settings.shuffle_notification_body,
track,
player_settings,
),
Action::Show,
),
RepeatChanged(track, player_settings) => (
process_template_placeholders(
&settings.repeat_notification_body,
track,
player_settings,
),
Action::Show,
),
AAAMode(track, player_settings) => (
process_template_placeholders(
&settings.aaa_mode_notification_body,
track,
player_settings,
),
Action::Show,
),
}
}
#[inline]
fn build_notification_summary(&self, settings: &Settings) -> (String, Action) {
use CmusEvent::*;
match self {
StatusChanged(track, player_settings) => (
process_template_placeholders(
&settings.status_notification_summary,
track,
player_settings,
),
Action::Show,
),
TrackChanged(track, player_settings) => (
process_template_placeholders(&settings.summary, track, player_settings),
Action::Show,
),
VolumeChanged(track, player_settings) => (
process_template_placeholders(
&settings.volume_notification_summary,
track,
player_settings,
),
Action::Show,
),
PositionChanged(_track, _player_settings) => {
(String::new(), Action::None) // TODO: Implement this
}
ShuffleChanged(track, player_settings) => (
process_template_placeholders(
&settings.shuffle_notification_summary,
track,
player_settings,
),
Action::Show,
),
RepeatChanged(track, player_settings) => (
process_template_placeholders(
&settings.repeat_notification_summary,
track,
player_settings,
),
Action::Show,
),
AAAMode(track, player_settings) => (
process_template_placeholders(
&settings.aaa_mode_notification_summary,
track,
player_settings,
),
Action::Show,
),
}
}
}

View File

@ -51,7 +51,7 @@ impl CmusQueryResponse {
if self.track_row.is_empty() || self.player_settings_row.is_empty() {
#[cfg(feature = "debug")]
info!("Cmus response is empty, returning empty events");
return Ok(Vec::new())
return Ok(Vec::new());
}
let mut events = Vec::new();

View File

@ -209,7 +209,11 @@ fn search(search_directory: &str, matcher: &regex::Regex) -> std::io::Result<Opt
}
/// Replace all the placeholders in the template with their matching value.
pub fn process_template_placeholders(template: &String, track: &cmus::Track) -> String {
pub fn process_template_placeholders(
template: &String,
track: &cmus::Track,
player_settings: &cmus::player_settings::PlayerSettings,
) -> String {
#[cfg(feature = "debug")]
{
info!("Processing the template placeholders.");

View File

@ -8,12 +8,25 @@ use crate::{process_template_placeholders, track_cover, TrackCover};
use log::{debug, info};
use notify_rust::Notification;
enum Action {
pub enum Action {
Show,
Update,
None,
}
impl Action {
pub fn max(self, other: Self) -> Self {
match (self, other) {
(Action::Show, Action::Show) => Action::Show,
(Action::Show, Action::Update) | (Action::Update, Action::Show | Action::Update) => {
Action::Update
}
(Action::None, _) => Action::None,
(_, Action::None) => Action::None,
}
}
}
pub struct NotificationsHandler {
cover_set: bool,
notification: Notification,
@ -46,36 +59,7 @@ impl NotificationsHandler {
#[cfg(feature = "debug")]
info!("event: {:?}", event);
match event {
CmusEvent::StatusChanged(track, player_settings) => {
#[cfg(feature = "debug")]
debug!("Status changed: {:?}", track.status);
action = self.build_status_notification(track, player_settings);
}
CmusEvent::TrackChanged(track, player_settings) => {
#[cfg(feature = "debug")]
debug!("Track changed: {:?}", track);
action = self.build_track_notification(track, player_settings);
}
CmusEvent::VolumeChanged(track, player_settings)
if self.settings.show_player_notifications =>
{
action = self.build_volume_notification(track, player_settings);
}
/*
CmusEvent::PositionChanged(position) => todo!(),
CmusEvent::ShuffleChanged(shuffle) if settings.show_player_notifications => {
build_shuffle_notification(shuffle, settings, notification)?
}
CmusEvent::RepeatChanged(repeat) if settings.show_player_notifications => {
build_repeat_notification(repeat, settings, notification)?
}
CmusEvent::AAAMode(aaa_mode) if settings.show_player_notifications => {
build_aaa_mode_notification(aaa_mode, settings, notification)?
}
*/
_ => {}
}
action = event.build_notification(&mut self.settings, &mut self.notification);
match action {
Action::Show => {
@ -86,63 +70,9 @@ impl NotificationsHandler {
};
}
Ok(())
}
#[inline(always)]
fn build_status_notification(
&mut self,
track: Track,
player_settings: PlayerSettings,
) -> Action {
// Set the summary and body of the notification.
self.notification
.summary(
process_template_placeholders(&self.settings.status_notification_summary, &track)
.as_str(),
)
.body(
process_template_placeholders(&self.settings.status_notification_body, &track)
.as_str(),
)
.timeout(self.settings.status_notification_timeout as i32 * 1000);
Action::Show
}
#[inline(always)]
fn build_track_notification(
&mut self,
track: Track,
player_settings: PlayerSettings,
) -> Action {
// Set the summary and body of the notification.
self.notification
.summary(process_template_placeholders(&self.settings.summary, &track).as_str())
.body(process_template_placeholders(&self.settings.body, &track).as_str());
Action::Show
}
#[inline(always)]
fn build_volume_notification(
&mut self,
track: Track,
player_settings: PlayerSettings,
) -> Action {
self.notification
.summary(
process_template_placeholders(&self.settings.volume_notification_summary, &track)
.as_str(),
)
.body(
process_template_placeholders(&self.settings.volume_notification_body, &track)
.as_str(),
);
Action::Show
}
#[inline(always)]
fn update_cover(&mut self, first_event: &CmusEvent, response: &CmusQueryResponse) {
// If the track is changed, we need to update the cover.
@ -179,7 +109,7 @@ impl NotificationsHandler {
self.settings.force_use_external_cover,
self.settings.no_use_external_cover,
)
.set_notification_image(&mut self.notification);
.set_notification_image(&mut self.notification);
// Flip the change flag
self.cover_set = true;
}