fractal/session/view/sidebar/
icon_item_row.rs

1use adw::{prelude::*, subclass::prelude::*};
2use gtk::{glib, CompositeTemplate};
3
4use crate::session::model::{SidebarIconItem, SidebarIconItemType};
5
6mod imp {
7    use std::cell::RefCell;
8
9    use glib::subclass::InitializingObject;
10
11    use super::*;
12
13    #[derive(Debug, Default, CompositeTemplate, glib::Properties)]
14    #[template(resource = "/org/gnome/Fractal/ui/session/view/sidebar/icon_item_row.ui")]
15    #[properties(wrapper_type = super::SidebarIconItemRow)]
16    pub struct SidebarIconItemRow {
17        /// The [`SidebarIconItem`] of this row.
18        #[property(get, set = Self::set_icon_item, explicit_notify, nullable)]
19        icon_item: RefCell<Option<SidebarIconItem>>,
20    }
21
22    #[glib::object_subclass]
23    impl ObjectSubclass for SidebarIconItemRow {
24        const NAME: &'static str = "SidebarIconItemRow";
25        type Type = super::SidebarIconItemRow;
26        type ParentType = adw::Bin;
27
28        fn class_init(klass: &mut Self::Class) {
29            Self::bind_template(klass);
30
31            klass.set_css_name("icon-item");
32        }
33
34        fn instance_init(obj: &InitializingObject<Self>) {
35            obj.init_template();
36        }
37    }
38
39    #[glib::derived_properties]
40    impl ObjectImpl for SidebarIconItemRow {}
41
42    impl WidgetImpl for SidebarIconItemRow {}
43    impl BinImpl for SidebarIconItemRow {}
44
45    impl SidebarIconItemRow {
46        /// Set the [`SidebarIconItem`] of this row.
47        fn set_icon_item(&self, icon_item: Option<SidebarIconItem>) {
48            if *self.icon_item.borrow() == icon_item {
49                return;
50            }
51            let obj = self.obj();
52
53            if icon_item
54                .as_ref()
55                .is_some_and(|i| i.item_type() == SidebarIconItemType::Forget)
56            {
57                obj.add_css_class("forget");
58            } else {
59                obj.remove_css_class("forget");
60            }
61
62            self.icon_item.replace(icon_item);
63            obj.notify_icon_item();
64        }
65    }
66}
67
68glib::wrapper! {
69    /// A row in the sidebar presenting a [`SidebarIconItem`].
70    pub struct SidebarIconItemRow(ObjectSubclass<imp::SidebarIconItemRow>)
71        @extends gtk::Widget, adw::Bin, @implements gtk::Accessible;
72}
73
74impl SidebarIconItemRow {
75    pub fn new() -> Self {
76        glib::Object::new()
77    }
78}
79
80impl Default for SidebarIconItemRow {
81    fn default() -> Self {
82        Self::new()
83    }
84}