From 12c82ad16a54c7e8d261030574bf8155fb604210 Mon Sep 17 00:00:00 2001 From: ByteAtATime Date: Sat, 26 Jul 2025 10:47:13 -0700 Subject: [PATCH] refactor: use tauri paths for cache path resolving --- src-tauri/src/cache.rs | 25 +++++++++++++------------ src-tauri/src/lib.rs | 14 +++++++------- src-tauri/src/system.rs | 7 +++++-- 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/src-tauri/src/cache.rs b/src-tauri/src/cache.rs index 96041da..9ef36bf 100644 --- a/src-tauri/src/cache.rs +++ b/src-tauri/src/cache.rs @@ -2,10 +2,11 @@ use crate::{app::App, desktop::DesktopFileManager, error::AppError}; use serde::{Deserialize, Serialize}; use std::{ collections::HashMap, - env, fs, + fs, path::{Path, PathBuf}, time::SystemTime, }; +use tauri::{AppHandle, Manager}; #[derive(Serialize, Deserialize)] pub struct AppCache { @@ -14,10 +15,10 @@ pub struct AppCache { } impl AppCache { - pub fn get_cache_path() -> Result { - let cache_dir = env::var("XDG_CACHE_HOME") - .map(PathBuf::from) - .or_else(|_| env::var("HOME").map(|home| PathBuf::from(home).join(".cache"))) + pub fn get_cache_path(app: &AppHandle) -> Result { + let cache_dir = app + .path() + .app_cache_dir() .map_err(|_| AppError::DirectoryNotFound)?; let app_cache_dir = cache_dir.join("raycast-linux"); @@ -52,8 +53,8 @@ impl AppCache { }) } - pub fn get_apps() -> Result, AppError> { - let cache_path = Self::get_cache_path()?; + pub fn get_apps(app: &AppHandle) -> Result, AppError> { + let cache_path = Self::get_cache_path(app)?; if let Ok(cached_data) = Self::read_from_file(&cache_path) { if !cached_data.is_stale() { @@ -61,17 +62,17 @@ impl AppCache { } } - Self::refresh_and_get_apps() + Self::refresh_and_get_apps(app) } - pub fn refresh_and_get_apps() -> Result, AppError> { + pub fn refresh_and_get_apps(app: &AppHandle) -> Result, AppError> { let (apps, dir_mod_times) = DesktopFileManager::scan_and_parse_apps()?; let cache_data = AppCache { apps: apps.clone(), dir_mod_times, }; - if let Ok(cache_path) = Self::get_cache_path() { + if let Ok(cache_path) = Self::get_cache_path(app) { if let Err(e) = cache_data.write_to_file(&cache_path) { eprintln!("Failed to write to app cache: {:?}", e); } @@ -80,8 +81,8 @@ impl AppCache { Ok(apps) } - pub fn refresh_background() { - if let Err(e) = Self::refresh_and_get_apps() { + pub fn refresh_background(app: AppHandle) { + if let Err(e) = Self::refresh_and_get_apps(&app) { eprintln!("Error refreshing app cache in background: {:?}", e); } } diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index f90a5df..8b0c6a1 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -30,11 +30,11 @@ use std::process::Command; use std::sync::Arc; use std::thread; use std::time::Duration; -use tauri::{Emitter, Manager}; +use tauri::{AppHandle, Emitter, Manager}; #[tauri::command] -fn get_installed_apps() -> Vec { - match AppCache::get_apps() { +fn get_installed_apps(app: tauri::AppHandle) -> Vec { + match AppCache::get_apps(&app) { Ok(apps) => apps, Err(e) => { eprintln!("Failed to get apps: {:?}", e); @@ -146,11 +146,11 @@ fn get_discovered_plugins(app: tauri::AppHandle) -> Result Result<(), String> { } #[tauri::command] -pub fn get_applications(_path: Option) -> Result, String> { +pub fn get_applications( + app: tauri::AppHandle, + _path: Option, +) -> Result, String> { #[cfg(target_os = "macos")] { let script = r#" @@ -92,7 +95,7 @@ pub fn get_applications(_path: Option) -> Result, Strin #[cfg(target_os = "linux")] { - Ok(crate::get_installed_apps() + Ok(crate::get_installed_apps(app) .into_iter() .map(|app| Application { name: app.name,