From 49d5c1665f1d90c5cfe8bcdf7afca2a8ddbd80c6 Mon Sep 17 00:00:00 2001 From: Kubat <mael.martin31@gmail.com> Date: Fri, 3 Feb 2023 22:18:32 +0100 Subject: [PATCH] AMADEUS: Add comments... --- src/rust/amadeus/src/amadeus.rs | 35 +++++++++++++++++-- src/rust/amadeus/src/buttons/mod.rs | 1 - src/rust/amadeus/src/client.rs | 18 ++++++++++ src/rust/amadeus/src/main.rs | 4 +-- .../src/{buttons => }/queue_control.rs | 6 ++++ 5 files changed, 57 insertions(+), 7 deletions(-) delete mode 100644 src/rust/amadeus/src/buttons/mod.rs rename src/rust/amadeus/src/{buttons => }/queue_control.rs (88%) diff --git a/src/rust/amadeus/src/amadeus.rs b/src/rust/amadeus/src/amadeus.rs index 66378b94..af12b72b 100644 --- a/src/rust/amadeus/src/amadeus.rs +++ b/src/rust/amadeus/src/amadeus.rs @@ -1,10 +1,11 @@ -use crate::{action, buttons::queue_control, client, AmadeusConfig}; +use crate::{action, client, queue_control, AmadeusConfig}; use iced::{ executor, subscription, widget::{column, row}, Alignment, Application, Command, Element, Event, Subscription, Theme, }; +/// The Amadeus application state. #[derive(Debug)] pub struct Amadeus { client: client::Client, @@ -14,12 +15,27 @@ pub struct Amadeus { #[derive(Debug, Clone)] pub enum Message { + /// Do nothing. None, + + /// Ask for a deconexion. Disconnect, + + /// Got an event from iced. Event(Event), + + /// Lektord send a notification. Notification(action::Action), + + /// Send a command, perform the action. Action(action::Action), + + /// The client sent a message / the message must be dispatched to the + /// client for handling it. Client(client::Message), + + /// The queue controler sent a message / the message must be dispatched to + /// the queue controller (update its status). QueueControl(queue_control::Message), } @@ -29,6 +45,8 @@ impl Application for Amadeus { type Theme = Theme; type Flags = AmadeusConfig; + /// Create a new application from the flags that were stored in the config + /// file (~/.config/amadeus/amadeus.toml). fn new(flags: Self::Flags) -> (Self, Command<Self::Message>) { ( Self { @@ -40,10 +58,13 @@ impl Application for Amadeus { ) } + /// Get the title of the application, `Amadeus - {x}` where `x` is the + /// status, the playing file, etc. fn title(&self) -> String { String::from("Amadeus") } + /// Update the state of the application, handling the passed message. fn update(&mut self, message: Self::Message) -> Command<Self::Message> { match message { Message::None => Command::none(), @@ -64,8 +85,10 @@ impl Application for Amadeus { Command::none() } + // We ignore events from iced for now. Message::Event(_) => Command::none(), + // Dispatch to the queue controler, re-dispatch any returned message. Message::QueueControl(message) => { self.queue_control .update(message) @@ -76,6 +99,7 @@ impl Application for Amadeus { }) } + // Dispatch to the client, re-dispatch any returned message. Message::Client(message) => { self.client .update(&self.flags, message) @@ -89,19 +113,22 @@ impl Application for Amadeus { }) } - // On notifications we update the queue controler. + // On notifications we update only the queue controler for now, in + // the future we may update something else. Message::Notification(message) => self.update(Message::QueueControl( queue_control::Message::Action(message), )), // On an action, we ask the client to perform it, it will return a - // notification latter. + // notification latter. In the future we may want to send that + // action to another component. Message::Action(message) => { self.update(Message::Client(client::Message::Action(message))) } } } + /// Display the whole application. fn view(&self) -> Element<Self::Message> { column![row![ self.queue_control.view().map(Message::QueueControl), @@ -112,10 +139,12 @@ impl Application for Amadeus { .into() } + /// Our theme. fn theme(&self) -> Theme { Theme::Dark } + /// Get all the subscriptions, from iced and the client. fn subscription(&self) -> Subscription<Self::Message> { Subscription::batch([ subscription::events().map(Message::Event), diff --git a/src/rust/amadeus/src/buttons/mod.rs b/src/rust/amadeus/src/buttons/mod.rs deleted file mode 100644 index fb269fde..00000000 --- a/src/rust/amadeus/src/buttons/mod.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod queue_control; diff --git a/src/rust/amadeus/src/client.rs b/src/rust/amadeus/src/client.rs index 770f2984..b04f55d9 100644 --- a/src/rust/amadeus/src/client.rs +++ b/src/rust/amadeus/src/client.rs @@ -9,11 +9,14 @@ use iced::{ use serde::{Deserialize, Serialize}; use std::sync::Arc; +/// The address of the lektord deamon. #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(tag = "type", content = "socket")] pub enum Address { + /// Connect over TCP/IP TCP(String, i16), + /// Connect over UNIX domain socket. #[cfg(unix)] UNIX(String), } @@ -29,8 +32,13 @@ impl std::fmt::Display for Address { #[derive(Debug, Clone)] pub enum Message { + /// Nothing to do... None, + + /// We ask for a connexion Connect, + + /// We are connected. Connected(Arc<Mutex<amalib::AmaClient>>), /// Got an error... @@ -52,16 +60,26 @@ pub enum Message { Multiple(Vec<Message>), } +/// The state of the coposent that communicate with lektord. #[derive(Debug)] enum State { + /// We are offline. Offline, + + /// We are online. Online(Arc<Mutex<amalib::AmaClient>>), + + /// We are asking to connect to an address. AskConnection(Address), } +/// The private state of a worker. #[derive(Debug)] struct WorkerState(Arc<Mutex<amalib::AmaClient>>); +/// The client thing in amadeus. Responsible to poll notifications and perform +/// actions when needed. We add a view for that thing to print some statuses +/// somewhere in the window. #[derive(Debug)] pub struct Client { state: State, diff --git a/src/rust/amadeus/src/main.rs b/src/rust/amadeus/src/main.rs index a24cf4b3..5663072b 100644 --- a/src/rust/amadeus/src/main.rs +++ b/src/rust/amadeus/src/main.rs @@ -6,10 +6,8 @@ use serde::{Deserialize, Serialize}; mod action; mod amadeus; -mod buttons; mod client; - -pub(crate) const CHANNEL_SIZE: usize = 128; +mod queue_control; #[derive(Debug, Deserialize, Serialize)] pub struct AmadeusRemoteConfig { diff --git a/src/rust/amadeus/src/buttons/queue_control.rs b/src/rust/amadeus/src/queue_control.rs similarity index 88% rename from src/rust/amadeus/src/buttons/queue_control.rs rename to src/rust/amadeus/src/queue_control.rs index edcf7420..cd288036 100644 --- a/src/rust/amadeus/src/buttons/queue_control.rs +++ b/src/rust/amadeus/src/queue_control.rs @@ -8,19 +8,23 @@ pub enum Message { Disconnect, } +/// The state of the queue controler. #[derive(Debug, Clone, Copy)] struct State(Option<amalib::LektorState>); +/// The queue controler. Can play/pause, play the next/previous kara, etc. #[derive(Debug, Clone, Copy)] pub struct QueueControl { state: State, } impl QueueControl { + /// Create a new queue controler pub fn new() -> Self { Self { state: State(None) } } + /// Update the queue controler, apply any change received from lektord. pub fn update(&mut self, message: Message) -> Command<Message> { use amalib::LektorState::*; match message { @@ -72,6 +76,8 @@ impl QueueControl { } } + /// View the queue controller. Depending on its state, we may want to send + /// commands to lektord. pub fn view(&self) -> Element<Message> { const SIZE: u16 = 20; use amalib::LektorState::*; -- GitLab