Better plugin creation interface
This commit is contained in:
parent
a5a597611e
commit
0cd68cb58c
@ -26,7 +26,10 @@ pub trait LauncherPlugin {
|
||||
// Only search/use this plugin if the prefix was typed
|
||||
fn by_prefix_only(&self) -> bool;
|
||||
// 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>>;
|
||||
}
|
||||
|
||||
|
@ -154,7 +154,7 @@ impl PluginConfig {
|
||||
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 {
|
||||
quote! {
|
||||
fn default_list(&self) -> Vec<Box<dyn waycast_core::LauncherListItem>> {
|
||||
@ -162,14 +162,11 @@ impl PluginConfig {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
quote! {
|
||||
fn default_list(&self) -> Vec<Box<dyn waycast_core::LauncherListItem>> {
|
||||
Vec::new()
|
||||
}
|
||||
}
|
||||
// Don't generate default_list method - user must implement it themselves
|
||||
quote! {}
|
||||
};
|
||||
|
||||
// Generate filter method
|
||||
// Generate filter method only if explicitly specified in config
|
||||
let filter_method = if let Some(ref filter_fn) = self.filter_fn {
|
||||
quote! {
|
||||
fn filter(&self, query: &str) -> Vec<Box<dyn waycast_core::LauncherListItem>> {
|
||||
@ -177,11 +174,8 @@ impl PluginConfig {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
quote! {
|
||||
fn filter(&self, query: &str) -> Vec<Box<dyn waycast_core::LauncherListItem>> {
|
||||
Vec::new()
|
||||
}
|
||||
}
|
||||
// Don't generate filter method - user must implement it themselves
|
||||
quote! {}
|
||||
};
|
||||
|
||||
quote! {
|
||||
|
@ -79,32 +79,7 @@ pub fn get_desktop_entries() -> Vec<DesktopEntry> {
|
||||
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;
|
||||
|
||||
@ -119,9 +94,34 @@ impl LauncherPlugin for DrunPlugin {
|
||||
name: "drun",
|
||||
priority: 1000,
|
||||
description: "List and launch an installed application",
|
||||
prefix: "app",
|
||||
default_list: drun_default_list,
|
||||
filter: drun_filter
|
||||
prefix: "app"
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -192,9 +192,7 @@ impl LauncherPlugin for FileSearchPlugin {
|
||||
name: "Files",
|
||||
priority: 500,
|
||||
description: "Search and open files",
|
||||
prefix: "f",
|
||||
default_list: file_search_default_list,
|
||||
filter: file_search_filter
|
||||
prefix: "f"
|
||||
}
|
||||
|
||||
fn init(&self) {
|
||||
@ -214,35 +212,33 @@ impl LauncherPlugin for FileSearchPlugin {
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
fn file_search_default_list(_plugin: &FileSearchPlugin) -> Vec<Box<dyn LauncherListItem>> {
|
||||
Vec::new()
|
||||
}
|
||||
fn filter(&self, query: &str) -> Vec<Box<dyn LauncherListItem>> {
|
||||
if query.is_empty() {
|
||||
return self.default_list();
|
||||
}
|
||||
|
||||
fn file_search_filter(plugin: &FileSearchPlugin, query: &str) -> Vec<Box<dyn LauncherListItem>> {
|
||||
if query.is_empty() {
|
||||
return file_search_default_list(plugin);
|
||||
}
|
||||
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
|
||||
if let Ok(files) = plugin.files.try_lock() {
|
||||
for f in files.iter() {
|
||||
if let Some(file_name) = f.path.file_name() {
|
||||
let cmp = file_name.to_string_lossy().to_lowercase();
|
||||
if cmp.contains(&query.to_lowercase()) {
|
||||
entries.push(Box::new(f.clone()));
|
||||
// Try to get files without blocking - if indexing is still in progress, return empty
|
||||
if let Ok(files) = self.files.try_lock() {
|
||||
for f in files.iter() {
|
||||
if let Some(file_name) = f.path.file_name() {
|
||||
let cmp = file_name.to_string_lossy().to_lowercase();
|
||||
if cmp.contains(&query.to_lowercase()) {
|
||||
entries.push(Box::new(f.clone()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
entries
|
||||
entries
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
pub fn new() -> FileSearchPlugin {
|
||||
FileSearchPlugin::new()
|
||||
}
|
@ -81,9 +81,7 @@ impl LauncherPlugin for ProjectsPlugin {
|
||||
name: "Projects",
|
||||
priority: 800,
|
||||
description: "Search and open code projects",
|
||||
prefix: "proj",
|
||||
default_list: projects_default_list,
|
||||
filter: projects_filter
|
||||
prefix: "proj"
|
||||
}
|
||||
|
||||
fn init(&self) {
|
||||
@ -133,35 +131,30 @@ impl LauncherPlugin for ProjectsPlugin {
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
fn projects_default_list(_plugin: &ProjectsPlugin) -> Vec<Box<dyn LauncherListItem>> {
|
||||
Vec::new()
|
||||
}
|
||||
fn filter(&self, query: &str) -> Vec<Box<dyn LauncherListItem>> {
|
||||
if query.is_empty() {
|
||||
return self.default_list();
|
||||
}
|
||||
|
||||
fn projects_filter(plugin: &ProjectsPlugin, query: &str) -> Vec<Box<dyn LauncherListItem>> {
|
||||
if query.is_empty() {
|
||||
return projects_default_list(plugin);
|
||||
}
|
||||
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
|
||||
if let Ok(files) = plugin.files.try_lock() {
|
||||
for f in files.iter() {
|
||||
if let Some(file_name) = f.path.file_name() {
|
||||
let cmp = file_name.to_string_lossy().to_lowercase();
|
||||
if cmp.contains(&query.to_lowercase()) {
|
||||
entries.push(Box::new(f.clone()));
|
||||
// Try to get files without blocking - if indexing is still in progress, return empty
|
||||
if let Ok(files) = self.files.try_lock() {
|
||||
for f in files.iter() {
|
||||
if let Some(file_name) = f.path.file_name() {
|
||||
let cmp = file_name.to_string_lossy().to_lowercase();
|
||||
if cmp.contains(&query.to_lowercase()) {
|
||||
entries.push(Box::new(f.clone()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
entries
|
||||
}
|
||||
|
||||
entries
|
||||
}
|
||||
|
||||
|
||||
pub fn new() -> ProjectsPlugin {
|
||||
ProjectsPlugin {
|
||||
search_paths: Vec::new(),
|
||||
|
Loading…
x
Reference in New Issue
Block a user