Plugins baybe

This commit is contained in:
Javier Feliz 2025-09-03 20:25:37 -04:00
parent 1f569c461c
commit e1720d8e31
3 changed files with 40 additions and 35 deletions

View File

@ -13,14 +13,14 @@ pub trait LauncherListItem {
} }
pub trait LauncherPlugin { pub trait LauncherPlugin {
fn name() -> String; fn name(&self) -> String;
fn priority() -> i32; fn priority(&self) -> i32;
fn description() -> Option<String>; fn description(&self) -> Option<String>;
// Prefix to isolate results to only use this plugin // Prefix to isolate results to only use this plugin
fn prefix() -> Option<String>; fn prefix(&self) -> Option<String>;
// Only search/use this plugin if the prefix was typed // 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 // Actual item searching functions
fn default_list() -> Vec<Box<dyn LauncherListItem>>; fn default_list(&self) -> Vec<Box<dyn LauncherListItem>>;
fn filter(query: &str) -> Vec<Box<dyn LauncherListItem>>; fn filter(&self, query: &str) -> Vec<Box<dyn LauncherListItem>>;
} }

View File

@ -11,7 +11,7 @@ use layerShell::LayerShell;
use std::cell::RefCell; use std::cell::RefCell;
use std::path::PathBuf; use std::path::PathBuf;
use std::rc::Rc; use std::rc::Rc;
use waycast::{LauncherListItem, LauncherPlugin, drun}; use waycast::{LauncherListItem, LauncherPlugin, drun, plugins};
struct AppModel { struct AppModel {
window: ApplicationWindow, window: ApplicationWindow,
@ -65,7 +65,7 @@ impl ListItem {
} }
impl AppModel { impl AppModel {
fn new(app: &Application) -> Rc<RefCell<Self>> { fn new(app: &Application) -> Rc<RefCell<AppModel>> {
let window = ApplicationWindow::builder() let window = ApplicationWindow::builder()
.application(app) .application(app)
.title("Waycast") .title("Waycast")
@ -106,14 +106,19 @@ impl AppModel {
let entries = drun::all(); let entries = drun::all();
let model = Rc::new(RefCell::new(AppModel { let plugins: Vec<Box<dyn LauncherPlugin>> = vec![Box::from(plugins::drun::DrunPlugin {})];
let model: Rc<RefCell<AppModel>> = Rc::new(RefCell::new(AppModel {
window, window,
list_box: list_box.clone(), list_box: list_box.clone(),
entries, entries,
plugins,
})); }));
// Populate the list // Populate the list
model.borrow().populate_list(); {
let model_ref = model.borrow();
model_ref.populate_list();
}
// Connect search input signal // Connect search input signal
let model_clone = model.clone(); let model_clone = model.clone();
@ -139,29 +144,29 @@ impl AppModel {
model model
} }
fn populate_list(&self) { fn clear_list_ui(&self) {
// Clear existing items
while let Some(child) = self.list_box.first_child() { while let Some(child) = self.list_box.first_child() {
self.list_box.remove(&child); self.list_box.remove(&child);
} }
}
for entry in &self.entries { fn populate_list(&self) {
let list_item = ListItem::new(entry.title(), entry.icon()); self.clear_list_ui();
let widget = list_item.create_widget();
self.list_box.append(&widget); 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) { fn filter_list(&self, query: &str) {
// Clear existing items self.clear_list_ui();
while let Some(child) = self.list_box.first_child() {
self.list_box.remove(&child);
}
let query_lower = query.to_lowercase(); for plugin in &self.plugins {
for entry in &self.entries { for entry in plugin.filter(query) {
let title_lower = entry.title().to_lowercase();
if query.is_empty() || title_lower.contains(&query_lower) {
let list_item = ListItem::new(entry.title(), entry.icon()); let list_item = ListItem::new(entry.title(), entry.icon());
let widget = list_item.create_widget(); let widget = list_item.create_widget();
self.list_box.append(&widget); self.list_box.append(&widget);

View File

@ -82,32 +82,32 @@ pub fn get_desktop_entries() -> Vec<DesktopEntry> {
entries entries
} }
struct DrunPlugin {} pub struct DrunPlugin {}
impl LauncherPlugin for DrunPlugin { impl LauncherPlugin for DrunPlugin {
fn name() -> String { fn name(&self) -> String {
return String::from("drun"); return String::from("drun");
} }
fn priority() -> i32 { fn priority(&self) -> i32 {
return 1000; return 1000;
} }
fn description() -> Option<String> { fn description(&self) -> Option<String> {
return Some(String::from("List and launch an installed application")); return Some(String::from("List and launch an installed application"));
} }
// Prefix to isolate results to only use this plugin // Prefix to isolate results to only use this plugin
fn prefix() -> Option<String> { fn prefix(&self) -> Option<String> {
return Some(String::from("app")); return Some(String::from("app"));
} }
// Only search/use this plugin if the prefix was typed // Only search/use this plugin if the prefix was typed
fn by_prefix_only() -> bool { fn by_prefix_only(&self) -> bool {
return false; return false;
} }
// Actual item searching functions // Actual item searching functions
fn default_list() -> Vec<Box<dyn LauncherListItem>> { fn default_list(&self) -> Vec<Box<dyn LauncherListItem>> {
let mut entries: Vec<Box<dyn LauncherListItem>> = Vec::new(); let mut entries: Vec<Box<dyn LauncherListItem>> = Vec::new();
for e in get_desktop_entries() { for e in get_desktop_entries() {
@ -117,14 +117,14 @@ impl LauncherPlugin for DrunPlugin {
entries entries
} }
fn filter(query: &str) -> Vec<Box<dyn LauncherListItem>> { fn filter(&self, query: &str) -> Vec<Box<dyn LauncherListItem>> {
if query.is_empty() { if query.is_empty() {
return DrunPlugin::default_list(); return self.default_list();
} }
let query_lower = query.to_lowercase(); let query_lower = query.to_lowercase();
let mut entries: Vec<Box<dyn LauncherListItem>> = Vec::new(); let mut entries: Vec<Box<dyn LauncherListItem>> = Vec::new();
for entry in DrunPlugin::default_list() { for entry in self.default_list() {
let title_lower = entry.title().to_lowercase(); let title_lower = entry.title().to_lowercase();
if title_lower.contains(&query_lower) { if title_lower.contains(&query_lower) {
entries.push(entry); entries.push(entry);