1 // Copyright 2012 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 #include "chrome/browser/ui/views/frame/immersive_mode_controller_ash.h"
8 #include "ash/wm/immersive_revealed_lock.h"
9 #include "ash/wm/window_state.h"
10 #include "chrome/browser/chrome_notification_types.h"
11 #include "chrome/browser/ui/exclusive_access/exclusive_access_manager.h"
12 #include "chrome/browser/ui/exclusive_access/fullscreen_controller.h"
13 #include "chrome/browser/ui/views/frame/browser_view.h"
14 #include "chrome/browser/ui/views/frame/top_container_view.h"
15 #include "chrome/browser/ui/views/tabs/tab_strip.h"
16 #include "content/public/browser/notification_service.h"
17 #include "content/public/browser/web_contents.h"
18 #include "ui/aura/window.h"
19 #include "ui/views/view.h"
20 #include "ui/views/widget/widget.h"
21 #include "ui/views/window/non_client_view.h"
25 // Revealing the TopContainerView looks better if the animation starts and ends
26 // just a few pixels before the view goes offscreen, which reduces the visual
27 // "pop" as the 3-pixel tall "light bar" style tab strip becomes visible.
28 const int kAnimationOffsetY
= 3;
30 // Converts from ImmersiveModeController::AnimateReveal to
31 // ash::ImmersiveFullscreenController::AnimateReveal.
32 ash::ImmersiveFullscreenController::AnimateReveal
33 ToImmersiveFullscreenControllerAnimateReveal(
34 ImmersiveModeController::AnimateReveal animate_reveal
) {
35 switch (animate_reveal
) {
36 case ImmersiveModeController::ANIMATE_REVEAL_YES
:
37 return ash::ImmersiveFullscreenController::ANIMATE_REVEAL_YES
;
38 case ImmersiveModeController::ANIMATE_REVEAL_NO
:
39 return ash::ImmersiveFullscreenController::ANIMATE_REVEAL_NO
;
42 return ash::ImmersiveFullscreenController::ANIMATE_REVEAL_NO
;
45 class ImmersiveRevealedLockAsh
: public ImmersiveRevealedLock
{
47 explicit ImmersiveRevealedLockAsh(ash::ImmersiveRevealedLock
* lock
)
51 scoped_ptr
<ash::ImmersiveRevealedLock
> lock_
;
53 DISALLOW_COPY_AND_ASSIGN(ImmersiveRevealedLockAsh
);
58 ImmersiveModeControllerAsh::ImmersiveModeControllerAsh()
59 : controller_(new ash::ImmersiveFullscreenController
),
60 browser_view_(nullptr),
61 native_window_(nullptr),
62 observers_enabled_(false),
63 use_tab_indicators_(false),
64 visible_fraction_(1) {
67 ImmersiveModeControllerAsh::~ImmersiveModeControllerAsh() {
68 EnableWindowObservers(false);
71 void ImmersiveModeControllerAsh::Init(BrowserView
* browser_view
) {
72 browser_view_
= browser_view
;
73 native_window_
= browser_view_
->GetNativeWindow();
74 controller_
->Init(this, browser_view_
->frame(),
75 browser_view_
->top_container());
78 void ImmersiveModeControllerAsh::SetEnabled(bool enabled
) {
79 if (controller_
->IsEnabled() == enabled
)
82 EnableWindowObservers(enabled
);
84 controller_
->SetEnabled(browser_view_
->browser()->is_app() ?
85 ash::ImmersiveFullscreenController::WINDOW_TYPE_HOSTED_APP
:
86 ash::ImmersiveFullscreenController::WINDOW_TYPE_BROWSER
90 bool ImmersiveModeControllerAsh::IsEnabled() const {
91 return controller_
->IsEnabled();
94 bool ImmersiveModeControllerAsh::ShouldHideTabIndicators() const {
95 return !use_tab_indicators_
;
98 bool ImmersiveModeControllerAsh::ShouldHideTopViews() const {
99 return controller_
->IsEnabled() && !controller_
->IsRevealed();
102 bool ImmersiveModeControllerAsh::IsRevealed() const {
103 return controller_
->IsRevealed();
106 int ImmersiveModeControllerAsh::GetTopContainerVerticalOffset(
107 const gfx::Size
& top_container_size
) const {
111 // The TopContainerView is flush with the top of |browser_view_| when the
112 // top-of-window views are fully closed so that when the tab indicators are
113 // used, the "light bar" style tab strip is flush with the top of
118 int height
= top_container_size
.height() - kAnimationOffsetY
;
119 return static_cast<int>(height
* (visible_fraction_
- 1));
122 ImmersiveRevealedLock
* ImmersiveModeControllerAsh::GetRevealedLock(
123 AnimateReveal animate_reveal
) {
124 return new ImmersiveRevealedLockAsh(controller_
->GetRevealedLock(
125 ToImmersiveFullscreenControllerAnimateReveal(animate_reveal
)));
128 void ImmersiveModeControllerAsh::OnFindBarVisibleBoundsChanged(
129 const gfx::Rect
& new_visible_bounds_in_screen
) {
130 find_bar_visible_bounds_in_screen_
= new_visible_bounds_in_screen
;
133 void ImmersiveModeControllerAsh::SetupForTest() {
134 controller_
->SetupForTest();
137 void ImmersiveModeControllerAsh::EnableWindowObservers(bool enable
) {
138 if (observers_enabled_
== enable
)
140 observers_enabled_
= enable
;
142 content::Source
<FullscreenController
> source(browser_view_
->browser()
143 ->exclusive_access_manager()
144 ->fullscreen_controller());
146 ash::wm::GetWindowState(native_window_
)->AddObserver(this);
147 registrar_
.Add(this, chrome::NOTIFICATION_FULLSCREEN_CHANGED
, source
);
149 ash::wm::GetWindowState(native_window_
)->RemoveObserver(this);
150 registrar_
.Remove(this, chrome::NOTIFICATION_FULLSCREEN_CHANGED
, source
);
154 void ImmersiveModeControllerAsh::LayoutBrowserRootView() {
155 views::Widget
* widget
= browser_view_
->frame();
156 // Update the window caption buttons.
157 widget
->non_client_view()->frame_view()->ResetWindowControls();
158 widget
->non_client_view()->frame_view()->InvalidateLayout();
159 browser_view_
->InvalidateLayout();
160 widget
->GetRootView()->Layout();
163 bool ImmersiveModeControllerAsh::UpdateTabIndicators() {
164 bool has_tabstrip
= browser_view_
->IsBrowserTypeNormal();
165 if (!IsEnabled() || !has_tabstrip
) {
166 use_tab_indicators_
= false;
168 bool in_tab_fullscreen
= browser_view_
->browser()
169 ->exclusive_access_manager()
170 ->fullscreen_controller()
171 ->IsWindowFullscreenForTabOrPending();
172 use_tab_indicators_
= !in_tab_fullscreen
;
175 bool show_tab_indicators
= use_tab_indicators_
&& !IsRevealed();
176 if (show_tab_indicators
!= browser_view_
->tabstrip()->IsImmersiveStyle()) {
177 browser_view_
->tabstrip()->SetImmersiveStyle(show_tab_indicators
);
183 void ImmersiveModeControllerAsh::OnImmersiveRevealStarted() {
184 visible_fraction_
= 0;
185 browser_view_
->top_container()->SetPaintToLayer(true);
186 browser_view_
->top_container()->SetFillsBoundsOpaquely(false);
187 UpdateTabIndicators();
188 LayoutBrowserRootView();
189 FOR_EACH_OBSERVER(Observer
, observers_
, OnImmersiveRevealStarted());
192 void ImmersiveModeControllerAsh::OnImmersiveRevealEnded() {
193 visible_fraction_
= 0;
194 browser_view_
->top_container()->SetPaintToLayer(false);
195 UpdateTabIndicators();
196 LayoutBrowserRootView();
199 void ImmersiveModeControllerAsh::OnImmersiveFullscreenExited() {
200 browser_view_
->top_container()->SetPaintToLayer(false);
201 UpdateTabIndicators();
202 LayoutBrowserRootView();
205 void ImmersiveModeControllerAsh::SetVisibleFraction(double visible_fraction
) {
206 if (visible_fraction_
!= visible_fraction
) {
207 visible_fraction_
= visible_fraction
;
208 browser_view_
->Layout();
212 std::vector
<gfx::Rect
>
213 ImmersiveModeControllerAsh::GetVisibleBoundsInScreen() const {
214 views::View
* top_container_view
= browser_view_
->top_container();
215 gfx::Rect top_container_view_bounds
= top_container_view
->GetVisibleBounds();
216 // TODO(tdanderson): Implement View::ConvertRectToScreen().
217 gfx::Point
top_container_view_bounds_in_screen_origin(
218 top_container_view_bounds
.origin());
219 views::View::ConvertPointToScreen(top_container_view
,
220 &top_container_view_bounds_in_screen_origin
);
221 gfx::Rect
top_container_view_bounds_in_screen(
222 top_container_view_bounds_in_screen_origin
,
223 top_container_view_bounds
.size());
225 std::vector
<gfx::Rect
> bounds_in_screen
;
226 bounds_in_screen
.push_back(top_container_view_bounds_in_screen
);
227 bounds_in_screen
.push_back(find_bar_visible_bounds_in_screen_
);
228 return bounds_in_screen
;
231 void ImmersiveModeControllerAsh::OnPostWindowStateTypeChange(
232 ash::wm::WindowState
* window_state
,
233 ash::wm::WindowStateType old_type
) {
234 // Disable immersive fullscreen when the user exits fullscreen without going
235 // through FullscreenController::ToggleBrowserFullscreenMode(). This is the
236 // case if the user exits fullscreen via the restore button.
237 if (controller_
->IsEnabled() &&
238 !window_state
->IsFullscreen() &&
239 !window_state
->IsMinimized()) {
240 browser_view_
->FullscreenStateChanged();
244 void ImmersiveModeControllerAsh::Observe(
246 const content::NotificationSource
& source
,
247 const content::NotificationDetails
& details
) {
248 DCHECK_EQ(chrome::NOTIFICATION_FULLSCREEN_CHANGED
, type
);
249 if (!controller_
->IsEnabled())
252 bool tab_indicator_visibility_changed
= UpdateTabIndicators();
254 // Auto hide the shelf in immersive browser fullscreen. When auto hidden, the
255 // shelf displays a 3px 'light bar' when it is closed. When in immersive
256 // browser fullscreen and tab fullscreen, hide the shelf completely and
257 // prevent it from being revealed.
258 bool in_tab_fullscreen
= content::Source
<FullscreenController
>(source
)->
259 IsWindowFullscreenForTabOrPending();
260 ash::wm::GetWindowState(native_window_
)->set_hide_shelf_when_fullscreen(
262 ash::Shell::GetInstance()->UpdateShelfVisibility();
264 if (tab_indicator_visibility_changed
)
265 LayoutBrowserRootView();