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

use super::{Category, CategoryType, SidebarIconItem};
use crate::utils::{BoundConstructOnlyObject, SingleItemListModel};

mod imp {
    use std::cell::{Cell, OnceCell};

    use super::*;

    #[derive(Debug, glib::Properties)]
    #[properties(wrapper_type = super::SidebarItem)]
    pub struct SidebarItem {
        /// The item wrapped by this `SidebarItem`.
        #[property(get, set = Self::set_inner_item, construct_only)]
        pub inner_item: BoundConstructOnlyObject<glib::Object>,
        /// Whether this item is visible.
        #[property(get)]
        pub is_visible: Cell<bool>,
        /// Whether to inhibit the expanded state.
        ///
        /// It means that all the categories will be expanded regardless of
        /// their "is-expanded" property.
        #[property(get, set = Self::set_inhibit_expanded, explicit_notify)]
        pub inhibit_expanded: Cell<bool>,
        is_visible_filter: gtk::CustomFilter,
        is_expanded_filter: gtk::CustomFilter,
        /// The inner model.
        model: OnceCell<gtk::FilterListModel>,
    }

    impl Default for SidebarItem {
        fn default() -> Self {
            Self {
                inner_item: Default::default(),
                is_visible: Cell::new(true),
                inhibit_expanded: Default::default(),
                is_visible_filter: Default::default(),
                is_expanded_filter: Default::default(),
                model: Default::default(),
            }
        }
    }

    #[glib::object_subclass]
    impl ObjectSubclass for SidebarItem {
        const NAME: &'static str = "SidebarItem";
        type Type = super::SidebarItem;
        type Interfaces = (gio::ListModel,);
    }

    #[glib::derived_properties]
    impl ObjectImpl for SidebarItem {}

    impl ListModelImpl for SidebarItem {
        fn item_type(&self) -> glib::Type {
            glib::Object::static_type()
        }

        fn n_items(&self) -> u32 {
            self.model.get().unwrap().n_items()
        }

        fn item(&self, position: u32) -> Option<glib::Object> {
            self.model.get().unwrap().item(position)
        }
    }

    impl SidebarItem {
        /// Set the item wrapped by this `SidebarItem`.
        fn set_inner_item(&self, item: glib::Object) {
            let mut handlers = Vec::new();

            let inner_model = if let Some(category) = item.downcast_ref::<Category>() {
                // Create a list model to have an item for the category itself.
                let category_model = SingleItemListModel::new(category);

                // Filter the children depending on whether the category is expanded or not.
                self.is_expanded_filter.set_filter_func(clone!(
                    #[weak(rename_to = imp)]
                    self,
                    #[weak]
                    category,
                    #[upgrade_or]
                    false,
                    move |_| imp.inhibit_expanded.get() || category.is_expanded()
                ));
                let children_model = gtk::FilterListModel::new(
                    Some(category.clone()),
                    Some(self.is_expanded_filter.clone()),
                );

                let is_expanded_handler = category.connect_is_expanded_notify(clone!(
                    #[weak(rename_to = imp)]
                    self,
                    move |_| {
                        imp.is_expanded_filter.changed(gtk::FilterChange::Different);
                    }
                ));
                handlers.push(is_expanded_handler);

                // Merge the models for the category and its children.
                let wrapper_model = gio::ListStore::new::<glib::Object>();
                wrapper_model.append(&category_model);
                wrapper_model.append(&children_model);

                gtk::FlattenListModel::new(Some(wrapper_model)).upcast::<gio::ListModel>()
            } else {
                // Create a list model for the item.
                SingleItemListModel::new(&item).upcast()
            };

            self.inner_item.set(item, handlers);

            self.is_visible_filter.set_filter_func(clone!(
                #[weak(rename_to = imp)]
                self,
                #[upgrade_or]
                false,
                move |_| imp.is_visible.get()
            ));
            let model =
                gtk::FilterListModel::new(Some(inner_model), Some(self.is_visible_filter.clone()));

            let obj = self.obj();
            model.connect_items_changed(clone!(
                #[weak]
                obj,
                move |_model, pos, removed, added| {
                    obj.items_changed(pos, removed, added);
                }
            ));

            self.model.set(model).unwrap();
        }

        /// Set whether this item is visible.
        pub(super) fn set_visible(&self, visible: bool) {
            if self.is_visible.get() == visible {
                return;
            }

            self.is_visible.set(visible);

            self.obj().notify_is_visible();
            self.is_visible_filter.changed(gtk::FilterChange::Different);
        }

        /// Set whether to inhibit the expanded state.
        fn set_inhibit_expanded(&self, inhibit: bool) {
            if self.inhibit_expanded.get() == inhibit {
                return;
            }

            self.inhibit_expanded.set(inhibit);

            self.obj().notify_inhibit_expanded();
            self.is_expanded_filter
                .changed(gtk::FilterChange::Different);
        }
    }
}

glib::wrapper! {
    /// A top-level item in the sidebar.
    ///
    /// This wraps the inner item to handle its visibility and whether it should
    /// show its children (i.e. whether it is "expanded").
    pub struct SidebarItem(ObjectSubclass<imp::SidebarItem>)
        @implements gio::ListModel;
}

impl SidebarItem {
    /// Construct a new `SidebarItem` for the given item.
    pub fn new(item: impl IsA<glib::Object>) -> Self {
        glib::Object::builder()
            .property("inner-item", &item)
            .build()
    }

    /// Update the visibility of this item for a drag-n-drop from the given
    /// category.
    pub fn update_visibility_for_category(&self, category_type: CategoryType) {
        let inner_item = self.inner_item();
        let visible = if let Some(category) = inner_item.downcast_ref::<Category>() {
            category.visible_for_category(category_type)
        } else if let Some(icon_item) = inner_item.downcast_ref::<SidebarIconItem>() {
            icon_item.visible_for_category(category_type)
        } else {
            true
        };

        self.imp().set_visible(visible);
    }
}