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

Forcefocus toggles menu bar visibility when set to auto hide #4

Open
maxannear opened this issue Nov 12, 2018 · 5 comments
Open

Forcefocus toggles menu bar visibility when set to auto hide #4

maxannear opened this issue Nov 12, 2018 · 5 comments
Labels
bug Something isn't working

Comments

@maxannear
Copy link

Hi Robin

Thanks for all the hard work on this one.

Unfortunately this doesn't play well with an app that wants to auto hide the menu bar, https://electronjs.org/docs/api/browser-window#winsetautohidemenubarhide

With that set to true, pressing Alt will toggle menu bar visibility. It looks like you're imitating pressing the Alt key to circumvent the ForegroundLockTimeout in Windows 10. Unfortunately for us every time we call forceFocus.focusWindow() it toggles the visibility of the menu bar.

I'm looking for workarounds but unfortunately win.setMenuBarVisibility(false); immediately afterwards isn't working.

Any suggestions?

@robinwassen
Copy link
Owner

Hi Max,

I see why this happens, but I can't come up with a proper solution straight away since it would probably be very complex. It would probably be to surpress the key event to Electron but still let it bubble up to the OS, or direct the key somewhere else.

The application where I use this has frame: false on the Window and no menu unless it's a dev build, so I have not encountered this problem.

One work around that you could give a shot is to detach the menu, focus and then reattach it.

Something like:

var originalMenu = menu.getApplicationMenu();
menu.setApplicationMenu(null);
forceFocus.focusWindow(currentWindow);
menu.setApplicationMenu(originalMenu);

Could that work?

@robinwassen robinwassen added the bug Something isn't working label Oct 6, 2020
@ericblade
Copy link

I'm trying to figure out a way to deal with this -- I don't want to lose my menu bar as that kills all keyboard shortcuts that are installed by default, but I do want to hide the menu bar. So, if you're using the default menu bar, when you call forceFocus once, it actually focuses the menu bar, but when you call it again, it clears the focus on the menu bar, and returns it to where it was in the application. HOWEVER, this doesn't seem to work in less than about a 1-second delay.

So, I've tried also using windows-foreground-love, and that doesn't seem to solve the problem at all -- i still have windows that refuse to come forward, so i'm not sure if maybe i'm failing to operate that package correctly, or not. So I started re-investigating this one.

alt followed immediately by an escape should close the menu, but if you do it immediately in electron, it throws the window to the far back Z order, which makes no sense (if you pause half a second before hitting esc, then electron ignores it)

So, anyway, I looked at your old idea above, and although that directly doesn't work, I did come up with this messier workaround that does seem to work:

    Menu.setApplicationMenu(null);
    setImmediate(() => {
        forceFocus(bw);
        setTimeout(() => Menu.setApplicationMenu(originalMenu), 50);
    });

calling forceFocus in the same tick that you remove the application menu in doesn't work, and putting the application menu back in in under 50ms from the time you call focusWindow doesn't seem to work, either. So, the answer seems to be chaining the forceFocus to the next tick, and running the setApplicationMenu some 50ms later.

It.. works. At least in my simple test, where I'm just calling forceFocus() on a specific browser window every 2 seconds, so i can see what the effect of doing different things is. I'm going to try it in my actual use case now that it seems to be actually working.

@ericblade
Copy link

Also, I'm not super familiar with the Win32 API, but if there's a way to set focus inside the window, instead of to the window's frame, then that might solve it. There might still be a quick flash of the menu bar, though, which would be not good.

@ericblade
Copy link

(as a somewhat related aside -- i just thought it would be nice if i could return focus to the app that i stole it from, but i may steal it several times from myself .. so i can't just alt-tab.. would you happen to know what calls one would have to make to query what the foreground app is, and how to put that app back into the foreground at a later time?)

@ericblade
Copy link

another discovery while using this, is that if you're typing into a window, and this is triggered, it also can trigger your world-wide hotkey alt+space, which is what i had set for something in Windows PowerToys. must change that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants