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

use super::LoadingBin;

mod imp {
    use std::marker::PhantomData;

    use super::*;

    #[derive(Debug, Default, glib::Properties)]
    #[properties(wrapper_type = super::LoadingButton)]
    pub struct LoadingButton {
        pub loading_bin: LoadingBin,
        /// The label of the content of the button.
        ///
        /// If an icon was set, it is removed.
        #[property(get = Self::content_label, set = Self::set_content_label, explicit_notify)]
        pub content_label: PhantomData<Option<glib::GString>>,
        /// The name of the icon of the content of the button.
        ///
        /// If a label was set, it is removed.
        #[property(get = Self::content_icon_name, set = Self::set_content_icon_name, explicit_notify)]
        pub content_icon_name: PhantomData<Option<glib::GString>>,
        /// Whether to display the loading spinner.
        ///
        /// If this is `false`, the text or icon will be displayed.
        #[property(get = Self::is_loading, set = Self::set_is_loading, explicit_notify)]
        pub is_loading: PhantomData<bool>,
    }

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

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

            self.obj().set_child(Some(&self.loading_bin));
        }
    }

    impl WidgetImpl for LoadingButton {}
    impl ButtonImpl for LoadingButton {}

    impl LoadingButton {
        /// The label of the content of the button.
        fn content_label(&self) -> Option<glib::GString> {
            self.loading_bin
                .child()
                .and_downcast::<gtk::Label>()
                .map(|l| l.label())
                .filter(|s| !s.is_empty())
        }

        /// Set the label of the content of the button.
        fn set_content_label(&self, label: &str) {
            if self.content_label().as_deref() == Some(label) {
                return;
            }
            let obj = self.obj();

            let child_label =
                if let Some(child_label) = self.loading_bin.child().and_downcast::<gtk::Label>() {
                    child_label
                } else {
                    let child_label = gtk::Label::builder()
                        .ellipsize(pango::EllipsizeMode::End)
                        .use_underline(true)
                        .mnemonic_widget(&*obj)
                        .css_classes(["text-button"])
                        .build();

                    self.loading_bin.set_child(Some(child_label.clone()));
                    // In case it was an image before.
                    obj.remove_css_class("image-button");
                    obj.update_relation(&[gtk::accessible::Relation::LabelledBy(&[
                        child_label.upcast_ref()
                    ])]);

                    child_label
                };

            child_label.set_label(label);

            obj.notify_content_label();
        }

        /// The name of the icon of the content of the button.
        fn content_icon_name(&self) -> Option<glib::GString> {
            self.loading_bin
                .child()
                .and_downcast::<gtk::Image>()
                .and_then(|i| i.icon_name())
        }

        /// Set the name of the icon of the content of the button.
        fn set_content_icon_name(&self, icon_name: &str) {
            if self.content_icon_name().as_deref() == Some(icon_name) {
                return;
            }
            let obj = self.obj();

            let child_image =
                if let Some(child_image) = self.loading_bin.child().and_downcast::<gtk::Image>() {
                    child_image
                } else {
                    let child_image = gtk::Image::builder()
                        .accessible_role(gtk::AccessibleRole::Presentation)
                        .build();

                    self.loading_bin.set_child(Some(child_image.clone()));
                    obj.add_css_class("image-button");

                    child_image
                };

            child_image.set_icon_name(Some(icon_name));

            obj.notify_content_icon_name();
        }

        /// Whether to display the loading spinner.
        ///
        /// If this is `false`, the text will be displayed.
        fn is_loading(&self) -> bool {
            self.loading_bin.is_loading()
        }

        /// Set whether to display the loading spinner.
        fn set_is_loading(&self, is_loading: bool) {
            if self.is_loading() == is_loading {
                return;
            }
            let obj = self.obj();

            // The action should have been enabled or disabled so the sensitive
            // state should update itself.
            if obj.action_name().is_none() {
                obj.set_sensitive(!is_loading);
            }

            self.loading_bin.set_is_loading(is_loading);

            obj.notify_is_loading();
        }
    }
}

glib::wrapper! {
    /// Button showing either a spinner or a label.
    ///
    /// Use the `content-label` and `content-icon-name` properties instead of `label` and `icon-name` respectively, otherwise the spinner will not appear.
    pub struct LoadingButton(ObjectSubclass<imp::LoadingButton>)
        @extends gtk::Widget, gtk::Button, @implements gtk::Accessible, gtk::Actionable;
}

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

impl Default for LoadingButton {
    fn default() -> Self {
        Self::new()
    }
}