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

Side by Side Diff: ui/views/view_model.h

Issue 598013003: Added views::ViewModelT<T>, a type-safe template version of ViewModel. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@appsgridview-static-casts
Patch Set: Created 6 years, 2 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 unified diff | Download patch
« no previous file with comments | « ui/app_list/views/contents_view.cc ('k') | ui/views/view_model.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/view.h"
13 #include "ui/views/views_export.h" 14 #include "ui/views/views_export.h"
14 15
15 namespace views { 16 namespace views {
16 17
17 class View; 18 class View;
18 19
19 // ViewModel is used to track an 'interesting' set of a views. Often times 20 // ViewModel is used to track an 'interesting' set of a views. Often times
20 // during animations views are removed after a delay, which makes for tricky 21 // during animations views are removed after a delay, which makes for tricky
21 // coordinate conversion as you have to account for the possibility of the 22 // coordinate conversion as you have to account for the possibility of the
22 // indices from the model not lining up with those you expect. This class lets 23 // indices from the model not lining up with those you expect. This class lets
23 // you define the 'interesting' views and operate on those views. 24 // you define the 'interesting' views and operate on those views.
24 class VIEWS_EXPORT ViewModel { 25 template <class T>
26 class ViewModel {
25 public: 27 public:
26 ViewModel(); 28 ViewModel();
27 ~ViewModel(); 29 ~ViewModel();
28 30
29 // Adds |view| to this model. This does not add |view| to a view hierarchy, 31 // Adds |view| to this model. This does not add |view| to a view hierarchy,
30 // only to this model. 32 // only to this model.
31 void Add(View* view, int index); 33 void Add(T* view, int index);
32 34
33 // Removes the view at the specified index. This does not actually remove the 35 // Removes the view at the specified index. This does not actually remove the
34 // view from the view hierarchy. 36 // view from the view hierarchy.
35 void Remove(int index); 37 void Remove(int index);
36 38
37 // Moves the view at |index| to |target_index|. |target_index| is in terms 39 // Moves the view at |index| to |target_index|. |target_index| is in terms
38 // of the model *after* the view at |index| is removed. 40 // of the model *after* the view at |index| is removed.
39 void Move(int index, int target_index); 41 void Move(int index, int target_index);
40 42
41 // Variant of Move() that leaves the bounds as is. That is, after invoking 43 // 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 44 // 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 45 // exactly the same as the bounds of the view at |target_index| before
44 // invoking this. 46 // invoking this.
45 void MoveViewOnly(int index, int target_index); 47 void MoveViewOnly(int index, int target_index);
46 48
47 // Returns the number of views. 49 // Returns the number of views.
48 int view_size() const { return static_cast<int>(entries_.size()); } 50 int view_size() const { return static_cast<int>(entries_.size()); }
49 51
50 // Removes and deletes all the views. 52 // Removes and deletes all the views.
51 void Clear(); 53 void Clear();
52 54
53 // Returns the view at the specified index. 55 // Returns the view at the specified index.
54 View* view_at(int index) const { 56 T* view_at(int index) const {
55 check_index(index); 57 check_index(index);
56 return entries_[index].view; 58 return entries_[index].view;
57 } 59 }
58 60
59 void set_ideal_bounds(int index, const gfx::Rect& bounds) { 61 void set_ideal_bounds(int index, const gfx::Rect& bounds) {
60 check_index(index); 62 check_index(index);
61 entries_[index].ideal_bounds = bounds; 63 entries_[index].ideal_bounds = bounds;
62 } 64 }
63 65
64 const gfx::Rect& ideal_bounds(int index) const { 66 const gfx::Rect& ideal_bounds(int index) const {
65 check_index(index); 67 check_index(index);
66 return entries_[index].ideal_bounds; 68 return entries_[index].ideal_bounds;
67 } 69 }
68 70
69 // Returns the index of the specified view, or -1 if the view isn't in the 71 // Returns the index of the specified view, or -1 if the view isn't in the
70 // model. 72 // model.
71 int GetIndexOfView(const View* view) const; 73 int GetIndexOfView(const View* view) const;
72 74
73 private: 75 private:
74 struct Entry { 76 struct Entry {
75 Entry() : view(NULL) {} 77 Entry() : view(NULL) {}
76 78
77 View* view; 79 T* view;
78 gfx::Rect ideal_bounds; 80 gfx::Rect ideal_bounds;
79 }; 81 };
80 typedef std::vector<Entry> Entries; 82 typedef std::vector<Entry> Entries;
81 83
82 #if !defined(NDEBUG) 84 #if !defined(NDEBUG)
83 void check_index(int index) const { 85 void check_index(int index) const {
84 DCHECK_LT(index, static_cast<int>(entries_.size())); 86 DCHECK_LT(index, static_cast<int>(entries_.size()));
85 DCHECK_GE(index, 0); 87 DCHECK_GE(index, 0);
86 } 88 }
87 #else 89 #else
88 void check_index(int index) const {} 90 void check_index(int index) const {}
89 #endif 91 #endif
90 92
91 Entries entries_; 93 Entries entries_;
92 94
93 DISALLOW_COPY_AND_ASSIGN(ViewModel); 95 DISALLOW_COPY_AND_ASSIGN(ViewModel);
94 }; 96 };
95 97
98 template <class T>
99 ViewModel<T>::ViewModel() {
sky 2014/09/29 17:54:52 Why don't you just inline all of these now?
Matt Giuca 2014/09/30 04:30:40 No longer relevant.
100 }
101
102 template <class T>
103 ViewModel<T>::~ViewModel() {
104 // view are owned by their parent, no need to delete them.
105 }
106
107 template <class T>
108 void ViewModel<T>::Add(T* view, int index) {
109 DCHECK_LE(index, static_cast<int>(entries_.size()));
110 DCHECK_GE(index, 0);
111 Entry entry;
112 entry.view = view;
113 entries_.insert(entries_.begin() + index, entry);
114 }
115
116 template <class T>
117 void ViewModel<T>::Remove(int index) {
118 if (index == -1)
119 return;
120
121 check_index(index);
122 entries_.erase(entries_.begin() + index);
123 }
124
125 template <class T>
126 void ViewModel<T>::Move(int index, int target_index) {
127 DCHECK_LT(index, static_cast<int>(entries_.size()));
128 DCHECK_GE(index, 0);
129 DCHECK_LT(target_index, static_cast<int>(entries_.size()));
130 DCHECK_GE(target_index, 0);
131
132 if (index == target_index)
133 return;
134 Entry entry(entries_[index]);
135 entries_.erase(entries_.begin() + index);
136 entries_.insert(entries_.begin() + target_index, entry);
137 }
138
139 template <class T>
140 void ViewModel<T>::MoveViewOnly(int index, int target_index) {
141 if (index == target_index)
142 return;
143 if (target_index < index) {
144 T* view = entries_[index].view;
145 for (int i = index; i > target_index; --i)
146 entries_[i].view = entries_[i - 1].view;
147 entries_[target_index].view = view;
148 } else {
149 T* view = entries_[index].view;
150 for (int i = index; i < target_index; ++i)
151 entries_[i].view = entries_[i + 1].view;
152 entries_[target_index].view = view;
153 }
154 }
155
156 template <class T>
157 void ViewModel<T>::Clear() {
158 Entries entries;
159 entries.swap(entries_);
160 for (size_t i = 0; i < entries.size(); ++i)
161 delete entries[i].view;
162 }
163
164 template <class T>
165 int ViewModel<T>::GetIndexOfView(const View* view) const {
166 for (size_t i = 0; i < entries_.size(); ++i) {
167 if (entries_[i].view == view)
168 return static_cast<int>(i);
169 }
170 return -1;
171 }
172
96 } // namespace views 173 } // namespace views
97 174
98 #endif // UI_VIEWS_VIEW_MODEL_H_ 175 #endif // UI_VIEWS_VIEW_MODEL_H_
OLDNEW
« no previous file with comments | « ui/app_list/views/contents_view.cc ('k') | ui/views/view_model.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698