summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/views/frame/immersive_mode_controller_ash.cc
blob: 8880b27bb63b776fa79f1cfc430eb7ae6ef67dab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// Copyright 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/views/frame/immersive_mode_controller_ash.h"

#include "ash/shell.h"
#include "ash/wm/window_state.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/ui/fullscreen/fullscreen_controller.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/frame/top_container_view.h"
#include "chrome/browser/ui/views/tabs/tab_strip.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_view.h"
#include "ui/aura/window.h"
#include "ui/views/view.h"
#include "ui/views/widget/widget.h"
#include "ui/views/window/non_client_view.h"

namespace {

// Revealing the TopContainerView looks better if the animation starts and ends
// just a few pixels before the view goes offscreen, which reduces the visual
// "pop" as the 3-pixel tall "light bar" style tab strip becomes visible.
const int kAnimationOffsetY = 3;

// The height of the region in pixels at the top edge of the screen in which to
// steal touch events targetted at the web contents while in immersive
// fullscreen. This region is used to allow us to get edge gestures even if the
// web contents consumes all touch events.
const int kStealTouchEventsFromWebContentsRegionHeightPx = 8;

// Converts from ImmersiveModeController::AnimateReveal to
// ash::ImmersiveFullscreenController::AnimateReveal.
ash::ImmersiveFullscreenController::AnimateReveal
ToImmersiveFullscreenControllerAnimateReveal(
    ImmersiveModeController::AnimateReveal animate_reveal) {
  switch (animate_reveal) {
    case ImmersiveModeController::ANIMATE_REVEAL_YES:
      return ash::ImmersiveFullscreenController::ANIMATE_REVEAL_YES;
    case ImmersiveModeController::ANIMATE_REVEAL_NO:
      return ash::ImmersiveFullscreenController::ANIMATE_REVEAL_NO;
  }
  NOTREACHED();
  return ash::ImmersiveFullscreenController::ANIMATE_REVEAL_NO;
}

}  // namespace

ImmersiveModeControllerAsh::ImmersiveModeControllerAsh()
    : controller_(new ash::ImmersiveFullscreenController),
      browser_view_(NULL),
      native_window_(NULL),
      observers_enabled_(false),
      use_tab_indicators_(false),
      visible_fraction_(1) {
}

ImmersiveModeControllerAsh::~ImmersiveModeControllerAsh() {
  EnableWindowObservers(false);
}

void ImmersiveModeControllerAsh::Init(BrowserView* browser_view) {
  browser_view_ = browser_view;
  native_window_ = browser_view_->GetNativeWindow();
  controller_->Init(this, browser_view_->frame(),
      browser_view_->top_container());
}

void ImmersiveModeControllerAsh::SetEnabled(bool enabled) {
  if (controller_->IsEnabled() == enabled)
    return;

  EnableWindowObservers(enabled);

  controller_->SetEnabled(browser_view_->browser()->is_app() ?
          ash::ImmersiveFullscreenController::WINDOW_TYPE_HOSTED_APP :
          ash::ImmersiveFullscreenController::WINDOW_TYPE_BROWSER
      , enabled);
}

bool ImmersiveModeControllerAsh::IsEnabled() const {
  return controller_->IsEnabled();
}

bool ImmersiveModeControllerAsh::ShouldHideTabIndicators() const {
  return !use_tab_indicators_;
}

bool ImmersiveModeControllerAsh::ShouldHideTopViews() const {
  return controller_->IsEnabled() && !controller_->IsRevealed();
}

bool ImmersiveModeControllerAsh::IsRevealed() const {
  return controller_->IsRevealed();
}

int ImmersiveModeControllerAsh::GetTopContainerVerticalOffset(
    const gfx::Size& top_container_size) const {
  if (!IsEnabled())
    return 0;

  // The TopContainerView is flush with the top of |browser_view_| when the
  // top-of-window views are fully closed so that when the tab indicators are
  // used, the "light bar" style tab strip is flush with the top of
  // |browser_view_|.
  if (!IsRevealed())
    return 0;

  int height = top_container_size.height() - kAnimationOffsetY;
  return static_cast<int>(height * (visible_fraction_ - 1));
}

ImmersiveRevealedLock* ImmersiveModeControllerAsh::GetRevealedLock(
    AnimateReveal animate_reveal) {
  return controller_->GetRevealedLock(
      ToImmersiveFullscreenControllerAnimateReveal(animate_reveal));
}

void ImmersiveModeControllerAsh::OnFindBarVisibleBoundsChanged(
    const gfx::Rect& new_visible_bounds_in_screen) {
  find_bar_visible_bounds_in_screen_ = new_visible_bounds_in_screen;
}

void ImmersiveModeControllerAsh::SetupForTest() {
  controller_->SetupForTest();
}

void ImmersiveModeControllerAsh::EnableWindowObservers(bool enable) {
  if (observers_enabled_ == enable)
    return;
  observers_enabled_ = enable;

  content::Source<FullscreenController> source(
      browser_view_->browser()->fullscreen_controller());
  if (enable) {
    ash::wm::GetWindowState(native_window_)->AddObserver(this);
    registrar_.Add(this, chrome::NOTIFICATION_FULLSCREEN_CHANGED, source);
  } else {
    ash::wm::GetWindowState(native_window_)->RemoveObserver(this);
    registrar_.Remove(this, chrome::NOTIFICATION_FULLSCREEN_CHANGED, source);
  }
}

