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 #import "ui/views/focus/focus_manager.h"
14 #include "ui/views/ime/input_method_delegate.h"
15 #include "ui/views/views_export.h"
16 #include "ui/views/widget/widget.h"
18 @
class BridgedContentView
;
19 @
class ViewsNSWindowDelegate
;
28 class NativeWidgetMac
;
31 // A bridge to an NSWindow managed by an instance of NativeWidgetMac or
32 // DesktopNativeWidgetMac. Serves as a helper class to bridge requests from the
33 // NativeWidgetMac to the Cocoa window. Behaves a bit like an aura::Window.
34 class VIEWS_EXPORT BridgedNativeWidget
: public internal::InputMethodDelegate
,
35 public FocusChangeListener
{
37 // Ways of changing the visibility of the bridged NSWindow.
38 enum WindowVisibilityState
{
39 HIDE_WINDOW
, // Hides with -[NSWindow orderOut:].
40 SHOW_AND_ACTIVATE_WINDOW
, // Shows with -[NSWindow makeKeyAndOrderFront:].
41 SHOW_INACTIVE
, // Shows with -[NSWindow orderWindow:..]. Orders
42 // the window above its parent if it has one.
45 // Creates one side of the bridge. |parent| must not be NULL.
46 explicit BridgedNativeWidget(NativeWidgetMac
* parent
);
47 virtual ~BridgedNativeWidget();
49 // Initialize the bridge, "retains" ownership of |window|.
50 void Init(base::scoped_nsobject
<NSWindow
> window
,
51 const Widget::InitParams
& params
);
53 // Sets or clears the focus manager to use for tracking focused views.
54 // This does NOT take ownership of |focus_manager|.
55 void SetFocusManager(FocusManager
* focus_manager
);
57 // Changes the bounds of the window and the hosted layer if present.
58 void SetBounds(const gfx::Rect
& new_bounds
);
60 // Set or clears the views::View bridged by the content view. This does NOT
61 // take ownership of |view|.
62 void SetRootView(views::View
* view
);
64 // Sets the desired visibility of the window and updates the visibility of
65 // descendant windows where necessary.
66 void SetVisibilityState(WindowVisibilityState new_state
);
68 // Called internally by the NSWindowDelegate when the window is closing.
69 void OnWindowWillClose();
71 // Called by the NSWindowDelegate when a fullscreen operation begins. If
72 // |target_fullscreen_state| is true, the target state is fullscreen.
73 // Otherwise, a transition has begun to come out of fullscreen.
74 void OnFullscreenTransitionStart(bool target_fullscreen_state
);
76 // Called when a fullscreen transition completes. If target_fullscreen_state()
77 // does not match |actual_fullscreen_state|, a new transition will begin.
78 void OnFullscreenTransitionComplete(bool actual_fullscreen_state
);
80 // Transition the window into or out of fullscreen. This will immediately
81 // invert the value of target_fullscreen_state().
82 void ToggleDesiredFullscreenState();
84 // Called by the NSWindowDelegate when the size of the window changes.
87 // Called by the NSWindowDelegate when the visibility of the window may have
88 // changed. For example, due to a (de)miniaturize operation, or the window
89 // being reordered in (or out of) the screen list.
90 void OnVisibilityChanged();
92 // See widget.h for documentation.
93 InputMethod
* CreateInputMethod();
94 ui::InputMethod
* GetHostInputMethod();
96 // The restored bounds will be derived from the current NSWindow frame unless
97 // fullscreen or transitioning between fullscreen states.
98 gfx::Rect
GetRestoredBounds() const;
100 NativeWidgetMac
* native_widget_mac() { return native_widget_mac_
; }
101 BridgedContentView
* ns_view() { return bridged_view_
; }
102 NSWindow
* ns_window() { return window_
; }
104 // The parent widget specified in Widget::InitParams::parent. If non-null, the
105 // parent will close children before the parent closes, and children will be
106 // raised above their parent when window z-order changes.
107 BridgedNativeWidget
* parent() { return parent_
; }
108 const std::vector
<BridgedNativeWidget
*>& child_windows() {
109 return child_windows_
;
112 bool target_fullscreen_state() const { return target_fullscreen_state_
; }
114 // Overridden from internal::InputMethodDelegate:
115 virtual void DispatchKeyEventPostIME(const ui::KeyEvent
& key
) override
;
118 // Closes all child windows. BridgedNativeWidget children will be destroyed.
119 void RemoveOrDestroyChildren();
121 // Remove the given |child| from |child_windows_|.
122 void RemoveChildWindow(BridgedNativeWidget
* child
);
124 // Notify descendants of a visibility change.
125 void NotifyVisibilityChangeDown();
127 // Overridden from FocusChangeListener:
128 void OnWillChangeFocus(View
* focused_before
,
129 View
* focused_now
) override
;
130 void OnDidChangeFocus(View
* focused_before
,
131 View
* focused_now
) override
;
133 views::NativeWidgetMac
* native_widget_mac_
; // Weak. Owns this.
134 base::scoped_nsobject
<NSWindow
> window_
;
135 base::scoped_nsobject
<ViewsNSWindowDelegate
> window_delegate_
;
136 base::scoped_nsobject
<BridgedContentView
> bridged_view_
;
137 scoped_ptr
<ui::InputMethod
> input_method_
;
138 FocusManager
* focus_manager_
; // Weak. Owned by our Widget.
140 BridgedNativeWidget
* parent_
; // Weak. If non-null, owns this.
141 std::vector
<BridgedNativeWidget
*> child_windows_
;
143 // Tracks the bounds when the window last started entering fullscreen. Used to
144 // provide an answer for GetRestoredBounds(), but not ever sent to Cocoa (it
145 // has its own copy, but doesn't provide access to it).
146 gfx::Rect bounds_before_fullscreen_
;
148 // Whether this window wants to be fullscreen. If a fullscreen animation is in
149 // progress then it might not be actually fullscreen.
150 bool target_fullscreen_state_
;
152 // Whether this window is in a fullscreen transition, and the fullscreen state
153 // can not currently be changed.
154 bool in_fullscreen_transition_
;
156 // Stores the value last read from -[NSWindow isVisible], to detect visibility
158 bool window_visible_
;
160 // If true, the window is either visible, or wants to be visible but is
161 // currently hidden due to having a hidden parent.
162 bool wants_to_be_visible_
;
164 DISALLOW_COPY_AND_ASSIGN(BridgedNativeWidget
);
169 #endif // UI_VIEWS_COCOA_BRIDGED_NATIVE_WIDGET_H_