Add custom_tabs_client to third_party.
[chromium-blink-merge.git] / ui / views / cocoa / bridged_native_widget.h
blob5f52831ecbbff6776393bc61fb7753c8dc3b1c1d
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/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;
25 namespace ui {
26 class InputMethod;
29 namespace views {
31 class CocoaMouseCapture;
32 class InputMethod;
33 class NativeWidgetMac;
34 class View;
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 {
46 public:
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();
92 bool HasCapture();
94 // See views::Widget.
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 NSWindowDelegate when the application receives a mouse-down, but
136 // before the event is processed by NSWindows. Returning true here will cause
137 // the event to be cancelled and reposted at the CGSessionEventTap level. This
138 // is used to determine whether a mouse-down should drag the window.
139 virtual bool ShouldRepostPendingLeftMouseDown(NSPoint location_in_window);
141 // Called by NativeWidgetMac when the window size constraints change.
142 void OnSizeConstraintsChanged();
144 // See widget.h for documentation.
145 InputMethod* CreateInputMethod();
146 ui::InputMethod* GetHostInputMethod();
148 // The restored bounds will be derived from the current NSWindow frame unless
149 // fullscreen or transitioning between fullscreen states.
150 gfx::Rect GetRestoredBounds() const;
152 // Creates a ui::Compositor which becomes responsible for drawing the window.
153 void CreateLayer(ui::LayerType layer_type, bool translucent);
155 NativeWidgetMac* native_widget_mac() { return native_widget_mac_; }
156 BridgedContentView* ns_view() { return bridged_view_; }
157 NSWindow* ns_window() { return window_; }
159 TooltipManager* tooltip_manager() { return tooltip_manager_.get(); }
161 // The parent widget specified in Widget::InitParams::parent. If non-null, the
162 // parent will close children before the parent closes, and children will be
163 // raised above their parent when window z-order changes.
164 BridgedNativeWidgetOwner* parent() { return parent_; }
165 const std::vector<BridgedNativeWidget*>& child_windows() {
166 return child_windows_;
169 bool target_fullscreen_state() const { return target_fullscreen_state_; }
170 bool window_visible() { return window_visible_; }
172 // Overridden from internal::InputMethodDelegate:
173 void DispatchKeyEventPostIME(const ui::KeyEvent& key) override;
175 private:
176 // Closes all child windows. BridgedNativeWidget children will be destroyed.
177 void RemoveOrDestroyChildren();
179 // Notify descendants of a visibility change.
180 void NotifyVisibilityChangeDown();
182 // Essentially NativeWidgetMac::GetClientAreaBoundsInScreen().size(), but no
183 // coordinate transformations are required from AppKit coordinates.
184 gfx::Size GetClientAreaSize() const;
186 // Creates an owned ui::Compositor. For consistency, these functions reflect
187 // those in aura::WindowTreeHost.
188 void CreateCompositor();
189 void InitCompositor();
190 void DestroyCompositor();
192 // Installs the NSView for hosting the composited layer. It is later provided
193 // to |compositor_widget_| via AcceleratedWidgetGetNSView().
194 void AddCompositorSuperview();
196 // Size the layer to match the client area bounds, taking into account display
197 // scale factor.
198 void UpdateLayerProperties();
200 // Sets mouseDownCanMoveWindow on |bridged_view_| and triggers the NSWindow to
201 // update its draggable region.
202 void SetDraggable(bool draggable);
204 // Overridden from CocoaMouseCaptureDelegate:
205 void PostCapturedEvent(NSEvent* event) override;
206 void OnMouseCaptureLost() override;
208 // Returns a properties dictionary associated with the NSWindow.
209 // Creates and attaches a new instance if not found.
210 NSMutableDictionary* GetWindowProperties() const;
212 // Overridden from FocusChangeListener:
213 void OnWillChangeFocus(View* focused_before,
214 View* focused_now) override;
215 void OnDidChangeFocus(View* focused_before,
216 View* focused_now) override;
218 // Overridden from ui::LayerDelegate:
219 void OnPaintLayer(const ui::PaintContext& context) override;
220 void OnDelegatedFrameDamage(const gfx::Rect& damage_rect_in_dip) override;
221 void OnDeviceScaleFactorChanged(float device_scale_factor) override;
222 base::Closure PrepareForLayerBoundsChange() override;
224 // Overridden from ui::AcceleratedWidgetMac:
225 NSView* AcceleratedWidgetGetNSView() const override;
226 bool AcceleratedWidgetShouldIgnoreBackpressure() const override;
227 void AcceleratedWidgetSwapCompleted(
228 const std::vector<ui::LatencyInfo>& latency_info) override;
229 void AcceleratedWidgetHitError() override;
231 // Overridden from BridgedNativeWidgetOwner:
232 NSWindow* GetNSWindow() override;
233 gfx::Vector2d GetChildWindowOffset() const override;
234 bool IsVisibleParent() const override;
235 void RemoveChildWindow(BridgedNativeWidget* child) override;
237 views::NativeWidgetMac* native_widget_mac_; // Weak. Owns this.
238 base::scoped_nsobject<NSWindow> window_;
239 base::scoped_nsobject<ViewsNSWindowDelegate> window_delegate_;
240 base::scoped_nsobject<BridgedContentView> bridged_view_;
241 scoped_ptr<ui::InputMethod> input_method_;
242 scoped_ptr<CocoaMouseCapture> mouse_capture_;
243 scoped_ptr<TooltipManager> tooltip_manager_;
244 FocusManager* focus_manager_; // Weak. Owned by our Widget.
245 Widget::InitParams::Type widget_type_;
247 BridgedNativeWidgetOwner* parent_; // Weak. If non-null, owns this.
248 std::vector<BridgedNativeWidget*> child_windows_;
250 base::scoped_nsobject<NSView> compositor_superview_;
251 scoped_ptr<ui::AcceleratedWidgetMac> compositor_widget_;
252 scoped_ptr<ui::Compositor> compositor_;
254 // Tracks the bounds when the window last started entering fullscreen. Used to
255 // provide an answer for GetRestoredBounds(), but not ever sent to Cocoa (it
256 // has its own copy, but doesn't provide access to it).
257 gfx::Rect bounds_before_fullscreen_;
259 // Whether this window wants to be fullscreen. If a fullscreen animation is in
260 // progress then it might not be actually fullscreen.
261 bool target_fullscreen_state_;
263 // Whether this window is in a fullscreen transition, and the fullscreen state
264 // can not currently be changed.
265 bool in_fullscreen_transition_;
267 // Stores the value last read from -[NSWindow isVisible], to detect visibility
268 // changes.
269 bool window_visible_;
271 // If true, the window is either visible, or wants to be visible but is
272 // currently hidden due to having a hidden parent.
273 bool wants_to_be_visible_;
275 DISALLOW_COPY_AND_ASSIGN(BridgedNativeWidget);
278 } // namespace views
280 #endif // UI_VIEWS_COCOA_BRIDGED_NATIVE_WIDGET_H_