fractal/session/view/content/room_details/invite_subpage/
row.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use adw::subclass::prelude::BinImpl;
use gtk::{glib, prelude::*, subclass::prelude::*, CompositeTemplate};

use super::InviteItem;

mod imp {
    use std::cell::RefCell;

    use glib::subclass::InitializingObject;

    use super::*;
    use crate::utils::template_callbacks::TemplateCallbacks;

    #[derive(Debug, Default, CompositeTemplate, glib::Properties)]
    #[template(
        resource = "/org/gnome/Fractal/ui/session/view/content/room_details/invite_subpage/row.ui"
    )]
    #[properties(wrapper_type = super::InviteRow)]
    pub struct InviteRow {
        /// The item displayed by this row.
        #[property(get, set = Self::set_item, explicit_notify, nullable)]
        pub item: RefCell<Option<InviteItem>>,
        pub binding: RefCell<Option<glib::Binding>>,
        #[template_child]
        pub check_button: TemplateChild<gtk::CheckButton>,
    }

    #[glib::object_subclass]
    impl ObjectSubclass for InviteRow {
        const NAME: &'static str = "RoomDetailsInviteRow";
        type Type = super::InviteRow;
        type ParentType = adw::Bin;

        fn class_init(klass: &mut Self::Class) {
            Self::bind_template(klass);
            TemplateCallbacks::bind_template_callbacks(klass);
        }

        fn instance_init(obj: &InitializingObject<Self>) {
            obj.init_template();
        }
    }

    #[glib::derived_properties]
    impl ObjectImpl for InviteRow {}

    impl WidgetImpl for InviteRow {}
    impl BinImpl for InviteRow {}

    impl InviteRow {
        /// Set the item displayed by this row.
        fn set_item(&self, item: Option<InviteItem>) {
            if *self.item.borrow() == item {
                return;
            }

            if let Some(binding) = self.binding.take() {
                binding.unbind();
            }

            if let Some(item) = &item {
                // We can't use `gtk::Expression` because we need a bidirectional binding
                let binding = item
                    .bind_property("is-invitee", &*self.check_button, "active")
                    .sync_create()
                    .bidirectional()
                    .build();

                self.binding.replace(Some(binding));
            }

            self.item.replace(item);
            self.obj().notify_item();
        }
    }
}

glib::wrapper! {
    /// A row presenting an item of the result of a search in the user directory.
    pub struct InviteRow(ObjectSubclass<imp::InviteRow>)
        @extends gtk::Widget, adw::Bin, @implements gtk::Accessible;
}

impl InviteRow {
    pub fn new(item: &InviteItem) -> Self {
        glib::Object::builder().property("item", item).build()
    }
}