libcosmic/examples/launcher/search_result_object/imp.rs

62 lines
1.7 KiB
Rust
Raw Normal View History

use std::cell::RefCell;
use std::rc::Rc;
2021-12-28 11:08:02 -05:00
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 crate::utils::BoxedSearchResult;
2021-11-23 18:09:35 -05:00
// Object holding the state
#[derive(Default)]
2021-12-28 11:19:55 -05:00
pub struct SearchResultObject {
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]
2021-12-28 11:19:55 -05:00
impl ObjectSubclass for SearchResultObject {
const NAME: &'static str = "SearchResultObject";
type Type = super::SearchResultObject;
2021-11-23 18:09:35 -05:00
type ParentType = glib::Object;
}
// Trait shared by all GObjects
2021-12-28 11:19:55 -05:00
impl ObjectImpl for SearchResultObject {
2021-11-23 18:09:35 -05:00
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!(),
}
}
}