Skip to content

Commit

Permalink
fix: make window without rounded corners closable (#32612)
Browse files Browse the repository at this point in the history
Co-authored-by: Cheng Zhao <zcbenz@gmail.com>
  • Loading branch information
trop[bot] and zcbenz committed Jan 25, 2022
1 parent d9b3ad4 commit 3cbfc6a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
8 changes: 4 additions & 4 deletions shell/browser/native_window_mac.mm
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
14 changes: 13 additions & 1 deletion shell/browser/ui/cocoa/electron_ns_window.mm
Expand Up @@ -159,6 +159,15 @@ - (NSView*)frameView {
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)
// will be smooth.
- (NSImage*)_cornerMask {
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions spec-main/api-browser-window-spec.ts
Expand Up @@ -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());
Expand Down

0 comments on commit 3cbfc6a

Please sign in to comment.