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 #ifndef UI_VIEWS_COCOA_BRIDGED_NATIVE_WIDGET_H_
6 #define UI_VIEWS_COCOA_BRIDGED_NATIVE_WIDGET_H_
8 #import <Cocoa/Cocoa.h>
11 #import "base/mac/scoped_nsobject.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "ui/compositor/layer_owner.h"
14 #import "ui/accelerated_widget_mac/accelerated_widget_mac.h"
15 #import "ui/views/cocoa/bridged_native_widget_owner.h"
16 #import "ui/views/cocoa/cocoa_mouse_capture_delegate.h"
17 #import "ui/views/focus/focus_manager.h"
18 #include "ui/views/ime/input_method_delegate.h"
19 #include "ui/views/views_export.h"
20 #include "ui/views/widget/widget.h"
22 @
class BridgedContentView
;
23 @
class ViewsNSWindowDelegate
;
31 class CocoaMouseCapture
;
33 class NativeWidgetMac
;
36 // A bridge to an NSWindow managed by an instance of NativeWidgetMac or
37 // DesktopNativeWidgetMac. Serves as a helper class to bridge requests from the
38 // NativeWidgetMac to the Cocoa window. Behaves a bit like an aura::Window.
39 class VIEWS_EXPORT BridgedNativeWidget
: public ui::LayerDelegate
,
40 public ui::LayerOwner
,
41 public internal::InputMethodDelegate
,
42 public CocoaMouseCaptureDelegate
,
43 public FocusChangeListener
,
44 public ui::AcceleratedWidgetMacNSView
,
45 public BridgedNativeWidgetOwner
{
47 // Ways of changing the visibility of the bridged NSWindow.
48 enum WindowVisibilityState
{
49 HIDE_WINDOW
, // Hides with -[NSWindow orderOut:].
50 SHOW_AND_ACTIVATE_WINDOW
, // Shows with -[NSWindow makeKeyAndOrderFront:].
51 SHOW_INACTIVE
, // Shows with -[NSWindow orderWindow:..]. Orders
52 // the window above its parent if it has one.
55 // Return the size that |window| will take for the given client area |size|,
56 // based on its current style mask.
57 static gfx::Size
GetWindowSizeForClientSize(NSWindow
* window
,
58 const gfx::Size
& size
);
60 // Creates one side of the bridge. |parent| must not be NULL.
61 explicit BridgedNativeWidget(NativeWidgetMac
* parent
);
62 ~BridgedNativeWidget() override
;
64 // Initialize the bridge, "retains" ownership of |window|.
65 void Init(base::scoped_nsobject
<NSWindow
> window
,
66 const Widget::InitParams
& params
);
68 // Sets or clears the focus manager to use for tracking focused views.
69 // This does NOT take ownership of |focus_manager|.
70 void SetFocusManager(FocusManager
* focus_manager
);
72 // Changes the bounds of the window and the hosted layer if present. The
73 // origin is a location in screen coordinates except for "child" windows,
74 // which are positioned relative to their parent(). SetBounds() considers a
75 // "child" window to be one initialized with InitParams specifying all of:
76 // a |parent| NSWindow, the |child| attribute, and a |type| that
77 // views::GetAuraWindowTypeForWidgetType does not consider a "popup" type.
78 void SetBounds(const gfx::Rect
& new_bounds
);
80 // Set or clears the views::View bridged by the content view. This does NOT
81 // take ownership of |view|.
82 void SetRootView(views::View
* view
);
84 // Sets the desired visibility of the window and updates the visibility of
85 // descendant windows where necessary.
86 void SetVisibilityState(WindowVisibilityState new_state
);
88 // Acquiring mouse capture first steals capture from any existing
89 // CocoaMouseCaptureDelegate, then captures all mouse events until released.
90 void AcquireCapture();
91 void ReleaseCapture();
95 void SetNativeWindowProperty(const char* key
, void* value
);
96 void* GetNativeWindowProperty(const char* key
) const;
98 // Sets the cursor associated with the NSWindow. Retains |cursor|.
99 void SetCursor(NSCursor
* cursor
);
101 // Called internally by the NSWindowDelegate when the window is closing.
102 void OnWindowWillClose();
104 // Called by the NSWindowDelegate when a fullscreen operation begins. If
105 // |target_fullscreen_state| is true, the target state is fullscreen.
106 // Otherwise, a transition has begun to come out of fullscreen.
107 void OnFullscreenTransitionStart(bool target_fullscreen_state
);
109 // Called when a fullscreen transition completes. If target_fullscreen_state()
110 // does not match |actual_fullscreen_state|, a new transition will begin.
111 void OnFullscreenTransitionComplete(bool actual_fullscreen_state
);
113 // Transition the window into or out of fullscreen. This will immediately
114 // invert the value of target_fullscreen_state().
115 void ToggleDesiredFullscreenState();
117 // Called by the NSWindowDelegate when the size of the window changes.
118 void OnSizeChanged();
120 // Called by the NSWindowDelegate when the visibility of the window may have
121 // changed. For example, due to a (de)miniaturize operation, or the window
122 // being reordered in (or out of) the screen list.
123 void OnVisibilityChanged();
125 // Explicitly set the visibility. This is called when Cocoa requests a draw,
126 // but hasn't updated the value of -[NSWindow isVisible] yet.
127 void OnVisibilityChangedTo(bool new_visibility
);
129 // Called by the NSWindowDelegate on a scale factor or color space change.
130 void OnBackingPropertiesChanged();
132 // Called by the NSWindowDelegate when the window becomes or resigns key.
133 void OnWindowKeyStatusChangedTo(bool is_key
);
135 // Called by NativeWidgetMac when the window size constraints change.
136 void OnSizeConstraintsChanged();
138 // See widget.h for documentation.
139 InputMethod
* CreateInputMethod();
140 ui::InputMethod
* GetHostInputMethod();
142 // The restored bounds will be derived from the current NSWindow frame unless
143 // fullscreen or transitioning between fullscreen states.
144 gfx::Rect
GetRestoredBounds() const;
146 // Creates a ui::Compositor which becomes responsible for drawing the window.
147 void CreateLayer(ui::LayerType layer_type
, bool translucent
);
149 NativeWidgetMac
* native_widget_mac() { return native_widget_mac_
; }
150 BridgedContentView
* ns_view() { return bridged_view_
; }
151 NSWindow
* ns_window() { return window_
; }
153 TooltipManager
* tooltip_manager() { return tooltip_manager_
.get(); }
155 // The parent widget specified in Widget::InitParams::parent. If non-null, the
156 // parent will close children before the parent closes, and children will be
157 // raised above their parent when window z-order changes.
158 BridgedNativeWidgetOwner
* parent() { return parent_
; }
159 const std::vector
<BridgedNativeWidget
*>& child_windows() {
160 return child_windows_
;
163 bool target_fullscreen_state() const { return target_fullscreen_state_
; }
164 bool window_visible() { return window_visible_
; }
166 // Overridden from internal::InputMethodDelegate:
167 void DispatchKeyEventPostIME(const ui::KeyEvent
& key
) override
;
170 // Closes all child windows. BridgedNativeWidget children will be destroyed.
171 void RemoveOrDestroyChildren();
173 // Notify descendants of a visibility change.
174 void NotifyVisibilityChangeDown();
176 // Essentially NativeWidgetMac::GetClientAreaBoundsInScreen().size(), but no
177 // coordinate transformations are required from AppKit coordinates.
178 gfx::Size
GetClientAreaSize() const;
180 // Creates an owned ui::Compositor. For consistency, these functions reflect
181 // those in aura::WindowTreeHost.
182 void CreateCompositor();
183 void InitCompositor();
184 void DestroyCompositor();
186 // Installs the NSView for hosting the composited layer. It is later provided
187 // to |compositor_widget_| via AcceleratedWidgetGetNSView().
188 void AddCompositorSuperview();
190 // Size the layer to match the client area bounds, taking into account display
192 void UpdateLayerProperties();
194 // Overridden from CocoaMouseCaptureDelegate:
195 void PostCapturedEvent(NSEvent
* event
) override
;
196 void OnMouseCaptureLost() override
;
198 // Returns a properties dictionary associated with the NSWindow.
199 // Creates and attaches a new instance if not found.
200 NSMutableDictionary
* GetWindowProperties() const;
202 // Overridden from FocusChangeListener:
203 void OnWillChangeFocus(View
* focused_before
,
204 View
* focused_now
) override
;
205 void OnDidChangeFocus(View
* focused_before
,
206 View
* focused_now
) override
;
208 // Overridden from ui::LayerDelegate:
209 void OnPaintLayer(const ui::PaintContext
& context
) override
;
210 void OnDelegatedFrameDamage(const gfx::Rect
& damage_rect_in_dip
) override
;
211 void OnDeviceScaleFactorChanged(float device_scale_factor
) override
;
212 base::Closure
PrepareForLayerBoundsChange() override
;
214 // Overridden from ui::AcceleratedWidgetMac:
215 NSView
* AcceleratedWidgetGetNSView() const override
;
216 bool AcceleratedWidgetShouldIgnoreBackpressure() const override
;
217 void AcceleratedWidgetSwapCompleted(
218 const std::vector
<ui::LatencyInfo
>& latency_info
) override
;
219 void AcceleratedWidgetHitError() override
;
221 // Overridden from BridgedNativeWidgetOwner:
222 NSWindow
* GetNSWindow() override
;
223 gfx::Vector2d
GetChildWindowOffset() const override
;
224 bool IsVisibleParent() const override
;
225 void RemoveChildWindow(BridgedNativeWidget
* child
) override
;
227 views::NativeWidgetMac
* native_widget_mac_
; // Weak. Owns this.
228 base::scoped_nsobject
<NSWindow
> window_
;
229 base::scoped_nsobject
<ViewsNSWindowDelegate
> window_delegate_
;
230 base::scoped_nsobject
<BridgedContentView
> bridged_view_
;
231 scoped_ptr
<ui::InputMethod
> input_method_
;
232 scoped_ptr
<CocoaMouseCapture
> mouse_capture_
;
233 scoped_ptr
<TooltipManager
> tooltip_manager_
;
234 FocusManager
* focus_manager_
; // Weak. Owned by our Widget.
235 Widget::InitParams::Type widget_type_
;
237 BridgedNativeWidgetOwner
* parent_
; // Weak. If non-null, owns this.
238 std::vector
<BridgedNativeWidget
*> child_windows_
;
240 base::scoped_nsobject
<NSView
> compositor_superview_
;
241 scoped_ptr
<ui::AcceleratedWidgetMac
> compositor_widget_
;
242 scoped_ptr
<ui::Compositor
> compositor_
;
244 // Tracks the bounds when the window last started entering fullscreen. Used to
245 // provide an answer for GetRestoredBounds(), but not ever sent to Cocoa (it
246 // has its own copy, but doesn't provide access to it).
247 gfx::Rect bounds_before_fullscreen_
;
249 // Whether this window wants to be fullscreen. If a fullscreen animation is in
250 // progress then it might not be actually fullscreen.
251 bool target_fullscreen_state_
;
253 // Whether this window is in a fullscreen transition, and the fullscreen state
254 // can not currently be changed.
255 bool in_fullscreen_transition_
;
257 // Stores the value last read from -[NSWindow isVisible], to detect visibility
259 bool window_visible_
;
261 // If true, the window is either visible, or wants to be visible but is
262 // currently hidden due to having a hidden parent.
263 bool wants_to_be_visible_
;
265 DISALLOW_COPY_AND_ASSIGN(BridgedNativeWidget
);
270 #endif // UI_VIEWS_COCOA_BRIDGED_NATIVE_WIDGET_H_