diff options
author | sky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-04-09 23:11:52 +0000 |
---|---|---|
committer | sky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-04-09 23:11:52 +0000 |
commit | 870764920553f9347c599a6aed97d1d889ffeba7 (patch) | |
tree | 32207fd5879811fa5400117be9a52017423cd5e9 /ash | |
parent | c3a396716daab081b57bdf443b0e0aac50af51b5 (diff) | |
download | chromium_src-870764920553f9347c599a6aed97d1d889ffeba7.zip chromium_src-870764920553f9347c599a6aed97d1d889ffeba7.tar.gz chromium_src-870764920553f9347c599a6aed97d1d889ffeba7.tar.bz2 |
Moves view_model and related classes to views. This way I can use it
in the tabstrip.
BUG=none
TEST=none
Review URL: https://chromiumcodereview.appspot.com/10031012
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@131472 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ash')
-rw-r--r-- | ash/ash.gyp | 6 | ||||
-rw-r--r-- | ash/launcher/launcher_view.cc | 14 | ||||
-rw-r--r-- | ash/launcher/launcher_view.h | 4 | ||||
-rw-r--r-- | ash/launcher/view_model.cc | 55 | ||||
-rw-r--r-- | ash/launcher/view_model.h | 82 | ||||
-rw-r--r-- | ash/launcher/view_model_unittest.cc | 41 | ||||
-rw-r--r-- | ash/launcher/view_model_utils.cc | 48 | ||||
-rw-r--r-- | ash/launcher/view_model_utils.h | 36 | ||||
-rw-r--r-- | ash/launcher/view_model_utils_unittest.cc | 46 |
9 files changed, 9 insertions, 323 deletions
diff --git a/ash/ash.gyp b/ash/ash.gyp index fc01656..7bda6e7 100644 --- a/ash/ash.gyp +++ b/ash/ash.gyp @@ -98,10 +98,6 @@ 'launcher/launcher_view.h', 'launcher/tabbed_launcher_button.cc', 'launcher/tabbed_launcher_button.h', - 'launcher/view_model.cc', - 'launcher/view_model.h', - 'launcher/view_model_utils.cc', - 'launcher/view_model_utils.h', 'monitor/monitor_controller.cc', 'monitor/monitor_controller.h', 'monitor/multi_monitor_manager.cc', @@ -341,8 +337,6 @@ 'ime/input_method_event_filter_unittest.cc', 'launcher/launcher_model_unittest.cc', 'launcher/launcher_unittest.cc', - 'launcher/view_model_unittest.cc', - 'launcher/view_model_utils_unittest.cc', 'monitor/multi_monitor_manager_unittest.cc', 'shell_unittest.cc', 'test/ash_unittests.cc', diff --git a/ash/launcher/launcher_view.cc b/ash/launcher/launcher_view.cc index b5bd803..f844b7e 100644 --- a/ash/launcher/launcher_view.cc +++ b/ash/launcher/launcher_view.cc @@ -8,8 +8,6 @@ #include "ash/launcher/launcher_delegate.h" #include "ash/launcher/launcher_model.h" #include "ash/launcher/tabbed_launcher_button.h" -#include "ash/launcher/view_model.h" -#include "ash/launcher/view_model_utils.h" #include "ash/shell.h" #include "ash/shell_delegate.h" #include "base/auto_reset.h" @@ -29,6 +27,8 @@ #include "ui/views/controls/menu/menu_model_adapter.h" #include "ui/views/controls/menu/menu_runner.h" #include "ui/views/focus/focus_search.h" +#include "ui/views/view_model.h" +#include "ui/views/view_model_utils.h" #include "ui/views/widget/root_view.h" #include "ui/views/widget/widget.h" @@ -55,7 +55,7 @@ namespace { // the ViewModel. class LauncherFocusSearch : public views::FocusSearch { public: - LauncherFocusSearch(ViewModel* view_model) + LauncherFocusSearch(views::ViewModel* view_model) : FocusSearch(NULL, true, true), view_model_(view_model) {} virtual ~LauncherFocusSearch() {} @@ -85,7 +85,7 @@ class LauncherFocusSearch : public views::FocusSearch { } private: - ViewModel* view_model_; + views::ViewModel* view_model_; DISALLOW_COPY_AND_ASSIGN(LauncherFocusSearch); }; @@ -254,7 +254,7 @@ LauncherButton* LauncherView::TestAPI::GetButton(int index) { LauncherView::LauncherView(LauncherModel* model, LauncherDelegate* delegate) : model_(model), delegate_(delegate), - view_model_(new ViewModel), + view_model_(new views::ViewModel), overflow_button_(NULL), dragging_(NULL), drag_view_(NULL), @@ -345,7 +345,7 @@ View* LauncherView::GetFocusTraversableParentView() { void LauncherView::LayoutToIdealBounds() { IdealBounds ideal_bounds; CalculateIdealBounds(&ideal_bounds); - ViewModelUtils::SetViewBoundsToIdealBounds(*view_model_); + views::ViewModelUtils::SetViewBoundsToIdealBounds(*view_model_); overflow_button_->SetBoundsRect(ideal_bounds.overflow_bounds); } @@ -527,7 +527,7 @@ void LauncherView::ContinueDrag(const views::MouseEvent& event) { drag_view_->SetX(x); int target_index = - ViewModelUtils::DetermineMoveIndex(*view_model_, drag_view_, x); + views::ViewModelUtils::DetermineMoveIndex(*view_model_, drag_view_, x); target_index = std::min(indices.second, std::max(target_index, indices.first)); if (target_index == current_index) diff --git a/ash/launcher/launcher_view.h b/ash/launcher/launcher_view.h index 9d3373b..dc1d8d1 100644 --- a/ash/launcher/launcher_view.h +++ b/ash/launcher/launcher_view.h @@ -20,6 +20,7 @@ namespace views { class BoundsAnimator; class ImageButton; class MenuRunner; +class ViewModel; } namespace ash { @@ -27,7 +28,6 @@ namespace ash { class LauncherDelegate; struct LauncherItem; class LauncherModel; -class ViewModel; namespace internal { @@ -167,7 +167,7 @@ class ASH_EXPORT LauncherView : public views::View, // Used to manage the set of active launcher buttons. There is a view per // item in |model_|. - scoped_ptr<ViewModel> view_model_; + scoped_ptr<views::ViewModel> view_model_; scoped_ptr<views::BoundsAnimator> bounds_animator_; diff --git a/ash/launcher/view_model.cc b/ash/launcher/view_model.cc deleted file mode 100644 index 28b0213..0000000 --- a/ash/launcher/view_model.cc +++ /dev/null @@ -1,55 +0,0 @@ -// Copyright (c) 2012 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/launcher/view_model.h" - -#include "base/logging.h" -#include "ui/views/view.h" - -namespace ash { - -ViewModel::ViewModel() { -} - -ViewModel::~ViewModel() { - // view are owned by their parent, no need to delete them. -} - -void ViewModel::Add(views::View* view, int index) { - Entry entry; - entry.view = view; - entries_.insert(entries_.begin() + index, entry); -} - -void ViewModel::Remove(int index) { - if (index == -1) - return; - - entries_.erase(entries_.begin() + index); -} - -void ViewModel::Move(int index, int target_index) { - if (index == target_index) - return; - Entry entry(entries_[index]); - entries_.erase(entries_.begin() + index); - entries_.insert(entries_.begin() + target_index, entry); -} - -void ViewModel::Clear() { - Entries entries; - entries.swap(entries_); - for (size_t i = 0; i < entries.size(); ++i) - delete entries[i].view; -} - -int ViewModel::GetIndexOfView(const views::View* view) const { - for (size_t i = 0; i < entries_.size(); ++i) { - if (entries_[i].view == view) - return static_cast<int>(i); - } - return -1; -} - -} // namespace ash diff --git a/ash/launcher/view_model.h b/ash/launcher/view_model.h deleted file mode 100644 index bff50b8..0000000 --- a/ash/launcher/view_model.h +++ /dev/null @@ -1,82 +0,0 @@ -// Copyright (c) 2012 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. - -#ifndef ASH_LAUNCHER_VIEW_MODEL_H_ -#define ASH_LAUNCHER_VIEW_MODEL_H_ -#pragma once - -#include <vector> - -#include "base/basictypes.h" -#include "ash/ash_export.h" -#include "ui/gfx/rect.h" - -namespace views { -class View; -} - -namespace ash { - -// ViewModel is used to track an 'interesting' set of a views. Often times -// during animations views are removed after a delay, which makes for tricky -// coordinate conversion as you have to account for the possibility of the -// indices from the model not lining up with those you expect. This class lets -// you define the 'interesting' views and operate on those views. -class ASH_EXPORT ViewModel { - public: - ViewModel(); - ~ViewModel(); - - // Adds |view| to this model. This does not add |view| to a view hierarchy, - // only to this model. - void Add(views::View* view, int index); - - // Removes the view at the specified index. This does not actually remove the - // view from the view hierarchy. - void Remove(int index); - - // Moves the view at |index| to |target_index|. |target_index| is in terms - // of the model *after* the view at |index| is removed. - void Move(int index, int target_index); - - // Returns the number of views. - int view_size() const { return static_cast<int>(entries_.size()); } - - // Removes and deletes all the views. - void Clear(); - - // Returns the view at the specified index. - views::View* view_at(int index) const { - return entries_[index].view; - } - - void set_ideal_bounds(int index, const gfx::Rect& bounds) { - entries_[index].ideal_bounds = bounds; - } - - const gfx::Rect& ideal_bounds(int index) const { - return entries_[index].ideal_bounds; - } - - // Returns the index of the specified view, or -1 if the view isn't in the - // model. - int GetIndexOfView(const views::View* view) const; - - private: - struct Entry { - Entry() : view(NULL) {} - - views::View* view; - gfx::Rect ideal_bounds; - }; - typedef std::vector<Entry> Entries; - - Entries entries_; - - DISALLOW_COPY_AND_ASSIGN(ViewModel); -}; - -} // namespace ash - -#endif // ASH_LAUNCHER_VIEW_MODEL_H_ diff --git a/ash/launcher/view_model_unittest.cc b/ash/launcher/view_model_unittest.cc deleted file mode 100644 index 089302c..0000000 --- a/ash/launcher/view_model_unittest.cc +++ /dev/null @@ -1,41 +0,0 @@ -// Copyright (c) 2011 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/launcher/view_model.h" - -#include "testing/gtest/include/gtest/gtest.h" -#include "ui/views/view.h" - -namespace ash { - -TEST(ViewModel, BasicAssertions) { - views::View v1; - ViewModel model; - model.Add(&v1, 0); - EXPECT_EQ(1, model.view_size()); - EXPECT_EQ(&v1, model.view_at(0)); - gfx::Rect v1_bounds(1, 2, 3, 4); - model.set_ideal_bounds(0, v1_bounds); - EXPECT_EQ(v1_bounds, model.ideal_bounds(0)); - EXPECT_EQ(0, model.GetIndexOfView(&v1)); -} - -TEST(ViewModel, Move) { - views::View v1, v2, v3; - ViewModel model; - model.Add(&v1, 0); - model.Add(&v2, 1); - model.Add(&v3, 2); - model.Move(0, 2); - EXPECT_EQ(&v1, model.view_at(2)); - EXPECT_EQ(&v2, model.view_at(0)); - EXPECT_EQ(&v3, model.view_at(1)); - - model.Move(2, 0); - EXPECT_EQ(&v1, model.view_at(0)); - EXPECT_EQ(&v2, model.view_at(1)); - EXPECT_EQ(&v3, model.view_at(2)); -} - -} // namespace ash diff --git a/ash/launcher/view_model_utils.cc b/ash/launcher/view_model_utils.cc deleted file mode 100644 index dca6d51..0000000 --- a/ash/launcher/view_model_utils.cc +++ /dev/null @@ -1,48 +0,0 @@ -// Copyright (c) 2011 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/launcher/view_model_utils.h" - -#include <algorithm> - -#include "ash/launcher/view_model.h" -#include "ui/views/view.h" - -namespace ash { - -// static -void ViewModelUtils::SetViewBoundsToIdealBounds(const ViewModel& model) { - for (int i = 0; i < model.view_size(); ++i) - model.view_at(i)->SetBoundsRect(model.ideal_bounds(i)); -} - -// static -int ViewModelUtils::DetermineMoveIndex(const ViewModel& model, - views::View* view, - int x) { - int current_index = model.GetIndexOfView(view); - DCHECK_NE(-1, current_index); - for (int i = 0; i < current_index; ++i) { - int mid_x = model.ideal_bounds(i).x() + model.ideal_bounds(i).width() / 2; - if (x < mid_x) - return i; - } - - if (current_index + 1 == model.view_size()) - return current_index; - - // For indices after the current index ignore the bounds of the view being - // dragged. This keeps the view from bouncing around as moved. - int delta = model.ideal_bounds(current_index + 1).x() - - model.ideal_bounds(current_index).x(); - for (int i = current_index + 1; i < model.view_size(); ++i) { - const gfx::Rect& bounds(model.ideal_bounds(i)); - int mid_x = bounds.x() + bounds.width() / 2 - delta; - if (x < mid_x) - return i - 1; - } - return model.view_size() - 1; -} - -} // namespace ash diff --git a/ash/launcher/view_model_utils.h b/ash/launcher/view_model_utils.h deleted file mode 100644 index 26d25de..0000000 --- a/ash/launcher/view_model_utils.h +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright (c) 2011 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. - -#ifndef ASH_LAUNCHER_VIEW_MODEL_UTILS_H_ -#define ASH_LAUNCHER_VIEW_MODEL_UTILS_H_ -#pragma once - -#include "base/basictypes.h" -#include "ash/ash_export.h" - -namespace views { -class View; -} - -namespace ash { - -class ViewModel; - -class ASH_EXPORT ViewModelUtils { - public: - // Sets the bounds of each view to its ideal bounds. - static void SetViewBoundsToIdealBounds(const ViewModel& model); - - // Returns the index to move |view| to based on a x-coordinate of |x|. - static int DetermineMoveIndex(const ViewModel& model, - views::View* view, - int x); - - private: - DISALLOW_IMPLICIT_CONSTRUCTORS(ViewModelUtils); -}; - -} // namespace ash - -#endif // ASH_LAUNCHER_VIEW_MODEL_UTILS_H_ diff --git a/ash/launcher/view_model_utils_unittest.cc b/ash/launcher/view_model_utils_unittest.cc deleted file mode 100644 index 54989fb..0000000 --- a/ash/launcher/view_model_utils_unittest.cc +++ /dev/null @@ -1,46 +0,0 @@ -// Copyright (c) 2011 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/launcher/view_model_utils.h" - -#include "ash/launcher/view_model.h" -#include "testing/gtest/include/gtest/gtest.h" -#include "ui/views/view.h" - -namespace ash { - -// Makes sure SetViewBoundsToIdealBounds updates the view appropriately. -TEST(ViewModelUtils, SetViewBoundsToIdealBounds) { - views::View v1; - ViewModel model; - model.Add(&v1, 0); - gfx::Rect v1_bounds(1, 2, 3, 4); - model.set_ideal_bounds(0, v1_bounds); - ViewModelUtils::SetViewBoundsToIdealBounds(model); - EXPECT_EQ(v1_bounds, v1.bounds()); -} - -// Assertions for DetermineMoveIndex. -TEST(ViewModelUtils, DetermineMoveIndex) { - views::View v1, v2, v3; - ViewModel model; - model.Add(&v1, 0); - model.Add(&v2, 1); - model.Add(&v3, 2); - model.set_ideal_bounds(0, gfx::Rect(0, 0, 10, 10)); - model.set_ideal_bounds(1, gfx::Rect(10, 0, 1000, 10)); - model.set_ideal_bounds(2, gfx::Rect(1010, 0, 2, 10)); - - EXPECT_EQ(0, ViewModelUtils::DetermineMoveIndex(model, &v1, -10)); - EXPECT_EQ(0, ViewModelUtils::DetermineMoveIndex(model, &v1, 4)); - EXPECT_EQ(1, ViewModelUtils::DetermineMoveIndex(model, &v1, 506)); - EXPECT_EQ(2, ViewModelUtils::DetermineMoveIndex(model, &v1, 1010)); - EXPECT_EQ(2, ViewModelUtils::DetermineMoveIndex(model, &v1, 2000)); - - EXPECT_EQ(0, ViewModelUtils::DetermineMoveIndex(model, &v2, -10)); - EXPECT_EQ(0, ViewModelUtils::DetermineMoveIndex(model, &v2, 4)); - EXPECT_EQ(2, ViewModelUtils::DetermineMoveIndex(model, &v2, 12)); -} - -} // namespace ash |