Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find a way to check if window is focused for long-running apps #190

Open
ologbonowiwi opened this issue Feb 7, 2025 · 2 comments
Open

Comments

@ologbonowiwi
Copy link

ologbonowiwi commented Feb 7, 2025

@nashaofu although the current solution for checking if the window is focused is good and it works, it does not work for long-running apps such as mediar-ai/screenpipe.

it'd be good to have some way to either:

  1. reload the Window state, so we can check if it's focused at the moment
  2. make is_focused a computed function, which will check against pid of currently focused window
  3. get the focused Window from the Monitor

also @louis030195, please share if you can think of any alternative solution

@neo773
Copy link

neo773 commented Feb 7, 2025

On macOS you can check for NSApplicationActivationPolicy status

a.mp4

@ologbonowiwi
Copy link
Author

I created a file under examples to test this. Currently it returns the same app multiple times. I've tried to change is_focused method to do the PID check against foremostApplication, but this doesn't work as the foremostApplication is always the same as when the program called workspace::foremostApplication for the first time.

use std::thread;
use xcap::Window;

fn main() {
    thread::sleep(std::time::Duration::from_secs(3));

    let windows = Window::all().unwrap();

    loop {
        let focused = windows.iter().find(|w| w.is_focused()).unwrap();

        println!(
            "Focused Window:\n id: {}\n title: {}\n app_name: {}\n pid: {}\n monitor: {:?}\n position: {:?}\n size {:?}\n state {:?}\n",
            focused.id(),
            focused.title(),
            focused.app_name(),
            focused.pid(),
            focused.current_monitor().name(),
            (focused.x(), focused.y(), focused.z()),
            (focused.width(), focused.height()),
            (focused.is_minimized(), focused.is_maximized(), focused.is_focused())
        );

        thread::sleep(std::time::Duration::from_secs(1));
    }
}

Here's the naive approach I've tried on turning is_focused into a computed function (on Mac) to see if it'd work:

    pub fn is_focused(&self) -> bool {
        let focused_app_pid = unsafe {
            let workspace = NSWorkspace::sharedWorkspace();
            let frontMostApplication = workspace.frontmostApplication();

            frontMostApplication.map(|focused_app| focused_app.processIdentifier())
        };

        focused_app_pid.eq(&Some(self.pid as i32))
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants