Giter VIP home page Giter VIP logo

active-win-pos-rs's Introduction

active-win-pos-rs

Build status

A small Rust library that lets you get position, size, title and a few other properties of the active window on Windows, MacOS and Linux

Usage

Add to Cargo.toml:

[dependencies]
active-win-pos-rs = "0.8"

Use:

use active_win_pos_rs::get_active_window;

fn main() {
    match get_active_window() {
        Ok(active_window) => {
            println!("active window: {:#?}", active_window);
        },
        Err(()) => {
            println!("error occurred while getting the active window");
        }
    }
}

Would give you an instance of ActiveWindow struct with unique window id, process id, window position and window title.

Or use active_win_pos_rs::get_position to get the WindowPosition only.

Window title on MacOS

On MacOS title property will always return an empty string unless you Enable Screen Recording permission for your app.

Build

% git clone https://github.com/dimusic/active-win-pos-rs.git
% cd active-win-pos-rs
% cargo build

Example

% cargo run --example active-window

Output:

active window: ActiveWindow {
    title: "cmd - cargo  run --example active-window",
    process_path: "C:\\Program Files\\WindowsApps\\Microsoft.WindowsTerminal_1.16.10262.0_x64__8wekyb3d8bbwe\\WindowsTerminal.exe",
    app_name: "WindowsTerminal",
    window_id: "HWND(9700584)",
    process_id: 8460,
    position: WindowPosition {
        x: 6.0,
        y: 296.0,
        width: 1129.0,
        height: 635.0,
    },
}

active-win-pos-rs's People

Contributors

dimusic avatar germanoeich avatar gertdreyer avatar isala404 avatar julyj avatar medzernik avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

active-win-pos-rs's Issues

napi-rs node pre-compilation

Hi there! I'm interested in using active-win-pos-rs from within electron which should be rather trivial nowadays with NAPI-RS, do you by any chance have already plans for doing this on your end or do you want to stay in rust-land and rather have somebody else do it?

Those type of only-glue-repos in some cases tend to get more attention than the actual implementation which is unfair, that's why I'm asking.

✌🏻

PartialEq impl only checks process id, but the window title can change

PartialEq impl only checks process id, but the window title can change

fn main() {
    let mut last_active_window;
    loop {
        let Ok(active_window) = active_win_pos_rs::get_active_window() else {
            continue;
        };
        if active_window == last_active_window {
            continue;
        };
        last_active_window = active_window;
        // ...
    }
}

If you want to detect the window title changing you have to explicitly check each property

fn main() {
    let mut last_active_window;
    loop {
        let Ok(active_window) = active_win_pos_rs::get_active_window() else {
            continue;
        };
        if active_window.process_id == last_active_window.process_id && active_window.title == last_active_window.title {
            continue;
        };
        last_active_window = active_window;
        // ...
    }
}

Process name isn't the process name

Using the value .process_name can result in wrong process name. For example:

STRING LANG CODE: \StringFileInfo\040904b0\FileDescription
DESCRIPTION: Visual Studio Code
DESCRIPTION_NEW: Visual Studio Code
active window: ActiveWindow {
    title: "active-window.rs - active-win-pos-rs - Visual Studio Code",
    process_name: "Visual Studio Code",
    window_id: "HWND(1968720)",
    process_id: 5576,
    position: WindowPosition {
        x: -7.0,
        y: -7.0,
        width: 2062.0,
        height: 1166.0,
    },
}
STRING LANG CODE: \StringFileInfo\040904e4\FileDescription
DESCRIPTION: VLC media player
DESCRIPTION_NEW: VLC media player
active window: ActiveWindow {
    title: "VLC media player",
    process_name: "VLC media player",
    window_id: "HWND(1314976)",
    process_id: 26212,
    position: WindowPosition {
        x: -1150.0,
        y: 333.0,
        width: 781.0,
        height: 628.0,
    },
}

image

image

The actual process names are different. I located the issue to this request:

"\\StringFileInfo\\{:04x}{:04x}\\FileDescription",

This line requests the FileDescription, which can differ from process name. One possible fix is to change this to InternalName, but this results in anomalies:

STRING LANG CODE: \StringFileInfo\040904b0\InternalName
DESCRIPTION: electron.exe
DESCRIPTION_NEW: electron.exe
active window: ActiveWindow {
    title: "active-window.rs - active-win-pos-rs - Visual Studio Code",
    process_name: "electron.exe",
    window_id: "HWND(1968720)",
    process_id: 5576,
    position: WindowPosition {
        x: -7.0,
        y: -7.0,
        width: 2062.0,
        height: 1166.0,
    },
}

VSCode is clearly not Electron.exe in the process tab.

I have a solution I will link to a PR, that is to use a completely different function to get data:

active window: ActiveWindow {
    title: "active-window.rs - active-win-pos-rs - Visual Studio Code",
    process_name: "Code.exe",
    window_id: "HWND(1968720)",
    process_id: 5576,
    position: WindowPosition {
        x: -7.0,
        y: -7.0,
        width: 2062.0,
        height: 1166.0,
    },
}

