From ec09d8e4e00078e58bcbba3c5fdd7f799cd8b1a3 Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Thu, 26 Oct 2023 22:28:41 +0200
Subject: [PATCH] AMADEUS: Directly handle the keyboard event in the
 subscription, mapping directly the playback request out of the subscription

---
 amadeus/src/app.rs     | 35 +++++++++++++----------------------
 amadeus/src/message.rs |  4 +---
 2 files changed, 14 insertions(+), 25 deletions(-)

diff --git a/amadeus/src/app.rs b/amadeus/src/app.rs
index 5bd13df5..2d929548 100644
--- a/amadeus/src/app.rs
+++ b/amadeus/src/app.rs
@@ -377,6 +377,7 @@ impl Amadeus {
         let cfg = self.connect_config.clone();
         match req {
             ChangePlayback(state) => send(set_playback_state(cfg, state)),
+            TogglePlaybackState => send(toggle_playback_state(self.connect_config.clone())),
             PlayNext => send(play_next(cfg)),
             PlayPrevious => send(play_previous(cfg)),
         }
@@ -497,26 +498,6 @@ impl Amadeus {
         }
     }
 
-    /// Handle the Event message. For now we only handle some keyboard events.
-    fn handle_event(&mut self, event: Event) -> Command<<Self as Application>::Message> {
-        match event {
-            Event::Keyboard(KbdEvent::KeyReleased {
-                key_code,
-                modifiers,
-            }) => match key_code {
-                KeyCode::Space => send(toggle_playback_state(self.connect_config.clone())),
-                KeyCode::N if modifiers.control() => {
-                    Command::perform(async {}, |_| PlaybackRequest::PlayNext.into())
-                }
-                KeyCode::P if modifiers.control() => {
-                    Command::perform(async {}, |_| PlaybackRequest::PlayPrevious.into())
-                }
-                _ => Command::none(),
-            },
-            _ => Command::none(),
-        }
-    }
-
     fn playlist_list(&self) -> &[Arc<str>] {
         self.sidebar.playlist_list()
     }
@@ -625,7 +606,6 @@ impl Application for Amadeus {
             Message::ExitApplication => iced::window::close(),
 
             // Messages got from subscriptions.
-            Message::Event(event) => self.handle_event(event),
             Message::Tick(instant) => {
                 let delta = instant.saturating_duration_since(self.last_instant);
                 self.last_instant = instant;
@@ -816,7 +796,18 @@ impl Application for Amadeus {
     /// We need a tick every second to query lektord plus we listen for any event.
     fn subscription(&self) -> iced::Subscription<Self::Message> {
         iced::Subscription::batch([
-            iced::subscription::events().map(Message::Event),
+            iced::subscription::events().map(|event| match event {
+                Event::Keyboard(KbdEvent::KeyReleased {
+                    key_code,
+                    modifiers,
+                }) => match key_code {
+                    KeyCode::Space => PlaybackRequest::TogglePlaybackState.into(),
+                    KeyCode::N if modifiers.control() => PlaybackRequest::PlayNext.into(),
+                    KeyCode::P if modifiers.control() => PlaybackRequest::PlayPrevious.into(),
+                    _ => Message::None,
+                },
+                _ => Message::None,
+            }),
             iced::time::every(iced::time::Duration::new(1, 0)).map(Message::Tick),
         ])
     }
diff --git a/amadeus/src/message.rs b/amadeus/src/message.rs
index 4fc437ff..a3ddca6d 100644
--- a/amadeus/src/message.rs
+++ b/amadeus/src/message.rs
@@ -20,6 +20,7 @@ pub enum MainPanelDisplay {
 #[derive(Debug, Clone, Copy)]
 pub enum PlaybackRequest {
     ChangePlayback(PlayState),
+    TogglePlaybackState,
     PlayNext,
     PlayPrevious,
 }
@@ -44,9 +45,6 @@ pub enum Message {
     /// We got a tick.
     Tick(iced::time::Instant),
 
-    /// We got an event.
-    Event(iced::Event),
-
     /// Open the issues link with firefox.
     OpenLinkInBrowser(&'static str),
 
-- 
GitLab