Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1324)

Unified Diff: ash/wm/overview/window_grid.cc

Issue 251103005: Added arrow key navigation to Overview Mode (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed a bug with panels and changing windows bounds Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ash/wm/overview/window_grid.cc
diff --git a/ash/wm/overview/window_grid.cc b/ash/wm/overview/window_grid.cc
new file mode 100644
index 0000000000000000000000000000000000000000..5211a651f78e35638c059775c9cabd43274de97a
--- /dev/null
+++ b/ash/wm/overview/window_grid.cc
@@ -0,0 +1,456 @@
+// Copyright 2014 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/overview/window_grid.h"
+
+#include "ash/screen_util.h"
+#include "ash/shell.h"
+#include "ash/shell_window_ids.h"
+#include "ash/wm/overview/scoped_transform_overview_window.h"
+#include "ash/wm/overview/window_selector.h"
+#include "ash/wm/overview/window_selector_item.h"
+#include "ash/wm/overview/window_selector_panels.h"
+#include "ash/wm/overview/window_selector_window.h"
+#include "ash/wm/window_state.h"
+#include "base/memory/scoped_vector.h"
+#include "third_party/skia/include/core/SkColor.h"
+#include "ui/aura/window.h"
+#include "ui/compositor/layer_animation_observer.h"
+#include "ui/compositor/scoped_layer_animation_settings.h"
+#include "ui/gfx/vector2d.h"
+#include "ui/views/background.h"
+#include "ui/views/view.h"
+#include "ui/views/widget/widget.h"
+#include "ui/wm/core/window_animations.h"
+
+namespace ash {
+namespace {
+
+// An observer which holds onto the passed widget until the animation is
+// complete.
+class CleanupWidgetAfterAnimationObserver
+ : public ui::ImplicitAnimationObserver {
+ public:
+ explicit CleanupWidgetAfterAnimationObserver(
+ scoped_ptr<views::Widget> widget);
+ virtual ~CleanupWidgetAfterAnimationObserver();
+
+ // ui::ImplicitAnimationObserver:
+ virtual void OnImplicitAnimationsCompleted() OVERRIDE;
+
+ // ui::LayerAnimationObserver:
+ virtual void OnLayerAnimationEnded(
+ ui::LayerAnimationSequence* sequence) OVERRIDE;
+ virtual void OnLayerAnimationAborted(
+ ui::LayerAnimationSequence* sequence) OVERRIDE;
+ virtual void OnLayerAnimationScheduled(
+ ui::LayerAnimationSequence* sequence) OVERRIDE;
flackr 2014/06/04 22:25:06 The ui::LayerAnimationObserver methods should not
Nina 2014/06/05 18:04:35 Done.
+
+ private:
+ scoped_ptr<views::Widget> widget_;
+
+ DISALLOW_COPY_AND_ASSIGN(CleanupWidgetAfterAnimationObserver);
+};
+
+CleanupWidgetAfterAnimationObserver::CleanupWidgetAfterAnimationObserver(
+ scoped_ptr<views::Widget> widget)
+ : widget_(widget.Pass()) {
+}
+
+CleanupWidgetAfterAnimationObserver::~CleanupWidgetAfterAnimationObserver() {
+}
+
+void CleanupWidgetAfterAnimationObserver::OnImplicitAnimationsCompleted() {
+ delete this;
+}
+
+void CleanupWidgetAfterAnimationObserver::OnLayerAnimationEnded(
+ ui::LayerAnimationSequence* sequence) {
+}
+void CleanupWidgetAfterAnimationObserver::OnLayerAnimationAborted(
+ ui::LayerAnimationSequence* sequence) {
+}
+void CleanupWidgetAfterAnimationObserver::OnLayerAnimationScheduled(
+ ui::LayerAnimationSequence* sequence) {
+}
+
+// A comparator for locating a given target window.
+struct WindowSelectorItemComparator
+ : public std::unary_function<WindowSelectorItem*, bool> {
+ explicit WindowSelectorItemComparator(const aura::Window* target_window)
+ : target(target_window) {
+ }
+
+ bool operator()(WindowSelectorItem* window) const {
+ return window->HasSelectableWindow(target);
+ }
+
+ const aura::Window* target;
+};
+
+// A comparator for locating a selectable window given a targeted window.
flackr 2014/06/04 22:25:06 nit: For locating a WindowSelectorItem
Nina 2014/06/05 18:04:35 Done.
+struct WindowSelectorItemTargetComparator
+ : public std::unary_function<WindowSelectorItem*, bool> {
+ explicit WindowSelectorItemTargetComparator(const aura::Window* target_window)
+ : target(target_window) {
+ }
+
+ bool operator()(WindowSelectorItem* window) const {
+ return window->Contains(target);
+ }
+
+ const aura::Window* target;
+};
+
+// Conceptually the window overview is a table or grid of cells having this
+// fixed aspect ratio. The number of columns is determined by maximizing the
+// area of them based on the number of window_list.
+const float kCardAspectRatio = 4.0f / 3.0f;
+
+// The minimum number of cards along the major axis (i.e. horizontally on a
+// landscape orientation).
+const int kMinCardsMajor = 3;
+
+const int kOverviewSelectorTransitionMilliseconds = 100;
+
+// The color and opacity of the overview selector.
+const SkColor kWindowOverviewSelectionColor = SK_ColorBLACK;
+const unsigned char kWindowOverviewSelectorOpacity = 128;
+
+// Returns the vector for the fade in animation.
+gfx::Vector2d GetSlideVectorForFadeIn(WindowSelector::Direction direction,
+ const gfx::Rect& bounds) {
+ gfx::Vector2d vector;
+ switch (direction) {
+ case WindowSelector::DOWN:
+ vector.set_y(bounds.width());
+ break;
+ case WindowSelector::RIGHT:
+ vector.set_x(bounds.height());
+ break;
+ case WindowSelector::UP:
+ vector.set_y(-bounds.width());
+ break;
+ case WindowSelector::LEFT:
+ vector.set_x(-bounds.height());
+ break;
+ }
+ return vector;
+}
+
+} // namespace
+
+WindowGrid::WindowGrid(aura::Window* root_window,
+ const std::vector<aura::Window*>& windows,
+ WindowSelector* window_selector)
+ : root_window_(root_window),
+ window_selector_(window_selector) {
+ WindowSelectorPanels* panels_item = NULL;
+ for (aura::Window::Windows::const_iterator iter = windows.begin();
+ iter != windows.end(); ++iter) {
+ if ((*iter)->GetRootWindow() != root_window)
+ continue;
+ (*iter)->AddObserver(this);
+ observed_windows_.push_back(*iter);
+ WindowSelectorItem* item = NULL;
+
+ if ((*iter)->type() == ui::wm::WINDOW_TYPE_PANEL &&
+ wm::GetWindowState(*iter)->panel_attached()) {
+ // Attached panel windows are grouped into a single overview item per
+ // grid.
+ if (!panels_item) {
+ panels_item = new WindowSelectorPanels();
+ window_list_.push_back(panels_item);
+ }
+ panels_item->AddWindow(*iter);
+ item = panels_item;
+ } else {
+ item = new WindowSelectorWindow(*iter);
+ window_list_.push_back(item);
+ }
+ // Verify that the window has been added to an item in overview.
+ CHECK(item->Contains(*iter));
flackr 2014/06/04 22:25:06 I think this check is no longer necessary.
Nina 2014/06/05 18:04:35 Done.
+ }
+ if (window_list_.empty())
+ return;
+
+ for (ScopedVector<WindowSelectorItem>::iterator iter = window_list_.begin();
+ iter != window_list_.end(); iter++) {
+ (*iter)->PrepareForOverview();
flackr 2014/06/04 22:25:06 I forget, but is there any reason we can't do this
Nina 2014/06/05 18:04:35 Well I did it for the panels, but now that we only
+ }
+
+ PositionWindows(true);
+}
+
+WindowGrid::~WindowGrid() {
+ for (std::vector<aura::Window*>::iterator iter = observed_windows_.begin();
+ iter != observed_windows_.end(); iter++) {
+ (*iter)->RemoveObserver(this);
+ }
+}
+
+void WindowGrid::PositionWindows(bool animate) {
+ CHECK(!window_list_.empty());
+
+ gfx::Size window_size;
+ gfx::Rect total_bounds = ScreenUtil::ConvertRectToScreen(
+ root_window_,
+ ScreenUtil::GetDisplayWorkAreaBoundsInParent(
+ Shell::GetContainer(root_window_, kShellWindowId_DefaultContainer)));
+
+ // Find the minimum number of window_list per row that will fit all of the
+ // window_list on screen.
+ num_columns_ = std::max(
+ total_bounds.width() > total_bounds.height() ? kMinCardsMajor : 1,
+ static_cast<int>(ceil(sqrt(total_bounds.width() * window_list_.size() /
+ (kCardAspectRatio * total_bounds.height())))));
+ int num_rows = ((window_list_.size() + num_columns_ - 1) / num_columns_);
+ window_size.set_width(std::min(
+ static_cast<int>(total_bounds.width() / num_columns_),
+ static_cast<int>(total_bounds.height() * kCardAspectRatio / num_rows)));
+ window_size.set_height(window_size.width() / kCardAspectRatio);
+
+ // Calculate the X and Y offsets necessary to center the grid.
+ int x_offset = total_bounds.x() + ((window_list_.size() >= num_columns_ ? 0 :
+ (num_columns_ - window_list_.size()) * window_size.width()) +
+ (total_bounds.width() - num_columns_ * window_size.width())) / 2;
+ int y_offset = total_bounds.y() + (total_bounds.height() -
+ num_rows * window_size.height()) / 2;
+ for (size_t i = 0; i < window_list_.size(); ++i) {
+ gfx::Transform transform;
+ int column = i % num_columns_;
+ int row = i / num_columns_;
+ gfx::Rect target_bounds(window_size.width() * column + x_offset,
+ window_size.height() * row + y_offset,
+ window_size.width(),
+ window_size.height());
+ window_list_[i]->SetBounds(root_window_, target_bounds, animate);
+ }
+
+ // If we have less than |kMinCardsMajor| windows, adjust the column_ value to
+ // reflect how many "real" columns we have.
+ if (num_columns_ > window_list_.size())
+ num_columns_ = window_list_.size();
+}
+
+bool WindowGrid::Move(WindowSelector::Direction direction) {
+ bool recreate_selection_widget = false;
+ bool out_of_bounds = MoveSelectedIndex(direction, &recreate_selection_widget);
flackr 2014/06/04 22:25:06 Rather than passing recreate_selection_widget in t
Nina 2014/06/05 18:04:35 While I preferred the other approach, this allows
+ // If the selection widget is already active, fade it out in the selection
+ // direction.
+ if (selection_widget_) {
+ if (recreate_selection_widget || out_of_bounds) {
+ // Animate the old selection widget and then destroy it.
+ views::Widget* old_selection = selection_widget_.get();
+ gfx::Vector2d fade_out_direction =
+ GetSlideVectorForFadeIn(
+ direction, old_selection->GetNativeWindow()->bounds());
+
+ ui::ScopedLayerAnimationSettings animation_settings(
+ old_selection->GetNativeWindow()->layer()->GetAnimator());
+ animation_settings.SetTransitionDuration(
+ base::TimeDelta::FromMilliseconds(
+ kOverviewSelectorTransitionMilliseconds));
+ animation_settings.SetPreemptionStrategy(
+ ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
+ // CleanupWidgetAfterAnimationObserver will delete itself (and the
+ // widget) when the movement animation is complete.
+ animation_settings.AddObserver(
+ new CleanupWidgetAfterAnimationObserver(selection_widget_.Pass()));
+ old_selection->SetOpacity(0);
+ old_selection->GetNativeWindow()->SetBounds(
+ old_selection->GetNativeWindow()->bounds() + fade_out_direction);
+ old_selection->Hide();
+ if (!out_of_bounds)
+ InitSelectionWidget(direction);
+ }
+ } else {
+ InitSelectionWidget(direction);
+ }
+ if (!out_of_bounds) {
+ // Send an a11y alert so that if ChromeVox is enabled, the item label is
+ // read.
+ SelectedWindow()->SendFocusAlert();
+
+ // The selection widget is moved to the newly selected item in the same
+ // grid.
+ ui::ScopedLayerAnimationSettings animation_settings(
+ selection_widget_->GetNativeWindow()->layer()->GetAnimator());
+ animation_settings.SetTransitionDuration(base::TimeDelta::FromMilliseconds(
+ kOverviewSelectorTransitionMilliseconds));
+ animation_settings.SetPreemptionStrategy(
+ ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
+ selection_widget_->SetBounds(SelectedWindow()->target_bounds());
+ selection_widget_->SetOpacity(kWindowOverviewSelectorOpacity);
+ }
+
+ return out_of_bounds;
+}
+
+WindowSelectorItem* WindowGrid::SelectedWindow() const {
+ CHECK(selected_index_ < window_list_.size());
+ return window_list_[selected_index_];
+}
+
+void WindowGrid::OnWindowDestroying(aura::Window* window) {
+ std::vector<aura::Window*>::iterator window_iter =
+ std::find(observed_windows_.begin(), observed_windows_.end(), window);
+ DCHECK(window_iter != observed_windows_.end());
+ window->RemoveObserver(this);
+ observed_windows_.erase(window_iter);
+ ScopedVector<WindowSelectorItem>::iterator iter =
+ std::find_if(window_list_.begin(), window_list_.end(),
+ WindowSelectorItemComparator(window));
+
+ DCHECK(iter != window_list_.end());
+
+ (*iter)->RemoveWindow(window);
+
+ // If there are still windows in this selector entry then the overview is
+ // still active and the active selection remains the same.
+ if (!(*iter)->empty())
+ return;
+
+ // We need to get this value before we erase the window to prevent unspecified
+ // behavior.
flackr 2014/06/04 22:25:06 I don't think this comment is necessary.
Nina 2014/06/05 18:04:35 Removed.
+ size_t removed_index = iter - window_list_.begin();
+ window_list_.erase(iter);
+
+ if (empty()) {
+ // If the grid is now empty, notify the window selector so that it erases us
+ // from its grid list.
+ window_selector_->OnGridEmpty(this);
+ return;
+ }
+
+ PositionWindows(true);
+
+ // If existing, reposition the selection widget.
+ if (selection_widget_) {
+ if (selected_index_ >= removed_index && selected_index_ != 0)
flackr 2014/06/04 22:25:06 If the selection changes it should cause another a
Nina 2014/06/05 18:04:35 Good point. Done.
+ selected_index_--;
+ ui::ScopedLayerAnimationSettings animation_settings(
+ selection_widget_->GetNativeWindow()->layer()->GetAnimator());
+ animation_settings.SetTransitionDuration(
+ base::TimeDelta::FromMilliseconds(
+ ScopedTransformOverviewWindow::kTransitionMilliseconds));
+ animation_settings.SetPreemptionStrategy(
+ ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
+ selection_widget_->SetBounds(SelectedWindow()->target_bounds());
flackr 2014/06/04 22:25:06 Seems like setting up an animated move to the sele
Nina 2014/06/05 18:04:35 Done.
+ }
+}
+
+void WindowGrid::OnWindowBoundsChanged(aura::Window* window,
+ const gfx::Rect& old_bounds,
+ const gfx::Rect& new_bounds) {
+ ScopedVector<WindowSelectorItem>::const_iterator iter =
+ std::find_if(window_list_.begin(), window_list_.end(),
+ WindowSelectorItemTargetComparator(window));
+ DCHECK(iter != window_list_.end());
+
+ // Immediately finish any active bounds animation.
+ window->layer()->GetAnimator()->StopAnimatingProperty(
+ ui::LayerAnimationElement::BOUNDS);
+
+ // Recompute the transform for the window.
+ (*iter)->RecomputeWindowTransforms();
+}
+
+void WindowGrid::InitSelectionWidget(WindowSelector::Direction direction) {
+ selection_widget_.reset(new views::Widget);
+ views::Widget::InitParams params;
+ params.type = views::Widget::InitParams::TYPE_POPUP;
+ params.keep_on_top = false;
+ params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
+ params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
+ params.parent = Shell::GetContainer(root_window_,
+ kShellWindowId_DefaultContainer);
+ params.accept_events = false;
+ selection_widget_->set_focus_on_creation(false);
+ selection_widget_->Init(params);
+ // Disable the "bounce in" animation when showing the window.
+ ::wm::SetWindowVisibilityAnimationTransition(
flackr 2014/06/04 22:25:06 Why ::wm?
Nina 2014/06/05 18:04:35 It's not in namespace ash. I think it wouldn't com
+ selection_widget_->GetNativeWindow(), ::wm::ANIMATE_NONE);
+ views::View* content_view = new views::View;
+ content_view->set_background(
+ views::Background::CreateSolidBackground(kWindowOverviewSelectionColor));
+ selection_widget_->SetContentsView(content_view);
+ selection_widget_->GetNativeWindow()->parent()->StackChildAtBottom(
+ selection_widget_->GetNativeWindow());
+ // New selection widget starts with 0 opacity and then fades in.
+ selection_widget_->GetNativeWindow()->layer()->SetOpacity(0);
+ selection_widget_->Show();
flackr 2014/06/04 22:25:06 We actually don't allow calling show on a window w
Nina 2014/06/05 18:04:35 If by this you mean calling Show before setting th
+
+ const gfx::Rect target_bounds = SelectedWindow()->target_bounds();
+ gfx::Vector2d fade_out_direction =
+ GetSlideVectorForFadeIn(direction, target_bounds);
+ gfx::Display dst_display = gfx::Screen::GetScreenFor(root_window_)->
+ GetDisplayMatching(target_bounds);
+ selection_widget_->GetNativeWindow()->SetBoundsInScreen(
+ target_bounds - fade_out_direction, dst_display);
+}
+
+bool WindowGrid::MoveSelectedIndex(WindowSelector::Direction direction,
+ bool* recreate_selection_widget) {
+ if (!selection_widget_) {
+ switch (direction) {
+ case WindowSelector::LEFT:
+ selected_index_ = window_list_.size() - 1;
+ break;
+ case WindowSelector::UP:
+ selected_index_ =
+ (window_list_.size() / num_columns_) * num_columns_ - 1;
+ break;
+ case WindowSelector::RIGHT:
+ case WindowSelector::DOWN:
+ selected_index_ = 0;
+ break;
+ }
+ return false;
+ }
flackr 2014/06/04 22:25:06 nit: newline before main switch.
Nina 2014/06/05 18:04:35 Now that we have an else there, it might not be ne
+ switch (direction) {
+ case WindowSelector::RIGHT:
+ if (selected_index_ >= window_list_.size() - 1)
+ return true;
+ selected_index_++;
+ if (selected_index_ % num_columns_ == 0)
+ (*recreate_selection_widget) = true;
+ break;
+ case WindowSelector::LEFT:
+ if (selected_index_ == 0)
+ return true;
+ selected_index_--;
+ if ((selected_index_ + 1) % num_columns_ == 0)
+ (*recreate_selection_widget) = true;
+ break;
+ case WindowSelector::DOWN:
+ selected_index_ += num_columns_;
+ if (selected_index_ >= window_list_.size()) {
+ selected_index_ = (selected_index_ + 1) % num_columns_;
+ if (selected_index_ == 0)
+ return true;
+ (*recreate_selection_widget) = true;
+ }
+ break;
+ case WindowSelector::UP:
+ if (selected_index_ == 0)
+ return true;
+ if (selected_index_ < num_columns_) {
+ selected_index_--;
+ if (!(selected_index_ + num_columns_ >= window_list_.size())) {
+ selected_index_ +=
+ num_columns_ * (window_list_.size() / num_columns_);
flackr 2014/06/04 22:25:06 This should probably be num_columns_ * ((window_li
Nina 2014/06/05 18:04:35 I combined both this comment and the previous one
+ if (selected_index_ >= window_list_.size())
+ selected_index_ -= num_columns_;
+ }
+ (*recreate_selection_widget) = true;
+ } else {
+ selected_index_ -= num_columns_;
+ }
+ break;
+ }
+ return false;
+}
+
+} // namespace ash

Powered by Google App Engine
This is Rietveld 408576698