miniben-90 / x-win

This package allows you to retrieve precise information about active and open windows on Windows, MacOS, and Linux. You can obtain the position, size, title, and other memory of windows.
MIT License
32 stars 6 forks source link

MacOS issue: `get_browser_url()` crashes Tauri app when window is open or closed #40

Open hoonweedev opened 11 hours ago

hoonweedev commented 11 hours ago

Hello, first of all I'd like to thank you for building this awesome project.

Currently I am building a Tauri app that retrieves user's browser url info every 1 second.
I am using x-win Rust crate in Tauri backend, and calling get_browser_url() to achieve this.

It works well at the beginning, but once I open a new app window or close an existing one, the tauri app crashes.

I'd like to know if I am using it wrong. Here's a code for getting url info every 1 second.

use std::time::Duration;
use tauri::{AppHandle, Emitter};
use x_win::{get_active_window, get_browser_url};

#[derive(serde::Serialize, Clone)]
pub struct ActiveAppInfo {
    desktop_app: String,
    web_app: String,
    timestamp: String,
}

#[tauri::command(async)]
pub fn start_app_monitor(app_handle: AppHandle) {
    loop {
        match get_active_window() {
            Ok(window) => match get_browser_url(&window) {
                Ok(url) => app_handle
                    .emit(
                        "active-app-info",
                        ActiveAppInfo {
                            desktop_app: window.info.name,
                            web_app: url,
                            timestamp: chrono::Local::now().to_rfc3339(),
                        },
                    )
                    .unwrap(),
                Err(_e) => app_handle
                    .emit(
                        "active-app-info",
                        ActiveAppInfo {
                            desktop_app: window.info.name,
                            web_app: String::from(""),
                            timestamp: chrono::Local::now().to_rfc3339(),
                        },
                    )
                    .unwrap(),
            },
            Err(_e) => app_handle
                .emit(
                    "active-app-info",
                    ActiveAppInfo {
                        desktop_app: String::from(""),
                        web_app: String::from(""),
                        timestamp: chrono::Local::now().to_rfc3339(),
                    },
                )
                .unwrap(),
        }

        std::thread::sleep(Duration::from_millis(1000));
    }
}