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
use crate::plugins::Plugin;
use gtk::{
    glib::{self, Boxed},
    prelude::*,
};

#[derive(Boxed, Clone, Debug)]
#[boxed_type(name = "PluginBoxed")]
pub struct PluginBoxed(pub Box<dyn Plugin>);

mod imp {
    use super::PluginBoxed;
    use gtk::glib::{self, prelude::*, subclass::prelude::*};
    use once_cell::unsync::OnceCell;

    #[derive(Default)]
    pub struct PluginObject {
        pub plugin: OnceCell<PluginBoxed>,
    }

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

    impl ObjectImpl for PluginObject {
        fn properties() -> &'static [glib::ParamSpec] {
            use once_cell::sync::Lazy;
            static PROPERTIES: Lazy<Vec<glib::ParamSpec>> = Lazy::new(|| {
                vec![glib::ParamSpecBoxed::builder::<PluginBoxed>("plugin")
                    .construct_only()
                    .readwrite()
                    .build()]
            });
            PROPERTIES.as_ref()
        }

        fn set_property(&self, _id: usize, value: &glib::Value, pspec: &glib::ParamSpec) {
            match pspec.name() {
                "plugin" => self.plugin.set(value.get().unwrap()).unwrap(),
                _ => unimplemented!(),
            }
        }

        fn property(&self, _id: usize, pspec: &glib::ParamSpec) -> glib::Value {
            match pspec.name() {
                "plugin" => self.plugin.get().unwrap().to_value(),
                _ => unimplemented!(),
            }
        }
    }
}

glib::wrapper! {
    /// A wrapper around a [Plugin] so we can store it in a [PluginList].
    pub struct PluginObject(ObjectSubclass<imp::PluginObject>);
}

impl PluginObject {
    pub fn new(plugin: Box<dyn Plugin>) -> Self {
        glib::Object::builder()
            .property("plugin", &PluginBoxed(plugin))
            .build()
    }

    pub fn plugin(&self) -> Box<dyn Plugin> {
        self.property::<PluginBoxed>("plugin").0
    }
}

#[cfg(test)]
mod test {
    use super::PluginObject;

    #[test]
    fn new() {
        PluginObject::new(Box::new(crate::plugins::steps::StepsPlugin::new()));
    }
}