Code Monkey home page Code Monkey logo

Comments (11)

s1hofmann avatar s1hofmann commented on May 27, 2024

Hi @DanielTran3 πŸ‘‹

The focus test on CI runs just fine, so far I haven't noticed this behaviour.
There are some scenarios where the OS does mitigate focusing a window, but without further info I can't say anything about it.

from nut.js.

DanielTran3 avatar DanielTran3 commented on May 27, 2024

It seems to have an issue if when I use nodejs to open a window using spawn(), then trying to focus on it after the window is up

from nut.js.

s1hofmann avatar s1hofmann commented on May 27, 2024

Ok, over the last couple of days I read more about focus rules on Windows than I initially anticipated, but I think I spotted the problem and also might already have a possible solution. Will need to investigate a bit.

from nut.js.

s1hofmann avatar s1hofmann commented on May 27, 2024

@DanielTran3 Please give @nut-tree/nut-js@next a try

from nut.js.

DanielTran3 avatar DanielTran3 commented on May 27, 2024

Hmmm, looks like it's still having difficulties focusing. I spawn a Revit window as the application and have it poll to focus, but looks like it still doesn't focus.

I pulled @nut-tree/nut-js@next and tried on version: ^4.0.1-next.20240318211618

Could not find Revit instance, starting one...


Window Found

Focusing on Revit window                                                                                                                                                                                                                              

Focus: false


Focus: false


Focus: false

Focus: false


Focus: false

from nut.js.

DanielTran3 avatar DanielTran3 commented on May 27, 2024

Also, looks like if I manually focus on the window, it will not say that it has focused, where as on v4.0.0 this was the case

from nut.js.

s1hofmann avatar s1hofmann commented on May 27, 2024

@DanielTran3 My own tests regarding window focus have passed the expectations I had given the context of this issue. Please provide the application you’re working with and the exact code how it’s launched so I can investigate properly.

from nut.js.

DanielTran3 avatar DanielTran3 commented on May 27, 2024

I am working in VS Code, using Node v21.1.0 in a Playwright project using Typescript.

This is the code I'm using to launch Autodesk Revit and to try to focus on that window based on the title of the application

let sleepTime = 1000;
keyboard.config.autoDelayMs = 10

/**
 * Find a window based on the title of the window
 * @param tries The number of attempts to find the window (spaced apart by the default 1s)
 * @param titles A list containing strings that show up in the application title. It will 
 * search for substrings from this list
 * @returns The window if it finds one, null otherwise
 */
async function findWindow(tries: number, ...titles: string[]): Promise<screen.Window> {
  while (tries != 0) {
    let matches = 0;
    let windows = await getWindows();
    for (let window of windows) {
      let windowTitle = await window.title;
      for (let title of titles) {
        if (windowTitle.includes(title)) {
          matches += 1;
        }
        else {
          break;
        }
      }
      if (matches == titles.length) {
        console.log("Window Found");
        return Promise.resolve(window);
      }
    }
    tries--;
    await sleep(sleepTime);
  }
  return Promise.resolve(null);
}

/**
 * Attempts to focus on a window
 * @param tries The number of attempts to find the window (spaced apart by the default 1s)
 * @param window The window to focus on
 * @returns True/false if the window has been focused on
 */
async function focusWindow(tries: number, window: screen.Window): Promise<Boolean> {
  await sleep(sleepTime);
  let focused = await window.focus();
  while (!focused && tries != 0) {
    tries--;
    await sleep(sleepTime);
    focused = await window.focus();
    console.log("Focus: %s", focused);
  }
  return Promise.resolve(focused);
}

/**
 * Launches the specified project in the specified version of Revit
 * @param revitVersion The version of Revit to launch (from the revit-versions.json file)
 * @param revitProject The Revit project to open (from the revit-projects.json file)
 * @param detach True will keep the Revit application open after the test finishes, false will close it
 * @returns Returns the Revit window
 */
