Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / views / cocoa / bridged_native_widget.h
bloba3421923cb2e489c4ed35c9f4bf1073a7dd24334
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>
9 #include <vector>
11 #import "base/mac/scoped_nsobject.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "ui/base/ime/input_method_delegate.h"
14 #include "ui/compositor/layer_owner.h"
15 #import "ui/accelerated_widget_mac/accelerated_widget_mac.h"
16 #import "ui/views/cocoa/bridged_native_widget_owner.h"
17 #import "ui/views/cocoa/cocoa_mouse_capture_delegate.h"
18 #import "ui/views/focus/focus_manager.h"
19 #include "ui/views/views_export.h"
20 #include "ui/views/widget/widget.h"
22 @class BridgedContentView;
23 @class ViewsNSWindowDelegate;
25 namespace ui {
26 class InputMethod;
29 namespace views {
30 namespace test {
31 class BridgedNativeWidgetTestApi;
34 class CocoaMouseCapture;
35 class NativeWidgetMac;
36 class View;
38 // A bridge to an NSWindow managed by an instance of NativeWidgetMac or
39 // DesktopNativeWidgetMac. Serves as a helper class to bridge requests from the
40 // NativeWidgetMac to the Cocoa window. Behaves a bit like an aura::Window.
41 class VIEWS_EXPORT BridgedNativeWidget
42 : public ui::LayerDelegate,
43 public ui::LayerOwner,
44 public ui::internal::InputMethodDelegate,
45 public CocoaMouseCaptureDelegate,
46 public FocusChangeListener,
47 public ui::AcceleratedWidgetMacNSView,
48 public BridgedNativeWidgetOwner {
49 public:
50 // Ways of changing the visibility of the bridged NSWindow.
51 enum WindowVisibilityState {
52 HIDE_WINDOW, // Hides with -[NSWindow orderOut:].
53 SHOW_AND_ACTIVATE_WINDOW, // Shows with -[NSWindow makeKeyAndOrderFront:].
54 SHOW_INACTIVE, // Shows with -[NSWindow orderWindow:..]. Orders
55 // the window above its parent if it has one.
58 // Return the size that |window| will take for the given client area |size|,
59 // based on its current style mask.
60 static gfx::Size GetWindowSizeForClientSize(NSWindow* window,
61 const gfx::Size& size);
63 // Creates one side of the bridge. |parent| must not be NULL.
64 explicit BridgedNativeWidget(NativeWidgetMac* parent);
65 ~BridgedNativeWidget() override;
67 // Initialize the bridge, "retains" ownership of |window|.
68 void Init(base::scoped_nsobject<NSWindow> window,
69 const Widget::InitParams& params);
71 // Sets or clears the focus manager to use for tracking focused views.
72 // This does NOT take ownership of |focus_manager|.
73 void SetFocusManager(FocusManager* focus_manager);
75 // Changes the bounds of the window and the hosted layer if present. The
76 // origin is a location in screen coordinates except for "child" windows,
77 // which are positioned relative to their parent(). SetBounds() considers a
78 // "child" window to be one initialized with InitParams specifying all of:
79 // a |parent| NSWindow, the |child| attribute, and a |type| that
80 // views::GetAuraWindowTypeForWidgetType does not consider a "popup" type.
81 void SetBounds(const gfx::Rect& new_bounds);
83 // Set or clears the views::View bridged by the content view. This does NOT
84 // take ownership of |view|.
85 void SetRootView(views::View* view);
87 // Sets the desired visibility of the window and updates the visibility of
88 // descendant windows where necessary.
89 void SetVisibilityState(WindowVisibilityState new_state);
91 // Acquiring mouse capture first steals capture from any existing
92 // CocoaMouseCaptureDelegate, then captures all mouse events until released.
93 void AcquireCapture();
94 void ReleaseCapture();
95 bool HasCapture();
97 // See views::Widget.
98 void SetNativeWindowProperty(const char* key, void* value);
99 void* GetNativeWindowProperty(const char* key) const;
101 // Sets the cursor associated with the NSWindow. Retains |cursor|.
102 void SetCursor(NSCursor* cursor);
104 // Called internally by the NSWindowDelegate when the window is closing.
105 void OnWindowWillClose();
107 // Called by the NSWindowDelegate when a fullscreen operation begins. If
108 // |target_fullscreen_state| is true, the target state is fullscreen.
109 // Otherwise, a transition has begun to come out of fullscreen.
110 void OnFullscreenTransitionStart(bool target_fullscreen_state);
112 // Called when a fullscreen transition completes. If target_fullscreen_state()
113 // does not match |actual_fullscreen_state|, a new transition will begin.
114 void OnFullscreenTransitionComplete(bool actual_fullscreen_state);
116 // Transition the window into or out of fullscreen. This will immediately
117 // invert the value of target_fullscreen_state().
118 void ToggleDesiredFullscreenState();
120 // Called by the NSWindowDelegate when the size of the window changes.
121 void OnSizeChanged();
123 // Called by the NSWindowDelegate when the visibility of the window may have
124 // changed. For example, due to a (de)miniaturize operation, or the window
125 // being reordered in (or out of) the screen list.
126 void OnVisibilityChanged();
128 // Explicitly set the visibility. This is called when Cocoa requests a draw,
129 // but hasn't updated the value of -[NSWindow isVisible] yet.
130 void OnVisibilityChangedTo(bool new_visibility);
132 // Called by the NSWindowDelegate on a scale factor or color space change.
133 void OnBackingPropertiesChanged();
135 // Called by the NSWindowDelegate when the window becomes or resigns key.
136 void OnWindowKeyStatusChangedTo(bool is_key);
138 // Called by NSWindowDelegate when the application receives a mouse-down, but
139 // before the event is processed by NSWindows. Returning true here will cause
140 // the event to be cancelled and reposted at the CGSessionEventTap level. This
141 // is used to determine whether a mouse-down should drag the window.
142 virtual bool ShouldRepostPendingLeftMouseDown(NSPoint location_in_window);
144 // Called by NativeWidgetMac when the window size constraints change.
145 void OnSizeConstraintsChanged();
147 // See widget.h for documentation.
148 ui::InputMethod* GetInputMethod();
150 // The restored bounds will be derived from the current NSWindow frame unless
151 // fullscreen or transitioning between fullscreen states.
152 gfx::Rect GetRestoredBounds() const;
154 // Creates a ui::Compositor which becomes responsible for drawing the window.
155 void CreateLayer(ui::LayerType layer_type, bool translucent);
157 NativeWidgetMac* native_widget_mac() { return native_widget_mac_; }
158 BridgedContentView* ns_view() { return bridged_view_; }
159 NSWindow* ns_window() { return window_; }
161 TooltipManager* tooltip_manager() { return tooltip_manager_.get(); }
163 // The parent widget specified in Widget::InitParams::parent. If non-null, the
164 // parent will close children before the parent closes, and children will be
165 // raised above their parent when window z-order changes.
166 BridgedNativeWidgetOwner* parent() { return parent_; }
167 const std::vector<BridgedNativeWidget*>& child_windows() {
168 return child_windows_;
171 bool target_fullscreen_state() const { return target_fullscreen_state_; }
172 bool window_visible() { return window_visible_; }
174 // Overridden from ui::internal::InputMethodDelegate:
175 ui::EventDispatchDetails DispatchKeyEventPostIME(ui::KeyEvent* key) override;
177 private:
178 friend class test::BridgedNativeWidgetTestApi;
180 // Closes all child windows. BridgedNativeWidget children will be destroyed.
181 void RemoveOrDestroyChildren();
183 // Notify descendants of a visibility change.
184 void NotifyVisibilityChangeDown();
186 // Essentially NativeWidgetMac::GetClientAreaBoundsInScreen().size(), but no
187 // coordinate transformations are required from AppKit coordinates.
188 gfx::Size GetClientAreaSize() const;
190 // Creates an owned ui::Compositor. For consistency, these functions reflect
191 // those in aura::WindowTreeHost.
192 void CreateCompositor();
193 void InitCompositor();
194 void DestroyCompositor();
196 // Installs the NSView for hosting the composited layer. It is later provided
197 // to |compositor_widget_| via AcceleratedWidgetGetNSView().
198 void AddCompositorSuperview();
200 // Size the layer to match the client area bounds, taking into account display
201 // scale factor.
202 void UpdateLayerProperties();
204 // Sets mouseDownCanMoveWindow on |bridged_view_| and triggers the NSWindow to
205 // update its draggable region.
206 void SetDraggable(bool draggable);
208 // Overridden from CocoaMouseCaptureDelegate:
209 void PostCapturedEvent(NSEvent* event) override;
210 void OnMouseCaptureLost() override;
212 // Returns a properties dictionary associated with the NSWindow.
213 // Creates and attaches a new instance if not found.
214 NSMutableDictionary* GetWindowProperties() const;
216 // Overridden from FocusChangeListener:
217 void OnWillChangeFocus(View* focused_before,
218 View* focused_now) override;
219 void OnDidChangeFocus(View* focused_before,
220 View* focused_now) override;
222 // Overridden from ui::LayerDelegate:
223 void OnPaintLayer(const ui::PaintContext& context) override;
224 void OnDelegatedFrameDamage(const gfx::Rect& damage_rect_in_dip) override;
225 void OnDeviceScaleFactorChanged(float device_scale_factor) override;
226 base::Closure PrepareForLayerBoundsChange() override;
228 // Overridden from ui::AcceleratedWidgetMac:
229 NSView* AcceleratedWidgetGetNSView() const override;
230 bool AcceleratedWidgetShouldIgnoreBackpressure() const override;
231 void AcceleratedWidgetGetVSyncParameters(
232 base::TimeTicks* timebase, base::TimeDelta* interval) const override;
233 void AcceleratedWidgetSwapCompleted(
234 const std::vector<ui::LatencyInfo>& latency_info) override;
235 void AcceleratedWidgetHitError() override;
237 // Overridden from BridgedNativeWidgetOwner:
238 NSWindow* GetNSWindow() override;
239 gfx::Vector2d GetChildWindowOffset() const override;
240 bool IsVisibleParent() const override;
241 void RemoveChildWindow(BridgedNativeWidget* child) override;
243 views::NativeWidgetMac* native_widget_mac_; // Weak. Owns this.
244 base::scoped_nsobject<NSWindow> window_;
245 base::scoped_nsobject<ViewsNSWindowDelegate> window_delegate_;
246 base::scoped_nsobject<BridgedContentView> bridged_view_;
247 scoped_ptr<ui::InputMethod> input_method_;
248 scoped_ptr<CocoaMouseCapture> mouse_capture_;
249 scoped_ptr<TooltipManager> tooltip_manager_;
250 FocusManager* focus_manager_; // Weak. Owned by our Widget.
251 Widget::InitParams::Type widget_type_;
253 BridgedNativeWidgetOwner* parent_; // Weak. If non-null, owns this.
254 std::vector<BridgedNativeWidget*> child_windows_;
256 base::scoped_nsobject<NSView> compositor_superview_;
257 scoped_ptr<ui::AcceleratedWidgetMac> compositor_widget_;
258 scoped_ptr<ui::Compositor> compositor_;
260 // Tracks the bounds when the window last started entering fullscreen. Used to
261 // provide an answer for GetRestoredBounds(), but not ever sent to Cocoa (it
262 // has its own copy, but doesn't provide access to it).
263 gfx::Rect bounds_before_fullscreen_;
265 // Whether this window wants to be fullscreen. If a fullscreen animation is in
266 // progress then it might not be actually fullscreen.
267 bool target_fullscreen_state_;
269 // Whether this window is in a fullscreen transition, and the fullscreen state
270 // can not currently be changed.
271 bool in_fullscreen_transition_;
273 // Stores the value last read from -[NSWindow isVisible], to detect visibility
274 // changes.
275 bool window_visible_;
277 // If true, the window is either visible, or wants to be visible but is
278 // currently hidden due to having a hidden parent.
279 bool wants_to_be_visible_;
281 // If true, the window has been made visible or changed shape and the window
282 // shadow needs to be invalidated when a frame is received for the new shape.
283 bool invalidate_shadow_on_frame_swap_ = false;
285 DISALLOW_COPY_AND_ASSIGN(BridgedNativeWidget);
288 } // namespace views
290 #endif // UI_VIEWS_COCOA_BRIDGED_NATIVE_WIDGET_H_