diff --git a/Cargo.toml b/Cargo.toml index 39ca08e..e687e65 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ gtk-macros = "0.2.0" gstreamer = "0.16.4" gstreamer-player = "0.16.3" isahc = "0.9.12" +once_cell = "1.5.2" rand = "0.7.3" secret-service = "1.1.1" serde = { version = "1.0.117", features = ["derive"] } diff --git a/src/widgets/indexed_list_model.rs b/src/widgets/indexed_list_model.rs index b7a2f90..ec8cb93 100644 --- a/src/widgets/indexed_list_model.rs +++ b/src/widgets/indexed_list_model.rs @@ -3,6 +3,7 @@ use glib::subclass; use glib::subclass::prelude::*; use gio::prelude::*; use gio::subclass::prelude::*; +use once_cell::sync::Lazy; use std::cell::Cell; glib::wrapper! { @@ -32,49 +33,44 @@ mod indexed_list_model { length: Cell, } - static PROPERTIES: [subclass::Property; 1] = [ - subclass::Property("length", |length| { - glib::ParamSpec::uint( - length, - "Length", - "Length", - 0, - std::u32::MAX, - 0, - glib::ParamFlags::READWRITE, - ) - }), - ]; - impl ObjectSubclass for IndexedListModel { const NAME: &'static str = "IndexedListModel"; type Type = super::IndexedListModel; type ParentType = glib::Object; + type Interfaces = (gio::ListModel,); type Instance = subclass::simple::InstanceStruct; type Class = subclass::simple::ClassStruct; glib::object_subclass!(); - fn type_init(type_: &mut subclass::InitializingType) { - type_.add_interface::(); - } - - fn class_init(klass: &mut Self::Class) { - klass.install_properties(&PROPERTIES); - } - fn new() -> Self { Self { length: Cell::new(0) } } } impl ObjectImpl for IndexedListModel { - fn set_property(&self, _obj: &Self::Type, id: usize, value: &glib::Value) { - let prop = &PROPERTIES[id]; + fn properties() -> &'static [glib::ParamSpec] { + static PROPERTIES: Lazy> = Lazy::new(|| { + vec![ + glib::ParamSpec::uint( + "length", + "Length", + "Length", + 0, + std::u32::MAX, + 0, + glib::ParamFlags::READWRITE, + ), + ] + }); - match *prop { - subclass::Property("length", ..) => { + PROPERTIES.as_ref() + } + + fn set_property(&self, _obj: &Self::Type, id: usize, value: &glib::Value, pspec: &glib::ParamSpec) { + match pspec.get_name() { + "length" => { let length = value.get().unwrap().unwrap(); self.length.set(length); } @@ -82,11 +78,9 @@ mod indexed_list_model { } } - fn get_property(&self, _obj: &Self::Type, id: usize) -> glib::Value { - let prop = &PROPERTIES[id]; - - match *prop { - subclass::Property("length", ..) => self.length.get().to_value(), + fn get_property(&self, _obj: &Self::Type, id: usize, pspec: &glib::ParamSpec) -> glib::Value { + match pspec.get_name() { + "length" => self.length.get().to_value(), _ => unimplemented!(), } } @@ -131,45 +125,44 @@ mod item_index { value: Cell, } - static PROPERTIES: [subclass::Property; 1] = [ - subclass::Property("value", |value| { - glib::ParamSpec::uint( - value, - "Value", - "Value", - 0, - std::u32::MAX, - 0, - glib::ParamFlags::READWRITE, - ) - }), - ]; - impl ObjectSubclass for ItemIndex { const NAME: &'static str = "ItemIndex"; type Type = super::ItemIndex; type ParentType = glib::Object; + type Interfaces = (); type Instance = subclass::simple::InstanceStruct; type Class = subclass::simple::ClassStruct; glib::object_subclass!(); - fn class_init(klass: &mut Self::Class) { - klass.install_properties(&PROPERTIES); - } - fn new() -> Self { Self { value: Cell::new(0) } } } impl ObjectImpl for ItemIndex { - fn set_property(&self, _obj: &Self::Type, id: usize, value: &glib::Value) { - let prop = &PROPERTIES[id]; + fn properties() -> &'static [glib::ParamSpec] { + static PROPERTIES: Lazy> = Lazy::new(|| { + vec![ + glib::ParamSpec::uint( + "value", + "Value", + "Value", + 0, + std::u32::MAX, + 0, + glib::ParamFlags::READWRITE, + ), + ] + }); - match *prop { - subclass::Property("value", ..) => { + PROPERTIES.as_ref() + } + + fn set_property(&self, _obj: &Self::Type, id: usize, value: &glib::Value, pspec: &glib::ParamSpec) { + match pspec.get_name() { + "value" => { let value = value.get().unwrap().unwrap(); self.value.set(value); } @@ -177,11 +170,9 @@ mod item_index { } } - fn get_property(&self, _obj: &Self::Type, id: usize) -> glib::Value { - let prop = &PROPERTIES[id]; - - match *prop { - subclass::Property("value", ..) => self.value.get().to_value(), + fn get_property(&self, _obj: &Self::Type, id: usize, pspec: &glib::ParamSpec) -> glib::Value { + match pspec.get_name() { + "value" => self.value.get().to_value(), _ => unimplemented!(), } }