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

nw2: blur event does not trigger when removing focus from application window for the first time #7982

Open
2 tasks done
bastimeyer opened this issue Oct 16, 2022 · 3 comments
Labels
bug has-min-repro Has a minimum reproduction nw2

Comments

@bastimeyer
Copy link

bastimeyer commented Oct 16, 2022

Issue Type

  • Bug Report
  • Successfully reproduced against the latest version of NW.js?

Current/Missing Behavior

Follow-up of issue #7488

When launching NW.js, the opened window is apparently not aware of its focused state, and when removing the focus of the application window for the first time, the blur event does not trigger. Upon focusing the application window again, the focus event triggers, as expected, and when removing the focus again, then the blur event triggers for the first time.

Manually calling nwWindow.focus()/nwWindow.blur() or any combination of that after launching the application or showing the window does not help (re)setting the state.

On the old nw1 implementation, the focus/blur events work as expected. This is only broken on nw2.

Related comment: #7981 (comment) (reason for my app having to use nw1)

Expected/Proposed Behavior

The application window state should be set correctly when opening and showing the window, so the blur event can trigger when the user removes focus from the application window for the first time.

Additional Info

  • Operating System: Arch Linux using KDE Plasma (didn't test anything else yet, but this looks like an NW.js JS API issue, so the OS should be irrelevant)
  • NW.js Version: 0.69.1

Reproduction

package.json

{
	"name": "nwjs-window-blur",
	"version": "0.0.1",
	"main": "index.html"
}

index.html

<!doctype html>
<script>
(function() {
const nwWindow = require("nw.gui").Window.get();
const log = msg => process.stdout.write(`${msg}\n`);
nwWindow.on("focus", () => log("focus"));
nwWindow.on("blur", () => log("blur"));
})()
</script>

Simply run /path/to/nw /path/to/app and observe the blur event not triggering when removing focus from the application window for the first time.

/path/to/nw --disable-features=nw2 /path/to/app works as expected.

@ayushmanchhabra ayushmanchhabra added bug nw2 has-min-repro Has a minimum reproduction labels Oct 20, 2022
@bastimeyer
Copy link
Author

@rogerwang

When looking at this piece of code

case 'blur':
this.cWindow = currentNWWindowInternal.getCurrent(this.cWindow.id, {'populate': true});
var cbf = wrap(function(windowId) {
if (self.cWindow.id === windowId) {
callback.__nw_cb.focused = true;
return;
}
if (!callback.__nw_cb.focused)
return;
callback.__nw_cb.focused = false;
callback.call(self);
});
chrome.windows.onFocusChanged.addListener(cbf);
break;

it looks like the initial focused state is undefined, hence why the blur callback is not firing for the first time. But that's just my quick conclusion after looking at the code for a few minutes.

Does the initial focused state not depend on the focus option from the open event?

if (params.focus === false)
options.focused = false;

if (params.focus === false)
options.focused = false;

How can I make modifications to the internal NW.js JS code for debugging purposes without rebuilding the entire thing? Setting breakpoints in the dev tools window unfortunately interferes with the focus and blur events, so I can't use this and would like to write simple debugging messages to stdout instead in order to understand the code/event flow with the current state.

@bastimeyer
Copy link
Author

bastimeyer commented Nov 6, 2022

Is there a chance for a quick bugfix for this? As said, this is only broken on NW2 and it doesn't seem too difficult to fix. If I knew how to make modifications to the internal NW.js JS modules without (re)building the entire thing, I could help debugging this. So far I haven't received any response here yet. @rogerwang

This bug is breaking my application if I want to switch from NW1 to NW2, and on NW1 I'm currently facing the close event bug linked above, so I'd really really appreciate if this could get fixed soon. Thanks.

@bastimeyer
Copy link
Author

Still broken on 0.87.0. Tested on Linux, macOS and Windows.
As explained, the first blur event doesn't trigger.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug has-min-repro Has a minimum reproduction nw2
Projects
None yet
Development

No branches or pull requests

2 participants