retrix/src/ui.rs

85 lines
1.9 KiB
Rust

//! The graphical iced-based interface
use iced::Command;
use crate::config;
pub mod login;
pub mod session;
/// Data for the running application
#[derive(Debug)]
pub struct Retrix {
/// The currently active view
pub view: View,
/// Configuration
pub config: config::Config,
}
/// Available application views.
#[derive(Debug)]
pub enum View {
/// The login prompt
Login(login::Login),
/// The main view
Main(session::View),
}
/// A message notifying application state should change
#[derive(Debug, Clone)]
pub enum Message {
/// Do nothing. A workaround for async stuff
Noop,
/// A message for the login view
Login(login::Message),
}
/// Data for application startup
#[derive(Debug)]
pub struct Flags {
/// The application configuration
pub config: config::Config,
/// The session data if we've logged in
pub session: Option<config::Session>,
}
impl iced::Application for Retrix {
type Executor = iced::executor::Default;
type Message = Message;
type Flags = Flags;
fn new(flags: Self::Flags) -> (Self, Command<Message>) {
let state = Retrix { view: View::Login(login::Login::default()), config: flags.config };
(state, Command::none())
}
fn title(&self) -> String {
String::from("Retrix")
}
fn update(&mut self, message: Self::Message) -> Command<Self::Message> {
match (&mut self.view, message) {
(_, Message::Login(login::Message::LoggedIn(client))) => {
self.view = View::Main(session::View::with_client(client));
Command::none()
}
(View::Login(ref mut view), Message::Login(message)) => view.update(message),
_ => {
eprint!("WARN: Received a message for an inactive view");
Command::none()
}
}
}
fn view(&mut self) -> iced::Element<'_, Self::Message> {
match self.view {
View::Login(ref mut login) => login.view(),
View::Main(ref mut view) => view.view(),
}
}
fn scale_factor(&self) -> f64 {
self.config.zoom
}
}