Skip to content

Commit

Permalink
🐛 fix initial sorting in a bunch of plugins (#129)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
friedow authored Apr 14, 2024
1 parent 94d275b commit 9987e63
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 12 deletions.
5 changes: 4 additions & 1 deletion client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,10 @@ impl Centerpiece {
self.scroll_to_selected_entry()
}

fn register_plugin(&mut self, plugin: crate::model::Plugin) -> iced::Command<Message> {
fn register_plugin(&mut self, mut plugin: crate::model::Plugin) -> iced::Command<Message> {
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()
Expand Down
3 changes: 1 addition & 2 deletions client/src/plugin/applications.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Expand Down
1 change: 1 addition & 0 deletions client/src/plugin/brave/bookmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ impl Plugin for BookmarksPlugin {
.map(|bookmark| bookmark.into())
.collect();

self.sort();
Ok(())
}

Expand Down
1 change: 1 addition & 0 deletions client/src/plugin/brave/progressive_web_apps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ impl Plugin for ProgressiveWebAppsPlugin {
.map(|bookmark| bookmark.into())
.collect();

self.sort();
Ok(())
}

Expand Down
23 changes: 18 additions & 5 deletions client/src/plugin/sway_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<crate::model::Entry> = Self::get_window_nodes(sway_root_node)
.into_iter()
.map(|node| {
let name = node
Expand All @@ -79,7 +89,10 @@ impl Plugin for SwayWindowsPlugin {
})
.collect();

Self { sway, entries }
self.set_entries(entries);
self.sort();

Ok(())
}

fn activate(
Expand Down
5 changes: 1 addition & 4 deletions client/src/plugin/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -135,9 +135,6 @@ pub trait Plugin {
query: &str,
plugin_channel_out: &mut iced::futures::channel::mpsc::Sender<crate::Message>,
) -> anyhow::Result<()> {
if query.is_empty() {
self.sort();
}
let filtered_entries = self
.entries()
.into_iter()
Expand Down

0 comments on commit 9987e63

Please sign in to comment.