diff --git a/res/main_window.glade b/res/main_window.glade index bf9f34ea..406eeeac 100644 --- a/res/main_window.glade +++ b/res/main_window.glade @@ -279,32 +279,76 @@ True True - + True False - 4 - + True False - address-book-new - 3 + 4 + + + True + False + True + + + False + True + 0 + + + + + True + False + Loading... + + + False + True + 1 + + - False - True - 0 + user_loading_page - + True False - Guest + 4 + + + True + False + address-book-new + 3 + + + False + True + 0 + + + + + True + False + <Display Name> + + + False + True + 1 + + - False - True + user_connected_page 1 diff --git a/src/app.rs b/src/app.rs index 26cbf304..37d66122 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,20 +1,17 @@ use gio; use gtk; use gtk::prelude::*; -use ruma_client_api::r0 as api; -use std::env; +use std::{env, sync, time, thread}; +use matrix_client; +// TODO: Is this the correct format for GApplication IDs? const APP_ID: &'static str = "jplatte.ruma_gtk"; pub struct App { gtk_app: gtk::Application, - matrix_connection: Option, -} - -pub struct MatrixConnection { - // reqwest::Client - matrix_access_token: String, - matrix_user_id: String, + gtk_builder: gtk::Builder, + dispatch_rx: sync::mpsc::Receiver>, + matrix_client_thread_join_handle: thread::JoinHandle<()>, } impl App { @@ -25,32 +22,46 @@ impl App { let gtk_app = gtk::Application::new(Some(APP_ID), gio::ApplicationFlags::empty()) .expect("Failed to initialize GtkApplication"); - let builder = gtk::Builder::new_from_file("res/main_window.glade"); + let gtk_builder = gtk::Builder::new_from_file("res/main_window.glade"); + let gtk_builder2 = gtk_builder.clone(); gtk_app.connect_activate(move |app| { - let user_button: gtk::Button = builder.get_object("user_button") + let user_button: gtk::Button = gtk_builder2.get_object("user_button") .expect("Couldn't find user_button in ui file."); - let user_menu: gtk::Popover = builder.get_object("user_menu") + let user_menu: gtk::Popover = gtk_builder2.get_object("user_menu") .expect("Couldn't find user_menu in ui file."); user_button.connect_clicked(move |_| user_menu.show()); // Set up shutdown callback - let window: gtk::Window = builder.get_object("main_window") + let window: gtk::Window = gtk_builder2.get_object("main_window") .expect("Couldn't find main_window in ui file."); + let app2 = app.clone(); + window.connect_delete_event(move |_, _| { + app2.quit(); + Inhibit(false) + }); + window.set_application(Some(app)); window.show_all(); }); + let (dispatch_tx, dispatch_rx) = sync::mpsc::channel::>(); + + let matrix_client_thread_join_handle = + thread::spawn(move || matrix_client::run_client_main(dispatch_tx)); + App { gtk_app: gtk_app, - matrix_connection: None, + gtk_builder: gtk_builder, + dispatch_rx: dispatch_rx, + matrix_client_thread_join_handle: matrix_client_thread_join_handle, } } - pub fn run(&self) { + pub fn run(self) { // Might just be the only way to go from impl Iterator to &[&str] let args = env::args().collect::>(); let mut args_refs = Vec::<&str>::new(); @@ -59,15 +70,19 @@ impl App { args_refs.push(r); } - self.gtk_app.run(args_refs.len() as i32, &args_refs); + let dispatch_rx = self.dispatch_rx; + let gtk_builder = self.gtk_builder; + gtk::idle_add(move || { + if let Ok(dispatch_fn) = dispatch_rx.recv_timeout(time::Duration::from_millis(5)) { + dispatch_fn(>k_builder); + } + + Continue(true) + }); - /*let client = reqwest::Client::new().unwrap(); - let res = client.post("https://matrix.org/_matrix/client/r0/register?kind=guest") - .body("{}") - .send() - .unwrap() - .json::(); + self.gtk_app.run(args_refs.len() as i32, &args_refs); - println!("{:?}", res);*/ + // Clean up + self.matrix_client_thread_join_handle.join(); } } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index a4ff5f51..0c222c11 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,9 @@ +#![feature(box_syntax)] + +// not using this yet because rustfmt doesn't support it: +// https://github.com/rust-lang-nursery/rustfmt/issues/1215 +//#![feature(field_init_shorthand)] + extern crate reqwest; extern crate ruma_client_api; extern crate gio; @@ -5,6 +11,7 @@ extern crate gtk; // extern crate xdg; mod app; +mod matrix_client; use app::App; // use std::fs::File; diff --git a/src/matrix_client.rs b/src/matrix_client.rs new file mode 100644 index 00000000..f762798d --- /dev/null +++ b/src/matrix_client.rs @@ -0,0 +1,30 @@ +use gtk; +use reqwest; +use ruma_client_api::r0 as api; +use std::sync::mpsc::Sender; + +pub fn run_client_main(dispatch_tx: Sender>) { + let client = reqwest::Client::new().unwrap(); + let res = client.post("https://matrix.org/_matrix/client/r0/register?kind=guest") + .body("{}") + .send() + .unwrap() + .json::() + .unwrap(); + + dispatch_tx.send(box move |builder| { + builder.get_object::("user_button_stack") + .expect("Can't find user_button_stack in ui file.") + .set_visible_child_name("user_connected_page"); + + builder.get_object::("display_name_label") + .expect("Can't find display_name_label in ui file.") + .set_text("Guest"); + }); +} + +struct MatrixConnection { + /// reqwest::Client, + matrix_access_token: String, + matrix_user_id: String, +}