Skip to main content

fractal/components/pill/
at_room.rs

1use gettextrs::gettext;
2use gtk::{glib, prelude::*, subclass::prelude::*};
3use ruma::RoomId;
4
5use crate::{components::PillSource, prelude::*, session::Room};
6
7mod imp {
8    use std::cell::OnceCell;
9
10    use super::*;
11
12    #[derive(Debug, Default, glib::Properties)]
13    #[properties(wrapper_type = super::AtRoom)]
14    pub struct AtRoom {
15        /// The room represented by this mention.
16        #[property(get, set = Self::set_room, construct_only)]
17        room: OnceCell<Room>,
18    }
19
20    #[glib::object_subclass]
21    impl ObjectSubclass for AtRoom {
22        const NAME: &'static str = "AtRoom";
23        type Type = super::AtRoom;
24        type ParentType = PillSource;
25    }
26
27    #[glib::derived_properties]
28    impl ObjectImpl for AtRoom {}
29
30    impl PillSourceImpl for AtRoom {
31        fn identifier(&self) -> String {
32            gettext("Notify the whole room")
33        }
34    }
35
36    impl AtRoom {
37        /// Set the room represented by this mention.
38        fn set_room(&self, room: Room) {
39            let room = self.room.get_or_init(|| room);
40
41            // Bind the avatar image so it always looks the same.
42            room.avatar_data()
43                .bind_property("image", &self.obj().avatar_data(), "image")
44                .sync_create()
45                .build();
46        }
47
48        /// The ID of the room represented by this mention.
49        pub(super) fn room_id(&self) -> &RoomId {
50            self.room
51                .get()
52                .expect("room should be initialized")
53                .room_id()
54        }
55    }
56}
57
58glib::wrapper! {
59    /// A helper `PillSource` to represent an `@room` mention.
60    pub struct AtRoom(ObjectSubclass<imp::AtRoom>) @extends PillSource;
61}
62
63impl AtRoom {
64    /// Constructs an `@room` mention for the given room.
65    pub fn new(room: &Room) -> Self {
66        glib::Object::builder()
67            .property("display-name", "@room")
68            .property("room", room)
69            .build()
70    }
71
72    /// The ID of the room represented by this mention.
73    pub fn room_id(&self) -> &RoomId {
74        self.imp().room_id()
75    }
76}