Skip to main content

fractal/components/rows/
button_count_row.rs

1use adw::{prelude::*, subclass::prelude::*};
2use gtk::glib;
3
4mod imp {
5    use std::marker::PhantomData;
6
7    use glib::subclass::InitializingObject;
8
9    use super::*;
10
11    #[derive(Debug, Default, gtk::CompositeTemplate, glib::Properties)]
12    #[template(resource = "/org/gnome/Fractal/ui/components/rows/button_count_row.ui")]
13    #[properties(wrapper_type = super::ButtonCountRow)]
14    pub struct ButtonCountRow {
15        #[template_child]
16        count_label: TemplateChild<gtk::Label>,
17        /// The count that is displayed.
18        #[property(get = Self::count, set = Self::set_count, explicit_notify)]
19        count: PhantomData<glib::GString>,
20    }
21
22    #[glib::object_subclass]
23    impl ObjectSubclass for ButtonCountRow {
24        const NAME: &'static str = "ButtonCountRow";
25        type Type = super::ButtonCountRow;
26        type ParentType = adw::ActionRow;
27
28        fn class_init(klass: &mut Self::Class) {
29            Self::bind_template(klass);
30        }
31
32        fn instance_init(obj: &InitializingObject<Self>) {
33            obj.init_template();
34        }
35    }
36
37    #[glib::derived_properties]
38    impl ObjectImpl for ButtonCountRow {}
39
40    impl WidgetImpl for ButtonCountRow {}
41    impl ListBoxRowImpl for ButtonCountRow {}
42    impl PreferencesRowImpl for ButtonCountRow {}
43    impl ActionRowImpl for ButtonCountRow {}
44
45    impl ButtonCountRow {
46        /// The count to display.
47        fn count(&self) -> glib::GString {
48            self.count_label.label()
49        }
50
51        /// Set the count to display.
52        fn set_count(&self, count: &str) {
53            if self.count() == count {
54                return;
55            }
56
57            self.count_label.set_label(count);
58            self.obj().notify_count();
59        }
60    }
61}
62
63glib::wrapper! {
64    /// An `AdwPreferencesRow` usable as a button, that optionally displays a count.
65    pub struct ButtonCountRow(ObjectSubclass<imp::ButtonCountRow>)
66        @extends gtk::Widget, gtk::ListBoxRow, adw::PreferencesRow, adw::ActionRow,
67        @implements gtk::Accessible, gtk::Buildable, gtk::ConstraintTarget, gtk::Actionable;
68}
69
70impl ButtonCountRow {
71    pub fn new() -> Self {
72        glib::Object::new()
73    }
74}