fix: make window without rounded corners closable (#32597)

This commit is contained in:
Cheng Zhao 2022-01-25 23:51:53 +09:00 committed by GitHub
parent a0b7e30fe7
commit 1cf36822e3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 5 deletions

View file

@ -295,12 +295,12 @@ NativeWindowMac::NativeWindowMac(const gin_helper::Dictionary& options,
NSUInteger styleMask = NSWindowStyleMaskTitled; NSUInteger styleMask = NSWindowStyleMaskTitled;
// The NSWindowStyleMaskFullSizeContentView style removes rounded corners // Removing NSWindowStyleMaskTitled removes window title, which removes
// for framless window. // rounded corners of window.
bool rounded_corner = true; bool rounded_corner = true;
options.Get(options::kRoundedCorners, &rounded_corner); options.Get(options::kRoundedCorners, &rounded_corner);
if (!rounded_corner && !has_frame()) if (!rounded_corner && !has_frame())
styleMask = NSWindowStyleMaskFullSizeContentView; styleMask = 0;
if (minimizable) if (minimizable)
styleMask |= NSMiniaturizableWindowMask; styleMask |= NSMiniaturizableWindowMask;
@ -1350,7 +1350,7 @@ void NativeWindowMac::UpdateVibrancyRadii(bool fullscreen) {
if (vibrantView != nil && !vibrancy_type_.empty()) { if (vibrantView != nil && !vibrancy_type_.empty()) {
const bool no_rounded_corner = const bool no_rounded_corner =
[window_ styleMask] & NSWindowStyleMaskFullSizeContentView; !([window_ styleMask] & NSWindowStyleMaskTitled);
if (!has_frame() && !is_modal() && !no_rounded_corner) { if (!has_frame() && !is_modal() && !no_rounded_corner) {
CGFloat radius; CGFloat radius;
if (fullscreen) { if (fullscreen) {

View file

@ -159,6 +159,15 @@ bool ScopedDisableResize::disable_resize_ = false;
return [[self contentView] superview]; return [[self contentView] superview];
} }
- (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)item {
// By default "Close Window" is always disabled when window has no title, to
// support closing a window without title we need to manually do menu item
// validation. This code path is used by the "roundedCorners" option.
if ([item action] == @selector(performClose:))
return shell_->IsClosable();
return [super validateUserInterfaceItem:item];
}
// By overriding this built-in method the corners of the vibrant view (if set) // By overriding this built-in method the corners of the vibrant view (if set)
// will be smooth. // will be smooth.
- (NSImage*)_cornerMask { - (NSImage*)_cornerMask {
@ -195,7 +204,10 @@ bool ScopedDisableResize::disable_resize_ = false;
if (shell_->title_bar_style() == if (shell_->title_bar_style() ==
electron::NativeWindowMac::TitleBarStyle::kCustomButtonsOnHover) { electron::NativeWindowMac::TitleBarStyle::kCustomButtonsOnHover) {
[[self delegate] windowShouldClose:self]; [[self delegate] windowShouldClose:self];
} else if (shell_->IsSimpleFullScreen()) { } else if (!([self styleMask] & NSWindowStyleMaskTitled)) {
// performClose does not work for windows without title, so we have to
// emulate its behavior. This code path is used by "simpleFullscreen" and
// "roundedCorners" options.
if ([[self delegate] respondsToSelector:@selector(windowShouldClose:)]) { if ([[self delegate] respondsToSelector:@selector(windowShouldClose:)]) {
if (![[self delegate] windowShouldClose:self]) if (![[self delegate] windowShouldClose:self])
return; return;

View file

@ -111,6 +111,14 @@ describe('BrowserWindow module', () => {
await closed; await closed;
}); });
it('closes window without rounded corners', async () => {
await closeWindow(w);
w = new BrowserWindow({ show: false, frame: false, roundedCorners: false });
const closed = emittedOnce(w, 'closed');
w.close();
await closed;
});
it('should not crash if called after webContents is destroyed', () => { it('should not crash if called after webContents is destroyed', () => {
w.webContents.destroy(); w.webContents.destroy();
w.webContents.on('destroyed', () => w.close()); w.webContents.on('destroyed', () => w.close());