Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚗️ Example plugin try_new() initialization #10

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/src/plugin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ pub mod resource_monitor;
pub mod utils;
pub mod windows;

pub mod plugin;
pub mod plugin;
29 changes: 23 additions & 6 deletions client/src/plugin/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ pub trait Plugin {
self.register_plugin(&mut plugin_channel_out, &mut app_channel_out)?;

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

Expand All @@ -57,19 +58,31 @@ pub trait 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<()> {
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::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)?,
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<()> {
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
Expand All @@ -85,5 +98,9 @@ pub trait Plugin {
return Ok(());
}

fn activate(&mut self, entry_id: String, plugin_channel_out: &mut iced::futures::channel::mpsc::Sender<crate::Message>) -> anyhow::Result<()>;
fn activate(
&mut self,
entry_id: String,
plugin_channel_out: &mut iced::futures::channel::mpsc::Sender<crate::Message>,
) -> anyhow::Result<()>;
}
52 changes: 32 additions & 20 deletions client/src/plugin/windows.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::Context;
use crate::plugin::plugin::Plugin;
use anyhow::Context;

pub struct WindowsPlugin {
sway: swayipc::Connection,
Expand All @@ -8,22 +8,25 @@ pub struct WindowsPlugin {

impl WindowsPlugin {
pub fn spawn() -> iced::Subscription<crate::Message> {
return iced::subscription::channel(
std::any::TypeId::of::<Self>(),
100,
|plugin_channel_out| async {
let (app_channel_out, plugin_channel_in) =
iced::futures::channel::mpsc::channel(100);
let mut plugin = Self::new();
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 {}
},
);
if let Ok(mut plugin) = Self::try_new() {
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 {}
},
);
} else {
iced::Subscription::<crate::Message>::none()
}
}

fn get_window_nodes(node: swayipc::Node) -> Vec<swayipc::Node> {
Expand All @@ -41,6 +44,10 @@ impl WindowsPlugin {

return vec![];
}

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

impl Plugin for WindowsPlugin {
Expand All @@ -59,7 +66,8 @@ impl Plugin for WindowsPlugin {
}

fn new() -> Self {
let connection_result = swayipc::Connection::new().context("Failed to establish sway ipc connection.");
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!("");
Expand Down Expand Up @@ -95,7 +103,11 @@ impl Plugin for WindowsPlugin {
return Self { sway, entries };
}

fn activate(&mut self, entry_id: String, plugin_channel_out: &mut iced::futures::channel::mpsc::Sender<crate::Message>) -> anyhow::Result<()> {
fn activate(
&mut self,
entry_id: String,
plugin_channel_out: &mut iced::futures::channel::mpsc::Sender<crate::Message>,
) -> anyhow::Result<()> {
self.sway
.run_command(format!("[con_id={}] focus", entry_id))
.context(format!(
Expand All @@ -112,4 +124,4 @@ impl Plugin for WindowsPlugin {

return Ok(());
}
}
}