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

[WIP] fix: avoid blurring window when using Character Viewer pop-up on macOS #19128

Closed
wants to merge 4 commits into from

Conversation

erickzhao
Copy link
Member

Description of Change

Context

In PR #18995, I had submitted a fix such that focus and blur events were listening for changes in the Key window rather than the Main window.

However, Issue #19124 showed that since a window would lose its Key status when the macOS Character Viewer was activated, the blur event would make any input on the window lose its focus.

Then, the character selected from the Character Viewer wouldn't be entered into the input.

Change

I did not find much documentation regarding the behavior of NSWindow with respect to the Character Viewer. After playing around with windowDidResignKey, I found that when the Character Viewer appears, the window loses its Key status, but the [NSApp keyWindow] variable still referenced the original window.

Thus, I added a conditional check to only apply the blur event if the resigning window and the new key window weren't the same.

cc @codebytere @zcbenz @javan

Checklist

Release Notes

Notes: Fixed bug on macOS that broke input from the Character Viewer popup.

@electron-cation electron-cation bot added new-pr 🌱 PR opened in the last 24 hours and removed new-pr 🌱 PR opened in the last 24 hours labels Jul 6, 2019
Copy link
Member

@zcbenz zcbenz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the root problem of #19124 and #18502 is rwhv->SetActive getting called when it shouldn't.

After checking the call stack of RenderWidgetHost::SetActive I found it is already being called inside content module, and it is not being called by other parts of Chrome, so removing the rwhv->SetActive calls in BrowserWindow should be enough to fix the issues.

(The rwhv->SetActive calls were inherited from ancient Chrome code.)

And we should also check whether removing rwhv->SetActive alone would be able to fix #18502, and if it is, we should also revert #18995 as focus/blur should map to the main window events instead of key window events.

Copy link
Member

@nornagon nornagon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like Chrome's app shim does the same thing.

Please remove the commented-out debugging code before you merge :)

shell/browser/ui/cocoa/atom_ns_window_delegate.mm Outdated Show resolved Hide resolved
@erickzhao
Copy link
Member Author

It seems like removing the rwhv->SetActive calls fixes #19124. However, #18502 persists even with those calls removed.

However, both solutions give less than desirable outcomes for firing focus and blur events. On browser and previous versions of Electron, opening the Character Viewer wouldn't fire either event, since the window's Main status wouldn't change.

With the Key change:

  1. Removing rwhv->SetActive fires both focus and blur.
  2. This PR's original change only fires focus since the blur event is blocked.

I'm not particularly sure what to do in this case. It seems like removing the rwhv calls gives a more reasonable behavior, though.

cc @zcbenz @nornagon

@erickzhao erickzhao force-pushed the intern/fix-character-palette-focus branch from a34c7d7 to 6bc0e08 Compare July 8, 2019 18:14
@zcbenz
Copy link
Member

zcbenz commented Jul 9, 2019

I think we got confused with 2 types of focus/blur here: one is the focus events of BrowserWindow, the other is the focus events of DOM Window.

For BrowserWindow, the focus events should be main window events, otherwise things like opening spotlight or status item menu would trigger the focus events. For example with PR, while it hacks windowDidResignKey so blur is emitted correctly for some cases, opening and closing spotlight would still tigger focus on BrowserWindow, and I don't think we can cover all edge cases.

For DOM Window, the focus events, which are controlled by SetActive or StoreFocus/RestoreFocus, should be key window events, as investigated by @erickzhao and @nornagon.

So to correctly fix both issues without breaking focus events of BrowserWindow, I think we should:

  1. Still call OnWindowBlur/OnWindowBlur(which emits focus/blur on BrowserWindow) on main window events.
  2. Call SetActive on key window events, with check in windowDidResignKey.
  3. Call StoreFocus/RestoreFocus on main window events. (Chrome calls them on tab change, which seems to map to the main window events, it needs some testing to confirm.)

We would need to add more methods to NativeWindowObserver to handle key window events separately instead of keeping everything in OnWindowBlur/OnWindowBlur.

@erickzhao erickzhao changed the title fix: avoid blurring window when using Character Viewer pop-up on macOS [WIP] fix: avoid blurring window when using Character Viewer pop-up on macOS Jul 11, 2019
@erickzhao
Copy link
Member Author

@zcbenz @codebytere @nornagon do you think that we should first move to revert the original change? PR #18995 was backported to 6, 5, and 4, and it seems like this issue is more immediately pressing for macOS users than the modal focus behavior.

@zcbenz
Copy link
Member

zcbenz commented Jul 11, 2019

Reverting #18995 first sounds good to me.

@erickzhao
Copy link
Member Author

Going to close this and attempt to resolve #18502 in a future PR as per the comments above.

@erickzhao erickzhao closed this Jul 12, 2019
@deermichel deermichel deleted the intern/fix-character-palette-focus branch August 5, 2019 22:21
@codebytere codebytere mentioned this pull request Jun 24, 2020
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants