Skip to content

fix: restore window event redispatching on mac - #27701

Merged
zcbenz merged 1 commit into
electron:masterfrom
clavin:make-more-keys-work-better
Feb 18, 2021
Merged

zcbenz merged 1 commit into
electron:masterfrom
clavin:make-more-keys-work-better

Conversation

@clavin

@clavin clavin commented Feb 10, 2021 •

Copy link
Copy Markdown
Member

Description of Change

A past refactor of what is now ElectronNSWindow removed EventDispatchingWindow from its inheritance chain, without also adding ElectronNSWindow where EventDispatchingWindow was previously used. This small change restores that functionality to ElectronNSWindow.

Fixes #27268 which is a modern duplicate of #6016 which was fixed in #6068 but then became unfixed in #12696 and has been (presumably) broken ever since.

cc @MarshallOfSound

Checklist

Release Notes

Notes: Fixed OS-level shortcuts on macOS (e.g. Ctrl + F2, ⌘ + ~).

@welcome

welcome Bot commented Feb 10, 2021

Copy link
Copy Markdown

💖 Thanks for opening this pull request! 💖

We use semantic commit messages to streamline the release process. Before your pull request can be merged, you should update your pull request title to start with a semantic prefix.

Examples of commit messages with semantic prefixes:

  • fix: don't overwrite prevent_default if default wasn't prevented
  • feat: add app.isPackaged() method
  • docs: app.isDefaultProtocolClient is now available on Linux

Things that will help get your PR across the finish line:

  • Follow the JavaScript, C++, and Python coding style.
  • Run npm run lint locally to catch formatting errors earlier.
  • Document any user-facing changes you've made following the documentation styleguide.
  • Include tests when adding/changing behavior.
  • Include screenshots and animated GIFs whenever possible.

We get a lot of pull requests on this repo, so please be patient and we will get back to you as soon as we can.

@electron-cation electron-cation Bot added the new-pr 🌱 PR opened recently label Feb 10, 2021
@MarshallOfSound MarshallOfSound added target/10-x-y semver/patch backwards-compatible bug fixes labels Feb 10, 2021
@clavin

clavin commented Feb 11, 2021

Copy link
Copy Markdown
Member Author

⚠️ Just realized I missed the one reference to EventDispatchingWindow: devtools detatched windows. Those would (and do) break with this current fix 😭. Working on seeing if I can somehow bring the EventDispatchingWindow functionality to ElectronNSWindow without duplicating anything, therefore keeping the working devtools windows while also getting working app windows.

@electron-cation electron-cation Bot removed the new-pr 🌱 PR opened recently label Feb 11, 2021
@clavin clavin closed this Feb 16, 2021
@clavin
clavin force-pushed the make-more-keys-work-better branch from 2f791de to b31217a Compare February 16, 2021 20:09
@clavin clavin reopened this Feb 16, 2021
@clavin

clavin commented Feb 16, 2021 •

Copy link
Copy Markdown
Member Author

