diff --git a/client/src/plugin/mod.rs b/client/src/plugin/mod.rs index f03e5f9..1ceb0f2 100644 --- a/client/src/plugin/mod.rs +++ b/client/src/plugin/mod.rs @@ -6,4 +6,4 @@ pub mod resource_monitor; pub mod utils; pub mod windows; -pub mod plugin; \ No newline at end of file +pub mod plugin; diff --git a/client/src/plugin/plugin.rs b/client/src/plugin/plugin.rs index 5ff4d60..2800e13 100644 --- a/client/src/plugin/plugin.rs +++ b/client/src/plugin/plugin.rs @@ -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?; } } @@ -57,19 +58,31 @@ pub trait Plugin { return Ok(()); } - async fn update(&mut self, plugin_channel_out: &mut iced::futures::channel::mpsc::Sender, plugin_channel_in: &mut iced::futures::channel::mpsc::Receiver) -> anyhow::Result<()> { + async fn update( + &mut self, + plugin_channel_out: &mut iced::futures::channel::mpsc::Sender, + plugin_channel_in: &mut iced::futures::channel::mpsc::Receiver, + ) -> 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) -> anyhow::Result<()> { + fn search( + &mut self, + query: &String, + plugin_channel_out: &mut iced::futures::channel::mpsc::Sender, + ) -> anyhow::Result<()> { let filtered_entries = crate::plugin::utils::search(self.entries(), query); plugin_channel_out @@ -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) -> anyhow::Result<()>; + fn activate( + &mut self, + entry_id: String, + plugin_channel_out: &mut iced::futures::channel::mpsc::Sender, + ) -> anyhow::Result<()>; } diff --git a/client/src/plugin/windows.rs b/client/src/plugin/windows.rs index 742c2a9..85c5323 100644 --- a/client/src/plugin/windows.rs +++ b/client/src/plugin/windows.rs @@ -1,5 +1,5 @@ -use anyhow::Context; use crate::plugin::plugin::Plugin; +use anyhow::Context; pub struct WindowsPlugin { sway: swayipc::Connection, @@ -8,22 +8,25 @@ pub struct WindowsPlugin { impl WindowsPlugin { pub fn spawn() -> iced::Subscription { - return iced::subscription::channel( - std::any::TypeId::of::(), - 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::(), + 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::::none() + } } fn get_window_nodes(node: swayipc::Node) -> Vec { @@ -41,6 +44,10 @@ impl WindowsPlugin { return vec![]; } + + fn try_new() -> Result { + Ok(Self::new()) + } } impl Plugin for WindowsPlugin { @@ -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!(""); @@ -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) -> anyhow::Result<()> { + fn activate( + &mut self, + entry_id: String, + plugin_channel_out: &mut iced::futures::channel::mpsc::Sender, + ) -> anyhow::Result<()> { self.sway .run_command(format!("[con_id={}] focus", entry_id)) .context(format!( @@ -112,4 +124,4 @@ impl Plugin for WindowsPlugin { return Ok(()); } -} \ No newline at end of file +}