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,31 +36,49 @@ 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");
match ctx.get_frontmost_application() {
Ok(window) => { let result = std::panic::catch_unwind(|| {
println!("Found frontmost application: {}", window.name); match ctx.get_frontmost_application() {
let name = window.name.clone(); Ok(window) => {
let icon = window println!("Found frontmost application: {}", window.name);
.load_icon() let name = window.name.clone();
.ok() let icon = window
.map(|i| { .load_icon()
println!("Loading icon for {}", name); .ok()
let png = i.to_png().unwrap(); .and_then(|i| {
let encoded = STANDARD.encode(png.get_bytes()); println!("Loading icon for {}", name);
println!("Icon encoded successfully"); i.to_png().ok().map(|png| {
encoded let encoded = STANDARD.encode(png.get_bytes());
}); println!("Icon encoded successfully");
println!("Returning app info: {} with icon: {}", name, icon.is_some()); encoded
(name, icon) })
});
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) ("System".to_string(), None)
} }
} }