[fractal/ui-refactor: 3/16] Make OP static variable getter non-optional
- From: Alejandro Domínguez <aledomu src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [fractal/ui-refactor: 3/16] Make OP static variable getter non-optional
- Date: Sat, 5 Dec 2020 15:21:33 +0000 (UTC)
commit 93d40bf939d67bf28c1034936626b1128fe3104d
Author: Alejandro Domínguez <adomu net-c com>
Date: Mon Oct 12 05:22:44 2020 +0200
Make OP static variable getter non-optional
fractal-gtk/src/actions/global.rs | 40 +++++++++++++++-----------------------
fractal-gtk/src/actions/message.rs | 3 +--
fractal-gtk/src/app/mod.rs | 35 ++++++++++++++++-----------------
3 files changed, 34 insertions(+), 44 deletions(-)
---
diff --git a/fractal-gtk/src/actions/global.rs b/fractal-gtk/src/actions/global.rs
index 75e063e6..d1c4ed62 100644
--- a/fractal-gtk/src/actions/global.rs
+++ b/fractal-gtk/src/actions/global.rs
@@ -290,18 +290,14 @@ pub fn new(app: >k::Application, op: &Arc<Mutex<AppOp>>) {
back.borrow_mut().pop();
if let Some(state) = back.borrow().last() {
debug!("Go back to state {:?}", state);
- if let Some(op) = app::get_op() {
- let mut op = op.lock().unwrap();
- op.set_state(*state);
- }
+ let mut op = app::get_op().lock().unwrap();
+ op.set_state(*state);
} else {
// Fallback when there is no back history
debug!("There is no state to go back to. Go back to state NoRoom");
- if let Some(op) = app::get_op() {
- let mut op = op.lock().unwrap();
- if op.login_data.is_some() {
- op.set_state(AppState::NoRoom);
- }
+ let mut op = app::get_op().lock().unwrap();
+ if op.login_data.is_some() {
+ op.set_state(AppState::NoRoom);
}
}
}
@@ -383,31 +379,27 @@ pub fn get_event_id(data: Option<&glib::Variant>) -> Option<EventId> {
/* TODO: get message from storage once implemented */
pub fn get_message_by_id(id: &EventId) -> Option<Message> {
- let op = app::get_op()?;
- let op = op.lock().unwrap();
+ let op = app::get_op().lock().unwrap();
let room_id = op.active_room.as_ref()?;
op.get_message_by_id(room_id, id)
}
fn open_viewer(data: Option<&glib::Variant>) -> Option<()> {
let msg = get_event_id(data).as_ref().and_then(get_message_by_id)?;
- let op = app::get_op()?;
- let mut op = op.lock().unwrap();
+ let mut op = app::get_op().lock().unwrap();
op.create_media_viewer(msg);
None
}
pub fn activate_action(action_group_name: &str, action_name: &str) {
- if let Some(op) = app::get_op() {
- let main_window = op
- .lock()
- .unwrap()
- .ui
- .builder
- .get_object::<gtk::Window>("main_window")
- .expect("Can't find main_window in ui file.");
- if let Some(action_group) = main_window.get_action_group(action_group_name) {
- action_group.activate_action(action_name, None);
- }
+ let main_window = app::get_op()
+ .lock()
+ .unwrap()
+ .ui
+ .builder
+ .get_object::<gtk::Window>("main_window")
+ .expect("Can't find main_window in ui file.");
+ if let Some(action_group) = main_window.get_action_group(action_group_name) {
+ action_group.activate_action(action_name, None);
}
}
diff --git a/fractal-gtk/src/actions/message.rs b/fractal-gtk/src/actions/message.rs
index 7ddf1651..f608f8ba 100644
--- a/fractal-gtk/src/actions/message.rs
+++ b/fractal-gtk/src/actions/message.rs
@@ -229,8 +229,7 @@ fn get_message(id: Option<&glib::Variant>) -> Option<Message> {
}
fn request_more_messages(session_client: MatrixClient, id: Option<RoomId>) -> Option<()> {
- let op = app::get_op()?;
- let op = op.lock().unwrap();
+ let op = app::get_op().lock().unwrap();
let id = id?;
let r = op.rooms.get(&id)?;
if let Some(prev_batch) = r.prev_batch.clone() {
diff --git a/fractal-gtk/src/app/mod.rs b/fractal-gtk/src/app/mod.rs
index 107a3844..f5e4b3ed 100644
--- a/fractal-gtk/src/app/mod.rs
+++ b/fractal-gtk/src/app/mod.rs
@@ -6,7 +6,7 @@ use lazy_static::lazy_static;
use libhandy::prelude::*;
use std::cell::RefCell;
use std::rc::Rc;
-use std::sync::{Arc, Mutex, Weak};
+use std::sync::{Arc, Mutex};
use tokio::runtime::Runtime as TokioRuntime;
use log::error;
@@ -23,7 +23,9 @@ mod windowstate;
use windowstate::WindowState;
-static mut OP: Option<Weak<Mutex<AppOp>>> = None;
+type GlobalAppOp = Arc<Mutex<AppOp>>;
+
+static mut OP: Option<GlobalAppOp> = None;
lazy_static! {
pub static ref RUNTIME: TokioRuntime = TokioRuntime::new().unwrap();
@@ -35,9 +37,7 @@ macro_rules! APPOP {
let ctx = glib::MainContext::default();
ctx.invoke(move || {
$( let $x = $x.clone(); )*
- if let Some(op) = crate::app::get_op() {
- op.lock().unwrap().$fn($($x),*);
- }
+ crate::app::get_op().lock().unwrap().$fn($($x),*);
});
}};
($fn: ident) => {{
@@ -53,7 +53,7 @@ pub struct App {
ui: uibuilder::UI,
// TODO: Remove op needed in connect, but since it is global we could remove it form here
- op: Arc<Mutex<AppOp>>,
+ op: GlobalAppOp,
}
pub type AppRef = Rc<App>;
@@ -78,6 +78,11 @@ impl App {
);
let ui = uibuilder::UI::new();
+
+ unsafe {
+ OP = Some(Arc::new(Mutex::new(AppOp::new(ui.clone()))));
+ }
+
let window: libhandy::ApplicationWindow = ui
.builder
.get_object("main_window")
@@ -143,29 +148,23 @@ impl App {
.expect("Can't find account_settings_box in ui file.");
view_stack.add_named(&child, "account-settings");
- let op = Arc::new(Mutex::new(AppOp::new(ui.clone())));
-
let main_stack = ui
.builder
.get_object::<gtk::Stack>("main_content_stack")
.expect("Can't find main_content_stack in ui file.");
// Add login view to the main stack
- let login = widgets::LoginWidget::new(&op);
+ let login = widgets::LoginWidget::new(get_op());
main_stack.add_named(&login.container, "login");
gtk_app.set_accels_for_action("login.back", &["Escape"]);
- unsafe {
- OP = Some(Arc::downgrade(&op));
- }
-
- actions::Global::new(gtk_app, &op);
+ actions::Global::new(gtk_app, get_op());
let app = AppRef::new(Self {
main_window: window,
ui,
- op,
+ op: get_op().clone(),
});
app.connect_gtk();
@@ -174,7 +173,7 @@ impl App {
}
pub fn on_startup(gtk_app: >k::Application) {
- // Create application
+ // Create application.
let app = App::new(gtk_app);
// Initialize libhandy
@@ -222,6 +221,6 @@ impl App {
}
}
-pub fn get_op() -> Option<Arc<Mutex<AppOp>>> {
- unsafe { OP.as_ref().and_then(|x| x.upgrade()) }
+pub fn get_op() -> &'static GlobalAppOp {
+ unsafe { OP.as_ref().expect("Fatal: AppOp has not been initialized") }
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]