Skip to content

macOS: window.focus() does not focus window anymore #19920

Closed

Description

@bpasero

Issue Details

  • Electron Version:
    • 6.0.4
  • Operating System:
    • macOS 10.14.6
  • Last Known Working Electron version:
    • 4.x

Expected Behavior

BrowserWindow.focus() puts the window to the front.

Actual Behavior

Window is not moved to front.

To Reproduce

Open a hello world app and add this code:

setTimeout(() => {
	mainWindow.focus();
}, 5000);

Click on another app. The window is not focused. It works fine with Electron 4.

Activity

  1. MarshallOfSound commented on Aug 24, 2019

    @MarshallOfSound
    Member

    @bpasero This will be caused by the UX change we made in #16400

    Can you describe your use case for seizing focus when your app isn't the focused app, it's generally bad UX for another app to randomly take focus away from another.

  2. bpasero commented on Aug 24, 2019

    @bpasero
    ContributorAuthor

    @MarshallOfSound starting VSCode from the terminal when VSCode is already running should bring the window of VSCode to the foreground as it used to be.

  3. MarshallOfSound commented on Aug 24, 2019

    @MarshallOfSound
    Member

    @bpasero How are you doing that communication? Are you using the single instance lock?

  4. bpasero commented on Aug 24, 2019

    @bpasero
    ContributorAuthor

    @MarshallOfSound we have never been using the Electron API for that purpose but have our own socket that we communicate through, just using node APIs.

  5. luke255 commented on Sep 18, 2019

    @luke255

    As per the Event: 'second-instance' section of the Electron documentation:

    Usually applications respond to this by making their primary window focused and non-minimized

    This is no longer possible, along with any other legitimate use for this functionality.

    Will this change be reverted, or will window.focus() be deprecated on Windows and Linux also?

  6. jsphweid commented on Jan 21, 2020

    @jsphweid

    @MarshallOfSound , any response to the other posts here for legitimate uses of this feature that was taken away?

    The whole point of the app I'm writing is to toggle the app from anywhere using a hotkey, which is not possible with the newest versions of Electron. Can't developers and UX people decide what is good/bad UX for their use case instead of this library?

  7. mksmtn commented on Apr 5, 2020

    @mksmtn

    Seems to be resolved here #22612

  8. Noctsyndrome commented on May 29, 2020

    @Noctsyndrome

    Same issue in 7.3.0.
    I'm making a multi-window app that the main-window opens sub-windows and should be able to call them up. The feature works well even in browser.
    Cannot know why electron trim this off.

  9. electron-triage commented on Mar 9, 2021

    @electron-triage

    The Electron version reported on this issue is no longer supported. See our supported versions documentation.

    If this is still reproducible on a supported version, please open a new issue with any other new information that a maintainer should know.

    Thank you for taking the time to report this issue and helping to make Electron better! Your help is appreciated.

  10. tonyarnold commented on Sep 10, 2021

    @tonyarnold

    Cannot know why electron trim this off.

    Because it causes all Electron apps to steal focus from the running app, even when they don't need to. This causes apps to steal focus no matter what you're doing in another app, whether the user requested the app be made frontmost or not.

    There are legitimate reasons to do this, such as if your app only runs in the background, and doesn't appear in the Dock. That being said, this stealing of focus should always be done as a result of a user's direct action, and always immediate.

    Making this an explicit option, as was done in #22612 is an excellent move.

  11. j-lord commented on Apr 7, 2024

    @j-lord

    I know this is old, but this issue came up for me as well. On MacOS, the only way that I was able to get this to work was be below (only tested on MacOS):

    let mainWindow
    ... 
    
    // mac: option + cmd + i
    // win: alt + ctrl + i
    globalShortcut.register('Alt+CommandOrControl+I', () => {
    	forceFocusWindow(mainWindow);
    	console.log('it works!')
    })
    
    ...
    
    function forceFocusWindow(window) {
    	// mac specific
    	if (process.platform === 'darwin') 
    		app.focus({ steal: true });
    	// windows/linux specific
    	else {
    	  // return if there is currently no application window 
    	  if (!window) return;
    	  // Restore the window if it's minimized.
    	  if (window.isMinimized()) window.restore();
    	  // Show the window in case it's hidden.
    	  window.show();
    }
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions