retrix/src/ui.rs

223 lines
7.4 KiB
Rust
Raw Normal View History

2020-11-23 07:52:07 +01:00
use iced::{
text_input::{self, TextInput},
2020-11-24 21:49:06 +01:00
Application, Button, Column, Command, Container, Element, Length, Scrollable, 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>,
},
2020-11-24 21:49:06 +01:00
AwaitLogin,
2020-11-24 09:12:24 +01:00
LoggedIn {
client: matrix_sdk::Client,
2020-11-24 21:49:06 +01:00
session: matrix::Session,
2020-11-24 16:11:27 +01:00
2020-11-24 21:49:06 +01:00
rooms: Vec<String>,
room_scroll: iced::scrollable::State,
2020-11-23 07:52:07 +01:00
},
}
2020-11-24 21:49:06 +01:00
impl Retrix {
pub fn new_prompt() -> Retrix {
Retrix::Prompt {
user_input: text_input::State::new(),
password_input: text_input::State::new(),
server_input: text_input::State::new(),
login_button: Default::default(),
user: String::new(),
password: String::new(),
server: String::new(),
error: None,
}
}
}
2020-11-23 07:52:07 +01:00
#[derive(Debug, Clone)]
pub enum Message {
2020-11-24 16:11:27 +01:00
// Login form messages
2020-11-23 07:52:07 +01:00
SetUser(String),
SetPassword(String),
SetServer(String),
2020-11-23 17:18:05 +01:00
Login,
2020-11-24 21:49:06 +01:00
LoggedIn(matrix_sdk::Client, matrix::Session),
LoginFailed(String),
2020-11-24 16:11:27 +01:00
// Main state messages
2020-11-24 21:49:06 +01:00
ResetRooms(Vec<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>) {
2020-11-24 21:49:06 +01:00
// Skip login prompt if we have a session saved
match matrix::get_session().ok().flatten() {
Some(session) => {
let command = Command::perform(
async move { matrix::restore_login(session).await },
|result| match result {
Ok((s, c)) => Message::LoggedIn(s, c),
Err(e) => Message::LoginFailed(e.to_string()),
},
);
(Retrix::AwaitLogin, command)
}
None => (Retrix::new_prompt(), 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,
..
} => 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::Login => {
let user = user.clone();
let password = password.clone();
let server = server.clone();
2020-11-24 21:49:06 +01:00
*self = Retrix::AwaitLogin;
2020-11-24 09:12:24 +01:00
return Command::perform(
async move { matrix::login(&user, &password, &server).await },
|result| match result {
Ok((c, r)) => Message::LoggedIn(c, r),
2020-11-24 21:49:06 +01:00
Err(e) => Message::LoginFailed(e.to_string()),
2020-11-24 09:12:24 +01:00
},
);
}
2020-11-24 21:49:06 +01:00
_ => (),
},
Retrix::AwaitLogin => match message {
Message::LoginFailed(e) => {
*self = Retrix::new_prompt();
if let Retrix::Prompt { ref mut error, .. } = *self {
*error = Some(e);
}
}
2020-11-24 16:11:27 +01:00
Message::LoggedIn(client, session) => {
*self = Retrix::LoggedIn {
client: client.clone(),
session,
rooms: Vec::new(),
2020-11-24 21:49:06 +01:00
room_scroll: Default::default(),
2020-11-24 16:11:27 +01:00
};
let client = client.clone();
2020-11-24 21:49:06 +01:00
return Command::perform(
2020-11-24 16:11:27 +01:00
async move {
let mut list = Vec::new();
2020-11-24 21:49:06 +01:00
for (_, room) in client.joined_rooms().read().await.iter() {
let name = room.read().await.display_name();
list.push(name);
2020-11-24 16:11:27 +01:00
}
list
},
|rooms| Message::ResetRooms(rooms),
);
}
_ => (),
2020-11-23 17:18:05 +01:00
},
2020-11-24 16:11:27 +01:00
Retrix::LoggedIn { ref mut rooms, .. } => match message {
Message::ResetRooms(r) => *rooms = r,
_ => (),
},
};
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 16:11:27 +01:00
// Login form
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 21:49:06 +01:00
Retrix::AwaitLogin => Container::new(Text::new("Logging in..."))
.center_x()
.center_y()
.width(Length::Fill)
.height(Length::Fill)
.into(),
Retrix::LoggedIn {
ref rooms,
ref mut room_scroll,
..
} => {
2020-11-24 16:11:27 +01:00
//let mut root_row = Row::new().width(Length::Fill).height(Length::Fill);
2020-11-24 21:49:06 +01:00
let mut room_col = Scrollable::new(room_scroll)
.width(400.into())
.height(Length::Fill)
.spacing(15);
2020-11-24 16:11:27 +01:00
for room in rooms {
2020-11-24 21:49:06 +01:00
room_col = room_col.push(Text::new(room));
2020-11-24 16:11:27 +01:00
}
room_col.into()
//root_row = root_row.push(room_col);
//root_row.into()
}
2020-11-23 07:52:07 +01:00
}
}
}