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