2018-04-19 07:53:12 +00:00
|
|
|
// Copyright (c) 2018 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/ui/cocoa/electron_ns_window.h"
|
2018-04-19 07:53:12 +00:00
|
|
|
|
2018-09-06 23:47:31 +00:00
|
|
|
#include "base/strings/sys_string_conversions.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/native_window_mac.h"
|
|
|
|
#include "shell/browser/ui/cocoa/electron_preview_item.h"
|
|
|
|
#include "shell/browser/ui/cocoa/electron_touch_bar.h"
|
|
|
|
#include "shell/browser/ui/cocoa/root_view_mac.h"
|
2018-05-02 12:28:28 +00:00
|
|
|
#include "ui/base/cocoa/window_size_constants.h"
|
2018-04-19 07:53:12 +00:00
|
|
|
|
|
|
|
namespace electron {
|
|
|
|
|
|
|
|
bool ScopedDisableResize::disable_resize_ = false;
|
|
|
|
|
|
|
|
} // namespace electron
|
|
|
|
|
2020-07-23 00:54:32 +00:00
|
|
|
@interface NSWindow (PrivateAPI)
|
|
|
|
- (NSImage*)_cornerMask;
|
|
|
|
@end
|
|
|
|
|
2018-04-19 07:53:12 +00:00
|
|
|
@implementation ElectronNSWindow
|
|
|
|
|
|
|
|
@synthesize acceptsFirstMouse;
|
|
|
|
@synthesize enableLargerThanScreen;
|
|
|
|
@synthesize disableAutoHideCursor;
|
|
|
|
@synthesize disableKeyOrMainWindow;
|
|
|
|
@synthesize vibrantView;
|
2020-01-13 06:43:00 +00:00
|
|
|
@synthesize cornerMask;
|
2018-04-19 07:53:12 +00:00
|
|
|
|
2018-05-02 12:28:28 +00:00
|
|
|
- (id)initWithShell:(electron::NativeWindowMac*)shell
|
|
|
|
styleMask:(NSUInteger)styleMask {
|
|
|
|
if ((self = [super initWithContentRect:ui::kWindowSizeDeterminedLater
|
|
|
|
styleMask:styleMask
|
|
|
|
backing:NSBackingStoreBuffered
|
2021-03-27 01:14:07 +00:00
|
|
|
defer:NO])) {
|
2018-05-02 12:28:28 +00:00
|
|
|
shell_ = shell;
|
|
|
|
}
|
|
|
|
return self;
|
2018-04-19 07:53:12 +00:00
|
|
|
}
|
|
|
|
|
2018-05-02 11:43:45 +00:00
|
|
|
- (electron::NativeWindowMac*)shell {
|
|
|
|
return shell_;
|
|
|
|
}
|
|
|
|
|
2018-09-06 23:47:31 +00:00
|
|
|
- (id)accessibilityFocusedUIElement {
|
|
|
|
views::Widget* widget = shell_->widget();
|
|
|
|
id superFocus = [super accessibilityFocusedUIElement];
|
|
|
|
if (!widget || shell_->IsFocused())
|
|
|
|
return superFocus;
|
|
|
|
return nil;
|
|
|
|
}
|
2018-05-02 12:28:28 +00:00
|
|
|
- (NSRect)originalContentRectForFrameRect:(NSRect)frameRect {
|
|
|
|
return [super contentRectForFrameRect:frameRect];
|
|
|
|
}
|
|
|
|
|
2018-04-19 07:53:12 +00:00
|
|
|
- (NSTouchBar*)makeTouchBar API_AVAILABLE(macosx(10.12.2)) {
|
|
|
|
if (shell_->touch_bar())
|
|
|
|
return [shell_->touch_bar() makeTouchBar];
|
|
|
|
else
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
// NSWindow overrides.
|
|
|
|
|
2018-04-20 18:47:04 +00:00
|
|
|
- (void)swipeWithEvent:(NSEvent*)event {
|
2018-04-19 07:53:12 +00:00
|
|
|
if (event.deltaY == 1.0) {
|
|
|
|
shell_->NotifyWindowSwipe("up");
|
|
|
|
} else if (event.deltaX == -1.0) {
|
|
|
|
shell_->NotifyWindowSwipe("right");
|
|
|
|
} else if (event.deltaY == -1.0) {
|
|
|
|
shell_->NotifyWindowSwipe("down");
|
|
|
|
} else if (event.deltaX == 1.0) {
|
|
|
|
shell_->NotifyWindowSwipe("left");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-23 19:42:26 +00:00
|
|
|
- (void)rotateWithEvent:(NSEvent*)event {
|
|
|
|
shell_->NotifyWindowRotateGesture(event.rotation);
|
|
|
|
}
|
|
|
|
|
2018-05-02 12:28:28 +00:00
|
|
|
- (NSRect)contentRectForFrameRect:(NSRect)frameRect {
|
|
|
|
if (shell_->has_frame())
|
|
|
|
return [super contentRectForFrameRect:frameRect];
|
|
|
|
else
|
|
|
|
return frameRect;
|
|
|
|
}
|
|
|
|
|
2018-04-19 07:53:12 +00:00
|
|
|
- (NSRect)constrainFrameRect:(NSRect)frameRect toScreen:(NSScreen*)screen {
|
|
|
|
// Resizing is disabled.
|
|
|
|
if (electron::ScopedDisableResize::IsResizeDisabled())
|
|
|
|
return [self frame];
|
|
|
|
|
2020-03-25 02:13:43 +00:00
|
|
|
NSRect result = [super constrainFrameRect:frameRect toScreen:screen];
|
2018-04-19 07:53:12 +00:00
|
|
|
// Enable the window to be larger than screen.
|
2020-06-09 18:52:14 +00:00
|
|
|
if ([self enableLargerThanScreen]) {
|
|
|
|
// If we have a frame, ensure that we only position the window
|
|
|
|
// somewhere where the user can move or resize it (and not
|
|
|
|
// behind the menu bar, for instance)
|
|
|
|
//
|
|
|
|
// If there's no frame, put the window wherever the developer
|
|
|
|
// wanted it to go
|
|
|
|
if (shell_->has_frame()) {
|
|
|
|
result.size = frameRect.size;
|
|
|
|
} else {
|
|
|
|
result = frameRect;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-25 02:13:43 +00:00
|
|
|
return result;
|
2018-04-19 07:53:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setFrame:(NSRect)windowFrame display:(BOOL)displayViews {
|
|
|
|
// constrainFrameRect is not called on hidden windows so disable adjusting
|
|
|
|
// the frame directly when resize is disabled
|
|
|
|
if (!electron::ScopedDisableResize::IsResizeDisabled())
|
|
|
|
[super setFrame:windowFrame display:displayViews];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id)accessibilityAttributeValue:(NSString*)attribute {
|
2018-09-06 23:47:31 +00:00
|
|
|
if ([attribute isEqual:NSAccessibilityEnabledAttribute])
|
|
|
|
return [NSNumber numberWithBool:YES];
|
2018-04-19 07:53:12 +00:00
|
|
|
if (![attribute isEqualToString:@"AXChildren"])
|
|
|
|
return [super accessibilityAttributeValue:attribute];
|
|
|
|
|
2020-07-22 22:29:01 +00:00
|
|
|
// We want to remove the window title (also known as
|
|
|
|
// NSAccessibilityReparentingCellProxy), which VoiceOver already sees.
|
2018-04-19 07:53:12 +00:00
|
|
|
// * when VoiceOver is disabled, this causes Cmd+C to be used for TTS but
|
|
|
|
// still leaves the buttons available in the accessibility tree.
|
|
|
|
// * when VoiceOver is enabled, the full accessibility tree is used.
|
|
|
|
// Without removing the title and with VO disabled, the TTS would always read
|
|
|
|
// the window title instead of using Cmd+C to get the selected text.
|
2020-07-22 22:29:01 +00:00
|
|
|
NSPredicate* predicate =
|
|
|
|
[NSPredicate predicateWithFormat:@"(self.className != %@)",
|
|
|
|
@"NSAccessibilityReparentingCellProxy"];
|
2018-04-19 07:53:12 +00:00
|
|
|
|
2018-04-20 18:47:04 +00:00
|
|
|
NSArray* children = [super accessibilityAttributeValue:attribute];
|
2020-03-02 02:25:40 +00:00
|
|
|
NSMutableArray* mutableChildren = [[children mutableCopy] autorelease];
|
|
|
|
[mutableChildren filterUsingPredicate:predicate];
|
|
|
|
|
|
|
|
return mutableChildren;
|
2018-04-19 07:53:12 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 23:03:00 +00:00
|
|
|
- (NSString*)accessibilityTitle {
|
|
|
|
return base::SysUTF8ToNSString(shell_->GetTitle());
|
|
|
|
}
|
|
|
|
|
2018-04-19 07:53:12 +00:00
|
|
|
- (BOOL)canBecomeMainWindow {
|
|
|
|
return !self.disableKeyOrMainWindow;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)canBecomeKeyWindow {
|
|
|
|
return !self.disableKeyOrMainWindow;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSView*)frameView {
|
|
|
|
return [[self contentView] superview];
|
|
|
|
}
|
|
|
|
|
2022-01-25 14:51:53 +00:00
|
|
|
- (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];
|
|
|
|
}
|
|
|
|
|
2020-01-13 06:43:00 +00:00
|
|
|
// By overriding this built-in method the corners of the vibrant view (if set)
|
|
|
|
// will be smooth.
|
|
|
|
- (NSImage*)_cornerMask {
|
2020-07-23 00:54:32 +00:00
|
|
|
if (self.vibrantView != nil) {
|
|
|
|
return [self cornerMask];
|
|
|
|
} else {
|
|
|
|
return [super _cornerMask];
|
|
|
|
}
|
2020-01-13 06:43:00 +00:00
|
|
|
}
|
|
|
|
|
2018-04-19 07:53:12 +00:00
|
|
|
// Quicklook methods
|
|
|
|
|
|
|
|
- (BOOL)acceptsPreviewPanelControl:(QLPreviewPanel*)panel {
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)beginPreviewPanelControl:(QLPreviewPanel*)panel {
|
|
|
|
panel.delegate = [self delegate];
|
|
|
|
panel.dataSource = static_cast<id<QLPreviewPanelDataSource>>([self delegate]);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)endPreviewPanelControl:(QLPreviewPanel*)panel {
|
|
|
|
panel.delegate = nil;
|
|
|
|
panel.dataSource = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Custom window button methods
|
|
|
|
|
2018-09-19 11:10:26 +00:00
|
|
|
- (BOOL)windowShouldClose:(id)sender {
|
|
|
|
return YES;
|
|
|
|
}
|
2018-09-17 05:22:09 +00:00
|
|
|
|
2018-04-19 07:53:12 +00:00
|
|
|
- (void)performClose:(id)sender {
|
2018-04-20 18:47:04 +00:00
|
|
|
if (shell_->title_bar_style() ==
|
2020-10-27 17:51:45 +00:00
|
|
|
electron::NativeWindowMac::TitleBarStyle::kCustomButtonsOnHover) {
|
2018-04-19 07:53:12 +00:00
|
|
|
[[self delegate] windowShouldClose:self];
|
2022-01-25 14:51:53 +00:00
|
|
|
} 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.
|
2018-09-19 11:10:26 +00:00
|
|
|
if ([[self delegate] respondsToSelector:@selector(windowShouldClose:)]) {
|
|
|
|
if (![[self delegate] windowShouldClose:self])
|
|
|
|
return;
|
|
|
|
} else if ([self respondsToSelector:@selector(windowShouldClose:)]) {
|
|
|
|
if (![self windowShouldClose:self])
|
|
|
|
return;
|
2018-09-17 05:22:09 +00:00
|
|
|
}
|
|
|
|
[self close];
|
2020-03-30 16:48:20 +00:00
|
|
|
} else if (shell_->is_modal() && shell_->parent() && shell_->IsVisible()) {
|
|
|
|
// We don't want to actually call [window close] here since
|
|
|
|
// we've already called endSheet on the modal sheet.
|
|
|
|
return;
|
2018-09-17 05:22:09 +00:00
|
|
|
} else {
|
2018-04-19 07:53:12 +00:00
|
|
|
[super performClose:sender];
|
2018-09-17 05:22:09 +00:00
|
|
|
}
|
2018-04-19 07:53:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)toggleFullScreenMode:(id)sender {
|
2019-09-12 17:38:16 +00:00
|
|
|
bool is_simple_fs = shell_->IsSimpleFullScreen();
|
|
|
|
bool always_simple_fs = shell_->always_simple_fullscreen();
|
|
|
|
|
|
|
|
// If we're in simple fullscreen mode and trying to exit it
|
|
|
|
// we need to ensure we exit it properly to prevent a crash
|
2020-04-08 03:40:32 +00:00
|
|
|
// with NSWindowStyleMaskTitled mode.
|
|
|
|
if (is_simple_fs || always_simple_fs) {
|
2019-09-12 17:38:16 +00:00
|
|
|
shell_->SetSimpleFullScreen(!is_simple_fs);
|
2020-04-08 03:40:32 +00:00
|
|
|
} else {
|
2021-04-21 14:56:25 +00:00
|
|
|
if (shell_->IsVisible()) {
|
|
|
|
// Until 10.13, AppKit would obey a call to -toggleFullScreen: made inside
|
|
|
|
// windowDidEnterFullScreen & windowDidExitFullScreen. Starting in 10.13,
|
|
|
|
// it behaves as though the transition is still in progress and just emits
|
|
|
|
// "not in a fullscreen state" when trying to exit fullscreen in the same
|
|
|
|
// runloop that entered it. To handle this, invoke -toggleFullScreen:
|
|
|
|
// asynchronously.
|
|
|
|
[super performSelector:@selector(toggleFullScreen:)
|
|
|
|
withObject:nil
|
|
|
|
afterDelay:0];
|
|
|
|
} else {
|
|
|
|
[super toggleFullScreen:sender];
|
|
|
|
}
|
2020-04-08 03:40:32 +00:00
|
|
|
|
|
|
|
// Exiting fullscreen causes Cocoa to redraw the NSWindow, which resets
|
|
|
|
// the enabled state for NSWindowZoomButton. We need to persist it.
|
2021-04-21 14:56:25 +00:00
|
|
|
bool maximizable = shell_->IsMaximizable();
|
2020-04-08 03:40:32 +00:00
|
|
|
shell_->SetMaximizable(maximizable);
|
|
|
|
}
|
2018-04-19 07:53:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)performMiniaturize:(id)sender {
|
2018-04-20 18:47:04 +00:00
|
|
|
if (shell_->title_bar_style() ==
|
2020-10-27 17:51:45 +00:00
|
|
|
electron::NativeWindowMac::TitleBarStyle::kCustomButtonsOnHover)
|
2018-04-19 07:53:12 +00:00
|
|
|
[self miniaturize:self];
|
|
|
|
else
|
|
|
|
[super performMiniaturize:sender];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|