From 3cbfc6ac30c2789de4dbd8c42b704d4fa5266d93 Mon Sep 17 00:00:00 2001 From: "trop[bot]" <37223003+trop[bot]@users.noreply.github.com> Date: Tue, 25 Jan 2022 13:28:30 -0500 Subject: [PATCH] fix: make window without rounded corners closable (#32612) Co-authored-by: Cheng Zhao --- shell/browser/native_window_mac.mm | 8 ++++---- shell/browser/ui/cocoa/electron_ns_window.mm | 14 +++++++++++++- spec-main/api-browser-window-spec.ts | 8 ++++++++ 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/shell/browser/native_window_mac.mm b/shell/browser/native_window_mac.mm index 07d0999777c76..668db19f4f9b2 100644 --- a/shell/browser/native_window_mac.mm +++ b/shell/browser/native_window_mac.mm @@ -295,12 +295,12 @@ void ViewDidMoveToSuperview(NSView* self, SEL _cmd) { NSUInteger styleMask = NSWindowStyleMaskTitled; - // The NSWindowStyleMaskFullSizeContentView style removes rounded corners - // for framless window. + // Removing NSWindowStyleMaskTitled removes window title, which removes + // rounded corners of window. bool rounded_corner = true; options.Get(options::kRoundedCorners, &rounded_corner); if (!rounded_corner && !has_frame()) - styleMask = NSWindowStyleMaskFullSizeContentView; + styleMask = 0; if (minimizable) styleMask |= NSMiniaturizableWindowMask; @@ -1349,7 +1349,7 @@ void ViewDidMoveToSuperview(NSView* self, SEL _cmd) { if (vibrantView != nil && !vibrancy_type_.empty()) { const bool no_rounded_corner = - [window_ styleMask] & NSWindowStyleMaskFullSizeContentView; + !([window_ styleMask] & NSWindowStyleMaskTitled); if (!has_frame() && !is_modal() && !no_rounded_corner) { CGFloat radius; if (fullscreen) { diff --git a/shell/browser/ui/cocoa/electron_ns_window.mm b/shell/browser/ui/cocoa/electron_ns_window.mm index f590d57b9fa36..f4cc2bc5bcbf1 100644 --- a/shell/browser/ui/cocoa/electron_ns_window.mm +++ b/shell/browser/ui/cocoa/electron_ns_window.mm @@ -159,6 +159,15 @@ - (NSView*)frameView { return [[self contentView] superview]; } +- (BOOL)validateUserInterfaceItem:(id)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) // will be smooth. - (NSImage*)_cornerMask { @@ -195,7 +204,10 @@ - (void)performClose:(id)sender { if (shell_->title_bar_style() == electron::NativeWindowMac::TitleBarStyle::kCustomButtonsOnHover) { [[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] windowShouldClose:self]) return; diff --git a/spec-main/api-browser-window-spec.ts b/spec-main/api-browser-window-spec.ts index 395e8ea5a7813..1168914f60504 100644 --- a/spec-main/api-browser-window-spec.ts +++ b/spec-main/api-browser-window-spec.ts @@ -110,6 +110,14 @@ describe('BrowserWindow module', () => { 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', () => { w.webContents.destroy(); w.webContents.on('destroyed', () => w.close());