handle favoriting from the dock popover menu

This commit is contained in:
Ashley Wulber 2022-01-04 16:53:28 -05:00
parent 69b7ab5906
commit 849333df3d
4 changed files with 71 additions and 4 deletions

View file

@ -13,9 +13,9 @@ use crate::utils::BoxedWindowList;
// Object holding the state
#[derive(Default)]
pub struct DockObject {
appinfo: RefCell<Option<DesktopAppInfo>>,
active: RefCell<BoxedWindowList>,
saved: Cell<bool>,
pub(super) appinfo: RefCell<Option<DesktopAppInfo>>,
pub(super) active: RefCell<BoxedWindowList>,
pub(super) saved: Cell<bool>,
pub(super) popover: Cell<bool>,
}

View file

@ -1,6 +1,7 @@
use std::path::Path;
use gdk4::glib::Object;
use gdk4::prelude::FileExt;
use gdk4::subclass::prelude::ObjectSubclassExt;
use gio::DesktopAppInfo;
use gtk4::glib;
@ -36,6 +37,22 @@ impl DockObject {
None
}
pub fn get_name(&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 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.iter().next() {
xdg::BaseDirectories::new()

View file

@ -245,7 +245,15 @@ impl DockPopover {
let self_ = self.clone();
favorite_item.connect_clicked(glib::clone!(@weak dock_object => move |_| {
println!("TODO handling favorite");
let saved = dock_object.property("saved").expect("DockObject must have saved property").get::<bool>().expect("Failed to convert value to bool");
glib::MainContext::default().spawn_local(async move {
if let Some(tx) = TX.get() {
if let Some(name) = dock_object.get_name() {
let _ = tx.send(Event::Favorite((name.into(), !saved))).await;
}
}
});
self_.emit_hide();
}));

View file

@ -42,6 +42,7 @@ pub enum Event {
WindowList(Vec<Item>),
Activate((u32, u32)),
Close((u32, u32)),
Favorite((String, bool)),
RefreshFromCache,
}
@ -143,6 +144,47 @@ fn main() {
.await
.expect("Failed to close selected window");
}
Event::Favorite((name, should_favorite)) => {
dbg!(&name);
dbg!(should_favorite);
let saved_app_model = window.model(DockListType::Saved);
let active_app_model = window.model(DockListType::Active);
if should_favorite {
let mut cur: u32 = 0;
let mut index: Option<u32> = None;
while let Some(item) = active_app_model.item(cur) {
if let Ok(cur_dock_object) = item.downcast::<DockObject>() {
if cur_dock_object.get_name() == Some(name.clone()) {
cur_dock_object.set_saved(true);
index = Some(cur);
}
}
cur += 1;
}
if let Some(index) = index {
let object = active_app_model.item(index).unwrap();
active_app_model.remove(index);
saved_app_model.append(&object);
}
} else {
let mut cur: u32 = 0;
let mut index: Option<u32> = None;
while let Some(item) = saved_app_model.item(cur) {
if let Ok(cur_dock_object) = item.downcast::<DockObject>() {
if cur_dock_object.get_name() == Some(name.clone()) {
cur_dock_object.set_saved(false);
index = Some(cur);
}
}
cur += 1;
}
if let Some(index) = index {
let object = saved_app_model.item(index).unwrap();
saved_app_model.remove(index);
active_app_model.append(&object);
}
}
}
Event::RefreshFromCache => {
// println!("refreshing model from cache");
let cached_results = cached_results.as_ref();