Skip to content

Commit

Permalink
♻️ capsule plugin spawn method and move plugin trait to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
friedow committed Oct 30, 2023
1 parent e123f0b commit 427978b
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 142 deletions.
2 changes: 1 addition & 1 deletion client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl Application for Centerpiece {
}
_ => None,
}),
crate::plugin::windows::WindowsPlugin::spawn(),
crate::plugin::utils::spawn::<crate::plugin::windows::WindowsPlugin>(),
crate::plugin::applications::Plugin::spawn(),
crate::plugin::brave::progressive_web_apps::Plugin::spawn(),
crate::plugin::git_repositories::Plugin::spawn(),
Expand Down
2 changes: 0 additions & 2 deletions client/src/plugin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,3 @@ pub mod git_repositories;
pub mod resource_monitor;
pub mod utils;
pub mod windows;

pub mod plugin;
106 changes: 0 additions & 106 deletions client/src/plugin/plugin.rs

This file was deleted.

121 changes: 121 additions & 0 deletions client/src/plugin/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,125 @@
use fuzzy_matcher::FuzzyMatcher;
use anyhow::Context;
use iced::futures::StreamExt;

pub fn spawn<PluginType: Plugin + std::marker::Send + 'static>(
) -> iced::Subscription<crate::Message> {
return iced::subscription::channel(
std::any::TypeId::of::<PluginType>(),
100,
|plugin_channel_out| async move {
let mut plugin = PluginType::new();

let main_loop_result = plugin.main(plugin_channel_out).await;
if let Err(error) = main_loop_result {
log::error!(
target: PluginType::id(),
"{:?}", error,
);
panic!();
}

loop {}
},
);
}

#[async_trait::async_trait]
pub trait Plugin {
fn id() -> &'static str;
fn priority() -> u32;
fn title() -> &'static str;

fn new() -> Self;

fn entries(&self) -> Vec<crate::model::Entry>;

fn plugin(
&self,
app_channel_out: &mut iced::futures::channel::mpsc::Sender<crate::model::PluginRequest>,
) -> crate::model::Plugin {
return crate::model::Plugin {
id: String::from(Self::id()),
priority: Self::priority(),
title: String::from(Self::title()),
app_channel_out: app_channel_out.clone(),
entries: self.entries(),
};
}

async fn main(
&mut self,
mut plugin_channel_out: iced::futures::channel::mpsc::Sender<crate::Message>,
) -> anyhow::Result<()> {
let (mut app_channel_out, mut plugin_channel_in) =
iced::futures::channel::mpsc::channel(100);
self.register_plugin(&mut plugin_channel_out, &mut app_channel_out)?;

loop {
self.update(&mut plugin_channel_out, &mut plugin_channel_in)
.await?;
}
}

fn register_plugin(
&mut self,
plugin_channel_out: &mut iced::futures::channel::mpsc::Sender<crate::Message>,
app_channel_out: &mut iced::futures::channel::mpsc::Sender<crate::model::PluginRequest>,
) -> anyhow::Result<()> {
plugin_channel_out
.try_send(crate::Message::RegisterPlugin(self.plugin(app_channel_out)))
.context("Failed to send message to register plugin.")?;

return Ok(());
}

async fn update(
&mut self,
plugin_channel_out: &mut iced::futures::channel::mpsc::Sender<crate::Message>,
plugin_channel_in: &mut iced::futures::channel::mpsc::Receiver<crate::model::PluginRequest>,
) -> anyhow::Result<()> {
let plugin_request = plugin_channel_in.select_next_some().await;

match plugin_request {
crate::model::PluginRequest::Search(query) => {
self.search(&query, plugin_channel_out)?
}
crate::model::PluginRequest::Timeout => (),
crate::model::PluginRequest::Activate(entry_id) => {
self.activate(entry_id, plugin_channel_out)?
}
}

return Ok(());
}

fn search(
&mut self,
query: &String,
plugin_channel_out: &mut iced::futures::channel::mpsc::Sender<crate::Message>,
) -> anyhow::Result<()> {
let filtered_entries = crate::plugin::utils::search(self.entries(), query);

plugin_channel_out
.try_send(crate::Message::UpdateEntries(
String::from(Self::id()),
filtered_entries,
))
.context(format!(
"Failed to send message to update entries while searching for '{}'.",
query
))?;

return Ok(());
}

fn activate(
&mut self,
entry_id: String,
plugin_channel_out: &mut iced::futures::channel::mpsc::Sender<crate::Message>,
) -> anyhow::Result<()>;
}


pub fn search(entries: Vec<crate::model::Entry>, query: &String) -> Vec<crate::model::Entry> {
let matcher = fuzzy_matcher::skim::SkimMatcherV2::default();
Expand Down
38 changes: 5 additions & 33 deletions client/src/plugin/windows.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::plugin::plugin::Plugin;
use crate::plugin::utils::Plugin;
use anyhow::Context;

pub struct WindowsPlugin {
Expand All @@ -7,30 +7,6 @@ pub struct WindowsPlugin {
}

impl WindowsPlugin {
pub fn spawn() -> iced::Subscription<crate::Message> {
let plugin_result = PluginType::new();
if let Err(_) = plugin_result {
return iced::Subscription::<crate::Message>::none()
}

let mut plugin = plugin_result.unwrap();
return iced::subscription::channel(
std::any::TypeId::of::<Self>(),
100,
|plugin_channel_out| async move {
let (app_channel_out, plugin_channel_in) =
iced::futures::channel::mpsc::channel(100);
let main_loop_result = plugin
.main(plugin_channel_out, app_channel_out, plugin_channel_in)
.await;
if let Err(error) = main_loop_result {
Self::log_and_panic(error);
}
loop {}
},
);
}

fn get_window_nodes(node: swayipc::Node) -> Vec<swayipc::Node> {
if !node.nodes.is_empty() {
return node
Expand All @@ -46,10 +22,6 @@ impl WindowsPlugin {

return vec![];
}

fn try_new() -> Result<Self, ()> {
Ok(Self::new())
}
}

impl Plugin for WindowsPlugin {
Expand All @@ -71,15 +43,15 @@ impl Plugin for WindowsPlugin {
let connection_result =
swayipc::Connection::new().context("Failed to establish sway ipc connection.");
if let Err(error) = connection_result {
Self::log_and_panic(error);
panic!("");
log::error!(target: Self::id(), "{:?}", error);
panic!();
}
let mut sway = connection_result.unwrap();

let root_node_result = sway.get_tree().context("Failed to get_tree from sway ipc.");
if let Err(error) = root_node_result {
Self::log_and_panic(error);
panic!("");
log::error!(target: Self::id(), "{:?}", error);
panic!();
}
let root_node = root_node_result.unwrap();

Expand Down

0 comments on commit 427978b

Please sign in to comment.