immersive_mode_controller_ash.cc revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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.
4
5#include "chrome/browser/ui/views/frame/immersive_mode_controller_ash.h"
6
7#include "ash/shell.h"
8#include "ash/wm/window_state.h"
9#include "chrome/browser/chrome_notification_types.h"
10#include "chrome/browser/ui/fullscreen/fullscreen_controller.h"
11#include "chrome/browser/ui/views/frame/browser_view.h"
12#include "chrome/browser/ui/views/frame/top_container_view.h"
13#include "chrome/browser/ui/views/tabs/tab_strip.h"
14#include "content/public/browser/notification_service.h"
15#include "content/public/browser/web_contents.h"
16#include "content/public/browser/web_contents_view.h"
17#include "ui/aura/window.h"
18#include "ui/views/view.h"
19#include "ui/views/widget/widget.h"
20#include "ui/views/window/non_client_view.h"
21
22namespace {
23
24// Revealing the TopContainerView looks better if the animation starts and ends
25// just a few pixels before the view goes offscreen, which reduces the visual
26// "pop" as the 3-pixel tall "light bar" style tab strip becomes visible.
27const int kAnimationOffsetY = 3;
28
29// Converts from ImmersiveModeController::AnimateReveal to
30// ash::ImmersiveFullscreenController::AnimateReveal.
31ash::ImmersiveFullscreenController::AnimateReveal
32ToImmersiveFullscreenControllerAnimateReveal(
33    ImmersiveModeController::AnimateReveal animate_reveal) {
34  switch (animate_reveal) {
35    case ImmersiveModeController::ANIMATE_REVEAL_YES:
36      return ash::ImmersiveFullscreenController::ANIMATE_REVEAL_YES;
37    case ImmersiveModeController::ANIMATE_REVEAL_NO:
38      return ash::ImmersiveFullscreenController::ANIMATE_REVEAL_NO;
39  }
40  NOTREACHED();
41  return ash::ImmersiveFullscreenController::ANIMATE_REVEAL_NO;
42}
43
44}  // namespace
45
46ImmersiveModeControllerAsh::ImmersiveModeControllerAsh()
47    : controller_(new ash::ImmersiveFullscreenController),
48      browser_view_(NULL),
49      native_window_(NULL),
50      observers_enabled_(false),
51      use_tab_indicators_(false),
52      visible_fraction_(1) {
53}
54
55ImmersiveModeControllerAsh::~ImmersiveModeControllerAsh() {
56  EnableWindowObservers(false);
57}
58
59void ImmersiveModeControllerAsh::Init(BrowserView* browser_view) {
60  browser_view_ = browser_view;
61  native_window_ = browser_view_->GetNativeWindow();
62  controller_->Init(this, browser_view_->frame(),
63      browser_view_->top_container());
64}
65
66void ImmersiveModeControllerAsh::SetEnabled(bool enabled) {
67  if (controller_->IsEnabled() == enabled)
68    return;
69
70  EnableWindowObservers(enabled);
71
72  controller_->SetEnabled(browser_view_->browser()->is_app() ?
73          ash::ImmersiveFullscreenController::WINDOW_TYPE_HOSTED_APP :
74          ash::ImmersiveFullscreenController::WINDOW_TYPE_BROWSER
75      , enabled);
76}
77
78bool ImmersiveModeControllerAsh::IsEnabled() const {
79  return controller_->IsEnabled();
80}
81
82bool ImmersiveModeControllerAsh::ShouldHideTabIndicators() const {
83  return !use_tab_indicators_;
84}
85
86bool ImmersiveModeControllerAsh::ShouldHideTopViews() const {
87  return controller_->IsEnabled() && !controller_->IsRevealed();
88}
89
90bool ImmersiveModeControllerAsh::IsRevealed() const {
91  return controller_->IsRevealed();
92}
93
94int ImmersiveModeControllerAsh::GetTopContainerVerticalOffset(
95    const gfx::Size& top_container_size) const {
96  if (!IsEnabled())
97    return 0;
98
99  // The TopContainerView is flush with the top of |browser_view_| when the
100  // top-of-window views are fully closed so that when the tab indicators are
101  // used, the "light bar" style tab strip is flush with the top of
102  // |browser_view_|.
103  if (!IsRevealed())
104    return 0;
105
106  int height = top_container_size.height() - kAnimationOffsetY;
107  return static_cast<int>(height * (visible_fraction_ - 1));
108}
109
110ImmersiveRevealedLock* ImmersiveModeControllerAsh::GetRevealedLock(
111    AnimateReveal animate_reveal) {
112  return controller_->GetRevealedLock(
113      ToImmersiveFullscreenControllerAnimateReveal(animate_reveal));
114}
115
116void ImmersiveModeControllerAsh::OnFindBarVisibleBoundsChanged(
117    const gfx::Rect& new_visible_bounds_in_screen) {
118  find_bar_visible_bounds_in_screen_ = new_visible_bounds_in_screen;
119}
120
121void ImmersiveModeControllerAsh::SetupForTest() {
122  controller_->SetupForTest();
123}
124
125void ImmersiveModeControllerAsh::EnableWindowObservers(bool enable) {
126  if (observers_enabled_ == enable)
127    return;
128  observers_enabled_ = enable;
129
130  content::Source<FullscreenController> source(
131      browser_view_->browser()->fullscreen_controller());
132  if (enable) {
133    ash::wm::GetWindowState(native_window_)->AddObserver(this);
134    registrar_.Add(this, chrome::NOTIFICATION_FULLSCREEN_CHANGED, source);
135  } else {
136    ash::wm::GetWindowState(native_window_)->RemoveObserver(this);
137    registrar_.Remove(this, chrome::NOTIFICATION_FULLSCREEN_CHANGED, source);
138  }
139}
140
141void ImmersiveModeControllerAsh::LayoutBrowserRootView() {
142  views::Widget* widget = browser_view_->frame();
143  // Update the window caption buttons.
144  widget->non_client_view()->frame_view()->ResetWindowControls();
145  widget->non_client_view()->frame_view()->InvalidateLayout();
146  browser_view_->InvalidateLayout();
147  widget->GetRootView()->Layout();
148}
149
150bool ImmersiveModeControllerAsh::UpdateTabIndicators() {
151  bool has_tabstrip = browser_view_->IsBrowserTypeNormal();
152  if (!IsEnabled() || !has_tabstrip) {
153    use_tab_indicators_ = false;
154  } else {
155    bool in_tab_fullscreen = browser_view_->browser()->fullscreen_controller()->
156        IsWindowFullscreenForTabOrPending();
157    use_tab_indicators_ = !in_tab_fullscreen;
158  }
159
160  bool show_tab_indicators = use_tab_indicators_ && !IsRevealed();
161  if (show_tab_indicators != browser_view_->tabstrip()->IsImmersiveStyle()) {
162    browser_view_->tabstrip()->SetImmersiveStyle(show_tab_indicators);
163    return true;
164  }
165  return false;
166}
167
168void ImmersiveModeControllerAsh::OnImmersiveRevealStarted() {
169  visible_fraction_ = 0;
170  browser_view_->top_container()->SetPaintToLayer(true);
171  browser_view_->top_container()->SetFillsBoundsOpaquely(false);
172  UpdateTabIndicators();
173  LayoutBrowserRootView();
174  FOR_EACH_OBSERVER(Observer, observers_, OnImmersiveRevealStarted());
175}
176
177void ImmersiveModeControllerAsh::OnImmersiveRevealEnded() {
178  visible_fraction_ = 0;
179  browser_view_->top_container()->SetPaintToLayer(false);
180  UpdateTabIndicators();
181  LayoutBrowserRootView();
182}
183
184void ImmersiveModeControllerAsh::OnImmersiveFullscreenExited() {
185  browser_view_->top_container()->SetPaintToLayer(false);
186  UpdateTabIndicators();
187  LayoutBrowserRootView();
188}
189
190void ImmersiveModeControllerAsh::SetVisibleFraction(double visible_fraction) {
191  if (visible_fraction_ != visible_fraction) {
192    visible_fraction_ = visible_fraction;
193    browser_view_->Layout();
194  }
195}
196
197std::vector<gfx::Rect>
198ImmersiveModeControllerAsh::GetVisibleBoundsInScreen() const {
199  views::View* top_container_view = browser_view_->top_container();
200  gfx::Rect top_container_view_bounds = top_container_view->GetVisibleBounds();
201  // TODO(tdanderson): Implement View::ConvertRectToScreen().
202  gfx::Point top_container_view_bounds_in_screen_origin(
203      top_container_view_bounds.origin());
204  views::View::ConvertPointToScreen(top_container_view,
205      &top_container_view_bounds_in_screen_origin);
206  gfx::Rect top_container_view_bounds_in_screen(
207      top_container_view_bounds_in_screen_origin,
208      top_container_view_bounds.size());
209
210  std::vector<gfx::Rect> bounds_in_screen;
211  bounds_in_screen.push_back(top_container_view_bounds_in_screen);
212  bounds_in_screen.push_back(find_bar_visible_bounds_in_screen_);
213  return bounds_in_screen;
214}
215
216void ImmersiveModeControllerAsh::OnPostWindowStateTypeChange(
217    ash::wm::WindowState* window_state,
218    ash::wm::WindowStateType old_type) {
219  // Disable immersive fullscreen when the user exits fullscreen without going
220  // through FullscreenController::ToggleBrowserFullscreenMode(). This is the
221  // case if the user exits fullscreen via the restore button.
222  if (controller_->IsEnabled() &&
223      !window_state->IsFullscreen() &&
224      !window_state->IsMinimized()) {
225    browser_view_->FullscreenStateChanged();
226  }
227}
228
229void ImmersiveModeControllerAsh::Observe(
230    int type,
231    const content::NotificationSource& source,
232    const content::NotificationDetails& details) {
233  DCHECK_EQ(chrome::NOTIFICATION_FULLSCREEN_CHANGED, type);
234  if (!controller_->IsEnabled())
235    return;
236
237  bool tab_indicator_visibility_changed = UpdateTabIndicators();
238
239  // Auto hide the shelf in immersive browser fullscreen. When auto hidden, the
240  // shelf displays a 3px 'light bar' when it is closed. When in immersive
241  // browser fullscreen and tab fullscreen, hide the shelf completely and
242  // prevent it from being revealed.
243  bool in_tab_fullscreen = content::Source<FullscreenController>(source)->
244      IsWindowFullscreenForTabOrPending();
245  ash::wm::GetWindowState(native_window_)->set_hide_shelf_when_fullscreen(
246      in_tab_fullscreen);
247  ash::Shell::GetInstance()->UpdateShelfVisibility();
248
249  if (tab_indicator_visibility_changed)
250    LayoutBrowserRootView();
251}
252