Better plugin creation interface

This commit is contained in:
Javier Feliz 2025-09-06 01:00:35 -04:00
parent a5a597611e
commit 0cd68cb58c
5 changed files with 80 additions and 94 deletions

View File

@ -26,7 +26,10 @@ pub trait LauncherPlugin {
// Only search/use this plugin if the prefix was typed // Only search/use this plugin if the prefix was typed
fn by_prefix_only(&self) -> bool; fn by_prefix_only(&self) -> bool;
// Actual item searching functions // Actual item searching functions
fn default_list(&self) -> Vec<Box<dyn LauncherListItem>>; fn default_list(&self) -> Vec<Box<dyn LauncherListItem>> {
// Default empty list - plugins can override this
Vec::new()
}
fn filter(&self, query: &str) -> Vec<Box<dyn LauncherListItem>>; fn filter(&self, query: &str) -> Vec<Box<dyn LauncherListItem>>;
} }

View File

@ -154,7 +154,7 @@ impl PluginConfig {
quote! {} quote! {}
}; };
// Generate default_list method // Generate default_list method only if explicitly specified in config
let default_list_method = if let Some(ref default_list_fn) = self.default_list_fn { let default_list_method = if let Some(ref default_list_fn) = self.default_list_fn {
quote! { quote! {
fn default_list(&self) -> Vec<Box<dyn waycast_core::LauncherListItem>> { fn default_list(&self) -> Vec<Box<dyn waycast_core::LauncherListItem>> {
@ -162,14 +162,11 @@ impl PluginConfig {
} }
} }
} else { } else {
quote! { // Don't generate default_list method - user must implement it themselves
fn default_list(&self) -> Vec<Box<dyn waycast_core::LauncherListItem>> { quote! {}
Vec::new()
}
}
}; };
// Generate filter method // Generate filter method only if explicitly specified in config
let filter_method = if let Some(ref filter_fn) = self.filter_fn { let filter_method = if let Some(ref filter_fn) = self.filter_fn {
quote! { quote! {
fn filter(&self, query: &str) -> Vec<Box<dyn waycast_core::LauncherListItem>> { fn filter(&self, query: &str) -> Vec<Box<dyn waycast_core::LauncherListItem>> {
@ -177,11 +174,8 @@ impl PluginConfig {
} }
} }
} else { } else {
quote! { // Don't generate filter method - user must implement it themselves
fn filter(&self, query: &str) -> Vec<Box<dyn waycast_core::LauncherListItem>> { quote! {}
Vec::new()
}
}
}; };
quote! { quote! {

View File

@ -79,32 +79,7 @@ pub fn get_desktop_entries() -> Vec<DesktopEntry> {
entries entries
} }
fn drun_default_list(_plugin: &DrunPlugin) -> 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 drun_filter(plugin: &DrunPlugin, query: &str) -> Vec<Box<dyn LauncherListItem>> {
if query.is_empty() {
return drun_default_list(plugin);
}
let query_lower = query.to_lowercase();
let mut entries: Vec<Box<dyn LauncherListItem>> = Vec::new();
for entry in drun_default_list(plugin) {
let title_lower = entry.title().to_lowercase();
if title_lower.contains(&query_lower) {
entries.push(entry);
}
}
entries
}
pub struct DrunPlugin; pub struct DrunPlugin;
@ -119,9 +94,34 @@ impl LauncherPlugin for DrunPlugin {
name: "drun", name: "drun",
priority: 1000, priority: 1000,
description: "List and launch an installed application", description: "List and launch an installed application",
prefix: "app", prefix: "app"
default_list: drun_default_list, }
filter: drun_filter
fn default_list(&self) -> 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(&self, query: &str) -> Vec<Box<dyn LauncherListItem>> {
if query.is_empty() {
return self.default_list();
}
let query_lower = query.to_lowercase();
let mut entries: Vec<Box<dyn LauncherListItem>> = Vec::new();
for entry in self.default_list() {
let title_lower = entry.title().to_lowercase();
if title_lower.contains(&query_lower) {
entries.push(entry);
}
}
entries
} }
} }

