Sélectionner une révision Git
amadeus.rs 5,20 Kio
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,
queue_control: queue_control::QueueControl,
flags: AmadeusConfig,
}
#[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),
}
impl Application for Amadeus {
type Executor = executor::Default;
type Message = Message;
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 {
client: client::Client::new(),
queue_control: queue_control::QueueControl::new(),
flags,
},
Command::none(),
)
}
/// 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(),
// We dispatch the multiple messages to handle them one by one.
Message::Client(client::Message::Multiple(messages)) => Command::batch(
messages
.into_iter()
.map(|message| self.update(Message::Client(message))),
),
// The disconnect message is a special one, we propage it
// everywhere.
Message::Disconnect => {
self.client.update(&self.flags, client::Message::Disconnect);
self.queue_control
.update(queue_control::Message::Disconnect);
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)
.map(|message| match message {
queue_control::Message::Action(message) => Message::Action(message),
queue_control::Message::SetState(_) => Message::None,
queue_control::Message::Disconnect => Message::Disconnect,
})
}
// Dispatch to the client, re-dispatch any returned message.
Message::Client(message) => {
self.client
.update(&self.flags, message)
.map(|message| match message {
client::Message::Disconnect => Message::Disconnect,
client::Message::Action(message) => Message::Notification(message),
client::Message::UpdateState(state) => {
Message::QueueControl(queue_control::Message::SetState(state))
}
message => Message::Client(message),
})
}
// 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. 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),
self.client.view().map(Message::Client)
]]
.padding(20)
.align_items(Alignment::Center)
.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),
self.client.subscription().map(Message::Client),
])
}
}