fractal/session/view/sidebar/
section_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
use adw::subclass::prelude::BinImpl;
use gettextrs::gettext;
use gtk::{glib, prelude::*, subclass::prelude::*, CompositeTemplate};

use crate::session::model::{RoomCategory, SidebarSection, SidebarSectionName};

mod imp {
    use std::{
        cell::{Cell, RefCell},
        marker::PhantomData,
    };

    use glib::subclass::InitializingObject;

    use super::*;

    #[derive(Debug, Default, CompositeTemplate, glib::Properties)]
    #[template(resource = "/org/gnome/Fractal/ui/session/view/sidebar/section_row.ui")]
    #[properties(wrapper_type = super::SidebarSectionRow)]
    pub struct SidebarSectionRow {
        /// The section of this row.
        #[property(get, set = Self::set_section, explicit_notify, nullable)]
        section: RefCell<Option<SidebarSection>>,
        section_binding: RefCell<Option<glib::Binding>>,
        /// Whether this row is expanded.
        #[property(get, set = Self::set_is_expanded, explicit_notify, construct, default = true)]
        is_expanded: Cell<bool>,
        /// The label to show for this row.
        #[property(get = Self::label)]
        label: PhantomData<Option<String>>,
        /// The room category to show a label for during a drag-and-drop
        /// operation.
        ///
        /// This will change the label according to the action that can be
        /// performed when dropping a room with the given category.
        show_label_for_room_category: Cell<Option<RoomCategory>>,
        /// The label showing the category name.
        #[template_child]
        pub(super) display_name: TemplateChild<gtk::Label>,
    }

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

        fn class_init(klass: &mut Self::Class) {
            Self::bind_template(klass);
            klass.set_css_name("sidebar-section");
        }

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

    #[glib::derived_properties]
    impl ObjectImpl for SidebarSectionRow {
        fn constructed(&self) {
            self.parent_constructed();

            self.obj().connect_parent_notify(|obj| {
                obj.set_expanded_accessibility_state(obj.is_expanded());
            });
        }

        fn dispose(&self) {
            if let Some(binding) = self.section_binding.take() {
                binding.unbind();
            }
        }
    }

    impl WidgetImpl for SidebarSectionRow {}
    impl BinImpl for SidebarSectionRow {}

    impl SidebarSectionRow {
        /// Set the section represented by this row.
        fn set_section(&self, section: Option<SidebarSection>) {
            if *self.section.borrow() == section {
                return;
            }

            if let Some(binding) = self.section_binding.take() {
                binding.unbind();
            }
            let obj = self.obj();

            if let Some(section) = &section {
                let section_binding = section
                    .bind_property("is-expanded", &*obj, "is-expanded")
                    .sync_create()
                    .build();
                self.section_binding.replace(Some(section_binding));
            }

            self.section.replace(section);

            obj.notify_section();
            obj.notify_label();
        }

        /// The label to show for this row.
        fn label(&self) -> Option<String> {
            let target_section_name = self.section.borrow().as_ref()?.name();
            let source_room_category = self.show_label_for_room_category.get();

            let label = match source_room_category {
                Some(RoomCategory::Invited) => match target_section_name {
                    // Translators: This is an action to join a room and put it in the "Favorites"
                    // section.
                    SidebarSectionName::Favorite => gettext("Join Room as Favorite"),
                    SidebarSectionName::Normal => gettext("Join Room"),
                    // Translators: This is an action to join a room and put it in the "Low
                    // Priority" section.
                    SidebarSectionName::LowPriority => gettext("Join Room as Low Priority"),
                    SidebarSectionName::Left => gettext("Reject Invite"),
                    _ => target_section_name.to_string(),
                },
                Some(RoomCategory::Favorite) => match target_section_name {
                    SidebarSectionName::Normal => gettext("Move to Rooms"),
                    SidebarSectionName::LowPriority => gettext("Move to Low Priority"),
                    SidebarSectionName::Left => gettext("Leave Room"),
                    _ => target_section_name.to_string(),
                },
                Some(RoomCategory::Normal) => match target_section_name {
                    SidebarSectionName::Favorite => gettext("Move to Favorites"),
                    SidebarSectionName::LowPriority => gettext("Move to Low Priority"),
                    SidebarSectionName::Left => gettext("Leave Room"),
                    _ => target_section_name.to_string(),
                },
                Some(RoomCategory::LowPriority) => match target_section_name {
                    SidebarSectionName::Favorite => gettext("Move to Favorites"),
                    SidebarSectionName::Normal => gettext("Move to Rooms"),
                    SidebarSectionName::Left => gettext("Leave Room"),
                    _ => target_section_name.to_string(),
                },
                Some(RoomCategory::Left) => match target_section_name {
                    // Translators: This is an action to rejoin a room and put it in the "Favorites"
                    // section.
                    SidebarSectionName::Favorite => gettext("Rejoin Room as Favorite"),
                    SidebarSectionName::Normal => gettext("Rejoin Room"),
                    // Translators: This is an action to rejoin a room and put it in the "Low
                    // Priority" section.
                    SidebarSectionName::LowPriority => gettext("Rejoin Room as Low Priority"),
                    _ => target_section_name.to_string(),
                },
                _ => target_section_name.to_string(),
            };

            Some(label)
        }

        /// Set whether this row is expanded.
        fn set_is_expanded(&self, is_expanded: bool) {
            if self.is_expanded.get() == is_expanded {
                return;
            }
            let obj = self.obj();

            if is_expanded {
                obj.set_state_flags(gtk::StateFlags::CHECKED, false);
            } else {
                obj.unset_state_flags(gtk::StateFlags::CHECKED);
            }

            self.is_expanded.set(is_expanded);
            obj.set_expanded_accessibility_state(is_expanded);
            obj.notify_is_expanded();
        }

        /// Set the room category to show the label for.
        pub(super) fn set_show_label_for_room_category(&self, category: Option<RoomCategory>) {
            if self.show_label_for_room_category.get() == category {
                return;
            }

            self.show_label_for_room_category.set(category);

            self.obj().notify_label();
        }
    }
}

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

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

    /// Set the room category to show the label for.
    pub fn set_show_label_for_room_category(&self, category: Option<RoomCategory>) {
        self.imp().set_show_label_for_room_category(category);
    }

    /// Set the expanded state of this row for a11y.
    fn set_expanded_accessibility_state(&self, is_expanded: bool) {
        if let Some(row) = self.parent() {
            row.update_state(&[gtk::accessible::State::Expanded(Some(is_expanded))]);
        }
    }

    /// The descendant that labels this row for a11y.
    pub fn labelled_by(&self) -> &gtk::Accessible {
        self.imp().display_name.upcast_ref()
    }
}