fractal/session/view/content/room_history/message_row/
reaction_list.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
use adw::subclass::prelude::*;
use gtk::{glib, glib::clone, prelude::*, CompositeTemplate};

use super::reaction::MessageReaction;
use crate::session::model::{MemberList, ReactionList};

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

    use super::*;

    #[derive(Debug, Default, CompositeTemplate)]
    #[template(
        resource = "/org/gnome/Fractal/ui/session/view/content/room_history/message_row/reaction_list.ui"
    )]
    pub struct MessageReactionList {
        #[template_child]
        flow_box: TemplateChild<gtk::FlowBox>,
    }

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

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

            klass.set_css_name("message-reactions");
        }

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

    impl ObjectImpl for MessageReactionList {}
    impl WidgetImpl for MessageReactionList {}
    impl BinImpl for MessageReactionList {}

    impl MessageReactionList {
        /// Set the list of reactions.
        pub(super) fn set_reaction_list(&self, members: &MemberList, reaction_list: &ReactionList) {
            self.flow_box.bind_model(
                Some(reaction_list),
                clone!(
                    #[weak]
                    members,
                    #[upgrade_or_else]
                    || { gtk::FlowBoxChild::new().upcast() },
                    move |obj| {
                        MessageReaction::new(
                            members,
                            obj.clone()
                                .downcast()
                                .expect("reaction list item is a reaction group"),
                        )
                        .upcast()
                    }
                ),
            );
        }
    }
}

glib::wrapper! {
    /// A widget displaying the reactions of a message.
    pub struct MessageReactionList(ObjectSubclass<imp::MessageReactionList>)
        @extends gtk::Widget, adw::Bin, @implements gtk::Accessible;
}

impl MessageReactionList {
    pub fn new() -> Self {
        glib::Object::new()
    }

    /// Set the list of reactions.
    pub(crate) fn set_reaction_list(&self, members: &MemberList, reaction_list: &ReactionList) {
        self.imp().set_reaction_list(members, reaction_list);
    }
}