libcosmic/examples/launcher/application_object/imp.rs

61 lines
1.7 KiB
Rust
Raw Normal View History

2021-12-28 11:08:02 -05:00
use crate::utils::BoxedSearchResult;
use glib::{ParamFlags, ParamSpec, Value};
2021-11-23 18:09:35 -05:00
use gtk4::glib;
use gtk4::prelude::*;
use gtk4::subclass::prelude::*;
use once_cell::sync::Lazy;
use std::cell::RefCell;
use std::rc::Rc;
// Object holding the state
#[derive(Default)]
pub struct ApplicationObject {
2021-12-28 11:08:02 -05:00
data: Rc<RefCell<BoxedSearchResult>>,
2021-11-23 18:09:35 -05:00
}
// The central trait for subclassing a GObject
#[glib::object_subclass]
impl ObjectSubclass for ApplicationObject {
2021-11-30 16:15:06 -05:00
const NAME: &'static str = "ApplicationObject";
2021-11-23 18:09:35 -05:00
type Type = super::ApplicationObject;
type ParentType = glib::Object;
}
// Trait shared by all GObjects
impl ObjectImpl for ApplicationObject {
fn properties() -> &'static [ParamSpec] {
static PROPERTIES: Lazy<Vec<ParamSpec>> = Lazy::new(|| {
2021-12-28 11:08:02 -05:00
vec![ParamSpec::new_boxed(
// Name
"data",
// Nickname
"data",
// Short description
"data",
BoxedSearchResult::static_type(),
// The property can be read and written to
ParamFlags::READWRITE,
)]
2021-11-23 18:09:35 -05:00
});
PROPERTIES.as_ref()
}
fn set_property(&self, _obj: &Self::Type, _id: usize, value: &Value, pspec: &ParamSpec) {
match pspec.name() {
2021-12-28 11:08:02 -05:00
"data" => {
let data = value.get().expect("Value needs to be BoxedSearchResult");
self.data.replace(data);
2021-11-23 18:09:35 -05:00
}
_ => unimplemented!(),
}
}
fn property(&self, _obj: &Self::Type, _id: usize, pspec: &ParamSpec) -> Value {
match pspec.name() {
2021-12-28 11:08:02 -05:00
"data" => self.data.borrow().to_value(),
2021-11-23 18:09:35 -05:00
_ => unimplemented!(),
}
}
}