electron/atom/browser/native_window_mac.mm

879 lines
27 KiB
Text
Raw Normal View History

// Copyright (c) 2013 GitHub, Inc.
2014-04-25 09:49:37 +00:00
// Use of this source code is governed by the MIT license that can be
2013-04-12 07:04:46 +00:00
// found in the LICENSE file.
2014-03-16 00:30:26 +00:00
#include "atom/browser/native_window_mac.h"
2013-04-12 07:04:46 +00:00
#include <string>
2014-03-16 00:30:26 +00:00
#import "atom/browser/ui/cocoa/event_processing_window.h"
#include "atom/common/draggable_region.h"
#include "atom/common/options_switches.h"
#include "base/mac/mac_util.h"
#include "base/strings/sys_string_conversions.h"
#include "brightray/browser/inspectable_web_contents.h"
#include "brightray/browser/inspectable_web_contents_view.h"
2015-04-13 12:10:51 +00:00
#include "content/public/browser/browser_accessibility_state.h"
#include "content/public/browser/native_web_keyboard_event.h"
2013-04-12 07:04:46 +00:00
#include "content/public/browser/web_contents.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/render_widget_host_view.h"
#include "native_mate/dictionary.h"
2013-04-12 07:04:46 +00:00
namespace {
// Prevents window from resizing during the scope.
class ScopedDisableResize {
public:
ScopedDisableResize() { disable_resize_ = true; }
~ScopedDisableResize() { disable_resize_ = false; }
static bool IsResizeDisabled() { return disable_resize_; }
private:
static bool disable_resize_;
};
bool ScopedDisableResize::disable_resize_ = false;
} // namespace
// This view always takes the size of its superview. It is intended to be used
// as a NSWindow's contentView. It is needed because NSWindow's implementation
// explicitly resizes the contentView at inopportune times.
@interface FullSizeContentView : NSView
@end
@implementation FullSizeContentView
// This method is directly called by NSWindow during a window resize on OSX
// 10.10.0, beta 2. We must override it to prevent the content view from
// shrinking.
- (void)setFrameSize:(NSSize)size {
if ([self superview])
size = [[self superview] bounds].size;
[super setFrameSize:size];
}
// The contentView gets moved around during certain full-screen operations.
// This is less than ideal, and should eventually be removed.
- (void)viewDidMoveToSuperview {
[self setFrame:[[self superview] bounds]];
}
@end
@interface AtomNSWindowDelegate : NSObject<NSWindowDelegate> {
@private
atom::NativeWindowMac* shell_;
}
- (id)initWithShell:(atom::NativeWindowMac*)shell;
@end
@implementation AtomNSWindowDelegate
- (id)initWithShell:(atom::NativeWindowMac*)shell {
if ((self = [super init])) {
shell_ = shell;
}
return self;
}
- (void)windowDidBecomeMain:(NSNotification*)notification {
2015-06-25 06:54:00 +00:00
content::WebContents* web_contents = shell_->web_contents();
if (!web_contents)
return;
2014-07-28 07:29:51 +00:00
web_contents->RestoreFocus();
content::RenderWidgetHostView* rwhv = web_contents->GetRenderWidgetHostView();
if (rwhv)
rwhv->SetActive(true);
shell_->NotifyWindowFocus();
}
2013-05-24 10:15:27 +00:00
- (void)windowDidResignMain:(NSNotification*)notification {
2015-06-25 06:54:00 +00:00
content::WebContents* web_contents = shell_->web_contents();
if (!web_contents)
return;
2014-07-28 07:29:51 +00:00
web_contents->StoreFocus();
content::RenderWidgetHostView* rwhv = web_contents->GetRenderWidgetHostView();
if (rwhv)
rwhv->SetActive(false);
shell_->NotifyWindowBlur();
}
- (NSSize)windowWillResize:(NSWindow*)sender toSize:(NSSize)frameSize {
NSSize newSize = frameSize;
double aspectRatio = shell_->GetAspectRatio();
if (aspectRatio > 0.0) {
gfx::Size windowSize = shell_->GetSize();
gfx::Size contentSize = shell_->GetContentSize();
gfx::Size extraSize = shell_->GetAspectRatioExtraSize();
double extraWidthPlusFrame =
windowSize.width() - contentSize.width() + extraSize.width();
double extraHeightPlusFrame =
windowSize.height() - contentSize.height() + extraSize.height();
newSize.width =
roundf((frameSize.height - extraHeightPlusFrame) * aspectRatio +
extraWidthPlusFrame);
// If the new width is less than the frame size use it as the primary
// constraint. This ensures that the value returned by this method will
// never be larger than the users requested window size.
if (newSize.width <= frameSize.width) {
newSize.height =
roundf((newSize.width - extraWidthPlusFrame) / aspectRatio +
extraHeightPlusFrame);
} else {
newSize.height =
roundf((frameSize.width - extraWidthPlusFrame) / aspectRatio +
extraHeightPlusFrame);
newSize.width =
roundf((newSize.height - extraHeightPlusFrame) * aspectRatio +
extraWidthPlusFrame);
}
}
return newSize;
}
- (void)windowDidResize:(NSNotification*)notification {
2015-05-09 15:55:10 +00:00
shell_->NotifyWindowResize();
}
- (void)windowDidMove:(NSNotification*)notification {
2015-05-20 08:37:13 +00:00
// TODO(zcbenz): Remove the alias after figuring out a proper
// way to disptach move.
2015-05-09 15:55:10 +00:00
shell_->NotifyWindowMove();
2015-05-20 08:37:13 +00:00
shell_->NotifyWindowMoved();
}
2014-11-25 05:16:51 +00:00
- (void)windowDidMiniaturize:(NSNotification*)notification {
shell_->NotifyWindowMinimize();
}
- (void)windowDidDeminiaturize:(NSNotification*)notification {
shell_->NotifyWindowRestore();
}
- (BOOL)windowShouldZoom:(NSWindow*)window toFrame:(NSRect)newFrame {
// Cocoa doen't have concept of maximize/unmaximize, so wee need to emulate
// them by calculating size change when zooming.
if (newFrame.size.width < [window frame].size.width ||
newFrame.size.height < [window frame].size.height)
shell_->NotifyWindowUnmaximize();
else
shell_->NotifyWindowMaximize();
return YES;
}
- (void)windowDidEnterFullScreen:(NSNotification*)notification {
shell_->NotifyWindowEnterFullScreen();
}
- (void)windowDidExitFullScreen:(NSNotification*)notification {
if (!shell_->has_frame()) {
NSWindow* window = shell_->GetNativeWindow();
[[window standardWindowButton:NSWindowFullScreenButton] setHidden:YES];
}
2014-11-25 05:16:51 +00:00
shell_->NotifyWindowLeaveFullScreen();
}
- (void)windowWillClose:(NSNotification*)notification {
shell_->NotifyWindowClosed();
2013-05-01 08:12:00 +00:00
}
2013-05-01 08:12:00 +00:00
- (BOOL)windowShouldClose:(id)window {
// When user tries to close the window by clicking the close button, we do
// not close the window immediately, instead we try to close the web page
// fisrt, and when the web page is closed the window will also be closed.
shell_->RequestToClosePage();
2013-05-01 08:12:00 +00:00
return NO;
}
@end
@interface AtomNSWindow : EventProcessingWindow {
@private
atom::NativeWindowMac* shell_;
bool enable_larger_than_screen_;
}
@property BOOL acceptsFirstMouse;
2015-04-27 04:08:22 +00:00
@property BOOL disableAutoHideCursor;
- (void)setShell:(atom::NativeWindowMac*)shell;
- (void)setEnableLargerThanScreen:(bool)enable;
@end
@implementation AtomNSWindow
- (void)setShell:(atom::NativeWindowMac*)shell {
shell_ = shell;
}
- (void)setEnableLargerThanScreen:(bool)enable {
enable_larger_than_screen_ = enable;
}
- (NSRect)constrainFrameRect:(NSRect)frameRect toScreen:(NSScreen*)screen {
// Resizing is disabled.
if (ScopedDisableResize::IsResizeDisabled())
return [self frame];
// Enable the window to be larger than screen.
if (enable_larger_than_screen_)
return frameRect;
else
return [super constrainFrameRect:frameRect toScreen:screen];
}
- (id)accessibilityAttributeValue:(NSString*)attribute {
2015-04-13 12:10:51 +00:00
if (![attribute isEqualToString:@"AXChildren"])
return [super accessibilityAttributeValue:attribute];
// Filter out objects that aren't the title bar buttons. This has the effect
// of removing the window title, which VoiceOver already sees.
// * 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.
NSPredicate *predicate = [NSPredicate predicateWithFormat:
@"(self isKindOfClass: %@) OR (self.className == %@)",
[NSButtonCell class],
@"RenderWidgetHostViewCocoa"];
NSArray *children = [super accessibilityAttributeValue:attribute];
return [children filteredArrayUsingPredicate:predicate];
}
@end
@interface ControlRegionView : NSView {
@private
atom::NativeWindowMac* shellWindow_; // Weak; owns self.
}
@end
@implementation ControlRegionView
- (id)initWithShellWindow:(atom::NativeWindowMac*)shellWindow {
if ((self = [super init]))
shellWindow_ = shellWindow;
return self;
}
- (BOOL)mouseDownCanMoveWindow {
return NO;
}
- (NSView*)hitTest:(NSPoint)aPoint {
if (!shellWindow_->IsWithinDraggableRegion(aPoint)) {
return nil;
}
return self;
}
- (void)mouseDown:(NSEvent*)event {
shellWindow_->HandleMouseEvent(event);
}
- (void)mouseDragged:(NSEvent*)event {
shellWindow_->HandleMouseEvent(event);
}
- (BOOL)acceptsFirstMouse:(NSEvent*)event {
return YES;
}
@end
2014-09-17 07:58:08 +00:00
@interface AtomProgressBar : NSProgressIndicator
@end
@implementation AtomProgressBar
- (void)drawRect:(NSRect)dirtyRect {
if (self.style != NSProgressIndicatorBarStyle)
return;
// Draw edges of rounded rect.
NSRect rect = NSInsetRect([self bounds], 1.0, 1.0);
2014-09-18 08:17:49 +00:00
CGFloat radius = rect.size.height / 2;
NSBezierPath* bezier_path = [NSBezierPath bezierPathWithRoundedRect:rect xRadius:radius yRadius:radius];
[bezier_path setLineWidth:2.0];
[[NSColor grayColor] set];
[bezier_path stroke];
2014-09-17 07:58:08 +00:00
// Fill the rounded rect.
2014-09-18 08:17:49 +00:00
rect = NSInsetRect(rect, 2.0, 2.0);
radius = rect.size.height / 2;
bezier_path = [NSBezierPath bezierPathWithRoundedRect:rect xRadius:radius yRadius:radius];
[bezier_path setLineWidth:1.0];
[bezier_path addClip];
2014-09-17 07:58:08 +00:00
// Calculate the progress width.
2014-09-18 08:17:49 +00:00
rect.size.width = floor(rect.size.width * ([self doubleValue] / [self maxValue]));
2014-09-17 07:58:08 +00:00
// Fill the progress bar with color blue.
[[NSColor colorWithSRGBRed:0.2 green:0.6 blue:1 alpha:1] set];
NSRectFill(rect);
}
@end
2013-04-12 07:04:46 +00:00
namespace atom {
NativeWindowMac::NativeWindowMac(
brightray::InspectableWebContents* web_contents,
const mate::Dictionary& options)
: NativeWindow(web_contents, options),
2013-04-12 07:04:46 +00:00
is_kiosk_(false),
attention_request_id_(0) {
int width = 800, height = 600;
options.Get(switches::kWidth, &width);
options.Get(switches::kHeight, &height);
2013-04-12 07:04:46 +00:00
2015-05-10 08:51:18 +00:00
NSRect main_screen_rect = [[[NSScreen screens] objectAtIndex:0] frame];
2013-04-12 07:04:46 +00:00
NSRect cocoa_bounds = NSMakeRect(
round((NSWidth(main_screen_rect) - width) / 2) ,
round((NSHeight(main_screen_rect) - height) / 2),
2013-04-12 07:04:46 +00:00
width,
height);
bool useStandardWindow = true;
options.Get(switches::kStandardWindow, &useStandardWindow);
bool resizable = true;
options.Get(switches::kResizable, &resizable);
2015-09-25 08:21:08 +00:00
// New title bar styles are available in Yosemite or newer
std::string titleBarStyle;
if (base::mac::IsOSYosemiteOrLater())
options.Get(switches::kTitleBarStyle, &titleBarStyle);
NSUInteger styleMask = NSTitledWindowMask | NSClosableWindowMask |
NSMiniaturizableWindowMask;
if (!useStandardWindow || transparent() || !has_frame()) {
styleMask |= NSTexturedBackgroundWindowMask;
}
if (resizable) {
styleMask |= NSResizableWindowMask;
}
2015-09-25 08:21:08 +00:00
if ((titleBarStyle == "hidden") || (titleBarStyle == "hidden-inset")) {
styleMask |= NSFullSizeContentViewWindowMask;
styleMask |= NSUnifiedTitleAndToolbarWindowMask;
}
window_.reset([[AtomNSWindow alloc]
initWithContentRect:cocoa_bounds
styleMask:styleMask
backing:NSBackingStoreBuffered
defer:YES]);
[window_ setShell:this];
[window_ setEnableLargerThanScreen:enable_larger_than_screen()];
window_delegate_.reset([[AtomNSWindowDelegate alloc] initWithShell:this]);
[window_ setDelegate:window_delegate_];
if (transparent()) {
2014-12-23 19:17:56 +00:00
// Make window has transparent background.
[window_ setOpaque:NO];
[window_ setHasShadow:NO];
[window_ setBackgroundColor:[NSColor clearColor]];
}
2015-04-25 02:35:28 +00:00
// Remove non-transparent corners, see http://git.io/vfonD.
if (!has_frame())
[window_ setOpaque:NO];
2014-12-23 19:17:56 +00:00
// We will manage window's lifetime ourselves.
2014-04-11 04:45:48 +00:00
[window_ setReleasedWhenClosed:NO];
// Hide the title bar.
2015-09-25 08:21:08 +00:00
if ((titleBarStyle == "hidden") || (titleBarStyle == "hidden-inset")) {
[window_ setTitlebarAppearsTransparent:YES];
[window_ setTitleVisibility:NSWindowTitleHidden];
if (titleBarStyle == "hidden-inset") {
base::scoped_nsobject<NSToolbar> toolbar(
[[NSToolbar alloc] initWithIdentifier:@"titlebarStylingToolbar"]);
[toolbar setShowsBaselineSeparator:NO];
[window_ setToolbar:toolbar];
}
// We should be aware of draggable regions when using hidden titlebar.
set_force_using_draggable_region(true);
}
// On OS X the initial window size doesn't include window frame.
bool use_content_size = false;
options.Get(switches::kUseContentSize, &use_content_size);
if (!has_frame() || !use_content_size)
SetSize(gfx::Size(width, height));
// Enable the NSView to accept first mouse event.
bool acceptsFirstMouse = false;
options.Get(switches::kAcceptFirstMouse, &acceptsFirstMouse);
[window_ setAcceptsFirstMouse:acceptsFirstMouse];
2013-04-12 07:04:46 +00:00
2015-04-27 04:08:22 +00:00
// Disable auto-hiding cursor.
bool disableAutoHideCursor = false;
options.Get(switches::kDisableAutoHideCursor, &disableAutoHideCursor);
[window_ setDisableAutoHideCursor:disableAutoHideCursor];
2013-04-12 07:04:46 +00:00
// Disable fullscreen button when 'fullscreen' is specified to false.
bool fullscreen;
if (!(options.Get(switches::kFullscreen, &fullscreen) &&
2013-04-12 07:04:46 +00:00
!fullscreen)) {
2014-04-11 04:45:48 +00:00
NSUInteger collectionBehavior = [window_ collectionBehavior];
2013-04-12 07:04:46 +00:00
collectionBehavior |= NSWindowCollectionBehaviorFullScreenPrimary;
2014-04-11 04:45:48 +00:00
[window_ setCollectionBehavior:collectionBehavior];
2013-04-12 07:04:46 +00:00
}
NSView* view = inspectable_web_contents()->GetView()->GetNativeView();
[view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
2013-04-12 07:04:46 +00:00
InstallView();
// Install the DraggableRegionView if it is forced to use draggable regions
// for normal window.
if (has_frame() && force_using_draggable_region())
InstallDraggableRegionView();
2013-04-12 07:04:46 +00:00
}
NativeWindowMac::~NativeWindowMac() {
Observe(nullptr);
2013-04-12 07:04:46 +00:00
}
void NativeWindowMac::Close() {
2014-04-11 04:45:48 +00:00
[window_ performClose:nil];
2013-04-12 07:04:46 +00:00
}
2013-05-01 08:12:00 +00:00
void NativeWindowMac::CloseImmediately() {
2014-04-11 04:45:48 +00:00
[window_ close];
2013-05-01 08:12:00 +00:00
}
2013-04-12 07:04:46 +00:00
void NativeWindowMac::Focus(bool focus) {
if (!IsVisible())
return;
if (focus) {
[[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
2014-04-11 04:45:48 +00:00
[window_ makeKeyAndOrderFront:nil];
} else {
2014-04-11 04:45:48 +00:00
[window_ orderBack:nil];
}
2013-04-12 07:04:46 +00:00
}
2013-05-16 14:56:52 +00:00
bool NativeWindowMac::IsFocused() {
2014-04-11 04:45:48 +00:00
return [window_ isKeyWindow];
2013-05-16 14:56:52 +00:00
}
2013-04-12 07:04:46 +00:00
void NativeWindowMac::Show() {
// This method is supposed to put focus on window, however if the app does not
// have focus then "makeKeyAndOrderFront" will only show the window.
[NSApp activateIgnoringOtherApps:YES];
2014-10-17 14:51:20 +00:00
[window_ makeKeyAndOrderFront:nil];
}
void NativeWindowMac::ShowInactive() {
[window_ orderFrontRegardless];
2013-04-12 07:04:46 +00:00
}
void NativeWindowMac::Hide() {
2014-04-11 04:45:48 +00:00
[window_ orderOut:nil];
2013-04-12 07:04:46 +00:00
}
2013-10-03 00:27:59 +00:00
bool NativeWindowMac::IsVisible() {
2014-04-11 04:45:48 +00:00
return [window_ isVisible];
2013-10-03 00:27:59 +00:00
}
2013-04-12 07:04:46 +00:00
void NativeWindowMac::Maximize() {
2014-04-11 04:45:48 +00:00
[window_ zoom:nil];
2013-04-12 07:04:46 +00:00
}
void NativeWindowMac::Unmaximize() {
2014-04-11 04:45:48 +00:00
[window_ zoom:nil];
2013-04-12 07:04:46 +00:00
}
2014-06-28 01:17:37 +00:00
bool NativeWindowMac::IsMaximized() {
2014-05-14 21:58:49 +00:00
return [window_ isZoomed];
}
2013-04-12 07:04:46 +00:00
void NativeWindowMac::Minimize() {
2014-04-11 04:45:48 +00:00
[window_ miniaturize:nil];
2013-04-12 07:04:46 +00:00
}
void NativeWindowMac::Restore() {
2014-04-11 04:45:48 +00:00
[window_ deminiaturize:nil];
2013-04-12 07:04:46 +00:00
}
2014-07-26 05:58:26 +00:00
bool NativeWindowMac::IsMinimized() {
return [window_ isMiniaturized];
}
2014-11-25 06:34:14 +00:00
void NativeWindowMac::SetFullScreen(bool fullscreen) {
if (fullscreen == IsFullscreen())
2013-04-12 07:04:46 +00:00
return;
if (!base::mac::IsOSLionOrLater()) {
LOG(ERROR) << "Fullscreen mode is only supported above Lion";
2013-04-12 07:04:46 +00:00
return;
}
2014-04-11 04:45:48 +00:00
[window_ toggleFullScreen:nil];
2013-04-12 07:04:46 +00:00
}
2015-04-21 13:35:36 +00:00
bool NativeWindowMac::IsFullscreen() const {
2014-04-11 04:45:48 +00:00
return [window_ styleMask] & NSFullScreenWindowMask;
2013-04-12 07:04:46 +00:00
}
void NativeWindowMac::SetBounds(const gfx::Rect& bounds) {
NSRect cocoa_bounds = NSMakeRect(bounds.x(), 0,
bounds.width(),
bounds.height());
// Flip coordinates based on the primary screen.
2015-05-10 08:51:18 +00:00
NSScreen* screen = [[NSScreen screens] objectAtIndex:0];
cocoa_bounds.origin.y =
NSHeight([screen frame]) - bounds.height() - bounds.y();
[window_ setFrame:cocoa_bounds display:YES];
}
gfx::Rect NativeWindowMac::GetBounds() {
NSRect frame = [window_ frame];
gfx::Rect bounds(frame.origin.x, 0, NSWidth(frame), NSHeight(frame));
2015-05-10 08:51:18 +00:00
NSScreen* screen = [[NSScreen screens] objectAtIndex:0];
bounds.set_y(NSHeight([screen frame]) - NSMaxY(frame));
return bounds;
}
void NativeWindowMac::SetContentSizeConstraints(
const extensions::SizeConstraints& size_constraints) {
auto convertSize = [this](const gfx::Size& size) {
// Our frameless window still has titlebar attached, so setting contentSize
// will result in actual content size being larger.
if (!has_frame()) {
NSRect frame = NSMakeRect(0, 0, size.width(), size.height());
NSRect content = [window_ contentRectForFrameRect:frame];
return content.size;
} else {
return NSMakeSize(size.width(), size.height());
}
};
2014-04-11 04:45:48 +00:00
NSView* content = [window_ contentView];
if (size_constraints.HasMinimumSize()) {
NSSize min_size = convertSize(size_constraints.GetMinimumSize());
[window_ setContentMinSize:[content convertSize:min_size toView:nil]];
}
if (size_constraints.HasMaximumSize()) {
NSSize max_size = convertSize(size_constraints.GetMaximumSize());
[window_ setContentMaxSize:[content convertSize:max_size toView:nil]];
}
NativeWindow::SetContentSizeConstraints(size_constraints);
2013-04-18 07:38:04 +00:00
}
2013-04-12 07:04:46 +00:00
void NativeWindowMac::SetResizable(bool resizable) {
// Change styleMask for frameless causes the window to change size, so we have
// to explicitly disables that.
ScopedDisableResize disable_resize;
2013-04-12 07:04:46 +00:00
if (resizable) {
2014-04-11 04:45:48 +00:00
[[window_ standardWindowButton:NSWindowZoomButton] setEnabled:YES];
2014-04-11 04:47:22 +00:00
[window_ setStyleMask:[window_ styleMask] | NSResizableWindowMask];
2013-04-12 07:04:46 +00:00
} else {
2014-04-11 04:45:48 +00:00
[[window_ standardWindowButton:NSWindowZoomButton] setEnabled:NO];
[window_ setStyleMask:[window_ styleMask] & (~NSResizableWindowMask)];
2013-04-12 07:04:46 +00:00
}
}
2013-04-18 07:38:04 +00:00
bool NativeWindowMac::IsResizable() {
2014-04-11 04:45:48 +00:00
return [window_ styleMask] & NSResizableWindowMask;
2013-04-18 07:38:04 +00:00
}
2013-04-12 07:04:46 +00:00
void NativeWindowMac::SetAlwaysOnTop(bool top) {
2014-04-11 04:45:48 +00:00
[window_ setLevel:(top ? NSFloatingWindowLevel : NSNormalWindowLevel)];
2013-04-12 07:04:46 +00:00
}
2013-04-18 07:38:04 +00:00
bool NativeWindowMac::IsAlwaysOnTop() {
2014-04-11 04:45:48 +00:00
return [window_ level] == NSFloatingWindowLevel;
2013-04-18 07:38:04 +00:00
}
void NativeWindowMac::Center() {
2014-04-11 04:45:48 +00:00
[window_ center];
2013-04-12 07:04:46 +00:00
}
void NativeWindowMac::SetTitle(const std::string& title) {
// We don't want the title to show in transparent or frameless window.
if (transparent() || !has_frame())
return;
2014-04-11 04:45:48 +00:00
[window_ setTitle:base::SysUTF8ToNSString(title)];
2013-04-12 07:04:46 +00:00
}
2013-04-18 06:30:05 +00:00
std::string NativeWindowMac::GetTitle() {
2014-04-11 04:45:48 +00:00
return base::SysNSStringToUTF8([window_ title]);
2013-04-18 06:30:05 +00:00
}
2013-04-12 07:04:46 +00:00
void NativeWindowMac::FlashFrame(bool flash) {
if (flash) {
attention_request_id_ = [NSApp requestUserAttention:NSInformationalRequest];
} else {
[NSApp cancelUserAttentionRequest:attention_request_id_];
attention_request_id_ = 0;
}
}
void NativeWindowMac::SetSkipTaskbar(bool skip) {
}
2013-04-12 07:04:46 +00:00
void NativeWindowMac::SetKiosk(bool kiosk) {
if (kiosk && !is_kiosk_) {
kiosk_options_ = [NSApp currentSystemPresentationOptions];
2013-04-12 07:04:46 +00:00
NSApplicationPresentationOptions options =
NSApplicationPresentationHideDock +
NSApplicationPresentationHideMenuBar +
2013-04-12 07:04:46 +00:00
NSApplicationPresentationDisableAppleMenu +
NSApplicationPresentationDisableProcessSwitching +
NSApplicationPresentationDisableForceQuit +
NSApplicationPresentationDisableSessionTermination +
NSApplicationPresentationDisableHideApplication;
[NSApp setPresentationOptions:options];
is_kiosk_ = true;
2014-11-25 06:34:14 +00:00
SetFullScreen(true);
} else if (!kiosk && is_kiosk_) {
is_kiosk_ = false;
2014-11-25 06:34:14 +00:00
SetFullScreen(false);
[NSApp setPresentationOptions:kiosk_options_];
2013-04-12 07:04:46 +00:00
}
}
bool NativeWindowMac::IsKiosk() {
return is_kiosk_;
}
void NativeWindowMac::SetRepresentedFilename(const std::string& filename) {
[window_ setRepresentedFilename:base::SysUTF8ToNSString(filename)];
}
std::string NativeWindowMac::GetRepresentedFilename() {
return base::SysNSStringToUTF8([window_ representedFilename]);
}
void NativeWindowMac::SetDocumentEdited(bool edited) {
[window_ setDocumentEdited:edited];
}
bool NativeWindowMac::IsDocumentEdited() {
return [window_ isDocumentEdited];
}
bool NativeWindowMac::HasModalDialog() {
2014-04-11 04:45:48 +00:00
return [window_ attachedSheet] != nil;
}
gfx::NativeWindow NativeWindowMac::GetNativeWindow() {
2014-04-11 04:45:48 +00:00
return window_;
}
2014-09-17 07:58:08 +00:00
void NativeWindowMac::SetProgressBar(double progress) {
NSDockTile* dock_tile = [NSApp dockTile];
// For the first time API invoked, we need to create a ContentView in DockTile.
if (dock_tile.contentView == NULL) {
NSImageView* image_view = [[NSImageView alloc] init];
[image_view setImage:[NSApp applicationIconImage]];
[dock_tile setContentView:image_view];
NSProgressIndicator* progress_indicator = [[AtomProgressBar alloc]
initWithFrame:NSMakeRect(0.0f, 0.0f, dock_tile.size.width, 15.0)];
[progress_indicator setStyle:NSProgressIndicatorBarStyle];
[progress_indicator setIndeterminate:NO];
[progress_indicator setBezeled:YES];
[progress_indicator setMinValue:0];
[progress_indicator setMaxValue:1];
[progress_indicator setHidden:NO];
[image_view addSubview:progress_indicator];
}
NSProgressIndicator* progress_indicator =
static_cast<NSProgressIndicator*>([[[dock_tile contentView] subviews]
objectAtIndex:0]);
if (progress < 0) {
[progress_indicator setHidden:YES];
} else if (progress > 1) {
[progress_indicator setHidden:NO];
2014-09-17 07:58:08 +00:00
[progress_indicator setIndeterminate:YES];
[progress_indicator setDoubleValue:1];
2014-09-17 07:58:08 +00:00
} else {
[progress_indicator setHidden:NO];
2014-09-17 07:58:08 +00:00
[progress_indicator setDoubleValue:progress];
}
[dock_tile display];
}
2015-02-11 01:14:26 +00:00
void NativeWindowMac::SetOverlayIcon(const gfx::Image& overlay,
const std::string& description) {
2015-02-07 19:56:03 +00:00
}
void NativeWindowMac::ShowDefinitionForSelection() {
2015-06-25 06:54:00 +00:00
if (!web_contents())
2014-12-19 20:48:53 +00:00
return;
2015-06-25 06:54:00 +00:00
auto rwhv = web_contents()->GetRenderWidgetHostView();
if (!rwhv)
return;
rwhv->ShowDefinitionForSelection();
}
void NativeWindowMac::SetVisibleOnAllWorkspaces(bool visible) {
NSUInteger collectionBehavior = [window_ collectionBehavior];
if (visible) {
collectionBehavior |= NSWindowCollectionBehaviorCanJoinAllSpaces;
} else {
collectionBehavior &= ~NSWindowCollectionBehaviorCanJoinAllSpaces;
}
[window_ setCollectionBehavior:collectionBehavior];
}
bool NativeWindowMac::IsVisibleOnAllWorkspaces() {
2015-03-27 11:41:07 +00:00
NSUInteger collectionBehavior = [window_ collectionBehavior];
return collectionBehavior & NSWindowCollectionBehaviorCanJoinAllSpaces;
}
bool NativeWindowMac::IsWithinDraggableRegion(NSPoint point) const {
if (!draggable_region())
return false;
2015-06-25 06:54:00 +00:00
if (!web_contents())
2014-12-23 20:31:27 +00:00
return false;
2015-06-25 06:54:00 +00:00
NSView* webView = web_contents()->GetNativeView();
NSInteger webViewHeight = NSHeight([webView bounds]);
// |draggable_region_| is stored in local platform-indepdent coordiate system
// while |point| is in local Cocoa coordinate system. Do the conversion
// to match these two.
return draggable_region()->contains(point.x, webViewHeight - point.y);
}
void NativeWindowMac::HandleMouseEvent(NSEvent* event) {
NSPoint eventLoc = [event locationInWindow];
NSRect mouseRect = [window_ convertRectToScreen:NSMakeRect(eventLoc.x, eventLoc.y, 0, 0)];
NSPoint current_mouse_location = mouseRect.origin;
if ([event type] == NSLeftMouseDown) {
2014-04-11 04:45:48 +00:00
NSPoint frame_origin = [window_ frame].origin;
last_mouse_offset_ = NSMakePoint(
frame_origin.x - current_mouse_location.x,
frame_origin.y - current_mouse_location.y);
} else if ([event type] == NSLeftMouseDragged) {
2014-04-11 04:45:48 +00:00
[window_ setFrameOrigin:NSMakePoint(
current_mouse_location.x + last_mouse_offset_.x,
current_mouse_location.y + last_mouse_offset_.y)];
}
}
void NativeWindowMac::HandleKeyboardEvent(
content::WebContents*,
const content::NativeWebKeyboardEvent& event) {
if (event.skip_in_browser ||
event.type == content::NativeWebKeyboardEvent::Char)
return;
if (event.os_event.window == window_.get()) {
EventProcessingWindow* event_window =
static_cast<EventProcessingWindow*>(window_);
DCHECK([event_window isKindOfClass:[EventProcessingWindow class]]);
[event_window redispatchKeyEvent:event.os_event];
} else {
// The event comes from detached devtools view, and it has already been
// handled by the devtools itself, we now send it to application menu to
// make menu acclerators work.
BOOL handled = [[NSApp mainMenu] performKeyEquivalent:event.os_event];
// Handle the cmd+~ shortcut.
if (!handled && (event.os_event.modifierFlags & NSCommandKeyMask) &&
(event.os_event.keyCode == 50 /* ~ key */))
Focus(true);
}
}
gfx::Size NativeWindowMac::ContentSizeToWindowSize(const gfx::Size& size) {
if (!has_frame())
return size;
NSRect content = NSMakeRect(0, 0, size.width(), size.height());
NSRect frame = [window_ frameRectForContentRect:content];
return gfx::Size(frame.size);
}
gfx::Size NativeWindowMac::WindowSizeToContentSize(const gfx::Size& size) {
if (!has_frame())
return size;
NSRect frame = NSMakeRect(0, 0, size.width(), size.height());
NSRect content = [window_ contentRectForFrameRect:frame];
return gfx::Size(content.size);
}
2013-04-12 07:04:46 +00:00
void NativeWindowMac::InstallView() {
// Make sure the bottom corner is rounded: http://crbug.com/396264.
[[window_ contentView] setWantsLayer:YES];
2013-04-12 08:41:15 +00:00
NSView* view = inspectable_web_contents()->GetView()->GetNativeView();
if (has_frame()) {
2014-04-11 04:45:48 +00:00
[view setFrame:[[window_ contentView] bounds]];
[[window_ contentView] addSubview:view];
} else {
// In OSX 10.10, adding subviews to the root view for the NSView hierarchy
// produces warnings. To eliminate the warnings, we resize the contentView
// to fill the window, and add subviews to that.
// http://crbug.com/380412
content_view_.reset([[FullSizeContentView alloc] init]);
[content_view_
setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
[content_view_ setFrame:[[[window_ contentView] superview] bounds]];
[window_ setContentView:content_view_];
[view setFrame:[content_view_ bounds]];
[content_view_ addSubview:view];
InstallDraggableRegionView();
2014-04-11 04:45:48 +00:00
[[window_ standardWindowButton:NSWindowZoomButton] setHidden:YES];
[[window_ standardWindowButton:NSWindowMiniaturizeButton] setHidden:YES];
[[window_ standardWindowButton:NSWindowCloseButton] setHidden:YES];
// Some third-party OS X utilities check the zoom button's enabled state to
// determine whether to show custom UI on hover, so we disable it here to
// prevent them from doing so in a frameless app window.
[[window_ standardWindowButton:NSWindowZoomButton] setEnabled:NO];
}
2013-04-12 07:04:46 +00:00
}
void NativeWindowMac::UninstallView() {
NSView* view = inspectable_web_contents()->GetView()->GetNativeView();
2013-04-12 07:04:46 +00:00
[view removeFromSuperview];
}
void NativeWindowMac::InstallDraggableRegionView() {
2015-06-25 06:54:00 +00:00
NSView* webView = web_contents()->GetNativeView();
base::scoped_nsobject<NSView> controlRegion(
[[ControlRegionView alloc] initWithShellWindow:this]);
[controlRegion setFrame:NSMakeRect(0, 0,
NSWidth([webView bounds]),
NSHeight([webView bounds]))];
[webView addSubview:controlRegion];
}
2013-04-12 07:04:46 +00:00
// static
NativeWindow* NativeWindow::Create(
brightray::InspectableWebContents* inspectable_web_contents,
const mate::Dictionary& options) {
return new NativeWindowMac(inspectable_web_contents, options);
2013-04-12 07:04:46 +00:00
}
} // namespace atom