failed to run custom build command for `appkit-nsworkspace-bindings v0.1.0`

Hi, I'm getting the following error on Xcode 15, which has macOS 14 SDK, Apologies if it's not related here

Caused by:
process didn't exit successfully: /Users/cyron/Documents/BeeBox/sandclock-tauri/src-tauri/target/debug/build/appkit-nsworkspace-bindings-dd1fe9d906edfc67/build-script-build (exit status: 101)
--- stdout
cargo:rustc-link-lib=framework=AppKit

--- stderr
thread 'main' panicked at '"wait_struct_(unnamed_at_/Applications/Xcode_app/Contents/Developer/Platforms/MacOSX_platform/Developer/SDKs/MacOSX14_0_sdk/usr/include/sys/wait_h_199_2)" is not a valid Ident', /Users/cyron/.cargo/registry/src/index.crates.io-6f17d22bba15001f/proc-macro2-1.0.67/src/fallback.rs:774:9
note: run with RUST_BACKTRACE=1 environment variable to display a backtrace

Focusing UWP apps returns ApplicationFrameHost.exe instead of proper data

Focusing any UWP app (not to mention a fullscreen one), returns invalid data in several fields:
image

active window: ActiveWindow {
    title: "Microsoft Store",
    process_path: "C:\\Windows\\System32\\ApplicationFrameHost.exe",
    app_name: "Application Frame Host",
    window_id: "HWND(3541104)",
    process_id: 15252,
    position: WindowPosition {
        x: 990.0,
        y: 190.0,
        width: 1538.0,
        height: 916.0,
    },
}

Fields process_path and app_name are affected (at least from a general look)

A way to subscribe to new active window event?

Hi, I was curious if its possible to implement subscribing to active window change event somehow? The thing is I know how to do this on linux, have vague understanding how to make it with winapi, but have any clue how (if at all possible) to do this for OSX.
I'm asking because polling is quite expensive, especially in OSX for some reason, and I can't find any library that could do this for some reason.
Thx.

Expanding the capabilities of this library

Love the approach and implementation on this!

I was wondering if you'd be willing to extend the capabilities to allow for fetching of additional window information like window name, class, instance, etc..

If you're interested, I prefer to incrementally add these additions to this repo instead of forking.

Resizing and setting position

Hi,
I am new to rust and still trying to understand what's what :)
Is it possible the manipulate the active window in any way? like setting the size or position?

I found this https://tauri.app/v1/api/js/window but it seems to only be geared towards the windows that are part of the main app?

Thank you!

Window out of screen ?

Hi,

I'm under "Ubuntu 22.04.1 LTS" whit X11 and I've a big 32/9 screen:

display_info DisplayInfo { id: 446, x: 0, y: 0, width: 5120, height: 1440, rotation: 0.0, scale_factor: 1.0, is_primary: true }

if I have 2 separate windows, one taking all place on left on the other on right, I get that:
What I really show on my Screen:
Capture d’écran du 2022-09-19 21-59-15

Now what ActiveWindow SHOW:

LEFT:
active window: ActiveWindow {
title: "syl21@XPS-8930: ~/projets/debug",
process_name: "Gnome-terminal",
window_id: "50331658",
process_id: 3663,
position: WindowPosition {
x: 140.0,
y: 14.0,

width: 2555.0,
height: 1453.0,
},
}

RIGHT:
active window: ActiveWindow {
title: "",
process_name: "firefox",
window_id: "46137405",
process_id: 4593,
position: WindowPosition {
x: 5158.0,
y: 8.0,

width: 2567.0,
height: 1465.0,
},
}

As you can see they are mistake in RIGHT window position, normaly:

  • x would be like 2695.0
  • y would be 14
  • width same as LEFT
  • height same as LEFT

Thx for your help ;-)

FYI: Macos window title is always nil; we use this workaround

I wasn't sure where else I should post this - feel free to close the issue.

However, if you need the macos title and search for a quick workaround, this is what we use.

// Raw data is like this:   7) "iTerm2" ASN:0x0-0x18018: (in front)
pub fn macos_get_active_window() -> String {
  let mut command = shell("lsappinfo | grep front");
  command.stdout(Stdio::piped());
  let output = command.execute_output().unwrap();
  let mut app_name : String = String::from_utf8(output.stdout).unwrap().to_string();
  app_name = app_name.replace("\n", "");

  app_name = extract_app_name(app_name);
  return app_name;
}


// Input:
// "95) \"DayCheckin\" ASN:0x0-0x2aa3aa1: (in front) "
// Output: DayCheckin
// "79) \"Code - Insiders\" ASN:0x0-0x2a3ca3a: (in front) "
// Code - Insiders
// Test it here: https://rustexp.lpil.uk/
pub fn extract_app_name(app_name: String) -> String {
    let re = Regex::new(r###""(.*)""###).unwrap();

    let extracted_name = match re.captures(&app_name) {
      Some(val) => { val[0].to_string().replace("\"", "") }
      None => {app_name}
    };

    extracted_name
}

LICENSE?

Awesome crate with a lot of potential, unfortunately unfree because of missing license.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.