fractal/session/view/sidebar/
room_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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
use adw::{prelude::*, subclass::prelude::*};
use gtk::{gdk, glib, glib::clone, CompositeTemplate};

use super::Row;
use crate::{
    i18n::{gettext_f, ngettext_f},
    prelude::*,
    session::model::{HighlightFlags, Room, RoomCategory},
    utils::BoundObject,
};

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

    use glib::subclass::InitializingObject;

    use super::*;

    #[derive(Debug, Default, CompositeTemplate, glib::Properties)]
    #[template(resource = "/org/gnome/Fractal/ui/session/view/sidebar/room_row.ui")]
    #[properties(wrapper_type = super::RoomRow)]
    pub struct RoomRow {
        /// The room represented by this row.
        #[property(get, set = Self::set_room, explicit_notify, nullable)]
        pub room: BoundObject<Room>,
        #[template_child]
        pub display_name_box: TemplateChild<gtk::Box>,
        #[template_child]
        pub display_name: TemplateChild<gtk::Label>,
        #[template_child]
        pub notification_count: TemplateChild<gtk::Label>,
        pub direct_icon: RefCell<Option<gtk::Image>>,
    }

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

        fn class_init(klass: &mut Self::Class) {
            Self::bind_template(klass);
            klass.set_css_name("room");

            klass.set_accessible_role(gtk::AccessibleRole::Group);
        }

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

    #[glib::derived_properties]
    impl ObjectImpl for RoomRow {
        fn constructed(&self) {
            self.parent_constructed();
            let obj = self.obj();

            // Allow to drag rooms
            let drag = gtk::DragSource::builder()
                .actions(gdk::DragAction::MOVE)
                .build();
            drag.connect_prepare(clone!(
                #[weak]
                obj,
                #[upgrade_or]
                None,
                move |drag, x, y| obj.drag_prepare(drag, x, y)
            ));
            drag.connect_drag_begin(clone!(
                #[weak]
                obj,
                move |_, _| {
                    obj.drag_begin();
                }
            ));
            drag.connect_drag_end(clone!(
                #[weak]
                obj,
                move |_, _, _| {
                    obj.drag_end();
                }
            ));
            obj.add_controller(drag);
        }
    }

    impl WidgetImpl for RoomRow {}
    impl BinImpl for RoomRow {}

    impl RoomRow {
        /// Set the room represented by this row.
        pub fn set_room(&self, room: Option<Room>) {
            if self.room.obj() == room {
                return;
            }
            let obj = self.obj();

            self.room.disconnect_signals();
            self.display_name.remove_css_class("dim-label");

            if let Some(room) = room {
                let highlight_handler = room.connect_highlight_notify(clone!(
                    #[weak]
                    obj,
                    move |_| {
                        obj.update_highlight();
                    }
                ));
                let direct_handler = room.connect_is_direct_notify(clone!(
                    #[weak]
                    obj,
                    move |_| {
                        obj.update_direct_icon();
                    }
                ));
                let name_handler = room.connect_display_name_notify(clone!(
                    #[weak]
                    obj,
                    move |_| {
                        obj.update_accessibility_label();
                    }
                ));
                let notifications_count_handler = room.connect_notification_count_notify(clone!(
                    #[weak]
                    obj,
                    move |_| {
                        obj.update_accessibility_label();
                    }
                ));

                if room.category() == RoomCategory::Left {
                    self.display_name.add_css_class("dim-label");
                }

                self.room.set(
                    room,
                    vec![
                        highlight_handler,
                        direct_handler,
                        name_handler,
                        notifications_count_handler,
                    ],
                );

                obj.update_accessibility_label();
            }

            obj.update_highlight();
            obj.update_direct_icon();
            obj.notify_room();
        }
    }
}

glib::wrapper! {
    /// A sidebar row representing a room.
    pub struct RoomRow(ObjectSubclass<imp::RoomRow>)
        @extends gtk::Widget, adw::Bin, @implements gtk::Accessible;
}

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

    /// Update how this row is highlighted according to the current state.
    fn update_highlight(&self) {
        let imp = self.imp();
        if let Some(room) = self.room() {
            let flags = room.highlight();

            if flags.contains(HighlightFlags::HIGHLIGHT) {
                imp.notification_count.add_css_class("highlight");
            } else {
                imp.notification_count.remove_css_class("highlight");
            }

            if flags.contains(HighlightFlags::BOLD) {
                imp.display_name.add_css_class("bold");
            } else {
                imp.display_name.remove_css_class("bold");
            }
        } else {
            imp.notification_count.remove_css_class("highlight");
            imp.display_name.remove_css_class("bold");
        }
    }

    fn drag_prepare(&self, drag: &gtk::DragSource, x: f64, y: f64) -> Option<gdk::ContentProvider> {
        let room = self.room()?;

        if let Some(parent) = self.parent() {
            let paintable = gtk::WidgetPaintable::new(Some(&parent));
            // FIXME: The hotspot coordinates don't work.
            // See https://gitlab.gnome.org/GNOME/gtk/-/issues/2341
            drag.set_icon(Some(&paintable), x as i32, y as i32);
        }

        Some(gdk::ContentProvider::for_value(&room.to_value()))
    }

    fn drag_begin(&self) {
        let Some(room) = self.room() else {
            return;
        };
        let Some(row) = self.parent().and_downcast::<Row>() else {
            return;
        };
        let Some(sidebar) = row.sidebar() else {
            return;
        };
        row.add_css_class("drag");

        sidebar.set_drop_source_category(Some(room.category()));
    }

    fn drag_end(&self) {
        let Some(row) = self.parent().and_downcast::<Row>() else {
            return;
        };
        let Some(sidebar) = row.sidebar() else {
            return;
        };
        sidebar.set_drop_source_category(None);
        row.remove_css_class("drag");
    }

    fn update_direct_icon(&self) {
        let imp = self.imp();
        let is_direct = self.room().is_some_and(|room| room.is_direct());

        if is_direct {
            if imp.direct_icon.borrow().is_none() {
                let icon = gtk::Image::builder()
                    .icon_name("avatar-default-symbolic")
                    .icon_size(gtk::IconSize::Normal)
                    .css_classes(["dim-label"])
                    .build();

                imp.display_name_box.prepend(&icon);
                imp.direct_icon.replace(Some(icon));
            }
        } else if let Some(icon) = imp.direct_icon.take() {
            imp.display_name_box.remove(&icon);
        }
    }

    fn update_accessibility_label(&self) {
        let Some(parent) = self.parent() else {
            return;
        };
        parent.update_property(&[gtk::accessible::Property::Label(&self.accessible_label())]);
    }

    fn accessible_label(&self) -> String {
        let Some(room) = self.room() else {
            return String::new();
        };

        let name = if room.is_direct() {
            gettext_f(
                // Translators: Do NOT translate the content between '{' and '}', this is a
                // variable name. Presented to screen readers when a
                // room is a direct chat with another user.
                "Direct chat with {name}",
                &[("name", &room.display_name())],
            )
        } else {
            room.display_name()
        };

        if room.notification_count() > 0 {
            let count = ngettext_f(
                // Translators: Do NOT translate the content between '{' and '}', this is a
                // variable name. Presented to screen readers when a room has notifications
                // for unread messages.
                "1 notification",
                "{count} notifications",
                room.notification_count() as u32,
                &[("count", &room.notification_count().to_string())],
            );
            format!("{name} {count}")
        } else {
            name
        }
    }
}