Split ui into modules, move some types to matrix.rs

This commit is contained in:
Amanda Graven 2020-12-19 10:00:55 +01:00
parent a212a0b3a0
commit 45c4b62150
Signed by: amanda
GPG key ID: 45C461CDC9286390
4 changed files with 589 additions and 535 deletions

141
src/ui/prompt.rs Normal file
View file

@ -0,0 +1,141 @@
//! Login prompt
use iced::{text_input, Button, Column, Container, Element, Radio, Row, Text, TextInput};
use crate::ui::Message;
/// View for the login prompt
#[derive(Debug, Clone, Default)]
pub struct PromptView {
/// Username input field
pub user_input: text_input::State,
/// Password input field
pub password_input: text_input::State,
/// Homeserver input field
pub server_input: text_input::State,
/// Device name input field
pub device_input: text_input::State,
/// Button to trigger login
pub login_button: iced::button::State,
/// Username
pub user: String,
/// Password
pub password: String,
/// Homeserver
pub server: String,
/// Device name to create login session under
pub device_name: String,
/// Whether to log in or sign up
pub action: PromptAction,
/// Error message
pub error: Option<String>,
}
impl PromptView {
pub fn new() -> Self {
Self::default()
}
pub fn view(&mut self) -> Element<Message> {
let mut content = Column::new()
.width(500.into())
.spacing(5)
.push(
Row::new()
.spacing(15)
.push(Radio::new(
PromptAction::Login,
"Login",
Some(self.action),
Message::SetAction,
))
.push(Radio::new(
PromptAction::Signup,
"Sign up",
Some(self.action),
Message::SetAction,
)),
)
.push(
Column::new().push(Text::new("Username")).push(
TextInput::new(
&mut self.user_input,
"Username",
&self.user,
Message::SetUser,
)
.padding(5),
),
)
.push(
Column::new().push(Text::new("Password")).push(
TextInput::new(
&mut self.password_input,
"Password",
&self.password,
Message::SetPassword,
)
.password()
.padding(5),
),
)
.push(
Column::new().push(Text::new("Homeserver")).push(
TextInput::new(
&mut self.server_input,
"https://homeserver.com",
&self.server,
Message::SetServer,
)
.padding(5),
),
)
.push(
Column::new().push(Text::new("Device name")).push(
TextInput::new(
&mut self.device_input,
"retrix on my laptop",
&self.device_name,
Message::SetDeviceName,
)
.padding(5),
),
);
let button = match self.action {
PromptAction::Login => {
Button::new(&mut self.login_button, Text::new("Login")).on_press(Message::Login)
}
PromptAction::Signup => {
content = content.push(
Text::new("NB: Signup is very naively implemented, and prone to breaking")
.color([1.0, 0.5, 0.0]),
);
Button::new(&mut self.login_button, Text::new("Sign up")).on_press(Message::Signup)
}
};
content = content.push(button);
if let Some(ref error) = self.error {
content = content.push(Text::new(error).color([1.0, 0.0, 0.0]));
}
Container::new(content)
.center_x()
.center_y()
.width(iced::Length::Fill)
.height(iced::Length::Fill)
.into()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PromptAction {
Login,
Signup,
}
impl Default for PromptAction {
fn default() -> Self {
PromptAction::Login
}
}

124
src/ui/settings.rs Normal file
View file

@ -0,0 +1,124 @@
//! Settings view.
use iced::{Button, Column, Container, Element, Length, Radio, Row, Text, TextInput};
use super::{Message, RoomSorting};
/// Settings menu
#[derive(Clone, Default, Debug)]
pub struct SettingsView {
/// Display name to set
pub display_name: String,
/// Are we saving the display name?
pub saving_name: bool,
/// Display name text input
pub display_name_input: iced::text_input::State,
/// Button to set display name
pub display_name_button: iced::button::State,
/// Path to import encryption keys from
pub key_path: String,
/// Password to decrypt the keys with
pub key_password: String,
/// Encryption key path entry
pub key_path_input: iced::text_input::State,
/// Entry for key password
pub key_password_input: iced::text_input::State,
/// Button to import keys
pub key_import_button: iced::button::State,
/// Button to close settings view
pub close_button: iced::button::State,
}
impl SettingsView {
pub fn new() -> Self {
Self::default()
}
pub fn view(&mut self, sort: RoomSorting) -> Element<Message> {
let content = Column::new()
.width(500.into())
.spacing(5)
.push(Text::new("Profile").size(25))
.push(
Column::new().push(Text::new("Display name")).push(
Row::new()
.push(
TextInput::new(
&mut self.display_name_input,
"Alice",
&self.display_name,
Message::SetDisplayNameInput,
)
.width(Length::Fill)
.padding(5),
)
.push(match self.saving_name {
false => Button::new(&mut self.display_name_button, Text::new("Save"))
.on_press(Message::SaveDisplayName),
true => {
Button::new(&mut self.display_name_button, Text::new("Saving..."))
}
}),
),
)
.push(Text::new("Appearance").size(25))
.push(Text::new("Sort messages by:"))
.push(Radio::new(
RoomSorting::Alphabetic,
"Name",
Some(sort),
Message::SetSort,
))
.push(Radio::new(
RoomSorting::Recent,
"Activity",
Some(sort),
Message::SetSort,
))
.push(Text::new("Encryption").size(25))
.push(
Column::new()
.push(Text::new("Import key (enter path)"))
.push(
TextInput::new(
&mut self.key_path_input,
"/home/user/exported_keys.txt",
&self.key_path,
Message::SetKeyPath,
)
.padding(5),
),
)
.push(
Column::new().push(Text::new("Key password")).push(
TextInput::new(
&mut self.key_password_input,
"SecretPassword42",
&self.key_password,
Message::SetKeyPassword,
)
.password()
.padding(5),
),
)
.push(
Button::new(&mut self.key_import_button, Text::new("Import keys"))
.on_press(Message::ImportKeys),
)
.push(
Row::new().width(Length::Fill).push(
Button::new(&mut self.close_button, Text::new("Close"))
.on_press(Message::CloseSettings),
),
);
Container::new(content)
.center_x()
.center_y()
.width(Length::Fill)
.height(Length::Fill)
.into()
}
}