Finish basic login prompt
This commit is contained in:
		
							parent
							
								
									675b788f81
								
							
						
					
					
						commit
						dca34c60a0
					
				
					 4 changed files with 74 additions and 22 deletions
				
			
		|  | @ -1,5 +1,6 @@ | |||
| use iced::Sandbox; | ||||
| 
 | ||||
| pub mod matrix; | ||||
| pub mod ui; | ||||
| 
 | ||||
| fn main() { | ||||
|  |  | |||
							
								
								
									
										17
									
								
								src/matrix.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								src/matrix.rs
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,17 @@ | |||
| use matrix_sdk::{reqwest::Url, Client, SyncSettings}; | ||||
| 
 | ||||
| async fn login( | ||||
|     username: &str, | ||||
|     password: &str, | ||||
|     server: &str, | ||||
| ) -> Result<Client, Box<dyn std::error::Error>> { | ||||
|     let url = Url::parse(server)?; | ||||
|     let client = Client::new(url)?; | ||||
| 
 | ||||
|     client | ||||
|         .login(username, password, None, Some("retrix")) | ||||
|         .await?; | ||||
|     client.sync(SyncSettings::new()).await; | ||||
| 
 | ||||
|     Ok(client) | ||||
| } | ||||
							
								
								
									
										76
									
								
								src/ui.rs
									
										
									
									
									
								
							
							
						
						
									
										76
									
								
								src/ui.rs
									
										
									
									
									
								
							|  | @ -1,16 +1,18 @@ | |||
| use iced::{ | ||||
|     text_input::{self, TextInput}, | ||||
|     Column, Container, Element, Row, Sandbox, Settings, Text, | ||||
|     Button, Column, Container, Element, Sandbox, Text, | ||||
| }; | ||||
| 
 | ||||
| #[derive(Debug, Clone)] | ||||
| pub enum Retrix { | ||||
|     Prompt { | ||||
|         user_input: text_input::State, | ||||
|         user: String, | ||||
|         password_input: text_input::State, | ||||
|         password: String, | ||||
|         server_input: text_input::State, | ||||
|         login_button: iced::button::State, | ||||
| 
 | ||||
|         user: String, | ||||
|         password: String, | ||||
|         server: String, | ||||
|     }, | ||||
|     LoggedIn, | ||||
|  | @ -21,6 +23,7 @@ pub enum Message { | |||
|     SetUser(String), | ||||
|     SetPassword(String), | ||||
|     SetServer(String), | ||||
|     Login, | ||||
| } | ||||
| 
 | ||||
| impl Sandbox for Retrix { | ||||
|  | @ -29,10 +32,12 @@ impl Sandbox for Retrix { | |||
|     fn new() -> Self { | ||||
|         Retrix::Prompt { | ||||
|             user_input: text_input::State::new(), | ||||
|             user: String::new(), | ||||
|             password_input: text_input::State::new(), | ||||
|             password: String::new(), | ||||
|             server_input: text_input::State::new(), | ||||
|             login_button: Default::default(), | ||||
| 
 | ||||
|             user: String::new(), | ||||
|             password: String::new(), | ||||
|             server: String::new(), | ||||
|         } | ||||
|     } | ||||
|  | @ -41,35 +46,64 @@ impl Sandbox for Retrix { | |||
|         String::from("Retrix matrix client") | ||||
|     } | ||||
| 
 | ||||
|     fn update(&mut self, message: Self::Message) {} | ||||
|     fn update(&mut self, message: Self::Message) { | ||||
|         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, | ||||
|                 Message::Login => (), | ||||
|             }, | ||||
|             _ => (), | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     fn view(&mut self) -> Element<Self::Message> { | ||||
|         match *self { | ||||
|             Retrix::Prompt { | ||||
|                 ref mut user_input, | ||||
|                 ref user, | ||||
|                 ref mut password_input, | ||||
|                 ref password, | ||||
|                 ref mut server_input, | ||||
|                 ref mut login_button, | ||||
|                 ref user, | ||||
|                 ref password, | ||||
|                 ref server, | ||||
|             } => { | ||||
|                 let content = Column::new() | ||||
|                     .width(500.into()) | ||||
|                     .push(Text::new("Username")) | ||||
|                     .push(TextInput::new(user_input, "Username", user, |val| { | ||||
|                         Message::SetUser(val) | ||||
|                     })) | ||||
|                     .push( | ||||
|                         TextInput::new(user_input, "Username", user, |val| Message::SetUser(val)) | ||||
|                             .padding(5), | ||||
|                     ) | ||||
|                     .push(Text::new("Password")) | ||||
|                     .push(TextInput::new( | ||||
|                         password_input, | ||||
|                         "Password", | ||||
|                         password, | ||||
|                         |val| Message::SetPassword(val), | ||||
|                     )) | ||||
|                     .push( | ||||
|                         TextInput::new(password_input, "Password", password, |val| { | ||||
|                             Message::SetPassword(val) | ||||
|                         }) | ||||
|                         .password() | ||||
|                         .padding(5), | ||||
|                     ) | ||||
|                     .push(Text::new("Homeserver")) | ||||
|                     .push(TextInput::new(server_input, "Server", server, |val| { | ||||
|                         Message::SetServer(val) | ||||
|                     })); | ||||
|                 content.into() | ||||
|                     .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)); | ||||
| 
 | ||||
|                 Container::new(content) | ||||
|                     .center_x() | ||||
|                     .center_y() | ||||
|                     .width(iced::Length::Fill) | ||||
|                     .height(iced::Length::Fill) | ||||
|                     .into() | ||||
|             } | ||||
|             _ => Text::new("Beep").into(), | ||||
|         } | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue