Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / views / frame / immersive_mode_controller_ash.cc
blob8880b27bb63b776fa79f1cfc430eb7ae6ef67dab
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"
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"
22 namespace {
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.
27 const int kAnimationOffsetY = 3;
29 // The height of the region in pixels at the top edge of the screen in which to
30 // steal touch events targetted at the web contents while in immersive
31 // fullscreen. This region is used to allow us to get edge gestures even if the
32 // web contents consumes all touch events.
33 const int kStealTouchEventsFromWebContentsRegionHeightPx = 8;
35 // Converts from ImmersiveModeController::AnimateReveal to
36 // ash::ImmersiveFullscreenController::AnimateReveal.
37 ash::ImmersiveFullscreenController::AnimateReveal
38 ToImmersiveFullscreenControllerAnimateReveal(
39 ImmersiveModeController::AnimateReveal animate_reveal) {
40 switch (animate_reveal) {
41 case ImmersiveModeController::ANIMATE_REVEAL_YES:
42 return ash::ImmersiveFullscreenController::ANIMATE_REVEAL_YES;
43 case ImmersiveModeController::ANIMATE_REVEAL_NO:
44 return ash::ImmersiveFullscreenController::ANIMATE_REVEAL_NO;
46 NOTREACHED();
47 return ash::ImmersiveFullscreenController::ANIMATE_REVEAL_NO;
50 } // namespace
52 ImmersiveModeControllerAsh::ImmersiveModeControllerAsh()
53 : controller_(new ash::ImmersiveFullscreenController),
54 browser_view_(NULL),
55 native_window_(NULL),
56 observers_enabled_(false),
57 use_tab_indicators_(false),
58 visible_fraction_(1) {
61 ImmersiveModeControllerAsh::~ImmersiveModeControllerAsh() {
62 EnableWindowObservers(false);
65 void ImmersiveModeControllerAsh::Init(BrowserView* browser_view) {
66 browser_view_ = browser_view;
67 native_window_ = browser_view_->GetNativeWindow();
68 controller_->Init(this, browser_view_->frame(),
69 browser_view_->top_container());
72 void ImmersiveModeControllerAsh::SetEnabled(bool enabled) {
73 if (controller_->IsEnabled() == enabled)
74 return;
76 EnableWindowObservers(enabled);
78 controller_->SetEnabled(browser_view_->browser()->is_app() ?
79 ash::ImmersiveFullscreenController::WINDOW_TYPE_HOSTED_APP :
80 ash::ImmersiveFullscreenController::WINDOW_TYPE_BROWSER
81 , enabled);
84 bool ImmersiveModeControllerAsh::IsEnabled() const {
85 return controller_->IsEnabled();
88 bool ImmersiveModeControllerAsh::ShouldHideTabIndicators() const {
89 return !use_tab_indicators_;
92 bool ImmersiveModeControllerAsh::ShouldHideTopViews() const {
93 return controller_->IsEnabled() && !controller_->IsRevealed();
96 bool ImmersiveModeControllerAsh::IsRevealed() const {
97 return controller_->IsRevealed();
100 int ImmersiveModeControllerAsh::GetTopContainerVerticalOffset(
101 const gfx::Size& top_container_size) const {
102 if (!IsEnabled())
103 return 0;
105 // The TopContainerView is flush with the top of |browser_view_| when the
106 // top-of-window views are fully closed so that when the tab indicators are
107 // used, the "light bar" style tab strip is flush with the top of
108 // |browser_view_|.
109 if (!IsRevealed())
110 return 0;
112 int height = top_container_size.height() - kAnimationOffsetY;
113 return static_cast<int>(height * (visible_fraction_ - 1));
116 ImmersiveRevealedLock* ImmersiveModeControllerAsh::GetRevealedLock(
117 AnimateReveal animate_reveal) {
118 return controller_->GetRevealedLock(
119 ToImmersiveFullscreenControllerAnimateReveal(animate_reveal));
122 void ImmersiveModeControllerAsh::OnFindBarVisibleBoundsChanged(
123 const gfx::Rect& new_visible_bounds_in_screen) {
124 find_bar_visible_bounds_in_screen_ = new_visible_bounds_in_screen;
127 void ImmersiveModeControllerAsh::SetupForTest() {
128 controller_->SetupForTest();
131 void ImmersiveModeControllerAsh::EnableWindowObservers(bool enable) {
132 if (observers_enabled_ == enable)
133 return;
134 observers_enabled_ = enable;
136 content::Source<FullscreenController> source(
137 browser_view_->browser()->fullscreen_controller());
138 if (enable) {
139 ash::wm::GetWindowState(native_window_)->AddObserver(this);
140 registrar_.Add(this, chrome::NOTIFICATION_FULLSCREEN_CHANGED, source);
141 } else {
142 ash::wm::GetWindowState(native_window_)->RemoveObserver(this);
143 registrar_.Remove(this, chrome::NOTIFICATION_FULLSCREEN_CHANGED, source);
147 void ImmersiveModeControllerAsh::LayoutBrowserRootView() {
148 views::Widget* widget = browser_view_->frame();
149 // Update the window caption buttons.
150 widget->non_client_view()->frame_view()->ResetWindowControls();
151 widget->non_client_view()->frame_view()->InvalidateLayout();
152 browser_view_->InvalidateLayout();
153 widget->GetRootView()->Layout();
156 void ImmersiveModeControllerAsh::SetRenderWindowTopInsetsForTouch(
157 int top_inset) {
158 content::WebContents* contents = browser_view_->GetActiveWebContents();
159 if (contents) {
160 aura::Window* window = contents->GetView()->GetContentNativeView();
161 // |window| is NULL if the renderer crashed.
162 if (window) {
163 gfx::Insets inset(top_inset, 0, 0, 0);
164 window->SetHitTestBoundsOverrideOuter(
165 window->hit_test_bounds_override_outer_mouse(),
166 inset);
171 bool ImmersiveModeControllerAsh::UpdateTabIndicators() {
172 bool has_tabstrip = browser_view_->IsBrowserTypeNormal();
173 if (!IsEnabled() || !has_tabstrip) {
174 use_tab_indicators_ = false;
175 } else {
176 bool in_tab_fullscreen = browser_view_->browser()->fullscreen_controller()->
177 IsFullscreenForTabOrPending();
178 use_tab_indicators_ = !in_tab_fullscreen;
181 bool show_tab_indicators = use_tab_indicators_ && !IsRevealed();
182 if (show_tab_indicators != browser_view_->tabstrip()->IsImmersiveStyle()) {
183 browser_view_->tabstrip()->SetImmersiveStyle(show_tab_indicators);
184 return true;
186 return false;
189 void ImmersiveModeControllerAsh::OnImmersiveRevealStarted() {
190 visible_fraction_ = 0;
191 browser_view_->top_container()->SetPaintToLayer(true);
192 UpdateTabIndicators();
193 SetRenderWindowTopInsetsForTouch(0);
194 LayoutBrowserRootView();
195 FOR_EACH_OBSERVER(Observer, observers_, OnImmersiveRevealStarted());
198 void ImmersiveModeControllerAsh::OnImmersiveRevealEnded() {
199 visible_fraction_ = 0;
200 browser_view_->top_container()->SetPaintToLayer(false);
201 UpdateTabIndicators();
202 SetRenderWindowTopInsetsForTouch(
203 kStealTouchEventsFromWebContentsRegionHeightPx);
204 LayoutBrowserRootView();
207 void ImmersiveModeControllerAsh::OnImmersiveFullscreenExited() {
208 browser_view_->top_container()->SetPaintToLayer(false);
209 UpdateTabIndicators();
210 SetRenderWindowTopInsetsForTouch(0);
211 LayoutBrowserRootView();
214 void ImmersiveModeControllerAsh::SetVisibleFraction(double visible_fraction) {
215 if (visible_fraction_ != visible_fraction) {
216 visible_fraction_ = visible_fraction;
217 browser_view_->Layout();
221 std::vector<gfx::Rect>
222 ImmersiveModeControllerAsh::GetVisibleBoundsInScreen() const {
223 views::View* top_container_view = browser_view_->top_container();
224 gfx::Rect top_container_view_bounds = top_container_view->GetVisibleBounds();
225 // TODO(tdanderson): Implement View::ConvertRectToScreen().
226 gfx::Point top_container_view_bounds_in_screen_origin(
227 top_container_view_bounds.origin());
228 views::View::ConvertPointToScreen(top_container_view,
229 &top_container_view_bounds_in_screen_origin);
230 gfx::Rect top_container_view_bounds_in_screen(
231 top_container_view_bounds_in_screen_origin,
232 top_container_view_bounds.size());
234 std::vector<gfx::Rect> bounds_in_screen;
235 bounds_in_screen.push_back(top_container_view_bounds_in_screen);
236 bounds_in_screen.push_back(find_bar_visible_bounds_in_screen_);
237 return bounds_in_screen;
240 void ImmersiveModeControllerAsh::OnWindowShowTypeChanged(
241 ash::wm::WindowState* window_state,
242 ash::wm::WindowShowType old_type) {
243 // Disable immersive fullscreen when the user exits fullscreen without going
244 // through FullscreenController::ToggleFullscreenMode(). This is the case if
245 // the user exits fullscreen via the restore button.
246 if (controller_->IsEnabled() &&
247 !window_state->IsFullscreen() &&
248 !window_state->IsMinimized()) {
249 browser_view_->FullscreenStateChanged();
253 void ImmersiveModeControllerAsh::Observe(
254 int type,
255 const content::NotificationSource& source,
256 const content::NotificationDetails& details) {
257 DCHECK_EQ(chrome::NOTIFICATION_FULLSCREEN_CHANGED, type);
258 if (!controller_->IsEnabled())
259 return;
261 bool tab_indicator_visibility_changed = UpdateTabIndicators();
263 // Auto hide the shelf in immersive browser fullscreen. When auto hidden, the
264 // shelf displays a 3px 'light bar' when it is closed. When in immersive
265 // browser fullscreen and tab fullscreen, hide the shelf completely and
266 // prevent it from being revealed.
267 bool in_tab_fullscreen = content::Source<FullscreenController>(source)->
268 IsFullscreenForTabOrPending();
269 ash::wm::GetWindowState(native_window_)->set_hide_shelf_when_fullscreen(
270 in_tab_fullscreen);
271 ash::Shell::GetInstance()->UpdateShelfVisibility();
273 if (tab_indicator_visibility_changed)
274 LayoutBrowserRootView();