Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "ash/wm/overview/window_grid.h" | |
| 6 | |
| 7 #include "ash/screen_util.h" | |
| 8 #include "ash/shell.h" | |
| 9 #include "ash/shell_window_ids.h" | |
| 10 #include "ash/wm/overview/scoped_transform_overview_window.h" | |
| 11 #include "ash/wm/overview/window_selector.h" | |
| 12 #include "ash/wm/overview/window_selector_item.h" | |
| 13 #include "base/memory/scoped_vector.h" | |
| 14 #include "third_party/skia/include/core/SkColor.h" | |
| 15 #include "ui/aura/window.h" | |
| 16 #include "ui/compositor/layer_animation_observer.h" | |
| 17 #include "ui/compositor/scoped_layer_animation_settings.h" | |
| 18 #include "ui/gfx/vector2d.h" | |
| 19 #include "ui/views/background.h" | |
| 20 #include "ui/views/view.h" | |
| 21 #include "ui/views/widget/widget.h" | |
| 22 | |
| 23 namespace ash { | |
| 24 namespace { | |
| 25 | |
| 26 // An observer which holds onto the passed widget until the animation is | |
| 27 // complete. | |
| 28 class CleanupWidgetAfterAnimationObserver : public ui::LayerAnimationObserver { | |
| 29 public: | |
| 30 explicit CleanupWidgetAfterAnimationObserver( | |
| 31 scoped_ptr<views::Widget> widget); | |
| 32 | |
| 33 // ui::LayerAnimationObserver: | |
| 34 virtual void OnLayerAnimationEnded( | |
| 35 ui::LayerAnimationSequence* sequence) OVERRIDE; | |
| 36 virtual void OnLayerAnimationAborted( | |
| 37 ui::LayerAnimationSequence* sequence) OVERRIDE; | |
| 38 virtual void OnLayerAnimationScheduled( | |
| 39 ui::LayerAnimationSequence* sequence) OVERRIDE; | |
| 40 | |
| 41 private: | |
| 42 virtual ~CleanupWidgetAfterAnimationObserver(); | |
| 43 | |
| 44 scoped_ptr<views::Widget> widget_; | |
| 45 | |
| 46 DISALLOW_COPY_AND_ASSIGN(CleanupWidgetAfterAnimationObserver); | |
| 47 }; | |
| 48 | |
| 49 CleanupWidgetAfterAnimationObserver::CleanupWidgetAfterAnimationObserver( | |
| 50 scoped_ptr<views::Widget> widget) | |
| 51 : widget_(widget.Pass()) { | |
| 52 widget_->GetNativeWindow()->layer()->GetAnimator()->AddObserver(this); | |
| 53 } | |
| 54 | |
| 55 CleanupWidgetAfterAnimationObserver::~CleanupWidgetAfterAnimationObserver() { | |
| 56 widget_->GetNativeWindow()->layer()->GetAnimator()->RemoveObserver(this); | |
| 57 } | |
| 58 | |
| 59 void CleanupWidgetAfterAnimationObserver::OnLayerAnimationEnded( | |
| 60 ui::LayerAnimationSequence* sequence) { | |
| 61 delete this; | |
| 62 } | |
| 63 | |
| 64 void CleanupWidgetAfterAnimationObserver::OnLayerAnimationAborted( | |
| 65 ui::LayerAnimationSequence* sequence) { | |
| 66 delete this; | |
| 67 } | |
| 68 | |
| 69 void CleanupWidgetAfterAnimationObserver::OnLayerAnimationScheduled( | |
| 70 ui::LayerAnimationSequence* sequence) { | |
| 71 } | |
| 72 | |
| 73 // A comparator for locating a given target window. | |
| 74 struct WindowSelectorItemComparator | |
| 75 : public std::unary_function<WindowSelectorItem*, bool> { | |
| 76 explicit WindowSelectorItemComparator(const aura::Window* target_window) | |
| 77 : target(target_window) { | |
| 78 } | |
| 79 | |
| 80 bool operator()(WindowSelectorItem* window) const { | |
| 81 return window->HasSelectableWindow(target); | |
| 82 } | |
| 83 | |
| 84 const aura::Window* target; | |
| 85 }; | |
| 86 | |
| 87 // Conceptually the window overview is a table or grid of cells having this | |
| 88 // fixed aspect ratio. The number of columns is determined by maximizing the | |
| 89 // area of them based on the number of window_list. | |
| 90 const float kCardAspectRatio = 4.0f / 3.0f; | |
| 91 | |
| 92 // The minimum number of cards along the major axis (i.e. horizontally on a | |
| 93 // landscape orientation). | |
| 94 const int kMinCardsMajor = 3; | |
| 95 | |
| 96 // In the conceptual overview table, the window margin is the space reserved | |
| 97 // around the window within the cell. This margin does not overlap so the | |
| 98 // closest distance between adjacent window_list will be twice this amount. | |
| 99 const int kWindowMargin = 30; | |
| 100 | |
| 101 const int kOverviewSelectorTransitionMilliseconds = 100; | |
| 102 | |
| 103 // The color and opacity of the overview selector. | |
| 104 const SkColor kWindowOverviewSelectionColor = SK_ColorBLACK; | |
| 105 const unsigned char kWindowOverviewSelectorOpacity = 128; | |
| 106 | |
| 107 // Returns the vector for the fade in animation. | |
| 108 gfx::Vector2d GetVectorFor(WindowSelector::Direction direction, | |
|
flackr
2014/05/29 20:04:25
nit: The name could better explain what the method
Nina
2014/06/02 22:04:38
Done.
| |
| 109 const gfx::Rect& bounds) { | |
| 110 gfx::Vector2d vector; | |
| 111 switch (direction) { | |
| 112 case WindowSelector::DOWN: | |
| 113 vector.set_y(bounds.width()); | |
| 114 break; | |
| 115 case WindowSelector::RIGHT: | |
| 116 vector.set_x(bounds.height()); | |
| 117 break; | |
| 118 case WindowSelector::UP: | |
| 119 vector.set_y(-bounds.width()); | |
| 120 break; | |
| 121 case WindowSelector::LEFT: | |
| 122 vector.set_x(-bounds.height()); | |
| 123 break; | |
| 124 } | |
| 125 return vector; | |
| 126 } | |
| 127 | |
| 128 } // namespace | |
| 129 | |
| 130 WindowGrid::WindowGrid(aura::Window* root_window, | |
| 131 std::vector<WindowSelectorItem*> window_list) | |
| 132 : root_window_(root_window), | |
|
flackr
2014/05/29 20:04:25
nit: I believe the : should be indented 4 from the
Nina
2014/06/02 22:04:38
Done.
| |
| 133 window_list_(window_list) { | |
|
flackr
2014/05/29 20:04:25
I believe this will create a copy when called and
Nina
2014/06/02 22:04:38
Done.
| |
| 134 PositionWindows(true); | |
| 135 } | |
| 136 | |
| 137 WindowGrid::~WindowGrid() { | |
| 138 } | |
| 139 | |
| 140 WindowSelectorItem* WindowGrid::RemoveWindow(aura::Window* window) { | |
| 141 ScopedVector<WindowSelectorItem>::iterator iter = | |
| 142 std::find_if(window_list_.begin(), window_list_.end(), | |
| 143 WindowSelectorItemComparator(window)); | |
| 144 if (iter == window_list_.end()) | |
| 145 return NULL; | |
| 146 | |
| 147 WindowSelectorItem* removed_item = *iter; | |
| 148 removed_item->RemoveWindow(window); | |
| 149 | |
| 150 // If there are still windows in this selector entry then the overview is | |
| 151 // still active and the active selection remains the same. | |
| 152 if (!removed_item->empty()) | |
| 153 return NULL; | |
|
flackr
2014/05/29 20:04:25
This seems like odd behavior to sometimes not retu
Nina
2014/06/02 22:04:38
Done.
| |
| 154 | |
| 155 // We need to get this value before we erase the window to prevent unspecified | |
| 156 // behavior. | |
| 157 size_t removed_index = iter - window_list_.begin(); | |
| 158 window_list_.erase(iter); | |
| 159 if (!empty()) { | |
| 160 PositionWindows(true); | |
| 161 | |
| 162 // If existing, reposition the selection widget. | |
| 163 if (selection_widget_) { | |
| 164 if (selected_index_ >= removed_index) { | |
| 165 if (selected_index_ != 0) { | |
|
flackr
2014/05/29 20:04:25
don't nest if's for a single condition, i.e.
if (s
Nina
2014/06/02 22:04:38
Done.
| |
| 166 selected_index_--; | |
| 167 } | |
| 168 } | |
| 169 ui::ScopedLayerAnimationSettings animation_settings( | |
| 170 selection_widget_->GetNativeWindow()->layer()->GetAnimator()); | |
| 171 animation_settings.SetTransitionDuration( | |
| 172 base::TimeDelta::FromMilliseconds( | |
| 173 ScopedTransformOverviewWindow::kTransitionMilliseconds)); | |
| 174 animation_settings.SetPreemptionStrategy( | |
| 175 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); | |
| 176 selection_widget_->SetBounds(GetSelectionBounds()); | |
| 177 } | |
| 178 } | |
| 179 | |
| 180 return removed_item; | |
| 181 } | |
| 182 | |
| 183 void WindowGrid::PositionWindows(bool animate) { | |
| 184 CHECK(!window_list_.empty()); | |
| 185 | |
| 186 gfx::Size window_size; | |
| 187 gfx::Rect total_bounds = ScreenUtil::ConvertRectToScreen( | |
| 188 root_window_, | |
| 189 ScreenUtil::GetDisplayWorkAreaBoundsInParent( | |
| 190 Shell::GetContainer(root_window_, kShellWindowId_DefaultContainer))); | |
| 191 | |
| 192 // Find the minimum number of window_list per row that will fit all of the | |
| 193 // window_list on screen. | |
| 194 num_columns_ = std::max( | |
| 195 total_bounds.width() > total_bounds.height() ? kMinCardsMajor : 1, | |
| 196 static_cast<int>(ceil(sqrt(total_bounds.width() * window_list_.size() / | |
| 197 (kCardAspectRatio * total_bounds.height()))))); | |
| 198 num_rows_ = ((window_list_.size() + num_columns_ - 1) / num_columns_); | |
| 199 window_size.set_width(std::min( | |
| 200 static_cast<int>(total_bounds.width() / num_columns_), | |
| 201 static_cast<int>(total_bounds.height() * kCardAspectRatio / num_rows_))); | |
| 202 window_size.set_height(window_size.width() / kCardAspectRatio); | |
| 203 | |
| 204 // Calculate the X and Y offsets necessary to center the grid. | |
| 205 int x_offset = total_bounds.x() + ((window_list_.size() >= num_columns_ ? 0 : | |
| 206 (num_columns_ - window_list_.size()) * window_size.width()) + | |
| 207 (total_bounds.width() - num_columns_ * window_size.width())) / 2; | |
| 208 int y_offset = total_bounds.y() + (total_bounds.height() - | |
| 209 num_rows_ * window_size.height()) / 2; | |
| 210 for (size_t i = 0; i < window_list_.size(); ++i) { | |
| 211 gfx::Transform transform; | |
| 212 int column = i % num_columns_; | |
| 213 int row = i / num_columns_; | |
| 214 gfx::Rect target_bounds(window_size.width() * column + x_offset, | |
| 215 window_size.height() * row + y_offset, | |
| 216 window_size.width(), | |
| 217 window_size.height()); | |
| 218 target_bounds.Inset(kWindowMargin, kWindowMargin); | |
| 219 window_list_[i]->SetBounds(root_window_, target_bounds, animate); | |
| 220 } | |
| 221 | |
| 222 // If we have less than |kMinCardsMajor| windows, adjust the column_ value to | |
| 223 // reflect how many "real" columns we have. | |
| 224 if (num_columns_ > window_list_.size()) | |
| 225 num_columns_ = window_list_.size(); | |
| 226 } | |
| 227 | |
| 228 void WindowGrid::CreateSelectionWidget(WindowSelector::Direction direction) { | |
| 229 // Position the index in the "first" window relative to the chosen direction. | |
| 230 switch (direction) { | |
| 231 case WindowSelector::LEFT: | |
| 232 selected_index_ = window_list_.size() - 1; | |
| 233 break; | |
| 234 case WindowSelector::UP: | |
| 235 selected_index_ = num_rows_ * num_columns_ - 1; | |
| 236 if (selected_index_ >= window_list_.size()) | |
| 237 selected_index_ -= num_columns_; | |
| 238 break; | |
| 239 case WindowSelector::RIGHT: | |
| 240 case WindowSelector::DOWN: | |
| 241 selected_index_ = 0; | |
| 242 break; | |
| 243 } | |
| 244 InitSelectionWidget(direction); | |
| 245 } | |
| 246 | |
| 247 bool WindowGrid::Move(WindowSelector::Direction direction) { | |
| 248 DCHECK(selection_widget_); | |
| 249 bool out_of_bounds = MoveSelectedIndex(direction); | |
| 250 | |
| 251 gfx::Vector2d fade_out_direction = | |
| 252 GetVectorFor(direction, selection_widget_->GetNativeWindow()->bounds()); | |
| 253 // If the selection widget is already active, fade it out in the selection | |
| 254 // direction. | |
| 255 views::Widget* old_selection = selection_widget_.get(); | |
| 256 | |
| 257 // CleanupWidgetAfterAnimationObserver will delete itself (and the | |
| 258 // widget) when the animation is complete. | |
| 259 { | |
| 260 new CleanupWidgetAfterAnimationObserver(selection_widget_.Pass()); | |
| 261 ui::ScopedLayerAnimationSettings animation_settings( | |
| 262 old_selection->GetNativeWindow()->layer()->GetAnimator()); | |
| 263 animation_settings.SetTransitionDuration( | |
| 264 base::TimeDelta::FromMilliseconds( | |
| 265 kOverviewSelectorTransitionMilliseconds)); | |
| 266 animation_settings.SetPreemptionStrategy( | |
| 267 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); | |
| 268 old_selection->SetOpacity(0); | |
| 269 old_selection->GetNativeWindow()->SetBounds( | |
| 270 old_selection->GetNativeWindow()->bounds() + fade_out_direction); | |
| 271 old_selection->Hide(); | |
| 272 } | |
| 273 if (!out_of_bounds) { | |
| 274 // The newly selected window is in this grid, animate a new selection | |
| 275 // widget. | |
| 276 InitSelectionWidget(direction); | |
|
flackr
2014/05/29 20:04:25
It looks like you're always destroying the old sel
Nina
2014/06/02 22:04:38
Well actually when you are leaving the boundaries
flackr
2014/06/03 17:12:07
I think this is necessary. The animations of the t
Nina
2014/06/04 20:21:13
OK, done!
| |
| 277 } else { | |
| 278 // The selection is out of the bounds of this grid, kill the selection | |
| 279 // widget. | |
| 280 selection_widget_.reset(); | |
|
flackr
2014/05/29 20:04:25
You've already passed off the selection_widget_ po
Nina
2014/06/02 22:04:38
Yep. Removed.
| |
| 281 } | |
| 282 return out_of_bounds; | |
| 283 } | |
| 284 | |
| 285 WindowSelectorItem* WindowGrid::SelectedWindow() const { | |
| 286 CHECK(selected_index_ < window_list_.size()); | |
| 287 return window_list_[selected_index_]; | |
| 288 } | |
| 289 | |
| 290 void WindowGrid::InitSelectionWidget(WindowSelector::Direction direction) { | |
| 291 selection_widget_.reset(new views::Widget); | |
| 292 views::Widget::InitParams params; | |
| 293 params.type = views::Widget::InitParams::TYPE_POPUP; | |
| 294 params.keep_on_top = false; | |
| 295 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | |
| 296 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; | |
| 297 params.parent = Shell::GetContainer(root_window_, | |
| 298 kShellWindowId_DefaultContainer); | |
| 299 params.accept_events = false; | |
| 300 selection_widget_->set_focus_on_creation(false); | |
| 301 selection_widget_->Init(params); | |
| 302 views::View* content_view = new views::View; | |
| 303 content_view->set_background( | |
| 304 views::Background::CreateSolidBackground(kWindowOverviewSelectionColor)); | |
| 305 selection_widget_->SetContentsView(content_view); | |
| 306 selection_widget_->GetNativeWindow()->parent()->StackChildAtBottom( | |
| 307 selection_widget_->GetNativeWindow()); | |
| 308 | |
| 309 const gfx::Rect target_bounds = GetSelectionBounds(); | |
| 310 gfx::Vector2d fade_out_direction = | |
| 311 GetVectorFor(direction, target_bounds); | |
| 312 gfx::Display dst_display = gfx::Screen::GetScreenFor(root_window_)-> | |
| 313 GetDisplayMatching(target_bounds); | |
| 314 | |
| 315 selection_widget_->Show(); | |
| 316 // New selection widget starts with 0 opacity and fades in. | |
| 317 selection_widget_->GetNativeWindow()->layer()->SetOpacity(0); | |
| 318 selection_widget_->GetNativeWindow()->SetBoundsInScreen( | |
| 319 target_bounds - fade_out_direction, dst_display); | |
| 320 ui::ScopedLayerAnimationSettings animation_settings( | |
| 321 selection_widget_->GetNativeWindow()->layer()->GetAnimator()); | |
| 322 animation_settings.SetTransitionDuration(base::TimeDelta::FromMilliseconds( | |
| 323 kOverviewSelectorTransitionMilliseconds)); | |
| 324 animation_settings.SetPreemptionStrategy( | |
| 325 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); | |
| 326 selection_widget_->SetBounds(target_bounds); | |
| 327 selection_widget_->SetOpacity(kWindowOverviewSelectorOpacity); | |
| 328 | |
| 329 // Send an a11y alert so that if ChromeVox is enabled, the item label is read. | |
| 330 SelectedWindow()->window_label()->GetContentsView()-> | |
| 331 NotifyAccessibilityEvent(ui::AX_EVENT_FOCUS, true); | |
|
flackr
2014/05/29 20:04:25
Perhaps we could even focus the current window sel
Nina
2014/06/02 22:04:38
I changed it so that it focuses the transparent bu
flackr
2014/06/03 17:12:07
Looks better! It's not actually focused but preten
Nina
2014/06/04 20:21:13
Yeah, we are artificially sending a "Focus" event
| |
| 332 } | |
| 333 | |
| 334 bool WindowGrid::MoveSelectedIndex(WindowSelector::Direction direction) { | |
| 335 bool out_of_bounds = false; | |
| 336 switch (direction) { | |
| 337 case WindowSelector::RIGHT: | |
| 338 if (selected_index_ >= window_list_.size() - 1) | |
| 339 out_of_bounds = true; | |
|
flackr
2014/05/29 20:04:25
Use early returns. It also feels strange that this
Nina
2014/06/02 22:04:38
Done.
| |
| 340 else | |
| 341 selected_index_++; | |
| 342 break; | |
| 343 case WindowSelector::LEFT: | |
| 344 if (selected_index_ == 0) | |
| 345 out_of_bounds = true; | |
| 346 else | |
| 347 selected_index_--; | |
| 348 break; | |
| 349 case WindowSelector::DOWN: | |
| 350 if (selected_index_ + num_columns_ >= window_list_.size()) | |
| 351 selected_index_ = (selected_index_ + 1) % num_columns_; | |
| 352 else | |
| 353 selected_index_ += num_columns_; | |
| 354 if (selected_index_ == 0 || selected_index_ >= window_list_.size()) | |
| 355 out_of_bounds = true; | |
| 356 break; | |
| 357 case WindowSelector::UP: | |
| 358 if (selected_index_ == 0) { | |
| 359 out_of_bounds = true; | |
| 360 } else { | |
| 361 if (selected_index_ < num_columns_) { | |
| 362 selected_index_--; | |
|
flackr
2014/05/29 20:04:25
This seems odd, up is the same as left on the top
Nina
2014/06/02 22:04:38
Please see the new docs on the header file that ex
| |
| 363 if (!(selected_index_ + num_columns_ >= window_list_.size())) { | |
| 364 selected_index_ += num_columns_ * (num_rows_ - 1); | |
| 365 if (selected_index_ >= window_list_.size()) | |
| 366 selected_index_ -= num_columns_; | |
| 367 } | |
| 368 } else { | |
| 369 selected_index_ -= num_columns_; | |
| 370 } | |
| 371 } | |
| 372 break; | |
| 373 } | |
| 374 return out_of_bounds; | |
| 375 } | |
| 376 | |
| 377 const gfx::Rect WindowGrid::GetSelectionBounds() const { | |
| 378 gfx::Rect bounds = SelectedWindow()->target_bounds(); | |
| 379 bounds.Inset(-kWindowMargin, -kWindowMargin); | |
| 380 return bounds; | |
| 381 } | |
| 382 | |
| 383 } // namespace ash | |
| OLD | NEW |