Native iCloud apps for Windows and Linux, built in Rust. Notes, Reminders, Calendar, Contacts, Drive and Photos, offline and in sync with your iPhone.
Rust apple-notesapple-reminderscloudkitcrdtdesktop-appicloudicloud-calendaricloud-driveicloud-notesicloud-photosiphonelinuxlinux-desktoplocal-firstoffline-firstprivacyrusttauritauri-appwindows
Desktop search answers without bringing Skykeep's window up
Yashwanth Kamireddifd2fe7d4e34a
diff --git a/packaging/io.github.yashwanthkamireddi.Skykeep.SearchProvider.service b/packaging/io.github.yashwanthkamireddi.Skykeep.SearchProvider.serviceindex 5f6c08c..bdacb1a 100644--- a/packaging/io.github.yashwanthkamireddi.Skykeep.SearchProvider.service+++ b/packaging/io.github.yashwanthkamireddi.Skykeep.SearchProvider.service@@ -1,6 +1,7 @@-# Lets the session bus start Skykeep, hidden in the tray (--background), when GNOME Shell or-# KRunner searches and it is not already running. Installed to /usr/share/dbus-1/services/.+# Lets the session bus start Skykeep, with no window (--search-provider), when GNOME Shell or+# KRunner searches and it is not already running. It answers from the library and ends after a+# few idle minutes unless a window is opened. Installed to /usr/share/dbus-1/services/.# The Flatpak manifest installs it with Exec pointing into /app instead.[D-BUS Service]Name=io.github.yashwanthkamireddi.Skykeep.SearchProvider-Exec=/usr/bin/skykeep --background+Exec=/usr/bin/skykeep --search-providerdiff --git a/packaging/io.github.yashwanthkamireddi.Skykeep.search-provider.ini b/packaging/io.github.yashwanthkamireddi.Skykeep.search-provider.iniindex 9169c26..0f3dafc 100644--- a/packaging/io.github.yashwanthkamireddi.Skykeep.search-provider.ini+++ b/packaging/io.github.yashwanthkamireddi.Skykeep.search-provider.ini@@ -1,6 +1,7 @@# GNOME Shell's search provider: what is typed in the Activities overview is asked of Skykeep# too (src-tauri/src/search_provider.rs). Installed to /usr/share/gnome-shell/search-providers/.-# BusName starts Skykeep hidden if it is not running (the .SearchProvider.service beside this).+# BusName starts Skykeep with no window if it is not running (the .SearchProvider.service+# beside this): it answers searches and shows nothing until a result is opened.# DesktopId names the launcher the .deb, .rpm and AppImage install (Tauri's Skykeep.desktop);# GNOME Shell drops a provider whose DesktopId is not an installed app. The Arch packages and# the Flatpak install the launcher under the app ID and rewrite this line to match.diff --git a/src-tauri/src/apps.rs b/src-tauri/src/apps.rsindex 99fb01a..54743fe 100644--- a/src-tauri/src/apps.rs+++ b/src-tauri/src/apps.rs@@ -277,6 +277,7 @@ pub fn show(window: &tauri::WebviewWindow) {.unwrap_or_else(std::sync::PoisonError::into_inner).retain(|(l, _)| l != window.label());crate::timing::mark(format_args!("{} shown", window.label()));+ crate::login::shown();window.show().ok();window.set_focus().ok();#[cfg(target_os = "linux")]diff --git a/src-tauri/src/login.rs b/src-tauri/src/login.rsindex 7ae4d4f..774d443 100644--- a/src-tauri/src/login.rs+++ b/src-tauri/src/login.rs@@ -21,12 +21,23 @@ static BACKGROUND: AtomicBool = AtomicBool::new(false);/// Whether the first window_ready has been answered: only that one may keep it hidden.static ASKED: AtomicBool = AtomicBool::new(false);+/// Started by the session bus for the desktop's search (`--search-provider`): it answers+/// searches and shows nothing until it is asked to.+static SEARCH_ONLY: AtomicBool = AtomicBool::new(false);+/// A window has been shown this run — asked for by the person, never by a launch.+static SHOWN: AtomicBool = AtomicBool::new(false);+/// What the login entry passes to start hidden.const FLAG: &str = "--background";+/// What the search provider's bus activation passes. Not `--background`: on GNOME there is no+/// tray, and a background launch with nowhere to wait shows its window — so every search in+/// the overview after the person had quit Skykeep brought its window up, or the sign-in screen.+pub const SEARCH_FLAG: &str = "--search-provider";/// Read once at launch, from the command line.pub fn note_launch() {BACKGROUND.store(std::env::args().any(|a| a == FLAG), Ordering::SeqCst);+ SEARCH_ONLY.store(std::env::args().any(|a| a == SEARCH_FLAG), Ordering::SeqCst);}/// Whether this run was started at login, hidden.@@ -34,11 +45,46 @@ pub fn background() -> bool {BACKGROUND.load(Ordering::SeqCst)}-/// Whether the window should stay hidden now: started at login, and nothing needs the person-/// (a code to enter, a sign-in). Only the first answer can be yes; after that the window-/// shows as usual.-pub fn stay_hidden(needs_you: bool) -> bool {- background() && !ASKED.swap(true, Ordering::SeqCst) && !needs_you+/// Whether this run was started by a search in the desktop's overview.+pub fn search_only() -> bool {+ SEARCH_ONLY.load(Ordering::SeqCst)+}++/// A window was shown.+pub fn shown() {+ SHOWN.store(true, Ordering::SeqCst);+}++/// Whether any window has been shown this run.+pub fn was_shown() -> bool {+ SHOWN.load(Ordering::SeqCst)+}++/// Whether the window should stay hidden now: started at login and nothing needs the person+/// (a code to enter, a sign-in), with a tray to come back from; or started for a search,+/// whatever it needs — nobody asked for a window. Only the first answer can be yes; after+/// that the window shows as usual.+pub fn stay_hidden(needs_you: bool, tray: impl FnOnce() -> bool) -> bool {+ if !(background() || search_only()) || ASKED.swap(true, Ordering::SeqCst) {+ return false;+ }+ hides(search_only(), needs_you, tray)+}++/// [`stay_hidden`]'s answer for a first launch started hidden. The tray is asked last, and+/// only when it matters: it is a call on the session bus.+fn hides(search_only: bool, needs_you: bool, tray: impl FnOnce() -> bool) -> bool {+ search_only || (!needs_you && tray())+}++/// How long a Skykeep started for a search goes on with no window shown and no search asked+/// before it ends: the person quit it, and a search does not change that.+pub const SEARCH_IDLE: std::time::Duration = std::time::Duration::from_secs(10 * 60);++/// Whether a run started for a search is done: no window shown, nothing asked for a while.+#[must_use]+pub fn search_done(shown: bool, idle: std::time::Duration) -> bool {+ !shown && idle >= SEARCH_IDLE}#[derive(Serialize)]@@ -243,3 +289,53 @@ mod tests {assert!(file.starts_with("[Desktop Entry]\nType=Application\n"));}}++#[cfg(test)]+mod search_launch_tests {+ use super::*;++ /// Started by a search on GNOME (no tray): no window, signed in or not. It used to show+ /// the main window — or the sign-in screen — on every search after a quit.+ #[test]+ fn a_launch_for_a_search_shows_nothing() {+ assert!(hides(true, true, || false));+ assert!(hides(true, false, || false));+ // At login: hidden only with a tray to come back from, and nothing needing the person.+ assert!(hides(false, false, || true));+ assert!(!hides(false, false, || false));+ assert!(!hides(false, true, || true));+ }++ #[test]+ fn a_search_launch_ends_once_idle_unless_a_window_was_asked_for() {+ assert!(!search_done(false, SEARCH_IDLE / 2));+ assert!(search_done(false, SEARCH_IDLE));+ assert!(!search_done(true, SEARCH_IDLE * 10));+ }++ #[test]+ fn a_program_upgraded_while_running_is_named_as_installed() {+ assert_eq!(+ still_installed(std::path::Path::new("/usr/bin/skykeep (deleted)")),+ PathBuf::from("/usr/bin/skykeep")+ );+ assert_eq!(+ still_installed(std::path::Path::new("/opt/Sky keep/skykeep")),+ PathBuf::from("/opt/Sky keep/skykeep")+ );+ }++ /// The bus starts it with the search flag, never the login one.+ #[test]+ fn the_bus_activation_starts_it_for_search() {+ let service = include_str!(+ "../../packaging/io.github.yashwanthkamireddi.Skykeep.SearchProvider.service"+ );+ let exec = service+ .lines()+ .find_map(|l| l.strip_prefix("Exec="))+ .unwrap();+ assert!(exec.ends_with(SEARCH_FLAG), "{exec}");+ assert!(!exec.contains(FLAG), "{exec}");+ }+}diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rsindex 6632a53..a0207eb 100644--- a/src-tauri/src/main.rs+++ b/src-tauri/src/main.rs@@ -5184,7 +5184,7 @@ fn window_ready(app: tauri::AppHandle, window: tauri::WebviewWindow, needs_you:// Never without the tray to come back from.// Asked in this order: started any other way the answer is no, and the tray's question —// a call on the session bus — was asked on the main thread just before every launch's show.- if login::stay_hidden(needs_you.unwrap_or(true)) && tray_reachable(&app) {+ if login::stay_hidden(needs_you.unwrap_or(true), || tray_reachable(&app)) {// Its page has done what a launch needs of it (the session taken up again): it waits,// drawn and hidden, so the tray opens it at once.return;@@ -5232,7 +5232,7 @@ fn window_state_kept() -> tauri_plugin_window_state::StateFlags {fn show_when_ready(app: &tauri::AppHandle) {// Started at login: the window is the first window_ready's to show, or not. Started as one// app: that app's window shows instead.- if login::background() || LAUNCHED_AS_APP.load(Ordering::SeqCst) {+ if login::background() || login::search_only() || LAUNCHED_AS_APP.load(Ordering::SeqCst) {return;}let app = app.clone();@@ -5286,6 +5286,7 @@ fn make_main(app: &tauri::AppHandle) -> Option<tauri::WebviewWindow> {/// Bring the window back, wherever it went; made first if this launch had not made it. Answers/// whether it had to be made — its page is then still loading, not yet listening.fn show_window(app: &tauri::AppHandle) -> bool {+ login::shown();if let Some(window) = app.get_webview_window("main") {timing::mark("main shown");window.show().ok();@@ -6090,6 +6091,9 @@ fn main() {// In the desktop's own search (GNOME Shell, KRunner) for as long as it runs.#[cfg(target_os = "linux")]search_provider::serve(app.handle().clone());+ if login::search_only() {+ search_provider::end_when_idle(app.handle().clone());+ }alerts::spawn(app.handle().clone());alerts::spawn_events(app.handle().clone());if let Err(e) = build_tray(app) {diff --git a/src-tauri/src/search_provider.rs b/src-tauri/src/search_provider.rsindex 5e83059..413e709 100644--- a/src-tauri/src/search_provider.rs+++ b/src-tauri/src/search_provider.rs@@ -9,7 +9,8 @@//! `packaging/<id>.krunner.desktop` in `/usr/share/krunner/dbusplugins/`.//!//! Neither desktop needs Skykeep already running: `packaging/<id>.SearchProvider.service` lets-//! the bus start it, hidden (`--background`), on the first search.+//! the bus start it with no window (`--search-provider`) on the first search; started so, it+//! ends after a few idle minutes unless a window is opened.//!//! The answer has to be quick — the shell waits on every provider as the person types — so a//! search reads the library only if its lock comes free within [`LOCK_WAIT`] (a sync pass may be@@ -343,6 +344,7 @@ impl Core {/// What the library holds for `query`, remembered so the desktop can ask after it by id.fn find(&self, query: &str) -> Vec<Hit> {+ asked();let found = (self.search)(query).map_or_else(Vec::new, |f| hits(f, EACH));let mut seen = self.seen.lock().unwrap_or_else(|e| e.into_inner());if seen.len() + found.len() > REMEMBERED {@@ -555,6 +557,38 @@ fn search_in_window(app: &AppHandle, query: String) {});}+/// When a search was last asked, in seconds since the epoch.+static LAST_ASKED: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);++fn now_seconds() -> u64 {+ std::time::SystemTime::now()+ .duration_since(std::time::UNIX_EPOCH)+ .map_or(0, |d| d.as_secs())+}++fn asked() {+ LAST_ASKED.store(now_seconds(), std::sync::atomic::Ordering::Relaxed);+}++/// Started for a search (`--search-provider`): ends once [`crate::login::SEARCH_IDLE`] passes+/// with no window shown and no search asked. The changes waiting for Apple are in the library,+/// and go with the next start.+pub fn end_when_idle(app: AppHandle) {+ asked();+ std::thread::spawn(move || loop {+ std::thread::sleep(Duration::from_secs(30));+ let idle = Duration::from_secs(+ now_seconds().saturating_sub(LAST_ASKED.load(std::sync::atomic::Ordering::Relaxed)),+ );+ if crate::login::was_shown() {+ return;+ }+ if crate::login::search_done(false, idle) {+ crate::shutdown(&app, 0);+ }+ });+}+/// Takes Skykeep's place in the desktop's search, for as long as it runs. Nothing is lost if/// there is no session bus or the name is taken: the desktop's search just goes without.pub fn serve(app: AppHandle) {