| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "athena/wm/split_view_controller.h" | 5 #include "athena/wm/split_view_controller.h" |
| 6 | 6 |
| 7 #include <cmath> | |
| 8 | |
| 9 #include "athena/wm/public/window_list_provider.h" | |
| 10 #include "athena/wm/public/window_manager.h" | |
| 11 #include "base/bind.h" | |
| 12 #include "ui/aura/window.h" | 7 #include "ui/aura/window.h" |
| 13 #include "ui/compositor/layer_animation_observer.h" | |
| 14 #include "ui/compositor/scoped_layer_animation_settings.h" | |
| 15 #include "ui/events/event_handler.h" | 8 #include "ui/events/event_handler.h" |
| 16 #include "ui/gfx/display.h" | |
| 17 #include "ui/gfx/screen.h" | |
| 18 | 9 |
| 19 namespace athena { | 10 namespace athena { |
| 20 namespace { | |
| 21 | 11 |
| 22 // An animation observer that runs a callback at the end of the animation, and | 12 SplitViewController::SplitViewController() { |
| 23 // destroys itself. | |
| 24 class CallbackAnimationObserver : public ui::ImplicitAnimationObserver { | |
| 25 public: | |
| 26 explicit CallbackAnimationObserver(const base::Closure& closure) | |
| 27 : closure_(closure) {} | |
| 28 | |
| 29 virtual ~CallbackAnimationObserver() {} | |
| 30 | |
| 31 private: | |
| 32 // Overridden from ui::ImplicitAnimationObserver: | |
| 33 virtual void OnImplicitAnimationsCompleted() OVERRIDE { | |
| 34 if (!closure_.is_null()) | |
| 35 closure_.Run(); | |
| 36 delete this; | |
| 37 } | |
| 38 | |
| 39 const base::Closure closure_; | |
| 40 | |
| 41 DISALLOW_COPY_AND_ASSIGN(CallbackAnimationObserver); | |
| 42 }; | |
| 43 | |
| 44 } // namespace | |
| 45 | |
| 46 SplitViewController::SplitViewController( | |
| 47 aura::Window* container, | |
| 48 WindowListProvider* window_list_provider, | |
| 49 WindowManager* window_manager) | |
| 50 : state_(INACTIVE), | |
| 51 container_(container), | |
| 52 window_manager_(window_manager), | |
| 53 window_list_provider_(window_list_provider), | |
| 54 current_activity_window_(NULL), | |
| 55 left_window_(NULL), | |
| 56 right_window_(NULL), | |
| 57 separator_position_(0), | |
| 58 weak_factory_(this) { | |
| 59 window_manager->AddObserver(this); | |
| 60 } | 13 } |
| 61 | 14 |
| 62 SplitViewController::~SplitViewController() { | 15 SplitViewController::~SplitViewController() { |
| 63 window_manager_->RemoveObserver(this); | |
| 64 } | 16 } |
| 65 | 17 |
| 66 bool SplitViewController::IsSplitViewModeActive() const { | 18 void SplitViewController::ScrollBegin(BezelController::Bezel bezel, |
| 67 return state_ == ACTIVE; | 19 float delta) { |
| 68 } | 20 } |
| 69 | 21 |
| 70 void SplitViewController::UpdateLayout(bool animate) { | |
| 71 if (!left_window_) | |
| 72 return; | |
| 73 CHECK(right_window_); | |
| 74 gfx::Transform left_transform; | |
| 75 gfx::Transform right_transform; | |
| 76 int container_width = container_->GetBoundsInScreen().width(); | |
| 77 if (state_ == ACTIVE) { | |
| 78 // This method should only be called once in ACTIVE state when | |
| 79 // the left and rightwindows are still full screen and need to be resized. | |
| 80 CHECK_EQ(left_window_->bounds().width(), container_width); | |
| 81 CHECK_EQ(right_window_->bounds().width(), container_width); | |
| 82 // Windows should be resized via an animation when entering the ACTIVE | |
| 83 // state. | |
| 84 CHECK(animate); | |
| 85 // We scale the windows here, but when the animation finishes, we reset | |
| 86 // the scaling and update the window bounds to the proper size - see | |
| 87 // OnAnimationCompleted(). | |
| 88 left_transform.Scale(.5, 1); | |
| 89 right_transform.Scale(.5, 1); | |
| 90 right_transform.Translate(container_width, 0); | |
| 91 } else { | |
| 92 left_transform.Translate(separator_position_ - container_width, 0); | |
| 93 right_transform.Translate(separator_position_, 0); | |
| 94 } | |
| 95 left_window_->Show(); | |
| 96 right_window_->Show(); | |
| 97 SetWindowTransform(left_window_, left_transform, animate); | |
| 98 SetWindowTransform(right_window_, right_transform, animate); | |
| 99 } | |
| 100 | |
| 101 void SplitViewController::SetWindowTransform(aura::Window* window, | |
| 102 const gfx::Transform& transform, | |
| 103 bool animate) { | |
| 104 if (animate) { | |
| 105 scoped_refptr<ui::LayerAnimator> animator = window->layer()->GetAnimator(); | |
| 106 ui::ScopedLayerAnimationSettings settings(animator); | |
| 107 settings.SetPreemptionStrategy( | |
| 108 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); | |
| 109 settings.AddObserver(new CallbackAnimationObserver( | |
| 110 base::Bind(&SplitViewController::OnAnimationCompleted, | |
| 111 weak_factory_.GetWeakPtr(), | |
| 112 window))); | |
| 113 window->SetTransform(transform); | |
| 114 } else { | |
| 115 window->SetTransform(transform); | |
| 116 } | |
| 117 } | |
| 118 | |
| 119 void SplitViewController::OnAnimationCompleted(aura::Window* window) { | |
| 120 DCHECK(window == left_window_ || window == right_window_); | |
| 121 if (state_ == ACTIVE) { | |
| 122 gfx::Rect window_bounds = gfx::Rect(container_->bounds().size()); | |
| 123 int container_width = window_bounds.width(); | |
| 124 window_bounds.set_width(container_width / 2); | |
| 125 window->SetTransform(gfx::Transform()); | |
| 126 if (window == left_window_) { | |
| 127 left_window_->SetBounds(window_bounds); | |
| 128 } else { | |
| 129 window_bounds.set_x(container_width / 2); | |
| 130 right_window_->SetBounds(window_bounds); | |
| 131 } | |
| 132 } else { | |
| 133 int container_width = container_->bounds().width(); | |
| 134 window->SetTransform(gfx::Transform()); | |
| 135 if (window == left_window_) { | |
| 136 if (separator_position_ == 0) | |
| 137 left_window_->Hide(); | |
| 138 if (state_ == INACTIVE) | |
| 139 left_window_ = NULL; | |
| 140 } else { | |
| 141 if (separator_position_ == container_width) | |
| 142 right_window_->Hide(); | |
| 143 if (state_ == INACTIVE) | |
| 144 right_window_ = NULL; | |
| 145 } | |
| 146 } | |
| 147 } | |
| 148 | |
| 149 void SplitViewController::UpdateSeparatorPositionFromScrollDelta(float delta) { | |
| 150 gfx::Screen* screen = gfx::Screen::GetScreenFor(container_); | |
| 151 const gfx::Rect& display_bounds = | |
| 152 screen->GetDisplayNearestWindow(container_).bounds(); | |
| 153 gfx::Rect container_bounds = container_->GetBoundsInScreen(); | |
| 154 separator_position_ = | |
| 155 delta > 0 ? ((int)delta) + display_bounds.x() - container_bounds.x() | |
| 156 : display_bounds.right() - container_bounds.x() + delta; | |
| 157 } | |
| 158 | |
| 159 aura::Window* SplitViewController::GetCurrentActivityWindow() { | |
| 160 if (!current_activity_window_) { | |
| 161 aura::Window::Windows windows = window_list_provider_->GetWindowList(); | |
| 162 if (windows.empty()) | |
| 163 return NULL; | |
| 164 current_activity_window_ = windows.back(); | |
| 165 } | |
| 166 return current_activity_window_; | |
| 167 } | |
| 168 | |
| 169 /////////////////////////////////////////////////////////////////////////////// | |
| 170 // Begin BezelController::ScrollDelegate overrides. | |
| 171 void SplitViewController::ScrollBegin(BezelController::Bezel bezel, | |
| 172 float delta) { | |
| 173 if (!CanScroll()) | |
| 174 return; | |
| 175 state_ = SCROLLING; | |
| 176 aura::Window* current_window = GetCurrentActivityWindow(); | |
| 177 CHECK(current_window); | |
| 178 | |
| 179 aura::Window::Windows windows = window_list_provider_->GetWindowList(); | |
| 180 CHECK(windows.size() >= 2); | |
| 181 aura::Window::Windows::const_iterator it = | |
| 182 std::find(windows.begin(), windows.end(), current_window); | |
| 183 CHECK(it != windows.end()); | |
| 184 | |
| 185 if (delta > 0) { | |
| 186 right_window_ = current_window; | |
| 187 // reverse iterator points to the position before normal iterator |it| | |
| 188 aura::Window::Windows::const_reverse_iterator rev_it(it); | |
| 189 // circle to end if needed. | |
| 190 left_window_ = rev_it == windows.rend() ? windows.back() : *(rev_it); | |
| 191 } else { | |
| 192 left_window_ = current_window; | |
| 193 ++it; | |
| 194 // circle to front if needed. | |
| 195 right_window_ = it == windows.end() ? windows.front() : *it; | |
| 196 } | |
| 197 | |
| 198 CHECK(left_window_); | |
| 199 CHECK(right_window_); | |
| 200 | |
| 201 // TODO(oshima|mfomitchev): crbug.com/388362 | |
| 202 // Until we are properly hiding off-screen windows in window manager: | |
| 203 // Loop through all windows and hide them | |
| 204 for (it = windows.begin(); it != windows.end(); ++it) { | |
| 205 if (*it != left_window_ && *it != right_window_) | |
| 206 (*it)->Hide(); | |
| 207 } | |
| 208 | |
| 209 UpdateSeparatorPositionFromScrollDelta(delta); | |
| 210 UpdateLayout(false); | |
| 211 } | |
| 212 | |
| 213 // Max distance from the scroll end position to the middle of the screen where | |
| 214 // we would go into the split view mode. | |
| 215 const int kMaxDistanceFromMiddle = 120; | |
| 216 void SplitViewController::ScrollEnd() { | 22 void SplitViewController::ScrollEnd() { |
| 217 if (state_ != SCROLLING) | |
| 218 return; | |
| 219 | |
| 220 int container_width = container_->GetBoundsInScreen().width(); | |
| 221 if (std::abs(container_width / 2 - separator_position_) <= | |
| 222 kMaxDistanceFromMiddle) { | |
| 223 state_ = ACTIVE; | |
| 224 separator_position_ = container_width / 2; | |
| 225 } else if (separator_position_ < container_width / 2) { | |
| 226 separator_position_ = 0; | |
| 227 current_activity_window_ = right_window_; | |
| 228 state_ = INACTIVE; | |
| 229 } else { | |
| 230 separator_position_ = container_width; | |
| 231 current_activity_window_ = left_window_; | |
| 232 state_ = INACTIVE; | |
| 233 } | |
| 234 UpdateLayout(true); | |
| 235 } | 23 } |
| 236 | 24 |
| 237 void SplitViewController::ScrollUpdate(float delta) { | 25 void SplitViewController::ScrollUpdate(float delta) { |
| 238 if (state_ != SCROLLING) | |
| 239 return; | |
| 240 UpdateSeparatorPositionFromScrollDelta(delta); | |
| 241 UpdateLayout(false); | |
| 242 } | 26 } |
| 243 | 27 |
| 244 bool SplitViewController::CanScroll() { | 28 bool SplitViewController::CanScroll() { |
| 245 // TODO(mfomitchev): return false in vertical orientation, in full screen. | 29 return false; |
| 246 bool result = (!window_manager_->IsOverviewModeActive() && | |
| 247 !IsSplitViewModeActive() && | |
| 248 window_list_provider_->GetWindowList().size() >= 2); | |
| 249 return result; | |
| 250 } | |
| 251 | |
| 252 /////////////////////////////////////////////////////////////////////////////// | |
| 253 // WindowManagerObserver overrides | |
| 254 void SplitViewController::OnOverviewModeEnter() { | |
| 255 if (state_ == ACTIVE) { | |
| 256 CHECK(left_window_); | |
| 257 CHECK(right_window_); | |
| 258 window_list_provider_->MoveToFront(right_window_); | |
| 259 window_list_provider_->MoveToFront(left_window_); | |
| 260 // TODO(mfomitchev): This shouldn't be done here, but the overview mode's | |
| 261 // transition animation currently looks bad if the starting transform of | |
| 262 // any window is not gfx::Transform(). | |
| 263 right_window_->SetTransform(gfx::Transform()); | |
| 264 } else if (current_activity_window_) { | |
| 265 window_list_provider_->MoveToFront(current_activity_window_); | |
| 266 } | |
| 267 state_ = INACTIVE; | |
| 268 left_window_ = NULL; | |
| 269 right_window_ = NULL; | |
| 270 current_activity_window_ = NULL; | |
| 271 } | |
| 272 | |
| 273 void SplitViewController::OnOverviewModeExit() { | |
| 274 } | 30 } |
| 275 | 31 |
| 276 } // namespace athena | 32 } // namespace athena |
| OLD | NEW |