refactor: use tauri paths for cache path resolving

This commit is contained in:
ByteAtATime 2025-07-26 10:47:13 -07:00
parent 664699794b
commit 12c82ad16a
No known key found for this signature in database
3 changed files with 25 additions and 21 deletions

View file

@ -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<PathBuf, AppError> {
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<PathBuf, AppError> {
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<Vec<App>, AppError> {
let cache_path = Self::get_cache_path()?;
pub fn get_apps(app: &AppHandle) -> Result<Vec<App>, 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<Vec<App>, AppError> {
pub fn refresh_and_get_apps(app: &AppHandle) -> Result<Vec<App>, 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);
}
}

View file

@ -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<App> {
match AppCache::get_apps() {
fn get_installed_apps(app: tauri::AppHandle) -> Vec<App> {
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<Vec<extensions::Plugi
extensions::discover_plugins(&app)
}
fn setup_background_refresh() {
thread::spawn(|| {
fn setup_background_refresh(app: tauri::AppHandle) {
thread::spawn(move || {
thread::sleep(Duration::from_secs(60));
loop {
AppCache::refresh_background();
AppCache::refresh_background(app.clone());
thread::sleep(Duration::from_secs(300));
}
});
@ -322,7 +322,7 @@ pub fn run() {
app.manage(SnippetManager::new(app.handle())?);
app.manage(AiUsageManager::new(app.handle())?);
setup_background_refresh();
setup_background_refresh(app.handle().clone());
if let Err(e) = setup_global_shortcut(app) {
eprintln!("Failed to set up global shortcut: {}", e);
}

View file

@ -45,7 +45,10 @@ pub fn show_in_finder(path: String) -> Result<(), String> {
}
#[tauri::command]
pub fn get_applications(_path: Option<String>) -> Result<Vec<Application>, String> {
pub fn get_applications(
app: tauri::AppHandle,
_path: Option<String>,
) -> Result<Vec<Application>, String> {
#[cfg(target_os = "macos")]
{
let script = r#"
@ -92,7 +95,7 @@ pub fn get_applications(_path: Option<String>) -> Result<Vec<Application>, Strin
#[cfg(target_os = "linux")]
{
Ok(crate::get_installed_apps()
Ok(crate::get_installed_apps(app)
.into_iter()
.map(|app| Application {
name: app.name,