app library popover for addign app group

This commit is contained in:
Ashley Wulber 2022-01-21 14:43:01 -05:00
parent df088dfe8e
commit 1fa7b8243a
8 changed files with 282 additions and 302 deletions

View file

@ -1,22 +1,19 @@
use std::cell::RefCell;
use std::rc::Rc;
use gdk4::glib::ParamSpecBoolean;
use gdk4::glib::ParamSpecString;
use gdk4::glib::ParamSpecUInt;
use gdk4::glib::ParamSpecVariant;
use glib::{FromVariant, ParamFlags, ParamSpec, ToVariant, Value, Variant, VariantTy};
use gdk4::glib::ParamSpecBoxed;
use glib::{ParamFlags, ParamSpec, Value};
use gtk4::glib;
use gtk4::prelude::*;
use gtk4::subclass::prelude::*;
use once_cell::sync::Lazy;
use super::AppGroupData;
use super::BoxedAppGroupType;
// Object holding the state
#[derive(Default)]
pub struct AppGroup {
pub data: Rc<RefCell<AppGroupData>>,
pub inner: Rc<RefCell<BoxedAppGroupType>>,
}
// The central trait for subclassing a GObject
@ -31,123 +28,26 @@ impl ObjectSubclass for AppGroup {
impl ObjectImpl for AppGroup {
fn properties() -> &'static [ParamSpec] {
static PROPERTIES: Lazy<Vec<ParamSpec>> = Lazy::new(|| {
vec![
ParamSpecUInt::new(
// Name
"id",
// Nickname
"id",
// Short description
"Name of the application group",
0,
u32::MAX,
// Default value
0,
// The property can be read and written to
ParamFlags::READWRITE,
),
ParamSpecString::new(
// Name
"name",
// Nickname
"name",
// Short description
"Name of the application group",
// Default value
Some(""),
// The property can be read and written to
ParamFlags::READWRITE,
),
ParamSpecBoolean::new(
// Name
"mutable",
// Nickname
"mutable",
// Short description
"Mutability of the application group",
// Default value
false,
// The property can be read and written to
ParamFlags::READWRITE,
),
ParamSpecString::new(
// Name
"icon",
// Nickname
"icon",
// Short description
"Icon of application Group",
// Default value
Some("folder"),
// The property can be read and written to
ParamFlags::READWRITE,
),
ParamSpecString::new(
// Name
"category",
// Nickname
"category",
// Short description
"Category of application Group",
// Default value
None,
// The property can be read and written to
ParamFlags::READWRITE,
),
ParamSpecVariant::new(
// Name
"appnames",
// Nickname
"appnames",
// Short description
"Names of applications in the App Group",
VariantTy::new("as").expect("Oops invalid string for VariantTy tuple."),
// Default value
None,
// The property can be read and written to
ParamFlags::READWRITE,
),
]
vec![ParamSpecBoxed::new(
// Name
"inner",
// Nickname
"inner",
// Short description
"inner",
BoxedAppGroupType::static_type(),
// The property can be read and written to
ParamFlags::READWRITE,
)]
});
PROPERTIES.as_ref()
}
fn set_property(&self, _obj: &Self::Type, _id: usize, value: &Value, pspec: &ParamSpec) {
match pspec.name() {
"id" => {
let id = value.get().expect("The value needs to be of type `u32`.");
self.data.borrow_mut().id = id;
}
"name" => {
let name = value
.get()
.expect("The value needs to be of type `String`.");
self.data.borrow_mut().name = name;
}
"icon" => {
let icon = value.get().expect("The icon needs to be of type `String`");
self.data.borrow_mut().icon = icon;
}
"category" => {
let category = value
.get()
.expect("The category needs to be of type `String`");
self.data.borrow_mut().category = category;
}
"mutable" => {
let mutable = value
.get()
.expect("The mutable property needs to be of type `bool`");
self.data.borrow_mut().mutable = mutable;
}
"appnames" => {
let appnames = <Vec<String>>::from_variant(
&value
.get::<Variant>()
.expect("The icon needs to be a Variant"),
)
.expect("The icon variant needs to be a Vec<String>");
self.data.borrow_mut().app_names = appnames;
"inner" => {
let inner = value.get().expect("The value needs to be of type `u32`.");
self.inner.replace(inner);
}
_ => unimplemented!(),
}
@ -155,12 +55,7 @@ impl ObjectImpl for AppGroup {
fn property(&self, _obj: &Self::Type, _id: usize, pspec: &ParamSpec) -> Value {
match pspec.name() {
"id" => self.data.borrow().id.to_value(),
"name" => self.data.borrow().name.to_value(),
"icon" => self.data.borrow().icon.to_value(),
"mutable" => self.data.borrow().mutable.to_value(),
"category" => self.data.borrow().category.to_value(),
"appnames" => self.data.borrow().app_names.to_variant().to_value(),
"inner" => self.inner.borrow().to_value(),
_ => unimplemented!(),
}
}

View file

@ -1,6 +1,4 @@
use glib::Object;
use glib::ObjectExt;
use glib::ToVariant;
use gtk4::glib;
use gtk4::subclass::prelude::*;
use serde::{Deserialize, Serialize};
@ -12,22 +10,68 @@ glib::wrapper! {
}
impl AppGroup {
pub fn new(data: AppGroupData) -> Self {
let self_: Self = Object::new(&[
("id", &data.id),
("name", &data.name),
("mutable", &data.mutable),
("icon", &data.icon),
("category", &data.category),
])
.expect("Failed to create `ApplicationObject`.");
self_.set_property("appnames", data.app_names.to_variant());
pub fn new(data: BoxedAppGroupType) -> Self {
let self_: Self =
Object::new(&[("inner", &data)]).expect("Failed to create `ApplicationObject`.");
self_
}
pub fn group_data(&self) -> AppGroupData {
pub fn popup(&self) {
let imp = imp::AppGroup::from_instance(self);
imp.data.borrow().clone()
let inner = imp.inner.borrow().clone();
match inner {
BoxedAppGroupType::Group(d) => {
// d.popup = true;
imp.inner.replace(BoxedAppGroupType::Group(d));
}
BoxedAppGroupType::NewGroup(_) => {
imp.inner.replace(BoxedAppGroupType::NewGroup(true));
}
};
}
pub fn popdown(&self) {
let imp = imp::AppGroup::from_instance(self);
let inner = imp.inner.borrow().clone();
match inner {
BoxedAppGroupType::Group(d) => {
// d.popup = false;
imp.inner.replace(BoxedAppGroupType::Group(d));
}
BoxedAppGroupType::NewGroup(_) => {
imp.inner.replace(BoxedAppGroupType::NewGroup(false));
}
};
}
pub fn is_popup_active(&self) -> bool {
let imp = imp::AppGroup::from_instance(self);
match imp.inner.borrow().clone() {
BoxedAppGroupType::Group(_d) => false,
BoxedAppGroupType::NewGroup(is_active) => is_active,
}
}
pub fn group_data(&self) -> Option<AppGroupData> {
let imp = imp::AppGroup::from_instance(self);
let inner = imp.inner.borrow().clone();
match inner {
BoxedAppGroupType::Group(d) => Some(d),
_ => None,
}
}
}
#[derive(Serialize, Deserialize, Clone, glib::Boxed)]
#[boxed_type(name = "BoxedAppGroupType")]
pub enum BoxedAppGroupType {
Group(AppGroupData),
NewGroup(bool),
}
impl Default for BoxedAppGroupType {
fn default() -> Self {
Self::NewGroup(false)
}
}
@ -40,4 +84,5 @@ pub struct AppGroupData {
pub mutable: bool,
pub app_names: Vec<String>,
pub category: String,
// pub popup: bool,
}