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

mod imp {
    use super::*;

    #[derive(Debug, Default)]
    pub struct Spinner {
        inner: gtk::Spinner,
    }

    #[glib::object_subclass]
    impl ObjectSubclass for Spinner {
        const NAME: &'static str = "Spinner";
        type Type = super::Spinner;
        type ParentType = gtk::Widget;

        fn class_init(klass: &mut Self::Class) {
            klass.set_layout_manager_type::<gtk::BinLayout>();
            klass.set_css_name("spinner-wrapper");
            klass.set_accessible_role(gtk::AccessibleRole::Status);
        }
    }

    impl ObjectImpl for Spinner {
        fn constructed(&self) {
            self.parent_constructed();
            let obj = self.obj();

            self.inner.set_parent(&*obj);
            obj.update_property(&[gtk::accessible::Property::Label(&gettext("Loading"))])
        }

        fn dispose(&self) {
            self.inner.unparent();
        }
    }

    impl WidgetImpl for Spinner {
        fn map(&self) {
            self.parent_map();
            self.inner.start();
        }

        fn unmap(&self) {
            self.inner.stop();
            self.parent_unmap();
        }
    }

    impl AccessibleImpl for Spinner {
        fn first_accessible_child(&self) -> Option<gtk::Accessible> {
            // Hide the children in the a11y tree.
            None
        }
    }
}

glib::wrapper! {
    /// A spinner.
    ///
    /// This is a wrapper around `GtkSpinner` that makes sure the spinner is stopped when it is not mapped.
    pub struct Spinner(ObjectSubclass<imp::Spinner>)
        @extends gtk::Widget, @implements gtk::Accessible;
}

impl Default for Spinner {
    fn default() -> Self {
        glib::Object::new()
    }
}