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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
use adw::{prelude::*, subclass::prelude::*};
use gettextrs::gettext;
use gtk::{glib, glib::clone, CompositeTemplate};

use crate::{session::model::Room, toast, utils::BoundObjectWeakRef, Window};

mod imp {
    use glib::subclass::InitializingObject;

    use super::*;

    #[derive(Debug, Default, CompositeTemplate, glib::Properties)]
    #[template(
        resource = "/org/gnome/Fractal/ui/session/view/content/room_history/state_row/tombstone.ui"
    )]
    #[properties(wrapper_type = super::StateTombstone)]
    pub struct StateTombstone {
        #[template_child]
        pub new_room_btn: TemplateChild<gtk::Button>,
        /// The [`Room`] this event belongs to.
        #[property(get, set = Self::set_room, construct_only)]
        pub room: BoundObjectWeakRef<Room>,
    }

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

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

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

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

    impl WidgetImpl for StateTombstone {}
    impl BinImpl for StateTombstone {}

    impl StateTombstone {
        /// Set the room this event belongs to.
        fn set_room(&self, room: Room) {
            let obj = self.obj();

            let successor_handler =
                room.connect_successor_id_string_notify(clone!(@weak self as imp => move |room| {
                    imp.new_room_btn.set_visible(room.successor_id().is_some());
                }));
            self.new_room_btn.set_visible(room.successor_id().is_some());

            let successor_room_handler =
                room.connect_successor_notify(clone!(@weak obj => move |room| {
                    obj.update_button_label(room);
                }));
            obj.update_button_label(&room);

            self.room
                .set(&room, vec![successor_handler, successor_room_handler]);
        }
    }
}

glib::wrapper! {
    /// A widget presenting a room tombstone state event.
    pub struct StateTombstone(ObjectSubclass<imp::StateTombstone>)
        @extends gtk::Widget, adw::Bin, @implements gtk::Accessible;
}

#[gtk::template_callbacks]
impl StateTombstone {
    /// Construct a new `StateTombstone` with the given room.
    pub fn new(room: &Room) -> Self {
        glib::Object::builder().property("room", room).build()
    }

    /// Update the button of the label.
    fn update_button_label(&self, room: &Room) {
        let button = &self.imp().new_room_btn;
        if room.successor().is_some() {
            // Translators: This is a verb, as in 'View Room'.
            button.set_label(&gettext("View"));
        } else {
            button.set_label(&gettext("Join"));
        }
    }

    /// Join or view the successor of this event's room.
    #[template_callback]
    async fn join_or_view_successor(&self) {
        let Some(room) = self.room() else {
            return;
        };
        let Some(session) = room.session() else {
            return;
        };
        let room_list = session.room_list();

        // Join or view the room with the given identifier.
        if let Some(successor) = room.successor() {
            let Some(window) = self.root().and_downcast::<Window>() else {
                return;
            };

            window.session_view().select_room(Some(successor));
        } else if let Some(successor_id) = room.successor_id().map(ToOwned::to_owned) {
            let via = successor_id
                .server_name()
                .map(ToOwned::to_owned)
                .into_iter()
                .collect();

            if let Err(error) = room_list
                .join_by_id_or_alias(successor_id.into(), via)
                .await
            {
                toast!(self, error);
            }
        }
    }
}