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
use adw::{prelude::*, subclass::prelude::*};
use gtk::glib;

use crate::session::model::MemberRole;

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

    use super::*;

    #[derive(Debug, Default, glib::Properties)]
    #[properties(wrapper_type = super::RoleBadge)]
    pub struct RoleBadge {
        pub label: gtk::Label,
        /// The role displayed by this badge.
        #[property(get, set = Self::set_role, explicit_notify, builder(MemberRole::default()))]
        pub role: Cell<MemberRole>,
        /// Whether the role displayed by this badge is the default role.
        #[property(get)]
        pub is_default_role: Cell<bool>,
    }

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

        fn class_init(klass: &mut Self::Class) {
            klass.set_css_name("role-badge");
        }
    }

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

            obj.set_child(Some(&self.label));
            self.update_badge();
            self.update_is_default_role();
        }
    }

    impl WidgetImpl for RoleBadge {}
    impl BinImpl for RoleBadge {}

    impl RoleBadge {
        /// Set the role displayed by this badge.
        fn set_role(&self, role: MemberRole) {
            if self.role.get() == role {
                return;
            }

            self.role.set(role);
            self.update_badge();
            self.update_is_default_role();
            self.obj().notify_role();
        }

        /// Update whether the role displayed by this badge is the default role.
        fn update_is_default_role(&self) {
            let is_default = self.role.get() == MemberRole::Default;

            if self.is_default_role.get() == is_default {
                return;
            }

            self.is_default_role.set(is_default);
            self.obj().notify_is_default_role();
        }

        /// Update the badge for the current state.
        fn update_badge(&self) {
            let obj = self.obj();
            let role = self.role.get();

            self.label.set_text(&role.to_string());

            if role == MemberRole::Administrator {
                obj.add_css_class("admin");
            } else {
                obj.remove_css_class("admin");
            }

            if role == MemberRole::Moderator {
                obj.add_css_class("mod");
            } else {
                obj.remove_css_class("mod");
            }

            if role == MemberRole::Muted {
                obj.add_css_class("muted");
            } else {
                obj.remove_css_class("muted");
            }
        }
    }
}

glib::wrapper! {
    /// Inline widget displaying a badge with the role of a room member.
    pub struct RoleBadge(ObjectSubclass<imp::RoleBadge>)
        @extends gtk::Widget, adw::Bin;
}

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