From e1720d8e3131f361e0ea321a93e3ff82afc9aade Mon Sep 17 00:00:00 2001 From: Javier Feliz Date: Wed, 3 Sep 2025 20:25:37 -0400 Subject: [PATCH] Plugins baybe --- src/lib.rs | 14 +++++++------- src/main.rs | 41 +++++++++++++++++++++++------------------ src/plugins/drun.rs | 20 ++++++++++---------- 3 files changed, 40 insertions(+), 35 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 26ff83a..3534528 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,14 +13,14 @@ pub trait LauncherListItem { } pub trait LauncherPlugin { - fn name() -> String; - fn priority() -> i32; - fn description() -> Option; + fn name(&self) -> String; + fn priority(&self) -> i32; + fn description(&self) -> Option; // Prefix to isolate results to only use this plugin - fn prefix() -> Option; + fn prefix(&self) -> Option; // Only search/use this plugin if the prefix was typed - fn by_prefix_only() -> bool; + fn by_prefix_only(&self) -> bool; // Actual item searching functions - fn default_list() -> Vec>; - fn filter(query: &str) -> Vec>; + fn default_list(&self) -> Vec>; + fn filter(&self, query: &str) -> Vec>; } diff --git a/src/main.rs b/src/main.rs index 904c764..298faa2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,7 +11,7 @@ use layerShell::LayerShell; use std::cell::RefCell; use std::path::PathBuf; use std::rc::Rc; -use waycast::{LauncherListItem, LauncherPlugin, drun}; +use waycast::{LauncherListItem, LauncherPlugin, drun, plugins}; struct AppModel { window: ApplicationWindow, @@ -65,7 +65,7 @@ impl ListItem { } impl AppModel { - fn new(app: &Application) -> Rc> { + fn new(app: &Application) -> Rc> { let window = ApplicationWindow::builder() .application(app) .title("Waycast") @@ -106,14 +106,19 @@ impl AppModel { let entries = drun::all(); - let model = Rc::new(RefCell::new(AppModel { + let plugins: Vec> = vec![Box::from(plugins::drun::DrunPlugin {})]; + let model: Rc> = Rc::new(RefCell::new(AppModel { window, list_box: list_box.clone(), entries, + plugins, })); // Populate the list - model.borrow().populate_list(); + { + let model_ref = model.borrow(); + model_ref.populate_list(); + } // Connect search input signal let model_clone = model.clone(); @@ -139,29 +144,29 @@ impl AppModel { model } - fn populate_list(&self) { - // Clear existing items + fn clear_list_ui(&self) { while let Some(child) = self.list_box.first_child() { self.list_box.remove(&child); } + } - for entry in &self.entries { - let list_item = ListItem::new(entry.title(), entry.icon()); - let widget = list_item.create_widget(); - self.list_box.append(&widget); + fn populate_list(&self) { + self.clear_list_ui(); + + for plugin in &self.plugins { + for entry in plugin.default_list() { + let list_item = ListItem::new(entry.title(), entry.icon()); + let widget = list_item.create_widget(); + self.list_box.append(&widget); + } } } fn filter_list(&self, query: &str) { - // Clear existing items - while let Some(child) = self.list_box.first_child() { - self.list_box.remove(&child); - } + self.clear_list_ui(); - let query_lower = query.to_lowercase(); - for entry in &self.entries { - let title_lower = entry.title().to_lowercase(); - if query.is_empty() || title_lower.contains(&query_lower) { + for plugin in &self.plugins { + for entry in plugin.filter(query) { let list_item = ListItem::new(entry.title(), entry.icon()); let widget = list_item.create_widget(); self.list_box.append(&widget); diff --git a/src/plugins/drun.rs b/src/plugins/drun.rs index 67c2584..31fe328 100644 --- a/src/plugins/drun.rs +++ b/src/plugins/drun.rs @@ -82,32 +82,32 @@ pub fn get_desktop_entries() -> Vec { entries } -struct DrunPlugin {} +pub struct DrunPlugin {} impl LauncherPlugin for DrunPlugin { - fn name() -> String { + fn name(&self) -> String { return String::from("drun"); } - fn priority() -> i32 { + fn priority(&self) -> i32 { return 1000; } - fn description() -> Option { + fn description(&self) -> Option { return Some(String::from("List and launch an installed application")); } // Prefix to isolate results to only use this plugin - fn prefix() -> Option { + fn prefix(&self) -> Option { return Some(String::from("app")); } // Only search/use this plugin if the prefix was typed - fn by_prefix_only() -> bool { + fn by_prefix_only(&self) -> bool { return false; } // Actual item searching functions - fn default_list() -> Vec> { + fn default_list(&self) -> Vec> { let mut entries: Vec> = Vec::new(); for e in get_desktop_entries() { @@ -117,14 +117,14 @@ impl LauncherPlugin for DrunPlugin { entries } - fn filter(query: &str) -> Vec> { + fn filter(&self, query: &str) -> Vec> { if query.is_empty() { - return DrunPlugin::default_list(); + return self.default_list(); } let query_lower = query.to_lowercase(); let mut entries: Vec> = Vec::new(); - for entry in DrunPlugin::default_list() { + for entry in self.default_list() { let title_lower = entry.title().to_lowercase(); if title_lower.contains(&query_lower) { entries.push(entry);