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
use adw::{prelude::*, subclass::prelude::*};
use gettextrs::gettext;
use gtk::{glib, CompositeTemplate};
use matrix_sdk::ruma::events::room::create::RoomCreateEventContent;
use ruma::events::FullStateEventContent;

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

    use super::*;

    #[derive(Debug, Default, CompositeTemplate)]
    #[template(
        resource = "/org/gnome/Fractal/ui/session/view/content/room_history/state_row/creation.ui"
    )]
    pub struct StateCreation {
        #[template_child]
        pub previous_room_btn: TemplateChild<gtk::Button>,
        #[template_child]
        pub description: TemplateChild<gtk::Label>,
    }

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

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

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

    impl ObjectImpl for StateCreation {}
    impl WidgetImpl for StateCreation {}
    impl BinImpl for StateCreation {}
}

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

impl StateCreation {
    pub fn new(event: &FullStateEventContent<RoomCreateEventContent>) -> Self {
        let obj: Self = glib::Object::new();
        obj.set_event(event);
        obj
    }

    fn set_event(&self, event: &FullStateEventContent<RoomCreateEventContent>) {
        let imp = self.imp();

        let predecessor = match event {
            FullStateEventContent::Original { content, .. } => content.predecessor.as_ref(),
            FullStateEventContent::Redacted(_) => None,
        };

        if let Some(predecessor) = &predecessor {
            imp.previous_room_btn
                .set_detailed_action_name(&format!("session.show-room::{}", predecessor.room_id));
            imp.previous_room_btn.set_visible(true);
            imp.description
                .set_label(&gettext("This is the continuation of an upgraded room."));
        } else {
            imp.previous_room_btn.set_visible(false);
            imp.previous_room_btn.set_action_name(None);
            imp.description
                .set_label(&gettext("This is the beginning of this room."));
        }
    }
}