1f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
2f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// found in the LICENSE file.
4f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
5f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "ash/wm/immersive_fullscreen_controller.h"
6f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
7f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include <set>
8f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
9f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "ash/shell.h"
10f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "ash/wm/window_state.h"
11d57369da7c6519fef57db42085f7b42d4c8845c1Torne (Richard Coles)#include "base/metrics/histogram.h"
12f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "ui/aura/client/activation_client.h"
13f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "ui/aura/client/aura_constants.h"
14f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "ui/aura/client/capture_client.h"
15f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "ui/aura/client/cursor_client.h"
16f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "ui/aura/client/screen_position_client.h"
17f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "ui/aura/env.h"
18f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "ui/aura/root_window.h"
19f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "ui/aura/window.h"
20f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "ui/gfx/animation/slide_animation.h"
21f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "ui/gfx/display.h"
22f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "ui/gfx/point.h"
23f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "ui/gfx/rect.h"
24f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "ui/gfx/screen.h"
25f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "ui/views/bubble/bubble_delegate.h"
26f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "ui/views/view.h"
27f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "ui/views/widget/widget.h"
28f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
29f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)using views::View;
30f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
31f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)namespace ash {
32f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
33f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)namespace {
34f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
35f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Duration for the reveal show/hide slide animation. The slower duration is
36f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// used for the initial slide out to give the user more change to see what
37f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// happened.
38f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)const int kRevealSlowAnimationDurationMs = 400;
39f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)const int kRevealFastAnimationDurationMs = 200;
40f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
41f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// The delay in milliseconds between the mouse stopping at the top edge of the
42f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// screen and the top-of-window views revealing.
43f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)const int kMouseRevealDelayMs = 200;
44f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
45f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// The maximum amount of pixels that the cursor can move for the cursor to be
46f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// considered "stopped". This allows the user to reveal the top-of-window views
47f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// without holding the cursor completely still.
48f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)const int kMouseRevealXThresholdPixels = 3;
49f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
50f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// How many pixels a gesture can start away from |top_container_| when in
51f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// closed state and still be considered near it. This is needed to overcome
52f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// issues with poor location values near the edge of the display.
53f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)const int kNearTopContainerDistance = 8;
54f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
55f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Used to multiply x value of an update in check to determine if gesture is
56f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// vertical. This is used to make sure that gesture is close to vertical instead
57f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// of just more vertical then horizontal.
58f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)const int kSwipeVerticalThresholdMultiplier = 3;
59f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
60f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// The height in pixels of the region above the top edge of the display which
61f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// hosts the immersive fullscreen window in which mouse events are ignored
62f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// (cannot reveal or unreveal the top-of-window views).
63f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// See ShouldIgnoreMouseEventAtLocation() for more details.
64f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)const int kHeightOfDeadRegionAboveTopContainer = 10;
65f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
66f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// The height in pixels of the region below the top edge of the display in which
67f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// the mouse can trigger revealing the top-of-window views. The height must be
68f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// greater than 1px because the top pixel is used to trigger moving the cursor
69f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// between displays if the user has a vertical display layout (primary display
70f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// above/below secondary display).
71f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)const int kMouseRevealBoundsHeight = 3;
72f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
73f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Returns the BubbleDelegateView corresponding to |maybe_bubble| if
74f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// |maybe_bubble| is a bubble.
75f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)views::BubbleDelegateView* AsBubbleDelegate(aura::Window* maybe_bubble) {
76f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (!maybe_bubble)
77f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return NULL;
78f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  views::Widget* widget = views::Widget::GetWidgetForNativeView(maybe_bubble);
79f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (!widget)
80f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return NULL;
81f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return widget->widget_delegate()->AsBubbleDelegate();
82f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
83f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
84f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Returns true if |maybe_transient| is a transient child of |toplevel|.
85f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)bool IsWindowTransientChildOf(aura::Window* maybe_transient,
86f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                              aura::Window* toplevel) {
87f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (!maybe_transient || !toplevel)
88f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return false;
89f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
90f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  for (aura::Window* window = maybe_transient; window;
91f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)       window = window->transient_parent()) {
92f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    if (window == toplevel)
93f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      return true;
94f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
95f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return false;
96f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
97f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
98f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Returns the location of |event| in screen coordinates.
99f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)gfx::Point GetEventLocationInScreen(const ui::LocatedEvent& event) {
100f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  gfx::Point location_in_screen = event.location();
101f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  aura::Window* target = static_cast<aura::Window*>(event.target());
102f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  aura::client::ScreenPositionClient* screen_position_client =
103f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      aura::client::GetScreenPositionClient(target->GetRootWindow());
104f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  screen_position_client->ConvertPointToScreen(target, &location_in_screen);
105f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return location_in_screen;
106f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
107f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
108f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Returns the bounds of the display nearest to |window| in screen coordinates.
109f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)gfx::Rect GetDisplayBoundsInScreen(aura::Window* window) {
110f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return Shell::GetScreen()->GetDisplayNearestWindow(window).bounds();
111f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
112f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
113f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}  // namespace
114f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
115f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
116f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
117f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Class which keeps the top-of-window views revealed as long as one of the
118f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// bubbles it is observing is visible. The logic to keep the top-of-window
119f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// views revealed based on the visibility of bubbles anchored to
120f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// children of |ImmersiveFullscreenController::top_container_| is separate from
121f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// the logic related to |ImmersiveFullscreenController::focus_revealed_lock_|
122f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// so that bubbles which are not activatable and bubbles which do not close
123f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// upon deactivation also keep the top-of-window views revealed for the
124f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// duration of their visibility.
125f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)class ImmersiveFullscreenController::BubbleManager
126f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    : public aura::WindowObserver {
127f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) public:
128f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  explicit BubbleManager(ImmersiveFullscreenController* controller);
129f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual ~BubbleManager();
130f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
131f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Start / stop observing changes to |bubble|'s visibility.
132f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  void StartObserving(aura::Window* bubble);
133f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  void StopObserving(aura::Window* bubble);
134f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
135f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) private:
136f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Updates |revealed_lock_| based on whether any of |bubbles_| is visible.
137f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  void UpdateRevealedLock();
138f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
139f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // aura::WindowObserver overrides:
140f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual void OnWindowVisibilityChanged(aura::Window* window,
141f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                         bool visible) OVERRIDE;
142f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual void OnWindowDestroying(aura::Window* window) OVERRIDE;
143f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
144f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  ImmersiveFullscreenController* controller_;
145f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
146f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  std::set<aura::Window*> bubbles_;
147f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
148f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Lock which keeps the top-of-window views revealed based on whether any of
149f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // |bubbles_| is visible.
150f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  scoped_ptr<ImmersiveRevealedLock> revealed_lock_;
151f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
152f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(BubbleManager);
153f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)};
154f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
155f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)ImmersiveFullscreenController::BubbleManager::BubbleManager(
156f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    ImmersiveFullscreenController* controller)
157f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    : controller_(controller) {
158f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
159f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
160f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)ImmersiveFullscreenController::BubbleManager::~BubbleManager() {
161f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  for (std::set<aura::Window*>::const_iterator it = bubbles_.begin();
162f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)       it != bubbles_.end(); ++it) {
163f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    (*it)->RemoveObserver(this);
164f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
165f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
166f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
167f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void ImmersiveFullscreenController::BubbleManager::StartObserving(
168f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    aura::Window* bubble) {
169f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (bubbles_.insert(bubble).second) {
170f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    bubble->AddObserver(this);
171f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    UpdateRevealedLock();
172f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
173f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
174f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
175f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void ImmersiveFullscreenController::BubbleManager::StopObserving(
176f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    aura::Window* bubble) {
177f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (bubbles_.erase(bubble)) {
178f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    bubble->RemoveObserver(this);
179f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    UpdateRevealedLock();
180f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
181f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
182f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
183f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void ImmersiveFullscreenController::BubbleManager::UpdateRevealedLock() {
184f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  bool has_visible_bubble = false;
185f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  for (std::set<aura::Window*>::const_iterator it = bubbles_.begin();
186f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)       it != bubbles_.end(); ++it) {
187f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    if ((*it)->IsVisible()) {
188f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      has_visible_bubble = true;
189f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      break;
190f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    }
191f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
192f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
193f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  bool was_revealed = controller_->IsRevealed();
194f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (has_visible_bubble) {
195f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    if (!revealed_lock_.get()) {
196f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      // Reveal the top-of-window views without animating because it looks
197f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      // weird for the top-of-window views to animate and the bubble not to
198f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      // animate along with the top-of-window views.
199f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      revealed_lock_.reset(controller_->GetRevealedLock(
200f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          ImmersiveFullscreenController::ANIMATE_REVEAL_NO));
201f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    }
202f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  } else {
203f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    revealed_lock_.reset();
204f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
205f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
206f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (!was_revealed && revealed_lock_.get()) {
207f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // Currently, there is no nice way for bubbles to reposition themselves
208f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // whenever the anchor view moves. Tell the bubbles to reposition themselves
209f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // explicitly instead. The hidden bubbles are also repositioned because
210f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // BubbleDelegateView does not reposition its widget as a result of a
211f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // visibility change.
212f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    for (std::set<aura::Window*>::const_iterator it = bubbles_.begin();
213f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)         it != bubbles_.end(); ++it) {
214f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      AsBubbleDelegate(*it)->OnAnchorBoundsChanged();
215f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    }
216f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
217f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
218f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
219f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void ImmersiveFullscreenController::BubbleManager::OnWindowVisibilityChanged(
220f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    aura::Window*,
221f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    bool visible) {
222f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  UpdateRevealedLock();
223f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
224f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
225f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void ImmersiveFullscreenController::BubbleManager::OnWindowDestroying(
226f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    aura::Window* window) {
227f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  StopObserving(window);
228f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
229f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
230f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
231f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
232f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)ImmersiveFullscreenController::ImmersiveFullscreenController()
233f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    : delegate_(NULL),
234f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      top_container_(NULL),
235f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      widget_(NULL),
236f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      native_window_(NULL),
237f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      observers_enabled_(false),
238f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      enabled_(false),
239f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      reveal_state_(CLOSED),
240f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      revealed_lock_count_(0),
241f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      mouse_x_when_hit_top_in_screen_(-1),
242f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      gesture_begun_(false),
243f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      animation_(new gfx::SlideAnimation(this)),
244f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      animations_disabled_for_test_(false),
245f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      weak_ptr_factory_(this) {
246f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
247f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
248f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)ImmersiveFullscreenController::~ImmersiveFullscreenController() {
249f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EnableWindowObservers(false);
250f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
251f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
252f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void ImmersiveFullscreenController::Init(Delegate* delegate,
253f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                         views::Widget* widget,
254f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                         views::View* top_container) {
255f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  delegate_ = delegate;
256f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  top_container_ = top_container;
257f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  widget_ = widget;
258f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  native_window_ = widget_->GetNativeWindow();
259f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
260f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
261d57369da7c6519fef57db42085f7b42d4c8845c1Torne (Richard Coles)void ImmersiveFullscreenController::SetEnabled(WindowType window_type,
262d57369da7c6519fef57db42085f7b42d4c8845c1Torne (Richard Coles)                                               bool enabled) {
263f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (enabled_ == enabled)
264f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return;
265f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  enabled_ = enabled;
266f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
267f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EnableWindowObservers(enabled_);
268f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
269f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Auto hide the shelf in immersive fullscreen instead of hiding it.
270f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  wm::GetWindowState(native_window_)->set_hide_shelf_when_fullscreen(!enabled);
271f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  Shell::GetInstance()->UpdateShelfVisibility();
272f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
273f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (enabled_) {
274f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // Animate enabling immersive mode by sliding out the top-of-window views.
275f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // No animation occurs if a lock is holding the top-of-window views open.
276f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
277f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // Do a reveal to set the initial state for the animation. (And any
278f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // required state in case the animation cannot run because of a lock holding
279f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // the top-of-window views open.)
280f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    MaybeStartReveal(ANIMATE_NO);
281f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
282f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // Reset the located event and the focus revealed locks so that they do not
283f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // affect whether the top-of-window views are hidden.
284f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    located_event_revealed_lock_.reset();
285f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    focus_revealed_lock_.reset();
286f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
287f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // Try doing the animation.
288f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    MaybeEndReveal(ANIMATE_SLOW);
289f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
290f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    if (reveal_state_ == REVEALED) {
291f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      // Reveal was unsuccessful. Reacquire the revealed locks if appropriate.
292a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      UpdateLocatedEventRevealedLock(NULL);
293f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      UpdateFocusRevealedLock();
294a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    } else {
295a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      // Clearing focus is important because it closes focus-related popups like
296a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      // the touch selection handles.
297a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      widget_->GetFocusManager()->ClearFocus();
298f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    }
299f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  } else {
300f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // Stop cursor-at-top tracking.
301f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    top_edge_hover_timer_.Stop();
302f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    reveal_state_ = CLOSED;
303f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
304f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    delegate_->OnImmersiveFullscreenExited();
305f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
306d57369da7c6519fef57db42085f7b42d4c8845c1Torne (Richard Coles)
307d57369da7c6519fef57db42085f7b42d4c8845c1Torne (Richard Coles)  if (enabled_) {
308d57369da7c6519fef57db42085f7b42d4c8845c1Torne (Richard Coles)    UMA_HISTOGRAM_ENUMERATION("Ash.ImmersiveFullscreen.WindowType",
309d57369da7c6519fef57db42085f7b42d4c8845c1Torne (Richard Coles)                              window_type,
310d57369da7c6519fef57db42085f7b42d4c8845c1Torne (Richard Coles)                              WINDOW_TYPE_COUNT);
311d57369da7c6519fef57db42085f7b42d4c8845c1Torne (Richard Coles)  }
312f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
313f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
314f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)bool ImmersiveFullscreenController::IsEnabled() const {
315f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return enabled_;
316f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
317f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
318f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)bool ImmersiveFullscreenController::IsRevealed() const {
319f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return enabled_ && reveal_state_ != CLOSED;
320f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
321f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
322f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)ImmersiveRevealedLock* ImmersiveFullscreenController::GetRevealedLock(
323f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    AnimateReveal animate_reveal) {
324f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return new ImmersiveRevealedLock(weak_ptr_factory_.GetWeakPtr(),
325f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                   animate_reveal);
326f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
327f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
328f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
329f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Testing interface:
330f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
331f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void ImmersiveFullscreenController::SetupForTest() {
332f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  DCHECK(!enabled_);
333f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  animations_disabled_for_test_ = true;
334f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
335f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Move the mouse off of the top-of-window views so that it does not keep the
336f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // top-of-window views revealed.
337f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  std::vector<gfx::Rect> bounds_in_screen(
338f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      delegate_->GetVisibleBoundsInScreen());
339f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  DCHECK(!bounds_in_screen.empty());
340f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  int bottommost_in_screen = bounds_in_screen[0].bottom();
341f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  for (size_t i = 1; i < bounds_in_screen.size(); ++i) {
342f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    if (bounds_in_screen[i].bottom() > bottommost_in_screen)
343f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      bottommost_in_screen = bounds_in_screen[i].bottom();
344f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
345f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  gfx::Point cursor_pos(0, bottommost_in_screen + 100);
346f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  aura::Env::GetInstance()->set_last_mouse_location(cursor_pos);
347a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  UpdateLocatedEventRevealedLock(NULL);
348f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
349f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
350f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
351f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// ui::EventHandler overrides:
352f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
353f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void ImmersiveFullscreenController::OnMouseEvent(ui::MouseEvent* event) {
354f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (!enabled_)
355f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return;
356f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
357f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (event->type() != ui::ET_MOUSE_MOVED &&
358f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      event->type() != ui::ET_MOUSE_PRESSED &&
359f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      event->type() != ui::ET_MOUSE_RELEASED &&
360f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      event->type() != ui::ET_MOUSE_CAPTURE_CHANGED) {
361f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return;
362f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
363f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
364a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Mouse hover can initiate revealing the top-of-window views while |widget_|
365a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // is inactive.
366f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
367a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  if (reveal_state_ == SLIDING_OPEN || reveal_state_ == REVEALED) {
368a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    top_edge_hover_timer_.Stop();
369a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    UpdateLocatedEventRevealedLock(event);
370a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  } else if (event->type() != ui::ET_MOUSE_CAPTURE_CHANGED) {
371a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    // Trigger a reveal if the cursor pauses at the top of the screen for a
372a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    // while.
373f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    UpdateTopEdgeHoverTimer(event);
374a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  }
375f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
376f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
377f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void ImmersiveFullscreenController::OnTouchEvent(ui::TouchEvent* event) {
378f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (!enabled_ || event->type() != ui::ET_TOUCH_PRESSED)
379f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return;
380f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
381a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Touch should not initiate revealing the top-of-window views while |widget_|
382a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // is inactive.
383a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  if (!widget_->IsActive())
384a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    return;
385a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
386a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  UpdateLocatedEventRevealedLock(event);
387f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
388f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
389f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void ImmersiveFullscreenController::OnGestureEvent(ui::GestureEvent* event) {
390f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (!enabled_)
391f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return;
392f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
393f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Touch gestures should not initiate revealing the top-of-window views while
394a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // |widget_| is inactive.
395a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  if (!widget_->IsActive())
396f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return;
397f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
398f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  switch (event->type()) {
399f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    case ui::ET_GESTURE_SCROLL_BEGIN:
400f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      if (ShouldHandleGestureEvent(GetEventLocationInScreen(*event))) {
401f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        gesture_begun_ = true;
402a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)        // Do not consume the event. Otherwise, we end up consuming all
403a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)        // ui::ET_GESTURE_SCROLL_BEGIN events in the top-of-window views
404a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)        // when the top-of-window views are revealed.
405f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      }
406f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      break;
407f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    case ui::ET_GESTURE_SCROLL_UPDATE:
408f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      if (gesture_begun_) {
409f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        if (UpdateRevealedLocksForSwipe(GetSwipeType(event)))
410f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          event->SetHandled();
411f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        gesture_begun_ = false;
412f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      }
413f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      break;
414f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    case ui::ET_GESTURE_SCROLL_END:
415f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    case ui::ET_SCROLL_FLING_START:
416f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      gesture_begun_ = false;
417f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      break;
418f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    default:
419f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      break;
420f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
421f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
422f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
423f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
424f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// views::FocusChangeListener overrides:
425f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
426f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void ImmersiveFullscreenController::OnWillChangeFocus(
427f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    views::View* focused_before,
428f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    views::View* focused_now) {
429f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
430f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
431f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void ImmersiveFullscreenController::OnDidChangeFocus(
432f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    views::View* focused_before,
433f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    views::View* focused_now) {
434f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  UpdateFocusRevealedLock();
435f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
436f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
437f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
438f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// views::WidgetObserver overrides:
439f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
440f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void ImmersiveFullscreenController::OnWidgetDestroying(views::Widget* widget) {
441f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EnableWindowObservers(false);
442f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  native_window_ = NULL;
443f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
444f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Set |enabled_| to false such that any calls to MaybeStartReveal() and
445f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // MaybeEndReveal() have no effect.
446f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  enabled_ = false;
447f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
448f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
449f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void ImmersiveFullscreenController::OnWidgetActivationChanged(
450f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    views::Widget* widget,
451f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    bool active) {
452f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  UpdateFocusRevealedLock();
453f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
454f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
455f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
456f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// gfx::AnimationDelegate overrides:
457f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
458f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void ImmersiveFullscreenController::AnimationEnded(
459f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const gfx::Animation* animation) {
460f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (reveal_state_ == SLIDING_OPEN) {
461f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    OnSlideOpenAnimationCompleted();
462f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  } else if (reveal_state_ == SLIDING_CLOSED) {
463f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    OnSlideClosedAnimationCompleted();
464f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
465f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
466f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
467f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void ImmersiveFullscreenController::AnimationProgressed(
468f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const gfx::Animation* animation) {
469f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  delegate_->SetVisibleFraction(animation->GetCurrentValue());
470f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
471f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
472f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
473f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// aura::WindowObserver overrides:
474f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
475f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void ImmersiveFullscreenController::OnAddTransientChild(aura::Window* window,
476f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                                     aura::Window* transient) {
477f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  views::BubbleDelegateView* bubble_delegate = AsBubbleDelegate(transient);
478f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (bubble_delegate &&
479f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      bubble_delegate->GetAnchorView() &&
480f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      top_container_->Contains(bubble_delegate->GetAnchorView())) {
481f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // Observe the aura::Window because the BubbleDelegateView may not be
482f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // parented to the widget's root view yet so |bubble_delegate->GetWidget()|
483f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // may still return NULL.
484f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    bubble_manager_->StartObserving(transient);
485f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
486f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
487f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
488f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void ImmersiveFullscreenController::OnRemoveTransientChild(
489f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    aura::Window* window,
490f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    aura::Window* transient) {
491f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  bubble_manager_->StopObserving(transient);
492f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
493f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
494f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
495f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// ash::ImmersiveRevealedLock::Delegate overrides:
496f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
497f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void ImmersiveFullscreenController::LockRevealedState(
498f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    AnimateReveal animate_reveal) {
499f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  ++revealed_lock_count_;
500f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  Animate animate = (animate_reveal == ANIMATE_REVEAL_YES) ?
501f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      ANIMATE_FAST : ANIMATE_NO;
502f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  MaybeStartReveal(animate);
503f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
504f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
505f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void ImmersiveFullscreenController::UnlockRevealedState() {
506f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  --revealed_lock_count_;
507f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  DCHECK_GE(revealed_lock_count_, 0);
508f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (revealed_lock_count_ == 0) {
509f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // Always animate ending the reveal fast.
510f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    MaybeEndReveal(ANIMATE_FAST);
511f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
512f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
513f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
514f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
515f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// private:
516f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
517f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void ImmersiveFullscreenController::EnableWindowObservers(bool enable) {
518f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (observers_enabled_ == enable)
519f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return;
520f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  observers_enabled_ = enable;
521f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
522a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  views::FocusManager* focus_manager = widget_->GetFocusManager();
523f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
524f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (enable) {
525a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    widget_->AddObserver(this);
526f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    focus_manager->AddFocusChangeListener(this);
527f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    Shell::GetInstance()->AddPreTargetHandler(this);
528f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    native_window_->AddObserver(this);
529f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
530f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    RecreateBubbleManager();
531f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  } else {
532a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    widget_->RemoveObserver(this);
533f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    focus_manager->RemoveFocusChangeListener(this);
534f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    Shell::GetInstance()->RemovePreTargetHandler(this);
535f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    native_window_->RemoveObserver(this);
536f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
537f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // We have stopped observing whether transient children are added or removed
538f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // to |native_window_|. The set of bubbles that BubbleManager is observing
539f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // will become stale really quickly. Destroy BubbleManager and recreate it
540f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // when we start observing |native_window_| again.
541f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    bubble_manager_.reset();
542f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
543f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    animation_->Stop();
544f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
545f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
546f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
547f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void ImmersiveFullscreenController::UpdateTopEdgeHoverTimer(
548f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    ui::MouseEvent* event) {
549f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  DCHECK(enabled_);
550a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  DCHECK(reveal_state_ == SLIDING_CLOSED || reveal_state_ == CLOSED);
551a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
552a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Check whether |native_window_| is the event target's parent window instead
553a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // of checking for activation. This allows the timer to be started when
554a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // |widget_| is inactive but prevents starting the timer if the mouse is over
555a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // a portion of the top edge obscured by an unrelated widget.
556a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  if (!top_edge_hover_timer_.IsRunning() &&
557a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      !native_window_->Contains(static_cast<aura::Window*>(event->target()))) {
558f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return;
559f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
560f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
561a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Mouse hover should not initiate revealing the top-of-window views while a
562a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // window has mouse capture.
563a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  if (aura::client::GetCaptureWindow(native_window_))
564a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    return;
565a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
566f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  gfx::Point location_in_screen = GetEventLocationInScreen(*event);
567f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (ShouldIgnoreMouseEventAtLocation(location_in_screen))
568f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return;
569f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
570f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Stop the timer if the cursor left the top edge or is on a different
571f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // display.
572f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  gfx::Rect hit_bounds_in_screen = GetDisplayBoundsInScreen(native_window_);
573f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  hit_bounds_in_screen.set_height(kMouseRevealBoundsHeight);
574f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (!hit_bounds_in_screen.Contains(location_in_screen)) {
575f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    top_edge_hover_timer_.Stop();
576f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return;
577f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
578f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
579f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // The cursor is now at the top of the screen. Consider the cursor "not
580f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // moving" even if it moves a little bit because users don't have perfect
581f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // pointing precision. (The y position is not tested because
582f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // |hit_bounds_in_screen| is short.)
583f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (top_edge_hover_timer_.IsRunning() &&
584f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      abs(location_in_screen.x() - mouse_x_when_hit_top_in_screen_) <=
585f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          kMouseRevealXThresholdPixels)
586f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return;
587f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
588f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Start the reveal if the cursor doesn't move for some amount of time.
589f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  mouse_x_when_hit_top_in_screen_ = location_in_screen.x();
590f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  top_edge_hover_timer_.Stop();
591f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Timer is stopped when |this| is destroyed, hence Unretained() is safe.
592f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  top_edge_hover_timer_.Start(
593f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      FROM_HERE,
594f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      base::TimeDelta::FromMilliseconds(kMouseRevealDelayMs),
595f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      base::Bind(
596f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          &ImmersiveFullscreenController::AcquireLocatedEventRevealedLock,
597f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          base::Unretained(this)));
598f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
599f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
600f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void ImmersiveFullscreenController::UpdateLocatedEventRevealedLock(
601a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    ui::LocatedEvent* event) {
602f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (!enabled_)
603f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return;
604f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  DCHECK(!event || event->IsMouseEvent() || event->IsTouchEvent());
605f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
606f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Neither the mouse nor touch can initiate a reveal when the top-of-window
607f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // views are sliding closed or are closed with the following exceptions:
608f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // - Hovering at y = 0 which is handled in OnMouseEvent().
609f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // - Doing a SWIPE_OPEN edge gesture which is handled in OnGestureEvent().
610a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  if (reveal_state_ == CLOSED || reveal_state_ == SLIDING_CLOSED)
611f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return;
612f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
613a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // For the sake of simplicity, ignore |widget_|'s activation in computing
614a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // whether the top-of-window views should stay revealed. Ideally, the
615a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // top-of-window views would stay revealed only when the mouse cursor is
616a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // hovered above a non-obscured portion of the top-of-window views. The
617a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // top-of-window views may be partially obscured when |widget_| is inactive.
618f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
619f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Ignore all events while a window has capture. This keeps the top-of-window
620f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // views revealed during a drag.
621f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (aura::client::GetCaptureWindow(native_window_))
622f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return;
623f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
624f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  gfx::Point location_in_screen;
625f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (event && event->type() != ui::ET_MOUSE_CAPTURE_CHANGED) {
626f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    location_in_screen = GetEventLocationInScreen(*event);
627f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  } else {
628f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    aura::client::CursorClient* cursor_client = aura::client::GetCursorClient(
629f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        native_window_->GetRootWindow());
630f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    if (!cursor_client->IsMouseEventsEnabled()) {
631f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      // If mouse events are disabled, the user's last interaction was probably
632f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      // via touch. Do no do further processing in this case as there is no easy
633f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      // way of retrieving the position of the user's last touch.
634f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      return;
635f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    }
636f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    location_in_screen = aura::Env::GetInstance()->last_mouse_location();
637f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
638f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
639f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if ((!event || event->IsMouseEvent()) &&
640f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      ShouldIgnoreMouseEventAtLocation(location_in_screen)) {
641f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return;
642f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
643f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
644f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // The visible bounds of |top_container_| should be contained in
645f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // |hit_bounds_in_screen|.
646f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  std::vector<gfx::Rect> hit_bounds_in_screen =
647f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      delegate_->GetVisibleBoundsInScreen();
648f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  bool keep_revealed = false;
649f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  for (size_t i = 0; i < hit_bounds_in_screen.size(); ++i) {
650f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // Allow the cursor to move slightly off the top-of-window views before
651f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // sliding closed. In the case of ImmersiveModeControllerAsh, this helps
652f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // when the user is attempting to click on the bookmark bar and overshoots
653f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // slightly.
654f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    if (event && event->type() == ui::ET_MOUSE_MOVED) {
655f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      const int kBoundsOffsetY = 8;
656f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      hit_bounds_in_screen[i].Inset(0, 0, 0, -kBoundsOffsetY);
657f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    }
658f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
659f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    if (hit_bounds_in_screen[i].Contains(location_in_screen)) {
660f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      keep_revealed = true;
661f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      break;
662f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    }
663f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
664f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
665f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (keep_revealed)
666f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    AcquireLocatedEventRevealedLock();
667f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  else
668f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    located_event_revealed_lock_.reset();
669f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
670f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
671f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void ImmersiveFullscreenController::AcquireLocatedEventRevealedLock() {
672f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // CAUTION: Acquiring the lock results in a reentrant call to
673f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // AcquireLocatedEventRevealedLock() when
674f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // |ImmersiveFullscreenController::animations_disabled_for_test_| is true.
675f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (!located_event_revealed_lock_.get())
676f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    located_event_revealed_lock_.reset(GetRevealedLock(ANIMATE_REVEAL_YES));
677f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
678f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
679f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void ImmersiveFullscreenController::UpdateFocusRevealedLock() {
680f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (!enabled_)
681f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return;
682f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
683f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  bool hold_lock = false;
684a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  if (widget_->IsActive()) {
685a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    views::View* focused_view = widget_->GetFocusManager()->GetFocusedView();
686f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    if (top_container_->Contains(focused_view))
687f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      hold_lock = true;
688f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  } else {
689f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    aura::Window* active_window = aura::client::GetActivationClient(
690f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        native_window_->GetRootWindow())->GetActiveWindow();
691f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    views::BubbleDelegateView* bubble_delegate =
692f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        AsBubbleDelegate(active_window);
693f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    if (bubble_delegate && bubble_delegate->anchor_widget()) {
694f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      // BubbleManager will already have locked the top-of-window views if the
695f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      // bubble is anchored to a child of |top_container_|. Don't acquire
696f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      // |focus_revealed_lock_| here for the sake of simplicity.
697f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      // Note: Instead of checking for the existence of the |anchor_view|,
698f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      // the existence of the |anchor_widget| is performed to avoid the case
699f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      // where the view is already gone (and the widget is still running).
700f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    } else {
701f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      // The currently active window is not |native_window_| and it is not a
702f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      // bubble with an anchor view. The top-of-window views should be revealed
703f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      // if:
704f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      // 1) The active window is a transient child of |native_window_|.
705f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      // 2) The top-of-window views are already revealed. This restriction
706f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      //    prevents a transient window opened by the web contents while the
707f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      //    top-of-window views are hidden from from initiating a reveal.
708f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      // The top-of-window views will stay revealed till |native_window_| is
709f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      // reactivated.
710f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      if (IsRevealed() &&
711f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          IsWindowTransientChildOf(active_window, native_window_)) {
712f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        hold_lock = true;
713f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      }
714f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    }
715f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
716f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
717f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (hold_lock) {
718f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    if (!focus_revealed_lock_.get())
719f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      focus_revealed_lock_.reset(GetRevealedLock(ANIMATE_REVEAL_YES));
720f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  } else {
721f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    focus_revealed_lock_.reset();
722f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
723f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
724f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
725f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)bool ImmersiveFullscreenController::UpdateRevealedLocksForSwipe(
726f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    SwipeType swipe_type) {
727f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (!enabled_ || swipe_type == SWIPE_NONE)
728f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return false;
729f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
730f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Swipes while |native_window_| is inactive should have been filtered out in
731f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // OnGestureEvent().
732a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  DCHECK(widget_->IsActive());
733f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
734f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (reveal_state_ == SLIDING_CLOSED || reveal_state_ == CLOSED) {
735f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    if (swipe_type == SWIPE_OPEN && !located_event_revealed_lock_.get()) {
736f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      located_event_revealed_lock_.reset(GetRevealedLock(ANIMATE_REVEAL_YES));
737f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      return true;
738f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    }
739f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  } else {
740f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    if (swipe_type == SWIPE_CLOSE) {
741f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      // Attempt to end the reveal. If other code is holding onto a lock, the
742f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      // attempt will be unsuccessful.
743f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      located_event_revealed_lock_.reset();
744f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      focus_revealed_lock_.reset();
745f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
746a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      if (reveal_state_ == SLIDING_CLOSED || reveal_state_ == CLOSED) {
747a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)        widget_->GetFocusManager()->ClearFocus();
748f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        return true;
749a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      }
750f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
751f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      // Ending the reveal was unsuccessful. Reaquire the locks if appropriate.
752a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      UpdateLocatedEventRevealedLock(NULL);
753f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      UpdateFocusRevealedLock();
754f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    }
755f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
756f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return false;
757f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
758f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
759f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)int ImmersiveFullscreenController::GetAnimationDuration(Animate animate) const {
760f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  switch (animate) {
761f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    case ANIMATE_NO:
762f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      return 0;
763f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    case ANIMATE_SLOW:
764f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      return kRevealSlowAnimationDurationMs;
765f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    case ANIMATE_FAST:
766f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      return kRevealFastAnimationDurationMs;
767f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
768f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  NOTREACHED();
769f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return 0;
770f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
771f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
772f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void ImmersiveFullscreenController::MaybeStartReveal(Animate animate) {
773f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (!enabled_)
774f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return;
775f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
776f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (animations_disabled_for_test_)
777f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    animate = ANIMATE_NO;
778f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
779f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Callers with ANIMATE_NO expect this function to synchronously reveal the
780f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // top-of-window views.
781f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (reveal_state_ == REVEALED ||
782f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      (reveal_state_ == SLIDING_OPEN && animate != ANIMATE_NO)) {
783f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return;
784f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
785f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
786f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  RevealState previous_reveal_state = reveal_state_;
787f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  reveal_state_ = SLIDING_OPEN;
788f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (previous_reveal_state == CLOSED) {
789f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    delegate_->OnImmersiveRevealStarted();
790f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
791f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // Do not do any more processing if OnImmersiveRevealStarted() changed
792f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // |reveal_state_|.
793f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    if (reveal_state_ != SLIDING_OPEN)
794f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      return;
795f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
796f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Slide in the reveal view.
797f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (animate == ANIMATE_NO) {
798f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    animation_->Reset(1);
799f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    OnSlideOpenAnimationCompleted();
800f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  } else {
801f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    animation_->SetSlideDuration(GetAnimationDuration(animate));
802f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    animation_->Show();
803f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
804f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
805f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
806f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void ImmersiveFullscreenController::OnSlideOpenAnimationCompleted() {
807f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  DCHECK_EQ(SLIDING_OPEN, reveal_state_);
808f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  reveal_state_ = REVEALED;
809f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  delegate_->SetVisibleFraction(1);
810f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
811f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // The user may not have moved the mouse since the reveal was initiated.
812f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Update the revealed lock to reflect the mouse's current state.
813a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  UpdateLocatedEventRevealedLock(NULL);
814f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
815f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
816f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void ImmersiveFullscreenController::MaybeEndReveal(Animate animate) {
817f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (!enabled_ || revealed_lock_count_ != 0)
818f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return;
819f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
820f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (animations_disabled_for_test_)
821f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    animate = ANIMATE_NO;
822f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
823f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Callers with ANIMATE_NO expect this function to synchronously close the
824f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // top-of-window views.
825f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (reveal_state_ == CLOSED ||
826f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      (reveal_state_ == SLIDING_CLOSED && animate != ANIMATE_NO)) {
827f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return;
828f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
829f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
830f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  reveal_state_ = SLIDING_CLOSED;
831f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  int duration_ms = GetAnimationDuration(animate);
832f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (duration_ms > 0) {
833f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    animation_->SetSlideDuration(duration_ms);
834f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    animation_->Hide();
835f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  } else {
836f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    animation_->Reset(0);
837f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    OnSlideClosedAnimationCompleted();
838f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
839f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
840f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
841f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void ImmersiveFullscreenController::OnSlideClosedAnimationCompleted() {
842f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  DCHECK_EQ(SLIDING_CLOSED, reveal_state_);
843f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  reveal_state_ = CLOSED;
844f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  delegate_->OnImmersiveRevealEnded();
845f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
846f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
847f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)ImmersiveFullscreenController::SwipeType
848f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)ImmersiveFullscreenController::GetSwipeType(ui::GestureEvent* event) const {
849f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (event->type() != ui::ET_GESTURE_SCROLL_UPDATE)
850f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return SWIPE_NONE;
851f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Make sure that it is a clear vertical gesture.
852f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (abs(event->details().scroll_y()) <=
853f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      kSwipeVerticalThresholdMultiplier * abs(event->details().scroll_x()))
854f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return SWIPE_NONE;
855f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (event->details().scroll_y() < 0)
856f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return SWIPE_CLOSE;
857f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  else if (event->details().scroll_y() > 0)
858f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return SWIPE_OPEN;
859f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return SWIPE_NONE;
860f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
861f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
862f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)bool ImmersiveFullscreenController::ShouldIgnoreMouseEventAtLocation(
863f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const gfx::Point& location) const {
864f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Ignore mouse events in the region immediately above the top edge of the
865f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // display. This is to handle the case of a user with a vertical display
866f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // layout (primary display above/below secondary display) and the immersive
867f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // fullscreen window on the bottom display. It is really hard to trigger a
868f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // reveal in this case because:
869f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // - It is hard to stop the cursor in the top |kMouseRevealBoundsHeight|
870f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  //   pixels of the bottom display.
871f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // - The cursor is warped to the top display if the cursor gets to the top
872f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  //   edge of the bottom display.
873f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Mouse events are ignored in the bottom few pixels of the top display
874f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // (Mouse events in this region cannot start or end a reveal). This allows a
875f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // user to overshoot the top of the bottom display and still reveal the
876f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // top-of-window views.
877f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  gfx::Rect dead_region = GetDisplayBoundsInScreen(native_window_);
878f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  dead_region.set_y(dead_region.y() - kHeightOfDeadRegionAboveTopContainer);
879f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  dead_region.set_height(kHeightOfDeadRegionAboveTopContainer);
880f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return dead_region.Contains(location);
881f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
882f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
883f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)bool ImmersiveFullscreenController::ShouldHandleGestureEvent(
884f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const gfx::Point& location) const {
885a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  DCHECK(widget_->IsActive());
886f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (reveal_state_ == REVEALED) {
887f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    std::vector<gfx::Rect> hit_bounds_in_screen(
888f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        delegate_->GetVisibleBoundsInScreen());
889f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    for (size_t i = 0; i < hit_bounds_in_screen.size(); ++i) {
890f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      if (hit_bounds_in_screen[i].Contains(location))
891f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        return true;
892f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    }
893f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return false;
894f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
895f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
896f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // When the top-of-window views are not fully revealed, handle gestures which
897f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // start in the top few pixels of the screen.
898f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  gfx::Rect hit_bounds_in_screen(GetDisplayBoundsInScreen(native_window_));
899f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  hit_bounds_in_screen.set_height(kNearTopContainerDistance);
900f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (hit_bounds_in_screen.Contains(location))
901f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return true;
902f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
903f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // There may be a bezel sensor off screen logically above
904f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // |hit_bounds_in_screen|. The check for the event not contained by the
905f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // closest screen ensures that the event is from a valid bezel (as opposed to
906f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // another screen in an extended desktop).
907f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  gfx::Rect screen_bounds =
908f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      Shell::GetScreen()->GetDisplayNearestPoint(location).bounds();
909f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return (!screen_bounds.Contains(location) &&
910f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          location.y() < hit_bounds_in_screen.y() &&
911f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          location.x() >= hit_bounds_in_screen.x() &&
912f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          location.x() < hit_bounds_in_screen.right());
913f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
914f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
915f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void ImmersiveFullscreenController::RecreateBubbleManager() {
916f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  bubble_manager_.reset(new BubbleManager(this));
917f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  const std::vector<aura::Window*> transient_children =
918f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      native_window_->transient_children();
919f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  for (size_t i = 0; i < transient_children.size(); ++i) {
920f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    aura::Window* transient_child = transient_children[i];
921f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    views::BubbleDelegateView* bubble_delegate =
922f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        AsBubbleDelegate(transient_child);
923f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    if (bubble_delegate &&
924f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        bubble_delegate->GetAnchorView() &&
925f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        top_container_->Contains(bubble_delegate->GetAnchorView())) {
926f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      bubble_manager_->StartObserving(transient_child);
927f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    }
928f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
929f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
930f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
931f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}  // namespace ash
932