authenticator/widgets/
url_row.rs

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

mod imp {
    use std::cell::RefCell;

    use adw::subclass::prelude::*;

    use super::*;
    use crate::utils::spawn;

    #[derive(Default, glib::Properties)]
    #[properties(wrapper_type = super::UrlRow)]
    pub struct UrlRow {
        #[property(get, set = Self::set_uri)]
        pub uri: RefCell<Option<String>>,
    }

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

    #[glib::derived_properties]
    impl ObjectImpl for UrlRow {
        fn constructed(&self) {
            self.parent_constructed();
            let obj = self.obj();
            obj.set_activatable(true);
            obj.connect_activated(clone!(
                #[weak(rename_to = row)]
                obj,
                move |_| {
                    if let Some(uri) = row.imp().uri.borrow().clone() {
                        spawn(async move {
                            let file = gio::File::for_uri(&uri);
                            let launcher = gtk::FileLauncher::new(Some(&file));
                            if let Err(err) = launcher.launch_future(gtk::Window::NONE).await {
                                tracing::error!("Failed to open URI {err}");
                            }
                        });
                    };
                }
            ));

            let image_suffix = gtk::Image::from_icon_name("link-symbolic");
            image_suffix.set_accessible_role(gtk::AccessibleRole::Presentation);
            obj.add_suffix(&image_suffix);
        }
    }
    impl WidgetImpl for UrlRow {}
    impl ListBoxRowImpl for UrlRow {}
    impl PreferencesRowImpl for UrlRow {}
    impl ActionRowImpl for UrlRow {}

    impl UrlRow {
        pub fn set_uri(&self, uri: &str) {
            self.obj().set_subtitle(uri);
            self.uri.borrow_mut().replace(uri.to_owned());
        }
    }
}

glib::wrapper! {
    pub struct UrlRow(ObjectSubclass<imp::UrlRow>)
        @extends gtk::Widget, gtk::ListBoxRow, adw::PreferencesRow, adw::ActionRow;
}