feat: add get_app_info command with error handling and panic safety for improved app info retrieval

This commit is contained in:
pandadev 2025-03-16 20:33:00 +01:00
parent dc638cb3ce
commit 5e669749d7
No known key found for this signature in database
GPG key ID: C39629DACB8E762F
2 changed files with 39 additions and 20 deletions

View file

@ -127,7 +127,8 @@ fn main() {
db::history::read_image, db::history::read_image,
db::settings::get_setting, db::settings::get_setting,
db::settings::save_setting, db::settings::save_setting,
utils::commands::fetch_page_meta utils::commands::fetch_page_meta,
utils::commands::get_app_info
] ]
) )
.run(tauri::generate_context!()) .run(tauri::generate_context!())

View file

@ -36,12 +36,20 @@ pub fn center_window_on_current_monitor(window: &tauri::WebviewWindow) {
} }
} }
#[tauri::command]
pub fn get_app_info() -> (String, Option<String>) { pub fn get_app_info() -> (String, Option<String>) {
println!("Getting app info"); println!("Getting app info");
let mut ctx = AppInfoContext::new(vec![]); let mut ctx = AppInfoContext::new(vec![]);
println!("Created AppInfoContext"); 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"); println!("Refreshed apps");
let result = std::panic::catch_unwind(|| {
match ctx.get_frontmost_application() { match ctx.get_frontmost_application() {
Ok(window) => { Ok(window) => {
println!("Found frontmost application: {}", window.name); println!("Found frontmost application: {}", window.name);
@ -49,12 +57,13 @@ pub fn get_app_info() -> (String, Option<String>) {
let icon = window let icon = window
.load_icon() .load_icon()
.ok() .ok()
.map(|i| { .and_then(|i| {
println!("Loading icon for {}", name); println!("Loading icon for {}", name);
let png = i.to_png().unwrap(); i.to_png().ok().map(|png| {
let encoded = STANDARD.encode(png.get_bytes()); let encoded = STANDARD.encode(png.get_bytes());
println!("Icon encoded successfully"); println!("Icon encoded successfully");
encoded encoded
})
}); });
println!("Returning app info: {} with icon: {}", name, icon.is_some()); println!("Returning app info: {} with icon: {}", name, icon.is_some());
(name, icon) (name, icon)
@ -64,6 +73,15 @@ pub fn get_app_info() -> (String, Option<String>) {
("System".to_string(), None) ("System".to_string(), None)
} }
} }
});
match result {
Ok(info) => info,
Err(_) => {
println!("Panic occurred while getting app info");
("System".to_string(), None)
}
}
} }
fn _process_icon_to_base64(path: &str) -> Result<String, Box<dyn std::error::Error>> { fn _process_icon_to_base64(path: &str) -> Result<String, Box<dyn std::error::Error>> {