diff --git a/Cargo.lock b/Cargo.lock index 7d7e66f..c324f4a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -154,6 +154,30 @@ dependencies = [ "syn", ] +[[package]] +name = "atk" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a83b21d2aa75e464db56225e1bda2dd5993311ba1095acaa8fa03d1ae67026ba" +dependencies = [ + "atk-sys", + "bitflags", + "glib", + "libc", +] + +[[package]] +name = "atk-sys" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "badcf670157c84bb8b1cf6b5f70b650fed78da2033c9eed84c4e49b11cbe83ea" +dependencies = [ + "glib-sys", + "gobject-sys", + "libc", + "system-deps", +] + [[package]] name = "atomic" version = "0.5.0" @@ -225,12 +249,45 @@ version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "631ae5198c9be5e753e5cc215e1bd73c2b466a3565173db433f52bb9d3e66dba" +[[package]] +name = "cairo-rs" +version = "0.14.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f859ade407c19810ae920b4fafab92189ed312adad490d08fb16b5f49f1e2207" +dependencies = [ + "bitflags", + "cairo-sys-rs", + "glib", + "libc", + "thiserror", +] + +[[package]] +name = "cairo-sys-rs" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7c9c3928781e8a017ece15eace05230f04b647457d170d2d9641c94a444ff80" +dependencies = [ + "glib-sys", + "libc", + "system-deps", +] + [[package]] name = "cc" version = "1.0.69" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e70cc2f62c6ce1868963827bd677764c62d07c3d9a3e1fb1177ee1a9ab199eb2" +[[package]] +name = "cfg-expr" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b412e83326147c2bb881f8b40edfbf9905b9b8abaebd0e47ca190ba62fda8f0e" +dependencies = [ + "smallvec", +] + [[package]] name = "cfg-if" version = "0.1.10" @@ -370,6 +427,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "either" +version = "1.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" + [[package]] name = "enumflags2" version = "0.6.4" @@ -406,6 +469,16 @@ dependencies = [ "instant", ] +[[package]] +name = "field-offset" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e1c54951450cbd39f3dbcf1005ac413b49487dabf18a720ad2383eccfeffb92" +dependencies = [ + "memoffset", + "rustc_version", +] + [[package]] name = "fnv" version = "1.0.7" @@ -563,6 +636,64 @@ dependencies = [ "pin-project 0.4.28", ] +[[package]] +name = "gdk" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "679e22651cd15888e7acd01767950edca2ee9fcd6421fbf5b3c3b420d4e88bb0" +dependencies = [ + "bitflags", + "cairo-rs", + "gdk-pixbuf", + "gdk-sys", + "gio", + "glib", + "libc", + "pango", +] + +[[package]] +name = "gdk-pixbuf" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "534192cb8f01daeb8fab2c8d4baa8f9aae5b7a39130525779f5c2608e235b10f" +dependencies = [ + "gdk-pixbuf-sys", + "gio", + "glib", + "libc", +] + +[[package]] +name = "gdk-pixbuf-sys" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f097c0704201fbc8f69c1762dc58c6947c8bb188b8ed0bc7e65259f1894fe590" +dependencies = [ + "gio-sys", + "glib-sys", + "gobject-sys", + "libc", + "system-deps", +] + +[[package]] +name = "gdk-sys" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e091b3d3d6696949ac3b3fb3c62090e5bfd7bd6850bef5c3c5ea701de1b1f1e" +dependencies = [ + "cairo-sys-rs", + "gdk-pixbuf-sys", + "gio-sys", + "glib-sys", + "gobject-sys", + "libc", + "pango-sys", + "pkg-config", + "system-deps", +] + [[package]] name = "gen-z" version = "0.1.0" @@ -603,6 +734,156 @@ dependencies = [ "temp-dir", ] +[[package]] +name = "gio" +version = "0.14.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "402a7057cd21d64bfa7ac027b344a7f50f677fb3308693df0e8c70fb55d29f0d" +dependencies = [ + "bitflags", + "futures-channel", + "futures-core", + "futures-io", + "gio-sys", + "glib", + "libc", + "once_cell", + "thiserror", +] + +[[package]] +name = "gio-sys" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0a41df66e57fcc287c4bcf74fc26b884f31901ea9792ec75607289b456f48fa" +dependencies = [ + "glib-sys", + "gobject-sys", + "libc", + "system-deps", + "winapi", +] + +[[package]] +name = "glib" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8fb802e3798d75b415bea8f016eed88d50106ce82f1274e80f31d80cfd4b056" +dependencies = [ + "bitflags", + "futures-channel", + "futures-core", + "futures-executor", + "futures-task", + "glib-macros", + "glib-sys", + "gobject-sys", + "libc", + "once_cell", + "smallvec", +] + +[[package]] +name = "glib-macros" +version = "0.14.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2aad66361f66796bfc73f530c51ef123970eb895ffba991a234fcf7bea89e518" +dependencies = [ + "anyhow", + "heck", + "proc-macro-crate 1.0.0", + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "glib-sys" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c1d60554a212445e2a858e42a0e48cece1bd57b311a19a9468f70376cf554ae" +dependencies = [ + "libc", + "system-deps", +] + +[[package]] +name = "gobject-sys" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa92cae29759dae34ab5921d73fff5ad54b3d794ab842c117e36cafc7994c3f5" +dependencies = [ + "glib-sys", + "libc", + "system-deps", +] + +[[package]] +name = "gtk" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10ae864e5eab8bc8b6b8544ed259eb02dd61b25323b20e777a77aa289c05fd0c" +dependencies = [ + "atk", + "bitflags", + "cairo-rs", + "field-offset", + "futures-channel", + "gdk", + "gdk-pixbuf", + "gio", + "glib", + "gtk-sys", + "gtk3-macros", + "libc", + "once_cell", + "pango", + "pkg-config", +] + +[[package]] +name = "gtk-sys" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c14c8d3da0545785a7c5a120345b3abb534010fb8ae0f2ef3f47c027fba303e" +dependencies = [ + "atk-sys", + "cairo-sys-rs", + "gdk-pixbuf-sys", + "gdk-sys", + "gio-sys", + "glib-sys", + "gobject-sys", + "libc", + "pango-sys", + "system-deps", +] + +[[package]] +name = "gtk3-macros" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21de1da96dc117443fb03c2e270b2d34b7de98d0a79a19bbb689476173745b79" +dependencies = [ + "anyhow", + "heck", + "proc-macro-crate 1.0.0", + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "heck" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c" +dependencies = [ + "unicode-segmentation", +] + [[package]] name = "hermit-abi" version = "0.1.19" @@ -639,6 +920,15 @@ dependencies = [ "cfg-if 1.0.0", ] +[[package]] +name = "itertools" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69ddb889f9d0d08a67338271fa9b62996bc788c7796a5c18cf057420aaed5eaf" +dependencies = [ + "either", +] + [[package]] name = "itoa" version = "0.4.7" @@ -703,6 +993,15 @@ version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" +[[package]] +name = "memoffset" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59accc507f1338036a0477ef61afdae33cde60840f4dfe481319ce3ad116ddf9" +dependencies = [ + "autocfg", +] + [[package]] name = "mime" version = "0.3.16" @@ -806,12 +1105,46 @@ version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "692fcb63b64b1758029e0a96ee63e049ce8c5948587f2f7208df04625e5f6b56" +[[package]] +name = "pango" +version = "0.14.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1fc88307d9797976ea62722ff2ec5de3fae279c6e20100ed3f49ca1a4bf3f96" +dependencies = [ + "bitflags", + "glib", + "libc", + "once_cell", + "pango-sys", +] + +[[package]] +name = "pango-sys" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2367099ca5e761546ba1d501955079f097caa186bb53ce0f718dca99ac1942fe" +dependencies = [ + "glib-sys", + "gobject-sys", + "libc", + "system-deps", +] + [[package]] name = "parking" version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "427c3892f9e783d91cc128285287e70a59e206ca452770ece88a76f7a3eddd72" +[[package]] +name = "pest" +version = "2.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10f4872ae94d7b90ae48754df22fd42ad52ce740b8f370b03da4835417403e53" +dependencies = [ + "ucd-trie", +] + [[package]] name = "pin-project" version = "0.4.28" @@ -864,6 +1197,12 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" +[[package]] +name = "pkg-config" +version = "0.3.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3831453b3449ceb48b6d9c7ad7c96d5ea673e9b470a1dc578c2ce6521230884c" + [[package]] name = "polling" version = "2.1.0" @@ -916,6 +1255,7 @@ dependencies = [ "freedesktop-desktop-entry", "futures-lite", "futures_codec", + "gtk", "human-sort", "human_format", "new_mime_guess", @@ -987,6 +1327,40 @@ dependencies = [ "toml", ] +[[package]] +name = "proc-macro-crate" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41fdbd1df62156fbc5945f4762632564d7d038153091c3fcf1067f6aef7cff92" +dependencies = [ + "thiserror", + "toml", +] + +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + [[package]] name = "proc-macro-hack" version = "0.5.19" @@ -1073,6 +1447,15 @@ dependencies = [ "serde", ] +[[package]] +name = "rustc_version" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0dfe2087c51c460008730de8b57e6a320782fbfb312e1f4d520e6c6fae155ee" +dependencies = [ + "semver", +] + [[package]] name = "rustversion" version = "1.0.5" @@ -1091,6 +1474,24 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ea6a9290e3c9cf0f18145ef7ffa62d68ee0bf5fcd651017e586dc7fd5da448c2" +[[package]] +name = "semver" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f301af10236f6df4160f7c3f04eec6dbc70ace82d23326abad5edee88801c6b6" +dependencies = [ + "semver-parser", +] + +[[package]] +name = "semver-parser" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00b0bef5b7f9e0df16536d3961cfb6e84331c065b4066afb39768d0e319411f7" +dependencies = [ + "pest", +] + [[package]] name = "serde" version = "1.0.127" @@ -1236,6 +1637,24 @@ version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" +[[package]] +name = "strum" +version = "0.21.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aaf86bbcfd1fa9670b7a129f64fc0c9fcbbfe4f1bc4210e9e98fe71ffc12cde2" + +[[package]] +name = "strum_macros" +version = "0.21.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d06aaeeee809dbc59eb4556183dd927df67db1540de5be8d3ec0b6636358a5ec" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "syn" version = "1.0.74" @@ -1247,6 +1666,24 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "system-deps" +version = "3.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "480c269f870722b3b08d2f13053ce0c2ab722839f472863c3e2d61ff3a1c2fa6" +dependencies = [ + "anyhow", + "cfg-expr", + "heck", + "itertools", + "pkg-config", + "strum", + "strum_macros", + "thiserror", + "toml", + "version-compare", +] + [[package]] name = "temp-dir" version = "0.1.11" @@ -1366,6 +1803,12 @@ dependencies = [ "tracing-serde", ] +[[package]] +name = "ucd-trie" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56dee185309b50d1f11bfedef0fe6d036842e3fb77413abef29f8f8d1c5d4c1c" + [[package]] name = "unicase" version = "2.6.0" @@ -1375,6 +1818,12 @@ dependencies = [ "version_check", ] +[[package]] +name = "unicode-segmentation" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8895849a949e7845e06bd6dc1aa51731a103c42707010a5b591c0038fb73385b" + [[package]] name = "unicode-xid" version = "0.2.2" @@ -1387,6 +1836,12 @@ version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "68b90931029ab9b034b300b797048cf23723400aa757e8a2bfb9d748102f9821" +[[package]] +name = "version-compare" +version = "0.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c18c859eead79d8b95d09e4678566e8d70105c4e7b251f707a03df32442661b" + [[package]] name = "version_check" version = "0.9.3" @@ -1471,7 +1926,7 @@ version = "1.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a482c56029e48681b89b92b5db3c446db0915e8dd1052c0328a574eda38d5f93" dependencies = [ - "proc-macro-crate", + "proc-macro-crate 0.1.5", "proc-macro2", "quote", "syn", @@ -1495,7 +1950,7 @@ version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "27d7c34325a35020b94343389cc9391e0f8ac245cca9155429c4022d93141241" dependencies = [ - "proc-macro-crate", + "proc-macro-crate 0.1.5", "proc-macro2", "quote", "syn", diff --git a/Makefile b/Makefile index db38ac6..efe2fb2 100644 --- a/Makefile +++ b/Makefile @@ -81,7 +81,7 @@ install: ln -sf $(BIN) $(PLUGIN_DIR)/files/files # Recent plugin - install -Dm0755 plugins/src/recent/recent.js $(PLUGIN_DIR)/recent + ln -sf $(BIN) $(PLUGIN_DIR)/recent/recent # Pulse plugin ln -sf $(BIN) $(PLUGIN_DIR)/pulse/pulse diff --git a/bin/src/main.rs b/bin/src/main.rs index 4cb7c28..01b375b 100644 --- a/bin/src/main.rs +++ b/bin/src/main.rs @@ -22,6 +22,7 @@ fn main() { "pop-launcher" => block_on(service::main()), "pop-shell" => block_on(plugins::pop_shell::main()), "pulse" => block_on(plugins::pulse::main()), + "recent" => block_on(plugins::recent::main()), "scripts" => block_on(plugins::scripts::main()), "terminal" => block_on(plugins::terminal::main()), "web" => block_on(plugins::web::main()), diff --git a/debian/pop-launcher.links b/debian/pop-launcher.links index 930316f..46d6267 100644 --- a/debian/pop-launcher.links +++ b/debian/pop-launcher.links @@ -4,6 +4,7 @@ /usr/bin/pop-launcher /usr/lib/pop-launcher/plugins/find/find /usr/bin/pop-launcher /usr/lib/pop-launcher/plugins/pop_shell/pop-shell /usr/bin/pop-launcher /usr/lib/pop-launcher/plugins/pulse/pulse +/usr/bin/pop-launcher /usr/lib/pop-launcher/plugins/recent/recent /usr/bin/pop-launcher /usr/lib/pop-launcher/plugins/scripts/scripts /usr/bin/pop-launcher /usr/lib/pop-launcher/plugins/terminal/terminal /usr/bin/pop-launcher /usr/lib/pop-launcher/plugins/web/web diff --git a/plugins/Cargo.toml b/plugins/Cargo.toml index 03c472b..759116a 100644 --- a/plugins/Cargo.toml +++ b/plugins/Cargo.toml @@ -6,10 +6,14 @@ edition = "2018" publish = false [dependencies] +async-pidfd = "0.1" fork = "0.1" freedesktop-desktop-entry = "0.4" futures_codec = "0.4" futures-lite = "1" +gtk = "0.14" +human_format = "1.0" +human-sort = "0.2" new_mime_guess = "3" pop-launcher = { path = "../" } postage = "0.4" @@ -25,6 +29,3 @@ tracing-subscriber = "0.2" urlencoding = "2" zbus = "1" zvariant = "=2.6" # Restrict for 1.47 -human-sort = "0.2.2" -human_format = "1.0.3" -async-pidfd = "0.1.4" diff --git a/plugins/src/lib.rs b/plugins/src/lib.rs index e29ff70..5b6c38b 100644 --- a/plugins/src/lib.rs +++ b/plugins/src/lib.rs @@ -4,6 +4,7 @@ pub mod files; pub mod find; pub mod pop_shell; pub mod pulse; +pub mod recent; pub mod scripts; pub mod terminal; pub mod web; diff --git a/plugins/src/pulse/mod.rs b/plugins/src/pulse/mod.rs index 8a679ed..6aff68b 100644 --- a/plugins/src/pulse/mod.rs +++ b/plugins/src/pulse/mod.rs @@ -131,7 +131,7 @@ async fn command_spawn(cmd: &str, args: &[&str]) -> io::Result<()> { .args(args) .spawn()?; - AsyncPidFd::from_pid(child.id() as i32)?.wait().await; + let _ = AsyncPidFd::from_pid(child.id() as i32)?.wait().await; Ok(()) } @@ -152,7 +152,7 @@ fn pactl_sinks() -> postage::mpsc::Receiver { while let Some(Ok(line)) = lines.next().await { if let Some(stripped) = line.strip_prefix("Sink #") { use postage::prelude::Sink; - tx.send(stripped.trim().to_owned()).await; + let _ = tx.send(stripped.trim().to_owned()).await; } } } diff --git a/plugins/src/recent/mod.rs b/plugins/src/recent/mod.rs index 6c61133..5f9a652 100644 --- a/plugins/src/recent/mod.rs +++ b/plugins/src/recent/mod.rs @@ -1,21 +1,32 @@ use futures_lite::prelude::*; +use gtk::prelude::*; use pop_launcher::*; +use slab::Slab; use smol::Unblock; -use std::io; +use std::{borrow::Cow, io}; pub struct App { + manager: gtk::RecentManager, out: Unblock, + uris: Slab, } impl Default for App { fn default() -> Self { Self { + manager: gtk::RecentManager::new(), out: async_stdout(), + uris: Slab::new(), } } } pub async fn main() { + if gtk::init().is_err() { + tracing::error!("failed to initialize GTK"); + return; + } + let mut requests = json_input_stream(async_stdin()); let mut app = App::default(); @@ -36,9 +47,47 @@ pub async fn main() { } impl App { - async fn activate(&mut self, id: u32) {} + async fn activate(&mut self, id: u32) { + if let Some(uri) = self.uris.get(id as usize) { + crate::xdg_open(uri); + crate::send(&mut self.out, PluginResponse::Close).await; + } + } async fn search(&mut self, query: String) { + self.uris.clear(); + if let Some(query) = normalized(&query) { + for item in self.manager.items() { + if let Some(name) = item.display_name() { + if name.to_ascii_lowercase().contains(&query) { + if let Some((mime, uri)) = item.mime_type().zip(item.uri()) { + let id = self.uris.insert(uri.to_string()); + crate::send( + &mut self.out, + PluginResponse::Append(PluginSearchResult { + id: id as u32, + name: name.to_string(), + description: item + .uri_display() + .map(String::from) + .unwrap_or_default(), + icon: Some(IconSource::Mime(Cow::Owned(mime.to_string()))), + ..Default::default() + }), + ) + .await; + } + } + } + } + } + crate::send(&mut self.out, PluginResponse::Finished).await; } } + +fn normalized(input: &str) -> Option { + input + .find(' ') + .map(|pos| input[pos + 1..].trim().to_ascii_lowercase()) +} diff --git a/plugins/src/recent/plugin.ron b/plugins/src/recent/plugin.ron index 53b76fa..30f0f3d 100644 --- a/plugins/src/recent/plugin.ron +++ b/plugins/src/recent/plugin.ron @@ -6,6 +6,6 @@ help: "recent ", isolate: true ), - bin: (path: "recent.js"), + bin: (path: "recent"), icon: Name("system-file-manager") ) \ No newline at end of file diff --git a/plugins/src/recent/recent.js b/plugins/src/recent/recent.js deleted file mode 100755 index 36240d2..0000000 --- a/plugins/src/recent/recent.js +++ /dev/null @@ -1,142 +0,0 @@ -#!/usr/bin/gjs - -const { GLib, Gio, Gtk } = imports.gi - -const STDIN = new Gio.DataInputStream({ base_stream: new Gio.UnixInputStream({ fd: 0 }) }) -const STDOUT = new Gio.DataOutputStream({ base_stream: new Gio.UnixOutputStream({ fd: 1 }) }) - -class App { - constructor() { - this.last_query = "" - this.manager = Gtk.RecentManager.get_default() - this.results = new Array() - } - - /** - * @returns {null | Array} - */ - items() { - const recent_items = this.manager.get_items() - log(`got items`) - - if (!recent_items) { return null } - - const items = new Array() - - for (const item of recent_items) { - if (item.exists()) { - items.push({ - display_name: item.get_display_name(), - mime: item.get_mime_type(), - uri: item.get_uri() - }) - } - } - - return items - } - - query(input) { - input = input.substr(input.indexOf(" ") + 1).trim() - - try { - const items = this.items() - - if (items) { - const normalized = input.toLowerCase() - - this.results = items - .filter(item => item.display_name.toLowerCase().includes(normalized)) - .sort((a, b) => a.display_name.localeCompare(b.display_name)) - .slice(0, 7) - - log(`sorted`) - - let id = 0 - - for (const item of this.results) { - this.send({ "Append": { - id, - name: item.display_name, - description: decodeURI(item.uri), - icon: { Mime: item.mime } - }}) - - id += 1 - } - } - } catch (why) { - log(`query exception: ${why}`) - } - - this.send("Finished") - } - - submit(id) { - const result = this.results[id] - - if (result) { - try { - GLib.spawn_command_line_async(`xdg-open '${result.uri}'`) - } catch (e) { - log(`xdg-open failed: ${e}`) - } - } - - this.send("Close") - } - - send(object) { - STDOUT.write_bytes(new GLib.Bytes(JSON.stringify(object) + "\n"), null) - STDOUT.flush(null) - } -} - -function main() { - /** @type {null | ByteArray} */ - let input_array - - /** @type {string} */ - let input_str - - /** @type {null | LauncherRequest} */ - let event - - let app = new App() - - mainloop: - while (true) { - try { - [input_array,] = STDIN.read_line(null) - } catch (e) { - break - } - - input_str = imports.byteArray.toString(input_array) - if ((event = parse_event(input_str)) !== null) { - if ("Search" in event) { - app.query(event.Search) - } else if ("Activate" in event) { - app.submit(event.Activate); - } else if (event === "Exit") { - break mainloop - } - } - } -} - -/** - * Parses an IPC event received from STDIN - * @param {string} input - * @returns {null | LauncherRequest} - */ -function parse_event(input) { - try { - return JSON.parse(input) - } catch (e) { - log(`Input not valid JSON`) - return null - } -} - -main()