diff --git a/src/session/view/session_view.rs b/src/session/view/session_view.rs index 14d53338..21abf4d3 100644 --- a/src/session/view/session_view.rs +++ b/src/session/view/session_view.rs @@ -17,12 +17,12 @@ mod imp { use std::cell::RefCell; use glib::subclass::InitializingObject; - use once_cell::sync::Lazy; use super::*; - #[derive(Debug, Default, CompositeTemplate)] + #[derive(Debug, Default, CompositeTemplate, glib::Properties)] #[template(resource = "/org/gnome/Fractal/ui/session/view/session_view.ui")] + #[properties(wrapper_type = super::SessionView)] pub struct SessionView { #[template_child] pub stack: TemplateChild, @@ -36,6 +36,8 @@ mod imp { pub content: TemplateChild, #[template_child] pub media_viewer: TemplateChild, + /// The current session. + #[property(get, set = Self::set_session, explicit_notify, nullable)] pub session: glib::WeakRef, pub window_active_handler_id: RefCell>, } @@ -115,35 +117,8 @@ mod imp { } } + #[glib::derived_properties] impl ObjectImpl for SessionView { - fn properties() -> &'static [glib::ParamSpec] { - static PROPERTIES: Lazy> = Lazy::new(|| { - vec![glib::ParamSpecObject::builder::("session") - .explicit_notify() - .build()] - }); - - PROPERTIES.as_ref() - } - - fn set_property(&self, _id: usize, value: &glib::Value, pspec: &glib::ParamSpec) { - let obj = self.obj(); - - match pspec.name() { - "session" => obj.set_session(value.get().unwrap()), - _ => unimplemented!(), - } - } - - fn property(&self, _id: usize, pspec: &glib::ParamSpec) -> glib::Value { - let obj = self.obj(); - - match pspec.name() { - "session" => obj.session().to_value(), - _ => unimplemented!(), - } - } - fn constructed(&self) { self.parent_constructed(); let obj = self.obj(); @@ -206,6 +181,18 @@ mod imp { impl WidgetImpl for SessionView {} impl BinImpl for SessionView {} + + impl SessionView { + /// Set the current session. + fn set_session(&self, session: Option) { + if self.session.upgrade() == session { + return; + } + + self.session.set(session.as_ref()); + self.obj().notify_session(); + } + } } glib::wrapper! { @@ -215,26 +202,11 @@ glib::wrapper! { } impl SessionView { - /// Create a new session. + /// Create a new session view. pub async fn new() -> Self { glib::Object::new() } - /// The Matrix user session. - pub fn session(&self) -> Option { - self.imp().session.upgrade() - } - - /// Set the Matrix user session. - pub fn set_session(&self, session: Option<&Session>) { - if self.session().as_ref() == session { - return; - } - - self.imp().session.set(session); - self.notify("session"); - } - /// The currently selected room, if any. pub fn selected_room(&self) -> Option { self.imp().content.item().and_downcast() diff --git a/src/window.rs b/src/window.rs index 9dcb06b9..4282c692 100644 --- a/src/window.rs +++ b/src/window.rs @@ -343,7 +343,7 @@ impl Window { self.switch_to_loading_page(); } - imp.session.set_session(None); + imp.session.set_session(None::); } pub fn save_window_size(&self) -> Result<(), glib::BoolError> {