This commit is contained in:
Javier Feliz 2025-09-03 19:00:43 -04:00
parent bf252ed6ee
commit fa0af222f8
2 changed files with 33 additions and 103 deletions

View File

@ -1,26 +1,12 @@
use crate::util::files;
use crate::{LaunchError, LauncherListItem}; use crate::{LaunchError, LauncherListItem};
use gio::{AppInfo, DesktopAppInfo, Icon, prelude::*}; use gio::{AppInfo, DesktopAppInfo, Icon, prelude::*};
use std::env;
use std::path::{Path, PathBuf};
#[derive(Debug)] #[derive(Debug)]
pub struct DesktopEntry { pub struct DesktopEntry {
id: String, id: String,
name: String, name: String,
generic_name: Option<glib::GString>,
description: Option<glib::GString>, description: Option<glib::GString>,
icon: Option<Icon>, icon: Option<Icon>,
exec: Option<glib::GString>,
path: PathBuf,
no_display: bool,
is_terminal_app: bool,
}
impl DesktopEntry {
pub fn path(&self) -> PathBuf {
self.path.clone()
}
} }
impl LauncherListItem for DesktopEntry { impl LauncherListItem for DesktopEntry {
@ -37,7 +23,7 @@ impl LauncherListItem for DesktopEntry {
} }
fn execute(&self) -> Result<(), LaunchError> { fn execute(&self) -> Result<(), LaunchError> {
if let Some(di) = DesktopAppInfo::from_filename(&self.path) { if let Some(di) = DesktopAppInfo::new(&self.id) {
let app: AppInfo = di.upcast(); let app: AppInfo = di.upcast();
let ctx = gio::AppLaunchContext::new(); let ctx = gio::AppLaunchContext::new();
if app.launch(&[], Some(&ctx)).ok().is_none() { if app.launch(&[], Some(&ctx)).ok().is_none() {
@ -69,55 +55,27 @@ impl LauncherListItem for DesktopEntry {
} }
} }
fn get_desktop_files() -> Vec<PathBuf> {
let dir_envs =
env::var("XDG_DATA_DIRS").expect("XDG_DATA_DIRS not set. Please fix your environment");
let dir_string = String::from(dir_envs);
let dirs = dir_string.split(":");
let mut files = Vec::new();
for dir in dirs {
// println!("Data dir: {}", dir);
let apps_path = Path::new(dir).join("applications");
let desktop_files = match files::get_files_with_extension(&apps_path, "desktop") {
Ok(files) => files,
Err(_) => {
// eprintln!("Error reading {dir}: {err}");
continue;
}
};
for f in desktop_files {
files.push(f);
}
}
return files;
}
pub fn get_desktop_entries() -> Vec<DesktopEntry> { pub fn get_desktop_entries() -> Vec<DesktopEntry> {
let mut entries = Vec::new(); let mut entries = Vec::new();
for f in get_desktop_files() { for i in gio::AppInfo::all() {
if let Some(info) = DesktopAppInfo::from_filename(&f) { let info: gio::DesktopAppInfo;
if info.is_nodisplay() { match i.downcast_ref::<gio::DesktopAppInfo>() {
continue; Some(inf) => info = inf.to_owned(),
} None => continue,
let de = DesktopEntry {
id: info.id().unwrap_or_default().to_string(),
name: info.name().to_string(),
generic_name: info.generic_name(),
description: info.description(),
icon: info.icon(),
exec: info.string("Exec"),
no_display: info.is_nodisplay(),
path: f.clone(),
is_terminal_app: info.boolean("Terminal"),
};
entries.push(de);
} }
if !info.should_show() {
continue;
}
let de = DesktopEntry {
id: info.id().unwrap_or_default().to_string(),
name: info.display_name().to_string(),
description: info.description(),
icon: info.icon(),
};
entries.push(de);
} }
entries entries

View File

@ -278,51 +278,23 @@ fn find_icon_file(icon_name: &str, size: &str, theme_name: &str) -> Option<std::
} }
fn main() { fn main() {
let app = Application::builder() // let app = Application::builder()
.application_id("dev.thegrind.waycast") // .application_id("dev.thegrind.waycast")
.build(); // .build();
app.connect_activate(|app| { // app.connect_activate(|app| {
let model = AppModel::new(app); // let model = AppModel::new(app);
model.borrow().show(); // model.borrow().show();
}); // });
app.run(); // app.run();
// gtk::init().expect("Failed to init GTK"); gtk::init().expect("Failed to init GTK");
// let display = gtk::gdk::Display::default().unwrap(); let display = gtk::gdk::Display::default().unwrap();
// let icon_theme = gtk::IconTheme::for_display(&display); let icon_theme = gtk::IconTheme::for_display(&display);
// println!("Current icon theme: {:?}", icon_theme.theme_name()); println!("Current icon theme: {:?}", icon_theme.theme_name());
// for info in gio::AppInfo::all() {
// if !info.should_show() {
// continue;
// }
// println!("App: {}", info.display_name()); for p in icon_theme.search_path() {
// if let Some(icon) = info.icon() { println!("{}", p.to_string_lossy());
// if let Some(x) = icon.to_string() { }
// println!("Icon: {}", x.to_string());
// if let Some(path) = find_icon_file(&x, "48", icon_theme.theme_name().as_str()) {
// println!("Found at: {}", path.to_string_lossy());
// } else {
// println!("Not found");
// }
// }
// // if let Ok(ti) = icon.clone().downcast::<gio::ThemedIcon>() {
// // // ThemedIcon may have multiple names, we take the first
// // if let Some(name) = ti.names().first() {
// // println!("Themed: {}", name.to_string());
// // }
// // }
// // if let Ok(fi) = icon.clone().downcast::<gio::FileIcon>() {
// // if let Some(path) = fi.file().path() {
// // println!("File: {}", path.to_string_lossy().to_string());
// // }
// // }
// }
// println!("\n");
// }
// let appinfo = gio::AppInfo::all();
} }