retrix/src/ui.rs

146 lines
4.6 KiB
Rust
Raw Normal View History

2020-11-23 07:52:07 +01:00
use iced::{
text_input::{self, TextInput},
2020-11-24 09:12:24 +01:00
Application, Button, Column, Command, Container, Element, Text,
2020-11-23 07:52:07 +01:00
};
2020-11-24 09:12:24 +01:00
use crate::matrix;
2020-11-23 07:52:07 +01:00
#[derive(Debug, Clone)]
pub enum Retrix {
Prompt {
user_input: text_input::State,
password_input: text_input::State,
server_input: text_input::State,
2020-11-23 17:18:05 +01:00
login_button: iced::button::State,
user: String,
password: String,
2020-11-23 07:52:07 +01:00
server: String,
2020-11-24 09:12:24 +01:00
error: Option<String>,
},
LoggedIn {
client: matrix_sdk::Client,
session: matrix_sdk::Session,
2020-11-23 07:52:07 +01:00
},
}
#[derive(Debug, Clone)]
pub enum Message {
SetUser(String),
SetPassword(String),
SetServer(String),
2020-11-23 17:18:05 +01:00
Login,
2020-11-24 09:12:24 +01:00
LoggedIn(matrix_sdk::Client, matrix_sdk::Session),
SetError(String),
2020-11-23 07:52:07 +01:00
}
2020-11-24 09:12:24 +01:00
impl Application for Retrix {
2020-11-23 07:52:07 +01:00
type Message = Message;
2020-11-24 09:12:24 +01:00
type Executor = iced::executor::Default;
type Flags = ();
2020-11-23 07:52:07 +01:00
2020-11-24 09:12:24 +01:00
fn new(_flags: ()) -> (Self, Command<Self::Message>) {
let app = Retrix::Prompt {
2020-11-23 07:52:07 +01:00
user_input: text_input::State::new(),
password_input: text_input::State::new(),
server_input: text_input::State::new(),
2020-11-23 17:18:05 +01:00
login_button: Default::default(),
user: String::new(),
password: String::new(),
2020-11-23 07:52:07 +01:00
server: String::new(),
2020-11-24 09:12:24 +01:00
error: None,
};
(app, Command::none())
2020-11-23 07:52:07 +01:00
}
fn title(&self) -> String {
String::from("Retrix matrix client")
}
2020-11-24 09:12:24 +01:00
fn update(&mut self, message: Self::Message) -> Command<Self::Message> {
2020-11-23 17:18:05 +01:00
match *self {
Retrix::Prompt {
ref mut user,
ref mut password,
ref mut server,
2020-11-24 09:12:24 +01:00
ref mut error,
2020-11-23 17:18:05 +01:00
..
} => match message {
Message::SetUser(u) => *user = u,
Message::SetPassword(p) => *password = p,
Message::SetServer(s) => *server = s,
2020-11-24 09:12:24 +01:00
Message::SetError(e) => *error = Some(e),
Message::Login => {
let user = user.clone();
let password = password.clone();
let server = server.clone();
return Command::perform(
async move { matrix::login(&user, &password, &server).await },
|result| match result {
Ok((c, r)) => Message::LoggedIn(c, r),
Err(e) => Message::SetError(e.to_string()),
},
);
}
Message::LoggedIn(client, session) => *self = Retrix::LoggedIn { client, session },
2020-11-23 17:18:05 +01:00
},
_ => (),
}
2020-11-24 09:12:24 +01:00
Command::none()
2020-11-23 17:18:05 +01:00
}
2020-11-23 07:52:07 +01:00
fn view(&mut self) -> Element<Self::Message> {
match *self {
Retrix::Prompt {
ref mut user_input,
ref mut password_input,
ref mut server_input,
2020-11-23 17:18:05 +01:00
ref mut login_button,
ref user,
ref password,
2020-11-23 07:52:07 +01:00
ref server,
2020-11-24 09:12:24 +01:00
ref error,
2020-11-23 07:52:07 +01:00
} => {
2020-11-24 09:12:24 +01:00
let mut content = Column::new()
2020-11-23 17:18:05 +01:00
.width(500.into())
2020-11-23 07:52:07 +01:00
.push(Text::new("Username"))
2020-11-23 17:18:05 +01:00
.push(
TextInput::new(user_input, "Username", user, |val| Message::SetUser(val))
.padding(5),
)
2020-11-23 07:52:07 +01:00
.push(Text::new("Password"))
2020-11-23 17:18:05 +01:00
.push(
TextInput::new(password_input, "Password", password, |val| {
Message::SetPassword(val)
})
.password()
.padding(5),
)
2020-11-23 07:52:07 +01:00
.push(Text::new("Homeserver"))
2020-11-23 17:18:05 +01:00
.push(
TextInput::new(server_input, "Server", server, |val| {
Message::SetServer(val)
})
.padding(5),
)
.push(Button::new(login_button, Text::new("Login")).on_press(Message::Login));
2020-11-24 09:12:24 +01:00
if let Some(ref error) = error {
content = content.push(Text::new(error).color([1.0, 0.0, 0.0]));
}
2020-11-23 17:18:05 +01:00
Container::new(content)
.center_x()
.center_y()
.width(iced::Length::Fill)
.height(iced::Length::Fill)
.into()
2020-11-23 07:52:07 +01:00
}
2020-11-24 09:12:24 +01:00
Retrix::LoggedIn {
ref client,
ref session,
} => Text::new(format!("Logged in to {}", session.user_id)).into(),
2020-11-23 07:52:07 +01:00
}
}
}