chore: add cosmic app list and cosmic panel buttons

This commit is contained in:
Ashley Wulber 2022-06-06 11:52:45 -04:00
parent 853caa5b84
commit 464338585c
No known key found for this signature in database
GPG key ID: BCD0B777C3F6E2FD
54 changed files with 3855 additions and 187 deletions

View file

@ -0,0 +1,113 @@
// SPDX-License-Identifier: MPL-2.0-only
use std::cell::Cell;
use std::cell::RefCell;
use crate::utils::BoxedWindowList;
use gio::DesktopAppInfo;
use glib::{ParamFlags, ParamSpec, Value};
use gtk4::gdk::glib::ParamSpecBoolean;
use gtk4::gdk::glib::ParamSpecBoxed;
use gtk4::gdk::glib::ParamSpecObject;
use gtk4::glib;
use gtk4::prelude::*;
use gtk4::subclass::prelude::*;
use once_cell::sync::Lazy;
// Object holding the state
#[derive(Default)]
pub struct DockObject {
pub(super) appinfo: RefCell<Option<DesktopAppInfo>>,
pub(super) active: RefCell<BoxedWindowList>,
pub(super) saved: Cell<bool>,
pub(super) popover: Cell<bool>,
}
// 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![
ParamSpecObject::new(
// Name
"appinfo",
// Nickname
"appinfo",
// Short description
"app info",
DesktopAppInfo::static_type(),
// The property can be read and written to
ParamFlags::READWRITE,
),
ParamSpecBoxed::new(
// Name
"active",
// Nickname
"active",
// Short description
"active",
BoxedWindowList::static_type(),
// The property can be read and written to
ParamFlags::READWRITE,
),
ParamSpecBoolean::new(
"saved",
"saved",
"Indicates whether app is saved to the dock",
false,
ParamFlags::READWRITE,
),
ParamSpecBoolean::new(
"popover",
"popover",
"Indicates whether there is a popover menu displayed for this object",
false,
ParamFlags::READWRITE,
),
]
});
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");
self.active.replace(active);
}
"saved" => {
self.saved
.replace(value.get().expect("Value needs to be a boolean"));
}
"popover" => {
self.popover
.replace(value.get().expect("Value needs to be a boolean"));
}
_ => unimplemented!(),
}
}
fn property(&self, _obj: &Self::Type, _id: usize, pspec: &ParamSpec) -> Value {
match pspec.name() {
"appinfo" => self.appinfo.borrow().to_value(),
"active" => self.active.borrow().to_value(),
"saved" => self.saved.get().to_value(),
"popover" => self.popover.get().to_value(),
_ => unimplemented!(),
}
}
}

View file

@ -0,0 +1,122 @@
// SPDX-License-Identifier: MPL-2.0-only
use crate::utils::BoxedWindowList;
use gio::{DesktopAppInfo, Icon};
use gtk4::gdk::glib::Object;
use gtk4::gdk::subclass::prelude::ObjectSubclassExt;
use gtk4::prelude::*;
use gtk4::{glib, Image};
use std::path::Path;
mod imp;
glib::wrapper! {
pub struct DockObject(ObjectSubclass<imp::DockObject>);
}
impl DockObject {
pub fn new(appinfo: DesktopAppInfo) -> Self {
Object::new(&[("appinfo", &Some(appinfo)), ("saved", &true)])
.expect("Failed to create `DockObject`.")
}
pub fn from_app_info_path(path: &str) -> Option<Self> {
if let Some(path) = Path::new(path).file_name() {
if let Some(path) = path.to_str() {
if let Some(appinfo) = gio::DesktopAppInfo::new(path) {
if appinfo.should_show() {
return Some(
Object::new(&[("appinfo", &Some(appinfo)), ("saved", &true)])
.expect("Failed to create `DockObject`."),
);
}
}
}
}
None
}
pub fn get_path(&self) -> Option<String> {
let imp = imp::DockObject::from_instance(self);
if let Some(app_info) = imp.appinfo.borrow().as_ref() {
app_info
.filename()
.map(|name| name.to_string_lossy().into())
} else {
None
}
}
pub fn get_name(&self) -> Option<String> {
let imp = imp::DockObject::from_instance(self);
imp.appinfo.borrow().as_ref().map(|app_info| app_info.name().to_string())
}
pub fn get_image(&self) -> gtk4::Image {
let imp = imp::DockObject::from_instance(self);
if let Some(app_info) = imp.appinfo.borrow().as_ref() {
let image = Image::new();
let icon = app_info
.icon()
.unwrap_or_else(|| Icon::for_string("image-missing").expect("Failed to set default icon"));
image.set_from_gicon(&icon);
image.set_tooltip_text(None);
image
} else {
eprintln!("failed to load image");
let image = Image::new();
image.set_tooltip_text(None);
image
}
}
pub fn set_saved(&self, is_saved: bool) {
let imp = imp::DockObject::from_instance(self);
imp.saved.replace(is_saved);
}
pub fn from_search_results(results: BoxedWindowList) -> Self {
let appinfo = if let Some(first) = results.0.get(0) {
xdg::BaseDirectories::new()
.expect("could not access XDG Base directory")
.get_data_dirs()
.iter_mut()
.filter_map(|xdg_data_path| {
xdg_data_path.push("applications");
std::fs::read_dir(xdg_data_path).ok()
})
.flatten()
.filter_map(|dir_entry| {
if let Ok(dir_entry) = dir_entry {
if let Some(path) = dir_entry.path().file_name() {
if let Some(path) = path.to_str() {
if let Some(app_info) = gio::DesktopAppInfo::new(path) {
if app_info.should_show()
&& first.description.as_str() == app_info.name().as_str()
{
return Some(app_info);
}
}
}
}
}
None
})
.next()
} else {
None
};
// dbg!(&appinfo);
Object::new(&[("appinfo", &appinfo), ("active", &results)])
.expect("Failed to create `DockObject`.")
}
pub fn set_popover(&self, b: bool) {
let imp = imp::DockObject::from_instance(self);
imp.popover.replace(b);
}
}
#[derive(Clone, Debug, Default, glib::Boxed)]
#[boxed_type(name = "BoxedDockObject")]
pub struct BoxedDockObject(pub Option<DockObject>);