This commit is contained in:
Javier Feliz 2025-09-03 20:06:31 -04:00
parent 7194709955
commit 1f569c461c
5 changed files with 173 additions and 3 deletions

5
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,5 @@
{
"search.exclude": {
"**/target": true
}
}

View File

@ -1,4 +1,5 @@
pub mod drun;
pub mod plugins;
pub mod util;
pub enum LaunchError {
CouldNotLaunch(String),
@ -10,3 +11,16 @@ pub trait LauncherListItem {
fn execute(&self) -> Result<(), LaunchError>;
fn icon(&self) -> String;
}
pub trait LauncherPlugin {
fn name() -> String;
fn priority() -> i32;
fn description() -> Option<String>;
// Prefix to isolate results to only use this plugin
fn prefix() -> Option<String>;
// Only search/use this plugin if the prefix was typed
fn by_prefix_only() -> bool;
// Actual item searching functions
fn default_list() -> Vec<Box<dyn LauncherListItem>>;
fn filter(query: &str) -> Vec<Box<dyn LauncherListItem>>;
}

View File

@ -3,20 +3,21 @@ use gtk::gdk::Texture;
use gtk::gdk_pixbuf::Pixbuf;
use gtk::prelude::*;
use gtk::{
Application, ApplicationWindow, Box as GtkBox, Entry, IconTheme, Image, Label, ListBox,
Orientation, ScrolledWindow,
Application, ApplicationWindow, Box as GtkBox, Entry, EventControllerKey, IconTheme, Image,
Label, ListBox, Orientation, ScrolledWindow,
};
use gtk4_layer_shell as layerShell;
use layerShell::LayerShell;
use std::cell::RefCell;
use std::path::PathBuf;
use std::rc::Rc;
use waycast::{LauncherListItem, drun};
use waycast::{LauncherListItem, LauncherPlugin, drun};
struct AppModel {
window: ApplicationWindow,
list_box: ListBox,
entries: Vec<Box<dyn LauncherListItem>>,
plugins: Vec<Box<dyn LauncherPlugin>>,
}
struct ListItem {
@ -122,6 +123,19 @@ impl AppModel {
model_clone.borrow().filter_list(&query);
});
// Add ESC key handler to close window
let key_controller = EventControllerKey::new();
let window_clone = model.borrow().window.clone();
key_controller.connect_key_pressed(move |_controller, keyval, _keycode, _state| {
if keyval == gtk::gdk::Key::Escape {
window_clone.close();
gtk::glib::Propagation::Stop
} else {
gtk::glib::Propagation::Proceed
}
});
model.borrow().window.add_controller(key_controller);
model
}

136
src/plugins/drun.rs Normal file
View File

@ -0,0 +1,136 @@
use crate::LauncherPlugin;
use crate::{LaunchError, LauncherListItem};
use gio::{AppInfo, DesktopAppInfo, Icon, prelude::*};
#[derive(Debug)]
pub struct DesktopEntry {
id: String,
name: String,
description: Option<glib::GString>,
icon: Option<Icon>,
}
impl LauncherListItem for DesktopEntry {
fn title(&self) -> String {
return self.name.to_owned();
}
fn description(&self) -> Option<String> {
if let Some(glib_string) = &self.description {
return Some(glib_string.to_string().to_owned());
}
return None;
}
fn execute(&self) -> Result<(), LaunchError> {
if let Some(di) = DesktopAppInfo::new(&self.id) {
let app: AppInfo = di.upcast();
let ctx = gio::AppLaunchContext::new();
if app.launch(&[], Some(&ctx)).ok().is_none() {
return Err(LaunchError::CouldNotLaunch("App failed to launch".into()));
};
return Ok(());
}
return Err(LaunchError::CouldNotLaunch("Invalid .desktop entry".into()));
}
fn icon(&self) -> String {
if let Some(icon) = &self.icon {
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() {
return name.to_string();
}
}
if let Ok(fi) = icon.clone().downcast::<gio::FileIcon>() {
if let Some(path) = fi.file().path() {
return path.to_string_lossy().to_string();
}
}
}
return "application-x-executable".into();
}
}
pub fn get_desktop_entries() -> Vec<DesktopEntry> {
let mut entries = Vec::new();
for i in gio::AppInfo::all() {
let info: gio::DesktopAppInfo;
match i.downcast_ref::<gio::DesktopAppInfo>() {
Some(inf) => info = inf.to_owned(),
None => continue,
}
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
}
struct DrunPlugin {}
impl LauncherPlugin for DrunPlugin {
fn name() -> String {
return String::from("drun");
}
fn priority() -> i32 {
return 1000;
}
fn description() -> Option<String> {
return Some(String::from("List and launch an installed application"));
}
// Prefix to isolate results to only use this plugin
fn prefix() -> Option<String> {
return Some(String::from("app"));
}
// Only search/use this plugin if the prefix was typed
fn by_prefix_only() -> bool {
return false;
}
// Actual item searching functions
fn default_list() -> Vec<Box<dyn LauncherListItem>> {
let mut entries: Vec<Box<dyn LauncherListItem>> = Vec::new();
for e in get_desktop_entries() {
entries.push(Box::new(e));
}
entries
}
fn filter(query: &str) -> Vec<Box<dyn LauncherListItem>> {
if query.is_empty() {
return DrunPlugin::default_list();
}
let query_lower = query.to_lowercase();
let mut entries: Vec<Box<dyn LauncherListItem>> = Vec::new();
for entry in DrunPlugin::default_list() {
let title_lower = entry.title().to_lowercase();
if title_lower.contains(&query_lower) {
entries.push(entry);
}
}
entries
}
}

1
src/plugins/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod drun;