View File

@ -192,9 +192,7 @@ impl LauncherPlugin for FileSearchPlugin {
name: "Files", name: "Files",
priority: 500, priority: 500,
description: "Search and open files", description: "Search and open files",
prefix: "f", prefix: "f"
default_list: file_search_default_list,
filter: file_search_filter
} }
fn init(&self) { fn init(&self) {
@ -214,21 +212,16 @@ impl LauncherPlugin for FileSearchPlugin {
}); });
}); });
} }
}
fn file_search_default_list(_plugin: &FileSearchPlugin) -> Vec<Box<dyn LauncherListItem>> { fn filter(&self, query: &str) -> Vec<Box<dyn LauncherListItem>> {
Vec::new()
}
fn file_search_filter(plugin: &FileSearchPlugin, query: &str) -> Vec<Box<dyn LauncherListItem>> {
if query.is_empty() { if query.is_empty() {
return file_search_default_list(plugin); return self.default_list();
} }
let mut entries: Vec<Box<dyn LauncherListItem>> = Vec::new(); let mut entries: Vec<Box<dyn LauncherListItem>> = Vec::new();
// Try to get files without blocking - if indexing is still in progress, return empty // Try to get files without blocking - if indexing is still in progress, return empty
if let Ok(files) = plugin.files.try_lock() { if let Ok(files) = self.files.try_lock() {
for f in files.iter() { for f in files.iter() {
if let Some(file_name) = f.path.file_name() { if let Some(file_name) = f.path.file_name() {
let cmp = file_name.to_string_lossy().to_lowercase(); let cmp = file_name.to_string_lossy().to_lowercase();
@ -240,9 +233,12 @@ fn file_search_filter(plugin: &FileSearchPlugin, query: &str) -> Vec<Box<dyn Lau
} }
entries entries
}
} }
pub fn new() -> FileSearchPlugin { pub fn new() -> FileSearchPlugin {
FileSearchPlugin::new() FileSearchPlugin::new()
} }

View File

@ -81,9 +81,7 @@ impl LauncherPlugin for ProjectsPlugin {
name: "Projects", name: "Projects",
priority: 800, priority: 800,
description: "Search and open code projects", description: "Search and open code projects",
prefix: "proj", prefix: "proj"
default_list: projects_default_list,
filter: projects_filter
} }
fn init(&self) { fn init(&self) {
@ -133,21 +131,16 @@ impl LauncherPlugin for ProjectsPlugin {
}); });
}); });
} }
}
fn projects_default_list(_plugin: &ProjectsPlugin) -> Vec<Box<dyn LauncherListItem>> { fn filter(&self, query: &str) -> Vec<Box<dyn LauncherListItem>> {
Vec::new()
}
fn projects_filter(plugin: &ProjectsPlugin, query: &str) -> Vec<Box<dyn LauncherListItem>> {
if query.is_empty() { if query.is_empty() {
return projects_default_list(plugin); return self.default_list();
} }
let mut entries: Vec<Box<dyn LauncherListItem>> = Vec::new(); let mut entries: Vec<Box<dyn LauncherListItem>> = Vec::new();
// Try to get files without blocking - if indexing is still in progress, return empty // Try to get files without blocking - if indexing is still in progress, return empty
if let Ok(files) = plugin.files.try_lock() { if let Ok(files) = self.files.try_lock() {
for f in files.iter() { for f in files.iter() {
if let Some(file_name) = f.path.file_name() { if let Some(file_name) = f.path.file_name() {
let cmp = file_name.to_string_lossy().to_lowercase(); let cmp = file_name.to_string_lossy().to_lowercase();
@ -159,9 +152,9 @@ fn projects_filter(plugin: &ProjectsPlugin, query: &str) -> Vec<Box<dyn Launcher
} }
entries entries
}
} }
pub fn new() -> ProjectsPlugin { pub fn new() -> ProjectsPlugin {
ProjectsPlugin { ProjectsPlugin {
search_paths: Vec::new(), search_paths: Vec::new(),