libcosmic/examples/dock/dock_object/imp.rs

95 lines
3 KiB
Rust
Raw Normal View History

use crate::utils::BoxedWindowList;
2021-12-20 23:52:59 -05:00
use gio::DesktopAppInfo;
2021-12-21 01:00:46 -05:00
use glib::{ParamFlags, ParamSpec, Value};
2021-12-20 23:52:59 -05:00
use gtk4::glib;
use gtk4::prelude::*;
use gtk4::subclass::prelude::*;
use once_cell::sync::Lazy;
2021-12-21 13:01:32 -05:00
use std::cell::Cell;
2021-12-20 23:52:59 -05:00
use std::cell::RefCell;
// Object holding the state
#[derive(Default)]
pub struct DockObject {
appinfo: RefCell<Option<DesktopAppInfo>>,
active: RefCell<BoxedWindowList>,
2021-12-21 13:01:32 -05:00
saved: Cell<bool>,
2021-12-20 23:52:59 -05:00
}
// The central trait for subclassing a GObject
#[glib::object_subclass]
impl ObjectSubclass for DockObject {
const NAME: &'static str = "DockObject";
type Type = super::DockObject;
type ParentType = glib::Object;
}
// Trait shared by all GObjects
impl ObjectImpl for DockObject {
fn properties() -> &'static [ParamSpec] {
static PROPERTIES: Lazy<Vec<ParamSpec>> = Lazy::new(|| {
vec![
ParamSpec::new_object(
// Name
"appinfo",
// Nickname
"appinfo",
// Short description
"app info",
DesktopAppInfo::static_type(),
// The property can be read and written to
ParamFlags::READWRITE,
),
ParamSpec::new_boxed(
// Name
"active",
// Nickname
"active",
// Short description
"active",
BoxedWindowList::static_type(),
2021-12-20 23:52:59 -05:00
// The property can be read and written to
ParamFlags::READWRITE,
),
2021-12-21 13:01:32 -05:00
ParamSpec::new_boolean(
"saved",
"saved",
"Indicates whether app is saved to the dock",
false,
ParamFlags::READWRITE,
),
2021-12-20 23:52:59 -05:00
]
});
PROPERTIES.as_ref()
}
fn set_property(&self, _obj: &Self::Type, _id: usize, value: &Value, pspec: &ParamSpec) {
match pspec.name() {
"appinfo" => {
let appinfo = value
.get()
.expect("Value needs to be Option<DesktopAppInfo>");
self.appinfo.replace(appinfo);
}
"active" => {
let active = value.get().expect("Value needs to be BoxedWindowList");
2021-12-20 23:52:59 -05:00
self.active.replace(active);
}
2021-12-21 13:01:32 -05:00
"saved" => {
self.saved
.replace(value.get().expect("Value needs to be a boolean"));
}
2021-12-20 23:52:59 -05:00
_ => unimplemented!(),
}
}
fn property(&self, _obj: &Self::Type, _id: usize, pspec: &ParamSpec) -> Value {
match pspec.name() {
"appinfo" => self.appinfo.borrow().to_value(),
2021-12-21 13:01:32 -05:00
"active" => self.active.borrow().to_value(),
"saved" => self.saved.get().to_value(),
2021-12-20 23:52:59 -05:00
_ => unimplemented!(),
}
}
}