export async function launchRevit(revitVersion: RevitVersion, revitProject: RevitProject, detach: boolean = false): Promise<screen.Window> {
  try {
    // Find the Revit window and if it doesn't exist, then launch Revit
    let revit = await findWindow(3, revitVersion.name, revitProject.revitFilename);
    if (revit == null) {
      console.log("Could not find Revit instance, starting one...");
      spawn(revitVersion.path, [revitProject.path], {detached: detach});
      revit = await findWindow(60, revitVersion.name, revitProject.revitFilename);
    }

    // Focus on the Revit window
    console.log("Focusing on Revit window");
    let windowFocused = await focusWindow(30, revit)
    if (!windowFocused) {
      return Promise.resolve(null);
    }
    revit = await getActiveWindow()
    return Promise.resolve(revit)
  } catch (e) {
    console.log(e)
  }
  return Promise.resolve(null);
}

from nut.js.

s1hofmann avatar s1hofmann commented on May 27, 2024

@DanielTran3 Judging from your code snippet and given the fact that my test setup looks almost the same, I can't tell why you're running into problems without a detailed investigation.

If you're interested, feel free to reach out to [email protected]

from nut.js.

cihadturhan avatar cihadturhan commented on May 27, 2024

I am working in VS Code, using Node v21.1.0 in a Playwright project using Typescript.

This is the code I'm using to launch Autodesk Revit and to try to focus on that window based on the title of the application

let sleepTime = 1000;
keyboard.config.autoDelayMs = 10

/**
 * Find a window based on the title of the window
 * @param tries The number of attempts to find the window (spaced apart by the default 1s)
 * @param titles A list containing strings that show up in the application title. It will 
 * search for substrings from this list
 * @returns The window if it finds one, null otherwise
 */
async function findWindow(tries: number, ...titles: string[]): Promise<screen.Window> {
  while (tries != 0) {
    let matches = 0;
    let windows = await getWindows();
    for (let window of windows) {
      let windowTitle = await window.title;
      for (let title of titles) {
        if (windowTitle.includes(title)) {
          matches += 1;
        }
        else {
          break;
        }
      }
      if (matches == titles.length) {
        console.log("Window Found");
        return Promise.resolve(window);
      }
    }
    tries--;
    await sleep(sleepTime);
  }
  return Promise.resolve(null);
}

/**
 * Attempts to focus on a window
 * @param tries The number of attempts to find the window (spaced apart by the default 1s)
 * @param window The window to focus on
 * @returns True/false if the window has been focused on
 */
async function focusWindow(tries: number, window: screen.Window): Promise<Boolean> {
  await sleep(sleepTime);
  let focused = await window.focus();
  while (!focused && tries != 0) {
    tries--;
    await sleep(sleepTime);
    focused = await window.focus();
    console.log("Focus: %s", focused);
  }
  return Promise.resolve(focused);
}

/**
 * Launches the specified project in the specified version of Revit
 * @param revitVersion The version of Revit to launch (from the revit-versions.json file)
 * @param revitProject The Revit project to open (from the revit-projects.json file)
 * @param detach True will keep the Revit application open after the test finishes, false will close it
 * @returns Returns the Revit window
 */
export async function launchRevit(revitVersion: RevitVersion, revitProject: RevitProject, detach: boolean = false): Promise<screen.Window> {
  try {
    // Find the Revit window and if it doesn't exist, then launch Revit
    let revit = await findWindow(3, revitVersion.name, revitProject.revitFilename);
    if (revit == null) {
      console.log("Could not find Revit instance, starting one...");
      spawn(revitVersion.path, [revitProject.path], {detached: detach});
      revit = await findWindow(60, revitVersion.name, revitProject.revitFilename);
    }

    // Focus on the Revit window
    console.log("Focusing on Revit window");
    let windowFocused = await focusWindow(30, revit)
    if (!windowFocused) {
      return Promise.resolve(null);
    }
    revit = await getActiveWindow()
    return Promise.resolve(revit)
  } catch (e) {
    console.log(e)
  }
  return Promise.resolve(null);
}

Were you able to find a solution to this? Works fine on mac but fails to focus a previous window on Windows.

For now, I'm Alt+Tab'ing to switch the previous app but this is not a long term solution.

from nut.js.

s1hofmann avatar s1hofmann commented on May 27, 2024

All I can add here is that during my tests I didn't notice any problems:

https://share.cleanshot.com/tnYyNrSB

from nut.js.

Related Issues (20)

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.