Alright, this new solution uses the CommandDispatcher in NativeWidgetMacNSWindow (of which ElectronNSWindow inherits from) to redispatch events for electron windows. Both app windows and detatched devtools windows (the latter of which still uses the older EventDispatchingWindow, whose code was originally taken from NativeWidgetMacNSWindow) now handle all these keyboard shortcuts correctly, e.g. ⌘+` and Ctrl+F2.

In the future, a refactor could simplify the code here by changing detatched devtools windows to inherit from NativeWidgetMacNSWindow/ElectronNSWindow instead (or something of that nature), which would remove the EventDispatchingWindow class and its special handling here altogether.

I couldn't figure out a way to test this & couldn't find any tests for this functionality in the chromium source, so I haven't added any tests in this PR.

@clavin clavin changed the title fix: restored unused event redispatching on mac fix: restore window event redispatching on mac Feb 16, 2021
@clavin

clavin commented Feb 17, 2021

Copy link
Copy Markdown
Member Author

Just to 🧠 brain dump in a more digestible way:

This is what the inheritance looked like1 before #6016:

Inheritance before issue 6016

In this version, certain key shortcuts2 like Ctrl + F2 were getting lost and not carrying out their expected functionality.

In Chromium, this issue is handled by a class called CommandDispatcher which, essentially, will send some incoming events back to NSApp ("redispatching") and ignore those events when they come back down the second time3.

So, this original issue was fixed in #6068 by introducing a new class called EventDispatchingWindow between the windows in electron and NSWindow:

Inheritance after PR 6068

This EventDispatchingWindow class is a thin wrapper around NSWindow that provides only that redispatching functionality that CommandDispatcher provides.

A few years later, a refactor in #12696 changed the inheritance of windows created by apps:

Inheritance after the refactor in PR 12696

This refactored ElectronNSWindow to inherit from Chromium's NativeWidgetMacNSWindow class instead of EventDispatchingWindow. However, the code where redispatching actually happens was not updated with this change, meaning windows created by electron apps no longer had this fix. (Undocked/detatched devtools windows, however, were left unchanged and thus still worked.)

It is important to note here that NativeWidgetMacNSWindow owns a CommandDispatcher of its own. This is relevant because the functionality of EventDispatchingWindow comes from CommandDispatcher.

This PR takes the simplest route to fix #27268:

Inheritance after this PR

The code where redispatching actually happens is extended to also recognize windows that have a CommandDispatcher (ElectronNSWindow fits in this category) and redispatch using that.

An alternate route

This PR opts to take a simple path to fix a simple oversight in a refactor. However, this issue could also be fixed by having detatched devtools windows implemented as a window with a CommandDispatcher (this is represented by a protocol named CommandDispatchingWindow), e.g. NativeWidgetMacNSWindow. This would allow us to remove the slightly-redundant EventDispatchingWindow class.


What codebase does X come from?

Electron:

  • EventDispatchingWindow
  • ElectronNSWindow

Chromium:

  • CommandDispatcher
  • CommandDispatchingWindow
  • NativeWidgetMacNSWindow

Notes

1 Some parts of the chain are left out of each image, e.g. EventDispatchingWindow actually inherits directly from a class called UnderlayOpenGLHostingWindow, but that class has nearly zero functionality and isn't relevant here. Additionally, ElectronNSWindow had different names in the past when some of these changes took place. Basically, minor details have been cut in this brain dump.

2 The only shortcut that I could find that failed like this was Ctrl + F2, which focuses the menu bar/apple logo on macOS. Other related shortcuts, like Ctrl + F3 (focuses the dock) or Cmd + ` (switches between windows in the same app) mysteriously work without this event redispatching.

3 I do not know why this fixes the issue, but it does.

@zcbenz

zcbenz commented Feb 18, 2021

Copy link
Copy Markdown
Contributor

Very nice investigation 👍 !

@zcbenz
zcbenz merged commit 64eb02b into electron:master Feb 18, 2021
@welcome

welcome Bot commented Feb 18, 2021

Copy link
Copy Markdown

Congrats on merging your first pull request! 🎉🎉🎉

@release-clerk

release-clerk Bot commented Feb 18, 2021

Copy link
Copy Markdown

Release Notes Persisted

Fixed OS-level shortcuts on macOS (e.g. Ctrl + F2, ⌘ + ~).

@trop

trop Bot commented Feb 18, 2021

Copy link
Copy Markdown
Contributor

I was unable to backport this PR to "10-x-y" cleanly;
you will need to perform this backport manually.

@trop

trop Bot commented Feb 18, 2021

Copy link
Copy Markdown
Contributor

I was unable to backport this PR to "11-x-y" cleanly;
you will need to perform this backport manually.

@trop

trop Bot commented Feb 18, 2021

Copy link
Copy Markdown
Contributor

I have automatically backported this PR to "12-x-y", please check out #27769

@trop

trop Bot commented Feb 18, 2021

Copy link
Copy Markdown
Contributor

@clavin has manually backported this PR to "11-x-y", please check out #27787

@mxdbx

mxdbx commented Feb 18, 2021

Copy link
Copy Markdown

Is there any plan to backport this to 9-x-y?

@clavin

clavin commented Feb 18, 2021 •

Copy link
Copy Markdown
Member Author

@mxdbx I'm not planning on it since that version of electron is no longer supported I believe only receiving security updates 😕

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

semver/patch backwards-compatible bug fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[OS X] Ctrl+F2 keyboard shortcut to focus menu bar does not work (accessibility)

4 participants