Skip to content
Closed
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
78 changes: 66 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion plugins/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ publish = false
[dependencies]
async-pidfd = "0.1.4"
fork = "0.2.0"
freedesktop-desktop-entry = "0.6.2"
freedesktop-desktop-entry = "0.7.9"
human_format = "1.1.0"
human-sort = "0.2.2"
new_mime_guess = "4.0.4"
Expand Down
93 changes: 28 additions & 65 deletions plugins/src/cosmic_toplevel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use cctk::cosmic_protocols::toplevel_info::v1::client::zcosmic_toplevel_handle_v
use cctk::wayland_client::Proxy;
use cctk::{sctk::reexports::calloop, toplevel_info::ToplevelInfo};
use fde::DesktopEntry;
use freedesktop_desktop_entry as fde;
use freedesktop_desktop_entry::{self as fde, default_paths, Iter};
use toplevel_handler::ToplevelUpdate;
use tracing::{debug, error, info, warn};

Expand All @@ -21,7 +21,6 @@ use pop_launcher::{
};
use std::borrow::Cow;
use std::iter;
use std::time::Instant;
use tokio::io::{AsyncWrite, AsyncWriteExt};

use self::toplevel_handler::{toplevel_handler, ToplevelAction};
Expand Down Expand Up @@ -80,12 +79,12 @@ pub async fn main() {
{
if info.state.contains(&State::Activated) {
app.toplevels.remove(pos);
app.toplevels.push(Box::new(info));
app.toplevels.push(info);
} else {
app.toplevels[pos] = Box::new(info);
app.toplevels[pos] = info;
}
} else {
app.toplevels.push(Box::new(info));
app.toplevels.push(info);
}
}
ToplevelUpdate::Remove(foreign_toplevel) => {
Expand All @@ -111,9 +110,9 @@ pub async fn main() {

struct App<W> {
locales: Vec<String>,
desktop_entries: Vec<DesktopEntry<'static>>,
desktop_entries: Vec<DesktopEntry>,
ids_to_ignore: Vec<u32>,
toplevels: Vec<Box<ToplevelInfo>>,
toplevels: Vec<ToplevelInfo>,
calloop_tx: calloop::channel::Sender<ToplevelAction>,
tx: W,
}
Expand All @@ -125,11 +124,8 @@ impl<W: AsyncWrite + Unpin> App<W> {
let _handle = std::thread::spawn(move || toplevel_handler(toplevels_tx, calloop_rx));

let locales = fde::get_languages_from_env();

let paths = fde::Iter::new(fde::default_paths());

let desktop_entries = DesktopEntry::from_paths(paths, &locales)
.filter_map(|e| e.ok())
let desktop_entries = Iter::new(default_paths())
.entries(Some(&locales))
.collect::<Vec<_>>();

(
Expand Down Expand Up @@ -180,59 +176,26 @@ impl<W: AsyncWrite + Unpin> App<W> {
async fn search(&mut self, query: &str) {
let query = query.to_ascii_lowercase();

for info in self.toplevels.iter().rev() {
let entry = if query.is_empty() {
fde::matching::get_best_match(
&[&info.app_id, &info.title],
&self.desktop_entries,
fde::matching::MatchAppIdOptions::default(),
)
} else {
let lowercase_title = info.title.to_lowercase();
let window_words = lowercase_title
.split_whitespace()
.chain(iter::once(info.app_id.as_str()))
.chain(iter::once(info.title.as_str()))
.collect::<Vec<_>>();

// if there's an exact appid match, use that instead
let exact_appid_match = self
.desktop_entries
for de in self.desktop_entries.iter() {
let ret = match query.is_empty() {
true => self
.toplevels
.iter()
.find(|de| de.appid == info.app_id);
if exact_appid_match.is_some()
&& fde::matching::get_entry_score(
&query,
exact_appid_match.unwrap(),
&self.locales,
&window_words,
) > 0.8
{
exact_appid_match
} else {
fde::matching::get_best_match(
&window_words,
&self.desktop_entries,
fde::matching::MatchAppIdOptions::default(),
)
.and_then(|de| {
let score = fde::matching::get_entry_score(
&query,
de,
&self.locales,
&window_words,
);

if score > 0.8 {
Some(de)
} else {
None
}
})
}
.find(|info| de.match_query(&info.title, &self.locales, &[]) > 0.8),

false => self.toplevels.iter().find(|info| {
let lowercase_title = info.title.to_lowercase();
let window_words = lowercase_title
.split_whitespace()
.chain(iter::once(info.app_id.as_str()))
.chain(iter::once(info.title.as_str()))
.collect::<Vec<_>>();
de.appid == info.app_id
|| de.match_query(&info.title, &self.locales, &window_words) > 0.8
}),
};

if let Some(de) = entry {
if let Some(toplevel) = ret {
let icon_name = if let Some(icon) = de.icon() {
Cow::Owned(icon.to_owned())
} else {
Expand All @@ -241,9 +204,9 @@ impl<W: AsyncWrite + Unpin> App<W> {

let response = PluginResponse::Append(PluginSearchResult {
// XXX protocol id may be re-used later
id: info.foreign_toplevel.id().protocol_id(),
window: Some((0, info.foreign_toplevel.id().protocol_id())),
description: info.title.clone(),
id: toplevel.foreign_toplevel.id().protocol_id(),
window: Some((0, toplevel.foreign_toplevel.id().protocol_id())),
description: toplevel.title.clone(),
name: get_description(de, &self.locales),
icon: Some(IconSource::Name(icon_name)),
..Default::default()
Expand Down
Loading