fractal/components/
reaction_chooser.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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
use adw::subclass::prelude::*;
use gtk::{glib, glib::clone, prelude::*, CompositeTemplate};

use crate::session::model::ReactionList;

struct ReactionGridItem<'a> {
    key: &'a str,
    column: i32,
    row: i32,
}

static QUICK_REACTIONS: &[ReactionGridItem] = &[
    ReactionGridItem {
        key: "👍️",
        column: 0,
        row: 0,
    },
    ReactionGridItem {
        key: "👎️",
        column: 1,
        row: 0,
    },
    ReactionGridItem {
        key: "😄",
        column: 2,
        row: 0,
    },
    ReactionGridItem {
        key: "🎉",
        column: 3,
        row: 0,
    },
    ReactionGridItem {
        key: "😕",
        column: 0,
        row: 1,
    },
    ReactionGridItem {
        key: "❤️",
        column: 1,
        row: 1,
    },
    ReactionGridItem {
        key: "🚀",
        column: 2,
        row: 1,
    },
];

mod imp {

    use std::{cell::RefCell, collections::HashMap};

    use glib::subclass::InitializingObject;

    use super::*;

    #[derive(Debug, Default, CompositeTemplate)]
    #[template(resource = "/org/gnome/Fractal/ui/components/reaction_chooser.ui")]
    pub struct ReactionChooser {
        /// The `ReactionList` associated to this chooser
        pub reactions: RefCell<Option<ReactionList>>,
        pub reactions_handler: RefCell<Option<glib::SignalHandlerId>>,
        pub reaction_bindings: RefCell<HashMap<String, glib::Binding>>,
        #[template_child]
        pub reaction_grid: TemplateChild<gtk::Grid>,
    }

    #[glib::object_subclass]
    impl ObjectSubclass for ReactionChooser {
        const NAME: &'static str = "ReactionChooser";
        type Type = super::ReactionChooser;
        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 ReactionChooser {
        fn constructed(&self) {
            self.parent_constructed();

            let grid = &self.reaction_grid;
            for reaction_item in QUICK_REACTIONS {
                let button = gtk::ToggleButton::builder()
                    .label(reaction_item.key)
                    .action_name("event.toggle-reaction")
                    .action_target(&reaction_item.key.to_variant())
                    .css_classes(["flat", "circular"])
                    .build();
                button.connect_clicked(|button| {
                    button.activate_action("context-menu.close", None).unwrap();
                });
                grid.attach(&button, reaction_item.column, reaction_item.row, 1, 1);
            }
        }
    }

    impl WidgetImpl for ReactionChooser {}

    impl BinImpl for ReactionChooser {}
}

glib::wrapper! {
    /// A widget displaying a `ReactionChooser` for a `ReactionList`.
    pub struct ReactionChooser(ObjectSubclass<imp::ReactionChooser>)
        @extends gtk::Widget, adw::Bin, @implements gtk::Accessible;
}

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

    pub fn reactions(&self) -> Option<ReactionList> {
        self.imp().reactions.borrow().clone()
    }

    pub fn set_reactions(&self, reactions: Option<ReactionList>) {
        let imp = self.imp();
        let prev_reactions = self.reactions();

        if prev_reactions == reactions {
            return;
        }

        if let Some(reactions) = prev_reactions.as_ref() {
            if let Some(signal_handler) = imp.reactions_handler.take() {
                reactions.disconnect(signal_handler);
            }

            let mut reaction_bindings = imp.reaction_bindings.borrow_mut();
            for reaction_item in QUICK_REACTIONS {
                if let Some(binding) = reaction_bindings.remove(reaction_item.key) {
                    if let Some(button) = imp
                        .reaction_grid
                        .child_at(reaction_item.column, reaction_item.row)
                        .and_downcast::<gtk::ToggleButton>()
                    {
                        button.set_active(false);
                    }

                    binding.unbind();
                }
            }
        }

        if let Some(reactions) = reactions.as_ref() {
            let signal_handler = reactions.connect_items_changed(clone!(
                #[weak(rename_to = obj)]
                self,
                move |_, _, _, _| {
                    obj.update_reactions();
                }
            ));
            imp.reactions_handler.replace(Some(signal_handler));
        }
        imp.reactions.replace(reactions);
        self.update_reactions();
    }

    fn update_reactions(&self) {
        let imp = self.imp();
        let mut reaction_bindings = imp.reaction_bindings.borrow_mut();
        let reactions = self.reactions();

        for reaction_item in QUICK_REACTIONS {
            if let Some(reaction) = reactions
                .as_ref()
                .and_then(|reactions| reactions.reaction_group_by_key(reaction_item.key))
            {
                if reaction_bindings.get(reaction_item.key).is_none() {
                    let button = imp
                        .reaction_grid
                        .child_at(reaction_item.column, reaction_item.row)
                        .unwrap();
                    let binding = reaction
                        .bind_property("has-user", &button, "active")
                        .sync_create()
                        .build();
                    reaction_bindings.insert(reaction_item.key.to_string(), binding);
                }
            } else if let Some(binding) = reaction_bindings.remove(reaction_item.key) {
                if let Some(button) = imp
                    .reaction_grid
                    .child_at(reaction_item.column, reaction_item.row)
                    .and_downcast::<gtk::ToggleButton>()
                {
                    button.set_active(false);
                }

                binding.unbind();
            }
        }
    }
}

impl Default for ReactionChooser {
    fn default() -> Self {
        Self::new()
    }
}