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 {
fn name() -> String;
fn priority() -> i32;
fn description() -> Option<String>;
fn name(&self) -> String;
fn priority(&self) -> i32;
fn description(&self) -> Option<String>;
// 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
fn by_prefix_only() -> bool;
fn by_prefix_only(&self) -> bool;
// Actual item searching functions
fn default_list() -> Vec<Box<dyn LauncherListItem>>;
fn filter(query: &str) -> Vec<Box<dyn LauncherListItem>>;
fn default_list(&self) -> 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::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<RefCell<Self>> {
fn new(app: &Application) -> Rc<RefCell<AppModel>> {
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<Box<dyn LauncherPlugin>> = vec![Box::from(plugins::drun::DrunPlugin {})];
let model: Rc<RefCell<AppModel>> = 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 {
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);
}
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) {
fn filter_list(&self, query: &str) {
self.clear_list_ui();
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);

View File

@ -82,32 +82,32 @@ pub fn get_desktop_entries() -> Vec<DesktopEntry> {
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<String> {
fn description(&self) -> 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> {
fn prefix(&self) -> Option<String> {
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<Box<dyn LauncherListItem>> {
fn default_list(&self) -> Vec<Box<dyn LauncherListItem>> {
let mut entries: Vec<Box<dyn LauncherListItem>> = Vec::new();
for e in get_desktop_entries() {
@ -117,14 +117,14 @@ impl LauncherPlugin for DrunPlugin {
entries
}
fn filter(query: &str) -> Vec<Box<dyn LauncherListItem>> {
fn filter(&self, query: &str) -> Vec<Box<dyn LauncherListItem>> {
if query.is_empty() {
return DrunPlugin::default_list();
return self.default_list();
}
let query_lower = query.to_lowercase();
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();
if title_lower.contains(&query_lower) {
entries.push(entry);