void ImmersiveModeControllerAsh::LayoutBrowserRootView() {
  views::Widget* widget = browser_view_->frame();
  // Update the window caption buttons.
  widget->non_client_view()->frame_view()->ResetWindowControls();
  widget->non_client_view()->frame_view()->InvalidateLayout();
  browser_view_->InvalidateLayout();
  widget->GetRootView()->Layout();
}

void ImmersiveModeControllerAsh::SetRenderWindowTopInsetsForTouch(
    int top_inset) {
  content::WebContents* contents = browser_view_->GetActiveWebContents();
  if (contents) {
    aura::Window* window = contents->GetView()->GetContentNativeView();
    // |window| is NULL if the renderer crashed.
    if (window) {
      gfx::Insets inset(top_inset, 0, 0, 0);
      window->SetHitTestBoundsOverrideOuter(
          window->hit_test_bounds_override_outer_mouse(),
          inset);
    }
  }
}

bool ImmersiveModeControllerAsh::UpdateTabIndicators() {
  bool has_tabstrip = browser_view_->IsBrowserTypeNormal();
  if (!IsEnabled() || !has_tabstrip) {
    use_tab_indicators_ = false;
  } else {
    bool in_tab_fullscreen = browser_view_->browser()->fullscreen_controller()->
        IsFullscreenForTabOrPending();
    use_tab_indicators_ = !in_tab_fullscreen;
  }

  bool show_tab_indicators = use_tab_indicators_ && !IsRevealed();
  if (show_tab_indicators != browser_view_->tabstrip()->IsImmersiveStyle()) {
    browser_view_->tabstrip()->SetImmersiveStyle(show_tab_indicators);
    return true;
  }
  return false;
}

void ImmersiveModeControllerAsh::OnImmersiveRevealStarted() {
  visible_fraction_ = 0;
  browser_view_->top_container()->SetPaintToLayer(true);
  UpdateTabIndicators();
  SetRenderWindowTopInsetsForTouch(0);
  LayoutBrowserRootView();
  FOR_EACH_OBSERVER(Observer, observers_, OnImmersiveRevealStarted());
}

void ImmersiveModeControllerAsh::OnImmersiveRevealEnded() {
  visible_fraction_ = 0;
  browser_view_->top_container()->SetPaintToLayer(false);
  UpdateTabIndicators();
  SetRenderWindowTopInsetsForTouch(
      kStealTouchEventsFromWebContentsRegionHeightPx);
  LayoutBrowserRootView();
}

void ImmersiveModeControllerAsh::OnImmersiveFullscreenExited() {
  browser_view_->top_container()->SetPaintToLayer(false);
  UpdateTabIndicators();
  SetRenderWindowTopInsetsForTouch(0);
  LayoutBrowserRootView();
}

void ImmersiveModeControllerAsh::SetVisibleFraction(double visible_fraction) {
  if (visible_fraction_ != visible_fraction) {
    visible_fraction_ = visible_fraction;
    browser_view_->Layout();
  }
}

std::vector<gfx::Rect>
ImmersiveModeControllerAsh::GetVisibleBoundsInScreen() const {
  views::View* top_container_view = browser_view_->top_container();
  gfx::Rect top_container_view_bounds = top_container_view->GetVisibleBounds();
  // TODO(tdanderson): Implement View::ConvertRectToScreen().
  gfx::Point top_container_view_bounds_in_screen_origin(
      top_container_view_bounds.origin());
  views::View::ConvertPointToScreen(top_container_view,
      &top_container_view_bounds_in_screen_origin);
  gfx::Rect top_container_view_bounds_in_screen(
      top_container_view_bounds_in_screen_origin,
      top_container_view_bounds.size());

  std::vector<gfx::Rect> bounds_in_screen;
  bounds_in_screen.push_back(top_container_view_bounds_in_screen);
  bounds_in_screen.push_back(find_bar_visible_bounds_in_screen_);
  return bounds_in_screen;
}

void ImmersiveModeControllerAsh::OnWindowShowTypeChanged(
    ash::wm::WindowState* window_state,
    ash::wm::WindowShowType old_type) {
  // Disable immersive fullscreen when the user exits fullscreen without going
  // through FullscreenController::ToggleFullscreenMode(). This is the case if
  // the user exits fullscreen via the restore button.
  if (controller_->IsEnabled() &&
      !window_state->IsFullscreen() &&
      !window_state->IsMinimized()) {
    browser_view_->FullscreenStateChanged();
  }
}

void ImmersiveModeControllerAsh::Observe(
    int type,
    const content::NotificationSource& source,
    const content::NotificationDetails& details) {
  DCHECK_EQ(chrome::NOTIFICATION_FULLSCREEN_CHANGED, type);
  if (!controller_->IsEnabled())
    return;

  bool tab_indicator_visibility_changed = UpdateTabIndicators();

  // Auto hide the shelf in immersive browser fullscreen. When auto hidden, the
  // shelf displays a 3px 'light bar' when it is closed. When in immersive
  // browser fullscreen and tab fullscreen, hide the shelf completely and
  // prevent it from being revealed.
  bool in_tab_fullscreen = content::Source<FullscreenController>(source)->
      IsFullscreenForTabOrPending();
  ash::wm::GetWindowState(native_window_)->set_hide_shelf_when_fullscreen(
      in_tab_fullscreen);
  ash::Shell::GetInstance()->UpdateShelfVisibility();

  if (tab_indicator_visibility_changed)
    LayoutBrowserRootView();
}