session-view: Port to glib::Properties macro

This commit is contained in:
Kévin Commaille 2023-12-17 22:59:58 +01:00
parent 47072ab2c2
commit 9d51d57fed
No known key found for this signature in database
GPG Key ID: 29A48C1F03620416
2 changed files with 19 additions and 47 deletions

View File

@ -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<gtk::Stack>,
@ -36,6 +36,8 @@ mod imp {
pub content: TemplateChild<Content>,
#[template_child]
pub media_viewer: TemplateChild<MediaViewer>,
/// The current session.
#[property(get, set = Self::set_session, explicit_notify, nullable)]
pub session: glib::WeakRef<Session>,
pub window_active_handler_id: RefCell<Option<SignalHandlerId>>,
}
@ -115,35 +117,8 @@ mod imp {
}
}
#[glib::derived_properties]
impl ObjectImpl for SessionView {
fn properties() -> &'static [glib::ParamSpec] {
static PROPERTIES: Lazy<Vec<glib::ParamSpec>> = Lazy::new(|| {
vec![glib::ParamSpecObject::builder::<Session>("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<Session>) {
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<Session> {
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<Room> {
self.imp().content.item().and_downcast()

View File

@ -343,7 +343,7 @@ impl Window {
self.switch_to_loading_page();
}
imp.session.set_session(None);
imp.session.set_session(None::<Session>);
}
pub fn save_window_size(&self) -> Result<(), glib::BoolError> {