Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #ifndef UI_VIEWS_VIEW_MODEL_H_ | 5 #ifndef UI_VIEWS_VIEW_MODEL_H_ |
| 6 #define UI_VIEWS_VIEW_MODEL_H_ | 6 #define UI_VIEWS_VIEW_MODEL_H_ |
| 7 | 7 |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "ui/gfx/rect.h" | 12 #include "ui/gfx/rect.h" |
| 13 #include "ui/views/views_export.h" | 13 #include "ui/views/views_export.h" |
| 14 | 14 |
| 15 namespace views { | 15 namespace views { |
| 16 | 16 |
| 17 class View; | 17 class View; |
| 18 | 18 |
| 19 // ViewModel is used to track an 'interesting' set of a views. Often times | 19 // Internal implementation of the templated ViewModel class. Provides |
| 20 // during animations views are removed after a delay, which makes for tricky | 20 // non-templated "unsafe" methods for ViewModel to wrap around. Any methods that |
| 21 // coordinate conversion as you have to account for the possibility of the | 21 // allow insertion of or access to a View* should be protected, and have a |
| 22 // indices from the model not lining up with those you expect. This class lets | 22 // public method in the ViewModel subclass that provides type-safe access to the |
| 23 // you define the 'interesting' views and operate on those views. | 23 // correct View subclass. |
| 24 class VIEWS_EXPORT ViewModel { | 24 class VIEWS_EXPORT ViewModelBase { |
| 25 public: | 25 public: |
| 26 ViewModel(); | 26 ~ViewModelBase(); |
| 27 ~ViewModel(); | |
| 28 | |
| 29 // Adds |view| to this model. This does not add |view| to a view hierarchy, | |
| 30 // only to this model. | |
| 31 void Add(View* view, int index); | |
| 32 | 27 |
| 33 // Removes the view at the specified index. This does not actually remove the | 28 // Removes the view at the specified index. This does not actually remove the |
| 34 // view from the view hierarchy. | 29 // view from the view hierarchy. |
| 35 void Remove(int index); | 30 void Remove(int index); |
| 36 | 31 |
| 37 // Moves the view at |index| to |target_index|. |target_index| is in terms | 32 // Moves the view at |index| to |target_index|. |target_index| is in terms |
| 38 // of the model *after* the view at |index| is removed. | 33 // of the model *after* the view at |index| is removed. |
| 39 void Move(int index, int target_index); | 34 void Move(int index, int target_index); |
| 40 | 35 |
| 41 // Variant of Move() that leaves the bounds as is. That is, after invoking | 36 // Variant of Move() that leaves the bounds as is. That is, after invoking |
| 42 // this the bounds of the view at |target_index| (and all other indices) are | 37 // this the bounds of the view at |target_index| (and all other indices) are |
| 43 // exactly the same as the bounds of the view at |target_index| before | 38 // exactly the same as the bounds of the view at |target_index| before |
| 44 // invoking this. | 39 // invoking this. |
| 45 void MoveViewOnly(int index, int target_index); | 40 void MoveViewOnly(int index, int target_index); |
| 46 | 41 |
| 47 // Returns the number of views. | 42 // Returns the number of views. |
| 48 int view_size() const { return static_cast<int>(entries_.size()); } | 43 int view_size() const { return static_cast<int>(entries_.size()); } |
| 49 | 44 |
| 50 // Removes and deletes all the views. | 45 // Removes and deletes all the views. |
| 51 void Clear(); | 46 void Clear(); |
| 52 | 47 |
| 53 // Returns the view at the specified index. | 48 // Returns the view at the specified index. Note: Most users should use |
| 54 View* view_at(int index) const { | 49 // view_at() in the subclass, to get a view of the correct type. (Do not call |
| 50 // ViewAtBase then static_cast to the desired type.) | |
| 51 View* ViewAtBase(int index) const { | |
|
sky
2014/10/01 19:20:37
Can we name this view_at and the subclass implemen
Matt Giuca
2014/10/02 01:23:51
I think I prefer the friends approach (since "norm
| |
| 55 check_index(index); | 52 check_index(index); |
| 56 return entries_[index].view; | 53 return entries_[index].view; |
| 57 } | 54 } |
| 58 | 55 |
| 59 void set_ideal_bounds(int index, const gfx::Rect& bounds) { | 56 void set_ideal_bounds(int index, const gfx::Rect& bounds) { |
| 60 check_index(index); | 57 check_index(index); |
| 61 entries_[index].ideal_bounds = bounds; | 58 entries_[index].ideal_bounds = bounds; |
| 62 } | 59 } |
| 63 | 60 |
| 64 const gfx::Rect& ideal_bounds(int index) const { | 61 const gfx::Rect& ideal_bounds(int index) const { |
| 65 check_index(index); | 62 check_index(index); |
| 66 return entries_[index].ideal_bounds; | 63 return entries_[index].ideal_bounds; |
| 67 } | 64 } |
| 68 | 65 |
| 69 // Returns the index of the specified view, or -1 if the view isn't in the | 66 // Returns the index of the specified view, or -1 if the view isn't in the |
| 70 // model. | 67 // model. |
| 71 int GetIndexOfView(const View* view) const; | 68 int GetIndexOfView(const View* view) const; |
| 72 | 69 |
| 70 protected: | |
| 71 ViewModelBase(); | |
| 72 | |
| 73 // Adds |view| to this model. This does not add |view| to a view hierarchy, | |
| 74 // only to this model. | |
| 75 void AddUnsafe(View* view, int index); | |
| 76 | |
| 73 private: | 77 private: |
| 74 struct Entry { | 78 struct Entry { |
| 75 Entry() : view(NULL) {} | 79 Entry() : view(NULL) {} |
| 76 | 80 |
| 77 View* view; | 81 View* view; |
| 78 gfx::Rect ideal_bounds; | 82 gfx::Rect ideal_bounds; |
| 79 }; | 83 }; |
| 80 typedef std::vector<Entry> Entries; | 84 typedef std::vector<Entry> Entries; |
| 81 | 85 |
| 82 #if !defined(NDEBUG) | 86 #if !defined(NDEBUG) |
| 83 void check_index(int index) const { | 87 void check_index(int index) const { |
| 84 DCHECK_LT(index, static_cast<int>(entries_.size())); | 88 DCHECK_LT(index, static_cast<int>(entries_.size())); |
| 85 DCHECK_GE(index, 0); | 89 DCHECK_GE(index, 0); |
| 86 } | 90 } |
| 87 #else | 91 #else |
| 88 void check_index(int index) const {} | 92 void check_index(int index) const {} |
| 89 #endif | 93 #endif |
| 90 | 94 |
| 91 Entries entries_; | 95 Entries entries_; |
| 92 | 96 |
| 93 DISALLOW_COPY_AND_ASSIGN(ViewModel); | 97 DISALLOW_COPY_AND_ASSIGN(ViewModelBase); |
| 98 }; | |
| 99 | |
| 100 // ViewModel is used to track an 'interesting' set of a views. Often times | |
| 101 // during animations views are removed after a delay, which makes for tricky | |
| 102 // coordinate conversion as you have to account for the possibility of the | |
| 103 // indices from the model not lining up with those you expect. This class lets | |
| 104 // you define the 'interesting' views and operate on those views. | |
| 105 template <class T> | |
| 106 class ViewModel : public ViewModelBase { | |
|
sky
2014/10/01 19:20:38
I like naming this ViewModelT and having classes t
Matt Giuca
2014/10/02 01:23:51
Done.
| |
| 107 public: | |
| 108 ViewModel<T>() {} | |
| 109 | |
| 110 // Adds |view| to this model. This does not add |view| to a view hierarchy, | |
| 111 // only to this model. | |
| 112 void Add(T* view, int index) { AddUnsafe(view, index); } | |
| 113 | |
| 114 // Returns the view at the specified index. | |
| 115 T* view_at(int index) const { return static_cast<T*>(ViewAtBase(index)); } | |
| 116 | |
| 117 private: | |
| 118 DISALLOW_COPY_AND_ASSIGN(ViewModel<T>); | |
| 94 }; | 119 }; |
| 95 | 120 |
| 96 } // namespace views | 121 } // namespace views |
| 97 | 122 |
| 98 #endif // UI_VIEWS_VIEW_MODEL_H_ | 123 #endif // UI_VIEWS_VIEW_MODEL_H_ |
| OLD | NEW |