Index: ash/wm/window_selector.cc |
diff --git a/ash/wm/window_selector.cc b/ash/wm/window_selector.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..39e696455acebe17449849064e3d1fd0fa7cc36e |
--- /dev/null |
+++ b/ash/wm/window_selector.cc |
@@ -0,0 +1,190 @@ |
+// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "ash/wm/window_selector.h" |
+ |
+#include <algorithm> |
+ |
+#include "ash/screen_ash.h" |
+#include "ash/shell.h" |
+#include "ash/shell_window_ids.h" |
+#include "ash/wm/window_selector_delegate.h" |
+#include "ash/wm/window_util.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "ui/aura/client/aura_constants.h" |
+#include "ui/aura/root_window.h" |
+#include "ui/aura/window.h" |
+#include "ui/base/events/event.h" |
+#include "ui/compositor/scoped_layer_animation_settings.h" |
+#include "ui/gfx/interpolated_transform.h" |
+#include "ui/gfx/transform_util.h" |
+#include "ui/views/corewm/window_animations.h" |
+ |
+namespace ash { |
+ |
+namespace { |
+ |
+const float kCardAspectRatio = 4.0f / 3.0f; |
+const int kWindowMargin = 20; |
+const int kMinCardsMajor = 3; |
+const int kOverviewTransitionMilliseconds = 100; |
+ |
+// Applies a transform to |window| to fit within |target_bounds| while |
+// maintaining its aspect ratio. |
+void TransformWindowToFitBounds(aura::Window* window, |
+ const gfx::Rect& target_bounds) { |
+ const gfx::Rect bounds = window->bounds(); |
+ float scale = std::min(1.0f, |
+ std::min((float)target_bounds.width() / bounds.width(), |
+ (float)target_bounds.height() / bounds.height())); |
+ gfx::Transform transform; |
+ gfx::Vector2d offset( |
+ 0.5 * (target_bounds.width() - scale * bounds.width()), |
+ 0.5 * (target_bounds.height() - scale * bounds.height())); |
+ transform.Translate(target_bounds.x() - bounds.x() + offset.x(), |
+ target_bounds.y() - bounds.y() + offset.y()); |
+ transform.Scale(scale, scale); |
+ window->layer()->SetTransform(transform); |
+} |
+ |
+} // namespace |
+ |
+WindowSelector::WindowSelector(const WindowList& windows, |
+ WindowSelectorDelegate* delegate) |
+ : windows_(windows), |
+ delegate_(delegate) { |
+ DCHECK(delegate_); |
+ for (size_t i = 0; i < windows_.size(); ++i) { |
+ windows_[i]->AddObserver(this); |
+ if (windows_[i]->GetProperty(aura::client::kShowStateKey) == |
+ ui::SHOW_STATE_MINIMIZED) { |
+ windows_[i]->layer()->SetVisible(true); |
+ windows_[i]->layer()->SetOpacity(1); |
+ } |
+ } |
+ PositionWindows(); |
+ ash::Shell::GetInstance()->AddPreTargetHandler(this); |
+} |
+ |
+WindowSelector::~WindowSelector() { |
+ for (size_t i = 0; i < windows_.size(); i++) { |
+ ui::ScopedLayerAnimationSettings animation_settings( |
+ windows_[i]->layer()->GetAnimator()); |
+ animation_settings.SetPreemptionStrategy( |
+ ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); |
+ animation_settings.SetTransitionDuration( |
+ base::TimeDelta::FromMilliseconds(kOverviewTransitionMilliseconds)); |
+ windows_[i]->RemoveObserver(this); |
+ if (windows_[i]->GetProperty(aura::client::kShowStateKey) == |
+ ui::SHOW_STATE_MINIMIZED) { |
+ windows_[i]->layer()->SetOpacity(0); |
+ windows_[i]->layer()->SetVisible(false); |
+ } |
+ gfx::Transform transform; |
+ windows_[i]->layer()->SetTransform(transform); |
+ } |
+ ash::Shell::GetInstance()->RemovePreTargetHandler(this); |
+} |
+ |
+void WindowSelector::OnEvent(ui::Event* event) { |
+ // TODO(flackr): This will prevent anything else from working while overview |
+ // mode is active. This should only stop events from being sent to the windows |
+ // in the overview but still allow interaction with the launcher / tray and |
+ // hotkeys http://crbug.com/264289. |
+ EventHandler::OnEvent(event); |
+ event->StopPropagation(); |
+} |
+ |
+void WindowSelector::OnMouseEvent(ui::MouseEvent* event) { |
+ if (event->type() != ui::ET_MOUSE_RELEASED) |
+ return; |
+ HandleSelectionEvent(event); |
+} |
+ |
+void WindowSelector::OnGestureEvent(ui::GestureEvent* event) { |
+ if (event->type() != ui::ET_GESTURE_TAP) |
+ return; |
+ HandleSelectionEvent(event); |
+} |
+ |
+void WindowSelector::OnWindowDestroyed(aura::Window* window) { |
+ WindowList::iterator iter = |
+ std::find(windows_.begin(), windows_.end(), window); |
+ if (iter != windows_.end()) { |
+ windows_.erase(iter); |
+ PositionWindows(); |
+ } |
+} |
+ |
+void WindowSelector::HandleSelectionEvent(ui::Event* event) { |
+ aura::Window* target = static_cast<aura::Window*>(event->target()); |
+ |
+ for (size_t i = 0; i < windows_.size(); i++) { |
+ if (windows_[i]->Contains(target)) { |
+ // The delegate may delete the WindowSelector, assume the object may no |
+ // longer valid after calling this. |
+ delegate_->SelectWindow(windows_[i]); |
+ return; |
+ } |
+ } |
+} |
+ |
+void WindowSelector::PositionWindows() { |
+ Shell::RootWindowList root_window_list = Shell::GetAllRootWindows(); |
+ for (size_t i = 0; i < root_window_list.size(); ++i) { |
+ WindowList windows_in_root; |
+ for (size_t j = 0; j < windows_.size(); ++j) { |
+ if (windows_[j]->GetRootWindow() == root_window_list[i]) { |
+ windows_in_root.push_back(windows_[j]); |
+ } |
+ } |
+ PositionWindowsOnRoot(root_window_list[i], windows_in_root); |
+ } |
+} |
+ |
+void WindowSelector::PositionWindowsOnRoot(aura::RootWindow* root_window, |
+ const WindowList& windows) { |
+ gfx::Size window_size; |
+ gfx::Rect total_bounds = ScreenAsh::GetDisplayWorkAreaBoundsInParent( |
+ Shell::GetContainer(root_window, |
+ internal::kShellWindowId_DefaultContainer)); |
+ |
+ // Find the minimum number of windows per row that will fit all of the |
+ // windows on screen. |
+ size_t windows_per_row = 1; |
+ if (total_bounds.width() > total_bounds.height()) |
+ windows_per_row = kMinCardsMajor; |
+ for (;; ++windows_per_row) { |
+ window_size.set_width(total_bounds.width() / windows_per_row); |
+ window_size.set_height(window_size.width() / kCardAspectRatio); |
+ if (windows_per_row * (total_bounds.height() / |
+ window_size.height()) >= windows.size()) |
+ break; |
+ } |
+ // Calculate the Y offset necessary to vertically center the stack. |
+ int x_offset = total_bounds.x() + (windows.size() >= windows_per_row ? 0 : |
+ (windows_per_row - windows.size()) * window_size.width() / 2); |
+ int y_offset = total_bounds.y() + (total_bounds.height() - |
+ ((windows.size() + windows_per_row - 1) / windows_per_row) * |
+ window_size.height()) / 2; |
+ for (size_t i = 0; i < windows.size(); ++i) { |
+ ui::ScopedLayerAnimationSettings animation_settings( |
+ windows[i]->layer()->GetAnimator()); |
+ animation_settings.SetPreemptionStrategy( |
+ ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); |
+ animation_settings.SetTransitionDuration( |
+ base::TimeDelta::FromMilliseconds(kOverviewTransitionMilliseconds)); |
+ gfx::Transform transform; |
+ int column = i % windows_per_row; |
+ int row = i / windows_per_row; |
+ gfx::Rect target_bounds(window_size.width() * column + x_offset, |
+ window_size.height() * row + y_offset, |
+ window_size.width(), |
+ window_size.height()); |
+ target_bounds.Inset(kWindowMargin, kWindowMargin); |
+ TransformWindowToFitBounds(windows[i], target_bounds); |
+ } |
+} |
+ |
+} // namespace ash |