[update/improve] Improve the notification cereation mecarthm
This commit is contained in:
parent
31946b66ca
commit
10543a2cc1
2 changed files with 67 additions and 98 deletions
|
@ -1,8 +1,8 @@
|
||||||
use crate::cmus::player_settings::{AAAMode, PlayerSettings, Shuffle};
|
use crate::{process_template_placeholders, settings};
|
||||||
use crate::cmus::{Track, TrackStatus};
|
use crate::cmus::{Track, TrackStatus};
|
||||||
|
use crate::cmus::player_settings::{AAAMode, PlayerSettings, Shuffle};
|
||||||
use crate::notification::Action;
|
use crate::notification::Action;
|
||||||
use crate::settings::Settings;
|
use crate::settings::Settings;
|
||||||
use crate::{process_template_placeholders, settings};
|
|
||||||
|
|
||||||
#[derive(PartialEq)]
|
#[derive(PartialEq)]
|
||||||
#[cfg_attr(any(feature = "debug", test), derive(Debug))]
|
#[cfg_attr(any(feature = "debug", test), derive(Debug))]
|
||||||
|
@ -20,61 +20,36 @@ impl CmusEvent {
|
||||||
pub fn build_notification(
|
pub fn build_notification(
|
||||||
&self,
|
&self,
|
||||||
settings: &Settings,
|
settings: &Settings,
|
||||||
_notification: &mut notify_rust::Notification,
|
|
||||||
) -> Action {
|
) -> Action {
|
||||||
macro_rules! build_the_notification {
|
|
||||||
($body_template: expr, $summary_template: expr, $track: expr, $player_settings: expr) => {
|
|
||||||
_notification.body(&process_template_placeholders(
|
|
||||||
$body_template,
|
|
||||||
$track,
|
|
||||||
$player_settings,
|
|
||||||
))
|
|
||||||
.summary(&process_template_placeholders(
|
|
||||||
$summary_template,
|
|
||||||
$track,
|
|
||||||
$player_settings,
|
|
||||||
));
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
use CmusEvent::*;
|
use CmusEvent::*;
|
||||||
match self {
|
let (body_template, summary_template, track, player_settings) = match self {
|
||||||
StatusChanged(track, player_settings) => {
|
StatusChanged(track, player_settings) =>
|
||||||
build_the_notification!(settings.status_notification_body(), settings.status_notification_summary(), track, player_settings);
|
(settings.status_notification_body(), settings.status_notification_summary(), track, player_settings),
|
||||||
|
TrackChanged(track, player_settings) =>
|
||||||
|
(settings.body(), settings.summary(), track, player_settings),
|
||||||
|
VolumeChanged(track, player_settings) if settings.show_player_notifications =>
|
||||||
|
(settings.volume_notification_body(), settings.volume_notification_summary(), track, player_settings),
|
||||||
|
ShuffleChanged(track, player_settings) if settings.show_player_notifications =>
|
||||||
|
(settings.shuffle_notification_body(), settings.shuffle_notification_summary(), track, player_settings),
|
||||||
|
RepeatChanged(track, player_settings) if settings.show_player_notifications =>
|
||||||
|
(settings.repeat_notification_body(), settings.repeat_notification_summary(), track, player_settings),
|
||||||
|
AAAModeChanged(track, player_settings) if settings.show_player_notifications =>
|
||||||
|
(settings.aaa_mode_notification_body(), settings.aaa_mode_notification_summary(), track, player_settings),
|
||||||
|
_ => { return Action::None },
|
||||||
|
};
|
||||||
|
|
||||||
Action::Show
|
Action::Show {
|
||||||
}
|
notification_body: process_template_placeholders(
|
||||||
TrackChanged(track, player_settings) => {
|
body_template,
|
||||||
build_the_notification!(settings.body(), settings.summary(), track, player_settings);
|
track,
|
||||||
|
player_settings,
|
||||||
Action::Show
|
),
|
||||||
}
|
notification_summary: process_template_placeholders(
|
||||||
VolumeChanged(track, player_settings) if settings.show_player_notifications => {
|
summary_template,
|
||||||
build_the_notification!(settings.volume_notification_body(), settings.volume_notification_summary(), track, player_settings);
|
track,
|
||||||
|
player_settings,
|
||||||
Action::Show
|
),
|
||||||
}
|
save: false
|
||||||
PositionChanged(_track, _player_settings) if settings.show_player_notifications => {
|
|
||||||
//build_the_notification!(settings.volume_notification_body(), settings.volume_notification_summary(), track, player_settings);
|
|
||||||
//TODO: Implement this
|
|
||||||
Action::None
|
|
||||||
}
|
|
||||||
ShuffleChanged(track, player_settings) if settings.show_player_notifications => {
|
|
||||||
build_the_notification!(settings.shuffle_notification_body(), settings.shuffle_notification_summary(), track, player_settings);
|
|
||||||
|
|
||||||
Action::Show
|
|
||||||
}
|
|
||||||
RepeatChanged(track, player_settings) if settings.show_player_notifications => {
|
|
||||||
build_the_notification!(settings.repeat_notification_body(), settings.repeat_notification_summary(), track, player_settings);
|
|
||||||
|
|
||||||
Action::Show
|
|
||||||
}
|
|
||||||
AAAModeChanged(track, player_settings) if settings.show_player_notifications => {
|
|
||||||
build_the_notification!(settings.aaa_mode_notification_body(), settings.aaa_mode_notification_summary(), track, player_settings);
|
|
||||||
|
|
||||||
Action::Show
|
|
||||||
}
|
|
||||||
_ => Action::None,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,30 +1,21 @@
|
||||||
use crate::cmus::events::CmusEvent;
|
|
||||||
use crate::cmus::player_settings::PlayerSettings;
|
|
||||||
use crate::cmus::query::CmusQueryResponse;
|
|
||||||
use crate::cmus::{TemplateProcessor, Track, TrackStatus};
|
|
||||||
use crate::settings::Settings;
|
|
||||||
use crate::{process_template_placeholders, settings, track_cover, TrackCover};
|
|
||||||
#[cfg(feature = "debug")]
|
#[cfg(feature = "debug")]
|
||||||
use log::{debug, info};
|
use log::{debug, info};
|
||||||
use notify_rust::Notification;
|
use notify_rust::Notification;
|
||||||
|
|
||||||
pub enum Action {
|
use crate::{process_template_placeholders, settings, track_cover, TrackCover};
|
||||||
Show,
|
use crate::cmus::{TemplateProcessor, Track, TrackStatus};
|
||||||
Update,
|
use crate::cmus::events::CmusEvent;
|
||||||
None,
|
use crate::cmus::player_settings::PlayerSettings;
|
||||||
}
|
use crate::cmus::query::CmusQueryResponse;
|
||||||
|
use crate::settings::Settings;
|
||||||
|
|
||||||
impl Action {
|
pub enum Action {
|
||||||
pub fn max(self, other: Self) -> Self {
|
Show {
|
||||||
match (self, other) {
|
notification_body: String,
|
||||||
(Action::Show, Action::Show) => Action::Show,
|
notification_summary: String,
|
||||||
(Action::Show, Action::Update) | (Action::Update, Action::Show | Action::Update) => {
|
save: bool,
|
||||||
Action::Update
|
},
|
||||||
}
|
None,
|
||||||
(Action::None, _) => Action::None,
|
|
||||||
(_, Action::None) => Action::None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct NotificationsHandler {
|
pub struct NotificationsHandler {
|
||||||
|
@ -39,7 +30,7 @@ impl NotificationsHandler {
|
||||||
Self {
|
Self {
|
||||||
cover_set: false,
|
cover_set: false,
|
||||||
notification: Notification::new(),
|
notification: Notification::new(),
|
||||||
handlers: Vec::new(),
|
handlers: Vec::with_capacity(2),
|
||||||
settings,
|
settings,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -50,28 +41,31 @@ impl NotificationsHandler {
|
||||||
events: Vec<CmusEvent>,
|
events: Vec<CmusEvent>,
|
||||||
response: &CmusQueryResponse,
|
response: &CmusQueryResponse,
|
||||||
) -> Result<(), notify_rust::error::Error> {
|
) -> Result<(), notify_rust::error::Error> {
|
||||||
// Setup the notification cover
|
|
||||||
if self.settings.show_track_cover {
|
|
||||||
self.update_cover(&events[0], response);
|
|
||||||
} else if self.settings.notification_static_cover.is_some() && !self.cover_set {
|
|
||||||
self.setup_the_notification();
|
|
||||||
self.notification
|
|
||||||
.image_path(self.settings.notification_static_cover.as_ref().unwrap());
|
|
||||||
self.cover_set = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
for event in events {
|
for event in events {
|
||||||
self.setup_notification_timeout(&event);
|
self.setup_notification_timeout(&event);
|
||||||
#[cfg(feature = "debug")]
|
#[cfg(feature = "debug")]
|
||||||
info!("event: {:?}", event);
|
info!("event: {:?}", event);
|
||||||
|
|
||||||
let action = event.build_notification(&mut self.settings, &mut self.notification);
|
match event.build_notification(&self.settings) {
|
||||||
|
Action::Show { notification_body, notification_summary, save } => {
|
||||||
|
// Setup the notification cover
|
||||||
|
if self.settings.show_track_cover {
|
||||||
|
self.update_cover(&event, response);
|
||||||
|
} else if self.settings.notification_static_cover.is_some() && !self.cover_set {
|
||||||
|
self.setup_the_notification();
|
||||||
|
self.notification
|
||||||
|
.image_path(self.settings.notification_static_cover.as_ref().unwrap());
|
||||||
|
self.cover_set = true;
|
||||||
|
}
|
||||||
|
|
||||||
match action {
|
self.notification.summary(¬ification_summary).body(¬ification_body);
|
||||||
Action::Show => {
|
|
||||||
let _ = self.notification.show()?;
|
// Show the notification
|
||||||
|
let handle = self.notification.show()?;
|
||||||
|
if save {
|
||||||
|
self.handlers.push(handle);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Action::Update => todo!("Update notification"),
|
|
||||||
Action::None => {}
|
Action::None => {}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -80,10 +74,12 @@ impl NotificationsHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn update_cover(&mut self, first_event: &CmusEvent, response: &CmusQueryResponse) {
|
fn update_cover(&mut self, event: &CmusEvent, response: &CmusQueryResponse) {
|
||||||
// If the track is changed, we need to update the cover.
|
// If the track is changed, we need to update the cover.
|
||||||
match first_event {
|
match event {
|
||||||
CmusEvent::TrackChanged(track, _) => {
|
CmusEvent::TrackChanged(track, _) => {
|
||||||
|
// Reset the notification
|
||||||
|
self.setup_the_notification();
|
||||||
self.set_cover(track);
|
self.set_cover(track);
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
|
@ -99,8 +95,6 @@ impl NotificationsHandler {
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn set_cover(&mut self, track: &Track) {
|
fn set_cover(&mut self, track: &Track) {
|
||||||
// Reset the notification
|
|
||||||
self.setup_the_notification();
|
|
||||||
let path = match &self.settings.cover_path_template {
|
let path = match &self.settings.cover_path_template {
|
||||||
Some(template) => track.process(template.clone()),
|
Some(template) => track.process(template.clone()),
|
||||||
None => track.path.clone(),
|
None => track.path.clone(),
|
||||||
|
@ -142,7 +136,7 @@ impl NotificationsHandler {
|
||||||
match event {
|
match event {
|
||||||
TrackChanged(_, _) => self.settings.timeout(),
|
TrackChanged(_, _) => self.settings.timeout(),
|
||||||
StatusChanged(_, _) => self.settings.status_notification_timeout(),
|
StatusChanged(_, _) => self.settings.status_notification_timeout(),
|
||||||
AAAMode(_, _) => self.settings.aaa_mode_notification_timeout(),
|
AAAModeChanged(_, _) => self.settings.aaa_mode_notification_timeout(),
|
||||||
VolumeChanged(_, _) => self.settings.volume_notification_timeout(),
|
VolumeChanged(_, _) => self.settings.volume_notification_timeout(),
|
||||||
RepeatChanged(_, _) => self.settings.repeat_notification_timeout(),
|
RepeatChanged(_, _) => self.settings.repeat_notification_timeout(),
|
||||||
ShuffleChanged(_, _) => self.settings.shuffle_notification_timeout(),
|
ShuffleChanged(_, _) => self.settings.shuffle_notification_timeout(),
|
||||||
|
|
Loading…
Reference in a new issue