From 5e669749d7df364f813b8fbe96205c11caebd253 Mon Sep 17 00:00:00 2001 From: pandadev <70103896+0PandaDEV@users.noreply.github.com> Date: Sun, 16 Mar 2025 20:33:00 +0100 Subject: [PATCH] feat: add get_app_info command with error handling and panic safety for improved app info retrieval --- src-tauri/src/main.rs | 3 +- src-tauri/src/utils/commands.rs | 56 ++++++++++++++++++++++----------- 2 files changed, 39 insertions(+), 20 deletions(-) diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 3629212..ff0d817 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -127,7 +127,8 @@ fn main() { db::history::read_image, db::settings::get_setting, db::settings::save_setting, - utils::commands::fetch_page_meta + utils::commands::fetch_page_meta, + utils::commands::get_app_info ] ) .run(tauri::generate_context!()) diff --git a/src-tauri/src/utils/commands.rs b/src-tauri/src/utils/commands.rs index ebf71b1..dca7528 100644 --- a/src-tauri/src/utils/commands.rs +++ b/src-tauri/src/utils/commands.rs @@ -36,31 +36,49 @@ pub fn center_window_on_current_monitor(window: &tauri::WebviewWindow) { } } +#[tauri::command] pub fn get_app_info() -> (String, Option) { println!("Getting app info"); let mut ctx = AppInfoContext::new(vec![]); println!("Created AppInfoContext"); - ctx.refresh_apps().unwrap(); + + if let Err(e) = ctx.refresh_apps() { + println!("Failed to refresh apps: {:?}", e); + return ("System".to_string(), None); + } + println!("Refreshed apps"); - match ctx.get_frontmost_application() { - Ok(window) => { - println!("Found frontmost application: {}", window.name); - let name = window.name.clone(); - let icon = window - .load_icon() - .ok() - .map(|i| { - println!("Loading icon for {}", name); - let png = i.to_png().unwrap(); - let encoded = STANDARD.encode(png.get_bytes()); - println!("Icon encoded successfully"); - encoded - }); - println!("Returning app info: {} with icon: {}", name, icon.is_some()); - (name, icon) + + let result = std::panic::catch_unwind(|| { + match ctx.get_frontmost_application() { + Ok(window) => { + println!("Found frontmost application: {}", window.name); + let name = window.name.clone(); + let icon = window + .load_icon() + .ok() + .and_then(|i| { + println!("Loading icon for {}", name); + i.to_png().ok().map(|png| { + let encoded = STANDARD.encode(png.get_bytes()); + println!("Icon encoded successfully"); + encoded + }) + }); + println!("Returning app info: {} with icon: {}", name, icon.is_some()); + (name, icon) + } + Err(e) => { + println!("Failed to get frontmost application: {:?}", e); + ("System".to_string(), None) + } } - Err(e) => { - println!("Failed to get frontmost application: {:?}", e); + }); + + match result { + Ok(info) => info, + Err(_) => { + println!("Panic occurred while getting app info"); ("System".to_string(), None) } }