1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/views/widget/native_widget_mac.h"
7 #import <Cocoa/Cocoa.h>
9 #include "base/mac/foundation_util.h"
10 #include "base/mac/scoped_nsobject.h"
11 #include "base/strings/sys_string_conversions.h"
12 #import "ui/base/cocoa/window_size_constants.h"
13 #include "ui/gfx/font_list.h"
14 #import "ui/gfx/mac/coordinate_conversion.h"
15 #include "ui/native_theme/native_theme.h"
16 #import "ui/views/cocoa/bridged_content_view.h"
17 #import "ui/views/cocoa/bridged_native_widget.h"
18 #import "ui/views/cocoa/native_widget_mac_nswindow.h"
19 #import "ui/views/cocoa/views_nswindow_delegate.h"
20 #include "ui/views/window/native_frame_view.h"
25 NSInteger StyleMaskForParams(const Widget::InitParams& params) {
26 // TODO(tapted): Determine better masks when there are use cases for it.
27 if (params.remove_standard_frame)
28 return NSBorderlessWindowMask;
30 if (params.type == Widget::InitParams::TYPE_WINDOW) {
31 return NSTitledWindowMask | NSClosableWindowMask |
32 NSMiniaturizableWindowMask | NSResizableWindowMask;
34 return NSBorderlessWindowMask;
37 gfx::Size WindowSizeForClientAreaSize(NSWindow* window, const gfx::Size& size) {
38 NSRect content_rect = NSMakeRect(0, 0, size.width(), size.height());
39 NSRect frame_rect = [window frameRectForContentRect:content_rect];
40 return gfx::Size(NSWidth(frame_rect), NSHeight(frame_rect));
45 ////////////////////////////////////////////////////////////////////////////////
46 // NativeWidgetMac, public:
48 NativeWidgetMac::NativeWidgetMac(internal::NativeWidgetDelegate* delegate)
49 : delegate_(delegate),
50 bridge_(new BridgedNativeWidget(this)),
51 ownership_(Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET) {
54 NativeWidgetMac::~NativeWidgetMac() {
55 if (ownership_ == Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET)
62 BridgedNativeWidget* NativeWidgetMac::GetBridgeForNativeWindow(
63 gfx::NativeWindow window) {
64 id<NSWindowDelegate> window_delegate = [window delegate];
65 if ([window_delegate respondsToSelector:@selector(nativeWidgetMac)]) {
66 ViewsNSWindowDelegate* delegate =
67 base::mac::ObjCCastStrict<ViewsNSWindowDelegate>(window_delegate);
68 return [delegate nativeWidgetMac]->bridge_.get();
70 return nullptr; // Not created by NativeWidgetMac.
73 void NativeWidgetMac::OnWindowWillClose() {
74 delegate_->OnNativeWidgetDestroying();
75 // Note: If closed via CloseNow(), |bridge_| will already be reset. If closed
76 // by the user, or via Close() and a RunLoop, this will reset it.
78 delegate_->OnNativeWidgetDestroyed();
79 if (ownership_ == Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET)
83 ////////////////////////////////////////////////////////////////////////////////
84 // NativeWidgetMac, internal::NativeWidgetPrivate implementation:
86 void NativeWidgetMac::InitNativeWidget(const Widget::InitParams& params) {
87 ownership_ = params.ownership;
89 NSInteger style_mask = StyleMaskForParams(params);
90 base::scoped_nsobject<NSWindow> window([[NativeWidgetMacNSWindow alloc]
91 initWithContentRect:ui::kWindowSizeDeterminedLater
93 backing:NSBackingStoreBuffered
95 [window setReleasedWhenClosed:NO]; // Owned by scoped_nsobject.
96 bridge_->Init(window, params);
98 delegate_->OnNativeWidgetCreated(true);
100 bridge_->SetFocusManager(GetWidget()->GetFocusManager());
102 DCHECK(GetWidget()->GetRootView());
103 bridge_->SetRootView(GetWidget()->GetRootView());
105 // "Infer" must be handled by ViewsDelegate::OnBeforeWidgetInit().
106 DCHECK_NE(Widget::InitParams::INFER_OPACITY, params.opacity);
107 bool translucent = params.opacity == Widget::InitParams::TRANSLUCENT_WINDOW;
108 switch (params.layer_type) {
109 case aura::WINDOW_LAYER_NONE:
111 case aura::WINDOW_LAYER_TEXTURED:
112 bridge_->CreateLayer(ui::LAYER_TEXTURED, translucent);
114 case aura::WINDOW_LAYER_NOT_DRAWN:
115 bridge_->CreateLayer(ui::LAYER_NOT_DRAWN, translucent);
117 case aura::WINDOW_LAYER_SOLID_COLOR:
118 bridge_->CreateLayer(ui::LAYER_SOLID_COLOR, translucent);
123 NonClientFrameView* NativeWidgetMac::CreateNonClientFrameView() {
124 return new NativeFrameView(GetWidget());
127 bool NativeWidgetMac::ShouldUseNativeFrame() const {
131 bool NativeWidgetMac::ShouldWindowContentsBeTransparent() const {
132 // On Windows, this returns false when DWM is unavailable (e.g. XP, RDP or
133 // classic mode). OSX always has a compositing window manager.
137 void NativeWidgetMac::FrameTypeChanged() {
141 Widget* NativeWidgetMac::GetWidget() {
142 return delegate_->AsWidget();
145 const Widget* NativeWidgetMac::GetWidget() const {
146 return delegate_->AsWidget();
149 gfx::NativeView NativeWidgetMac::GetNativeView() const {
150 // Returns a BridgedContentView, unless there is no views::RootView set.
151 return [GetNativeWindow() contentView];
154 gfx::NativeWindow NativeWidgetMac::GetNativeWindow() const {
155 return bridge_ ? bridge_->ns_window() : nil;
158 Widget* NativeWidgetMac::GetTopLevelWidget() {
159 NativeWidgetPrivate* native_widget = GetTopLevelNativeWidget(GetNativeView());
160 return native_widget ? native_widget->GetWidget() : NULL;
163 const ui::Compositor* NativeWidgetMac::GetCompositor() const {
164 return bridge_ && bridge_->layer() ? bridge_->layer()->GetCompositor()
168 const ui::Layer* NativeWidgetMac::GetLayer() const {
169 return bridge_ ? bridge_->layer() : nullptr;
172 void NativeWidgetMac::ReorderNativeViews() {
174 bridge_->SetRootView(GetWidget()->GetRootView());
177 void NativeWidgetMac::ViewRemoved(View* view) {
181 void NativeWidgetMac::SetNativeWindowProperty(const char* name, void* value) {
183 bridge_->SetNativeWindowProperty(name, value);
186 void* NativeWidgetMac::GetNativeWindowProperty(const char* name) const {
188 return bridge_->GetNativeWindowProperty(name);
193 TooltipManager* NativeWidgetMac::GetTooltipManager() const {
198 void NativeWidgetMac::SetCapture() {
199 if (bridge_ && !bridge_->HasCapture())
200 bridge_->AcquireCapture();
203 void NativeWidgetMac::ReleaseCapture() {
205 bridge_->ReleaseCapture();
208 bool NativeWidgetMac::HasCapture() const {
209 return bridge_ && bridge_->HasCapture();
212 InputMethod* NativeWidgetMac::CreateInputMethod() {
213 return bridge_ ? bridge_->CreateInputMethod() : NULL;
216 internal::InputMethodDelegate* NativeWidgetMac::GetInputMethodDelegate() {
217 return bridge_.get();
220 ui::InputMethod* NativeWidgetMac::GetHostInputMethod() {
221 return bridge_ ? bridge_->GetHostInputMethod() : NULL;
224 void NativeWidgetMac::CenterWindow(const gfx::Size& size) {
225 SetSize(WindowSizeForClientAreaSize(GetNativeWindow(), size));
226 // Note that this is not the precise center of screen, but it is the standard
227 // location for windows like dialogs to appear on screen for Mac.
228 // TODO(tapted): If there is a parent window, center in that instead.
229 [GetNativeWindow() center];
232 void NativeWidgetMac::GetWindowPlacement(gfx::Rect* bounds,
233 ui::WindowShowState* maximized) const {
237 bool NativeWidgetMac::SetWindowTitle(const base::string16& title) {
238 NSWindow* window = GetNativeWindow();
239 NSString* current_title = [window title];
240 NSString* new_title = SysUTF16ToNSString(title);
241 if ([current_title isEqualToString:new_title])
244 [window setTitle:new_title];
248 void NativeWidgetMac::SetWindowIcons(const gfx::ImageSkia& window_icon,
249 const gfx::ImageSkia& app_icon) {
253 void NativeWidgetMac::InitModalType(ui::ModalType modal_type) {
257 gfx::Rect NativeWidgetMac::GetWindowBoundsInScreen() const {
258 return gfx::ScreenRectFromNSRect([GetNativeWindow() frame]);
261 gfx::Rect NativeWidgetMac::GetClientAreaBoundsInScreen() const {
262 NSWindow* window = GetNativeWindow();
263 return gfx::ScreenRectFromNSRect(
264 [window contentRectForFrameRect:[window frame]]);
267 gfx::Rect NativeWidgetMac::GetRestoredBounds() const {
268 return bridge_ ? bridge_->GetRestoredBounds() : gfx::Rect();
271 void NativeWidgetMac::SetBounds(const gfx::Rect& bounds) {
273 bridge_->SetBounds(bounds);
276 void NativeWidgetMac::SetSize(const gfx::Size& size) {
277 // Ensure the top-left corner stays in-place (rather than the bottom-left,
278 // which -[NSWindow setContentSize:] would do).
279 SetBounds(gfx::Rect(GetWindowBoundsInScreen().origin(), size));
282 void NativeWidgetMac::StackAbove(gfx::NativeView native_view) {
286 void NativeWidgetMac::StackAtTop() {
290 void NativeWidgetMac::StackBelow(gfx::NativeView native_view) {
294 void NativeWidgetMac::SetShape(gfx::NativeRegion shape) {
298 void NativeWidgetMac::Close() {
302 // Clear the view early to suppress repaints.
303 bridge_->SetRootView(NULL);
305 NSWindow* window = GetNativeWindow();
306 // Calling performClose: will momentarily highlight the close button, but
307 // AppKit will reject it if there is no close button.
308 SEL close_selector = ([window styleMask] & NSClosableWindowMask)
309 ? @selector(performClose:)
311 [window performSelector:close_selector withObject:nil afterDelay:0];
314 void NativeWidgetMac::CloseNow() {
315 // Reset |bridge_| to NULL before destroying it.
316 scoped_ptr<BridgedNativeWidget> bridge(bridge_.Pass());
319 void NativeWidgetMac::Show() {
320 ShowWithWindowState(ui::SHOW_STATE_NORMAL);
323 void NativeWidgetMac::Hide() {
327 bridge_->SetVisibilityState(BridgedNativeWidget::HIDE_WINDOW);
330 void NativeWidgetMac::ShowMaximizedWithBounds(
331 const gfx::Rect& restored_bounds) {
335 void NativeWidgetMac::ShowWithWindowState(ui::WindowShowState state) {
340 case ui::SHOW_STATE_DEFAULT:
341 case ui::SHOW_STATE_NORMAL:
342 case ui::SHOW_STATE_INACTIVE:
344 case ui::SHOW_STATE_MINIMIZED:
345 case ui::SHOW_STATE_MAXIMIZED:
346 case ui::SHOW_STATE_FULLSCREEN:
349 case ui::SHOW_STATE_END:
353 bridge_->SetVisibilityState(state == ui::SHOW_STATE_INACTIVE
354 ? BridgedNativeWidget::SHOW_INACTIVE
355 : BridgedNativeWidget::SHOW_AND_ACTIVATE_WINDOW);
358 bool NativeWidgetMac::IsVisible() const {
359 return bridge_ && bridge_->window_visible();
362 void NativeWidgetMac::Activate() {
366 bridge_->SetVisibilityState(BridgedNativeWidget::SHOW_AND_ACTIVATE_WINDOW);
369 void NativeWidgetMac::Deactivate() {
373 bool NativeWidgetMac::IsActive() const {
374 return [GetNativeWindow() isKeyWindow];
377 void NativeWidgetMac::SetAlwaysOnTop(bool always_on_top) {
381 bool NativeWidgetMac::IsAlwaysOnTop() const {
386 void NativeWidgetMac::SetVisibleOnAllWorkspaces(bool always_visible) {
390 void NativeWidgetMac::Maximize() {
391 NOTIMPLEMENTED(); // See IsMaximized().
394 void NativeWidgetMac::Minimize() {
395 NSWindow* window = GetNativeWindow();
396 // Calling performMiniaturize: will momentarily highlight the button, but
397 // AppKit will reject it if there is no miniaturize button.
398 if ([window styleMask] & NSMiniaturizableWindowMask)
399 [window performMiniaturize:nil];
401 [window miniaturize:nil];
404 bool NativeWidgetMac::IsMaximized() const {
405 // The window frame isn't altered on Mac unless going fullscreen. The green
406 // "+" button just makes the window bigger. So, always false.
410 bool NativeWidgetMac::IsMinimized() const {
411 return [GetNativeWindow() isMiniaturized];
414 void NativeWidgetMac::Restore() {
415 [GetNativeWindow() deminiaturize:nil];
418 void NativeWidgetMac::SetFullscreen(bool fullscreen) {
419 if (!bridge_ || fullscreen == IsFullscreen())
422 bridge_->ToggleDesiredFullscreenState();
425 bool NativeWidgetMac::IsFullscreen() const {
426 return bridge_ && bridge_->target_fullscreen_state();
429 void NativeWidgetMac::SetOpacity(unsigned char opacity) {
433 void NativeWidgetMac::SetUseDragFrame(bool use_drag_frame) {
437 void NativeWidgetMac::FlashFrame(bool flash_frame) {
441 void NativeWidgetMac::RunShellDrag(View* view,
442 const ui::OSExchangeData& data,
443 const gfx::Point& location,
445 ui::DragDropTypes::DragEventSource source) {
449 void NativeWidgetMac::SchedulePaintInRect(const gfx::Rect& rect) {
450 // TODO(tapted): This should use setNeedsDisplayInRect:, once the coordinate
451 // system of |rect| has been converted.
452 [GetNativeView() setNeedsDisplay:YES];
453 if (bridge_ && bridge_->layer())
454 bridge_->layer()->SchedulePaint(rect);
457 void NativeWidgetMac::SetCursor(gfx::NativeCursor cursor) {
459 bridge_->SetCursor(cursor);
462 bool NativeWidgetMac::IsMouseEventsEnabled() const {
467 void NativeWidgetMac::ClearNativeFocus() {
468 // To quote DesktopWindowTreeHostX11, "This method is weird and misnamed."
469 // The goal is to set focus to the content window, thereby removing focus from
470 // any NSView in the window that doesn't belong to toolkit-views.
471 [GetNativeWindow() makeFirstResponder:GetNativeView()];
474 gfx::Rect NativeWidgetMac::GetWorkAreaBoundsInScreen() const {
479 Widget::MoveLoopResult NativeWidgetMac::RunMoveLoop(
480 const gfx::Vector2d& drag_offset,
481 Widget::MoveLoopSource source,
482 Widget::MoveLoopEscapeBehavior escape_behavior) {
484 return Widget::MOVE_LOOP_CANCELED;
487 void NativeWidgetMac::EndMoveLoop() {
491 void NativeWidgetMac::SetVisibilityChangedAnimationsEnabled(bool value) {
495 void NativeWidgetMac::SetVisibilityAnimationDuration(
496 const base::TimeDelta& duration) {
500 void NativeWidgetMac::SetVisibilityAnimationTransition(
501 Widget::VisibilityTransition transition) {
505 ui::NativeTheme* NativeWidgetMac::GetNativeTheme() const {
506 return ui::NativeTheme::instance();
509 void NativeWidgetMac::OnRootViewLayout() {
513 bool NativeWidgetMac::IsTranslucentWindowOpacitySupported() const {
517 void NativeWidgetMac::OnSizeConstraintsChanged() {
521 void NativeWidgetMac::RepostNativeEvent(gfx::NativeEvent native_event) {
525 ////////////////////////////////////////////////////////////////////////////////
528 bool Widget::ConvertRect(const Widget* source,
529 const Widget* target,
536 ////////////////////////////////////////////////////////////////////////////////
537 // internal::NativeWidgetPrivate, public:
540 NativeWidgetPrivate* NativeWidgetPrivate::CreateNativeWidget(
541 internal::NativeWidgetDelegate* delegate) {
542 return new NativeWidgetMac(delegate);
546 NativeWidgetPrivate* NativeWidgetPrivate::GetNativeWidgetForNativeView(
547 gfx::NativeView native_view) {
548 return GetNativeWidgetForNativeWindow([native_view window]);
552 NativeWidgetPrivate* NativeWidgetPrivate::GetNativeWidgetForNativeWindow(
553 gfx::NativeWindow native_window) {
554 id<NSWindowDelegate> window_delegate = [native_window delegate];
555 if ([window_delegate respondsToSelector:@selector(nativeWidgetMac)]) {
556 ViewsNSWindowDelegate* delegate =
557 base::mac::ObjCCastStrict<ViewsNSWindowDelegate>(window_delegate);
558 return [delegate nativeWidgetMac];
560 return NULL; // Not created by NativeWidgetMac.
564 NativeWidgetPrivate* NativeWidgetPrivate::GetTopLevelNativeWidget(
565 gfx::NativeView native_view) {
566 BridgedNativeWidget* bridge =
567 NativeWidgetMac::GetBridgeForNativeWindow([native_view window]);
571 for (BridgedNativeWidget* parent;
572 (parent = bridge->parent());
575 return bridge->native_widget_mac();
579 void NativeWidgetPrivate::GetAllChildWidgets(gfx::NativeView native_view,
580 Widget::Widgets* children) {
581 BridgedNativeWidget* bridge =
582 NativeWidgetMac::GetBridgeForNativeWindow([native_view window]);
586 // If |native_view| is a subview of the contentView, it will share an
587 // NSWindow, but will itself be a native child of the Widget. That is, adding
588 // bridge->..->GetWidget() to |children| would be adding the _parent_ of
589 // |native_view|, not the Widget for |native_view|. |native_view| doesn't have
590 // a corresponding Widget of its own in this case (and so can't have Widget
591 // children of its own on Mac).
592 if (bridge->ns_view() != native_view)
595 // Code expects widget for |native_view| to be added to |children|.
596 if (bridge->native_widget_mac()->GetWidget())
597 children->insert(bridge->native_widget_mac()->GetWidget());
599 for (BridgedNativeWidget* child : bridge->child_windows())
600 GetAllChildWidgets(child->ns_view(), children);
604 void NativeWidgetPrivate::GetAllOwnedWidgets(gfx::NativeView native_view,
605 Widget::Widgets* owned) {
610 void NativeWidgetPrivate::ReparentNativeView(gfx::NativeView native_view,
611 gfx::NativeView new_parent) {
616 bool NativeWidgetPrivate::IsMouseButtonDown() {
617 return [NSEvent pressedMouseButtons] != 0;
621 gfx::FontList NativeWidgetPrivate::GetWindowTitleFontList() {
623 return gfx::FontList();
626 } // namespace internal