From 9987e6356ab901ba7df1a2f182f5f6e2130b77f0 Mon Sep 17 00:00:00 2001 From: Christian Friedow Date: Sun, 14 Apr 2024 08:52:41 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix=20initial=20sorting=20in=20a?= =?UTF-8?q?=20bunch=20of=20plugins=20(#129)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This sorts entries in the following plugins after they are updated: - applications - sway-windows - brave-bookmarks - brave-progressive-web-apps This fixes the initial sorting on those plugins. Furthermore, this removes the unnecessary sorting on an empty query and it fixes lower vs uppercase sorting problems. This fixes #122. --- client/src/main.rs | 5 +++- client/src/plugin/applications.rs | 3 +-- client/src/plugin/brave/bookmarks.rs | 1 + .../src/plugin/brave/progressive_web_apps.rs | 1 + client/src/plugin/sway_windows.rs | 23 +++++++++++++++---- client/src/plugin/utils.rs | 5 +--- 6 files changed, 26 insertions(+), 12 deletions(-) diff --git a/client/src/main.rs b/client/src/main.rs index 38a4a53..f8ab05d 100644 --- a/client/src/main.rs +++ b/client/src/main.rs @@ -449,7 +449,10 @@ impl Centerpiece { self.scroll_to_selected_entry() } - fn register_plugin(&mut self, plugin: crate::model::Plugin) -> iced::Command { + fn register_plugin(&mut self, mut plugin: crate::model::Plugin) -> iced::Command { + let _ = plugin + .app_channel_out + .try_send(crate::model::PluginRequest::Search(self.query.clone())); self.plugins.push(plugin); self.plugins.sort_by(|a, b| b.priority.cmp(&a.priority)); iced::Command::none() diff --git a/client/src/plugin/applications.rs b/client/src/plugin/applications.rs index 1e7d812..47ebf2a 100644 --- a/client/src/plugin/applications.rs +++ b/client/src/plugin/applications.rs @@ -172,8 +172,7 @@ impl Plugin for ApplicationsPlugin { .filter_map(|path| to_entry(path, terminal_command.clone())) .collect(); - self.entries.sort(); - self.entries.dedup(); + self.sort(); Ok(()) } diff --git a/client/src/plugin/brave/bookmarks.rs b/client/src/plugin/brave/bookmarks.rs index fbf63aa..da2134d 100644 --- a/client/src/plugin/brave/bookmarks.rs +++ b/client/src/plugin/brave/bookmarks.rs @@ -38,6 +38,7 @@ impl Plugin for BookmarksPlugin { .map(|bookmark| bookmark.into()) .collect(); + self.sort(); Ok(()) } diff --git a/client/src/plugin/brave/progressive_web_apps.rs b/client/src/plugin/brave/progressive_web_apps.rs index 2341041..3923c01 100644 --- a/client/src/plugin/brave/progressive_web_apps.rs +++ b/client/src/plugin/brave/progressive_web_apps.rs @@ -47,6 +47,7 @@ impl Plugin for ProgressiveWebAppsPlugin { .map(|bookmark| bookmark.into()) .collect(); + self.sort(); Ok(()) } diff --git a/client/src/plugin/sway_windows.rs b/client/src/plugin/sway_windows.rs index b94022c..966e47e 100644 --- a/client/src/plugin/sway_windows.rs +++ b/client/src/plugin/sway_windows.rs @@ -50,16 +50,26 @@ impl Plugin for SwayWindowsPlugin { log::error!(target: Self::id(), "{:?}", error); panic!(); } - let mut sway = connection_result.unwrap(); + let sway = connection_result.unwrap(); - let root_node_result = sway.get_tree().context("Failed to get_tree from sway ipc."); + Self { + sway, + entries: vec![], + } + } + + fn update_entries(&mut self) -> anyhow::Result<()> { + let root_node_result = self + .sway + .get_tree() + .context("Failed to get_tree from sway ipc."); if let Err(error) = root_node_result { log::error!(target: Self::id(), "{:?}", error); panic!(); } - let root_node = root_node_result.unwrap(); + let sway_root_node = root_node_result.unwrap(); - let entries = Self::get_window_nodes(root_node) + let entries: Vec = Self::get_window_nodes(sway_root_node) .into_iter() .map(|node| { let name = node @@ -79,7 +89,10 @@ impl Plugin for SwayWindowsPlugin { }) .collect(); - Self { sway, entries } + self.set_entries(entries); + self.sort(); + + Ok(()) } fn activate( diff --git a/client/src/plugin/utils.rs b/client/src/plugin/utils.rs index 648c5ac..6e92463 100644 --- a/client/src/plugin/utils.rs +++ b/client/src/plugin/utils.rs @@ -126,7 +126,7 @@ pub trait Plugin { fn sort(&mut self) { let mut entries = self.entries(); - entries.sort_by_key(|entry| entry.title.clone()); + entries.sort_by_key(|entry| entry.title.clone().to_lowercase()); self.set_entries(entries) } @@ -135,9 +135,6 @@ pub trait Plugin { query: &str, plugin_channel_out: &mut iced::futures::channel::mpsc::Sender, ) -> anyhow::Result<()> { - if query.is_empty() { - self.sort(); - } let filtered_entries = self .entries() .into_iter()