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
Document how to make a custom title bar on macOS respect native double-click behavior #16385
Comments
SolutionGood news, this is actually quite straight forward to implement (if you know the magic NSUserDefaults key). I actually implemented this in Electron Fiddle just last week --> electron/fiddle#156 The relevant code has been copied below const handleDoubleClick = () => {
const doubleClickAction = remote.systemPreferences.getUserDefault('AppleActionOnDoubleClick', 'string');
const win = remote.getCurrentWindow();
if (doubleClickAction === 'Minimize') {
win.minimize();
} else if (doubleClickAction === 'Maximize') {
if (!win.isMaximized()) {
win.maximize();
} else {
win.unmaximize();
}
}
}You can attach that as a IMOIMO this shouldn't be the default behavior, the Changing this behavior will mess with apps that don't expect this to occur. For example "floating" chat apps will not expect a double-click on that chat head to maximize the chat head This could maybe be added to the docs / guides around frameless windows to help people implement this behavior |
|
That's a lot of boilerplate code nobody is going to implement. Which means many Electron apps will continue to be second-class apps. I do agree, the solution should not affect existing apps. How about introducing an Electron-only CSS property called Alternatively, expose a method in the renderer that accepts a CSS selector or element to make the inset title bar. This method would make the element draggable (ala |
I'm not sure that's possible and would probably involve us patching blink, would have to look into it more though to see if that's feasible.
That would introduce this weird amount of time between your app loading and that method running where the title bar wouldn't be usable (I know because one of my apps used to do this and it got issues raised from people saying the couldn't move the app in the first 100-200ms or so) As an immediate action we can document this, secondarily we can look into making it a one-liner / work-out-of-the-box with a special CSS property. |
I don't see how the boilerplate you posted above would do this any faster. You need the element to be available. This imaginary method wouldn't need to wait for DOM ready. All it needs is the element matching the selector to be available.
Seems like the easiest immediate action would be to take your above boilerplate and expose it as just a simple method. For example: |
|
@MarshallOfSound Does it works in windows 10? |
There are a bunch of interesting things that can be done with Electron if only ones knows these "magic" strings. It might be a good idea to document them somewhere in the docs. |
|
For future readers: the function mentioned in this thread that is supposed to handle this correctly is not handling the case where the double click action on titlebars is disabled entirely in system preferences, the proper function for handling this should look more like this: const handleDoubleClick = () => {
const win = remote.getCurrentWindow();
if ( !win ) return; // No window, nothing to do, although this check doesn't make a whole lot of sense from the renderer process, but you shouldn't handle this from the renderer process
if ( process.platform === 'darwin' ) { // `getUserDefault` is only available under macOS
const action = remote.systemPreferences.getUserDefault('AppleActionOnDoubleClick', 'string');
if ( action === 'None' ) return; // Action disabled entirely, nothing to do
if ( action === 'Minimize') return win.minimize(); // The user prefers to minimize the window, weird
}
// Toggling maximization otherwise
// Under macOS this should actually trigger the "zoom" action, but I believe that's identical to toggling maximization for Electron apps, so we'll just do that for simplicity here
// In case you want to trigger the zoom action for some reason: Menu.sendActionToFirstResponder ( 'zoom:' );
if (win.isMaximized()) return win.unmaximize();
return win.maximize ();
}Errata corrige: actually it does technically handle the "None" case correctly, my bad |
|
In case it's useful, we noticed that on Big Sur, |
|
EDIT adding this solved https://stackoverflow.com/questions/37884130/electron-remote-is-undefined
|
node_modules/.bin/electron --version: v4.0.1Expected Behavior
When using a custom title bar on macOS with the BrowserWindow option
{titleBarStyle: 'hiddenInset'}and the CSS-webkit-app-region: drag;to mark the title bar, I expected double-clicking the title bar to zoom the window (or minimize depending on the Dock setting).Actual behavior
Nothing happens when I double-click the custom title bar.
To Reproduce
Additional Information
Electron gets a lot of bad reputation for not being native. It should strive to fit as much into the native environment as possible. Small things like this matters.
This can not even be correctly worked around as there's no way to know whether the user has chosen to zoom or minimize the window on double-click.
Related issue: sindresorhus/caprine#687
The text was updated successfully, but these errors were encountered: