Chromium Code Reviews| Index: ui/views/view_model.h |
| diff --git a/ui/views/view_model.h b/ui/views/view_model.h |
| index e8c84a9568edfdb311eeca8bafea671700b96eaa..763b239bceaaed3094467b6583319b23d723805a 100644 |
| --- a/ui/views/view_model.h |
| +++ b/ui/views/view_model.h |
| @@ -10,6 +10,7 @@ |
| #include "base/basictypes.h" |
| #include "base/logging.h" |
| #include "ui/gfx/rect.h" |
| +#include "ui/views/view.h" |
| #include "ui/views/views_export.h" |
| namespace views { |
| @@ -21,14 +22,15 @@ class View; |
| // 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 VIEWS_EXPORT ViewModel { |
| +template <class T> |
| +class ViewModel { |
| public: |
| ViewModel(); |
| ~ViewModel(); |
| // Adds |view| to this model. This does not add |view| to a view hierarchy, |
| // only to this model. |
| - void Add(View* view, int index); |
| + void Add(T* view, int index); |
| // Removes the view at the specified index. This does not actually remove the |
| // view from the view hierarchy. |
| @@ -51,7 +53,7 @@ class VIEWS_EXPORT ViewModel { |
| void Clear(); |
| // Returns the view at the specified index. |
| - View* view_at(int index) const { |
| + T* view_at(int index) const { |
| check_index(index); |
| return entries_[index].view; |
| } |
| @@ -74,7 +76,7 @@ class VIEWS_EXPORT ViewModel { |
| struct Entry { |
| Entry() : view(NULL) {} |
| - View* view; |
| + T* view; |
| gfx::Rect ideal_bounds; |
| }; |
| typedef std::vector<Entry> Entries; |
| @@ -93,6 +95,81 @@ class VIEWS_EXPORT ViewModel { |
| DISALLOW_COPY_AND_ASSIGN(ViewModel); |
| }; |
| +template <class T> |
| +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.
|
| +} |
| + |
| +template <class T> |
| +ViewModel<T>::~ViewModel() { |
| + // view are owned by their parent, no need to delete them. |
| +} |
| + |
| +template <class T> |
| +void ViewModel<T>::Add(T* view, int index) { |
| + DCHECK_LE(index, static_cast<int>(entries_.size())); |
| + DCHECK_GE(index, 0); |
| + Entry entry; |
| + entry.view = view; |
| + entries_.insert(entries_.begin() + index, entry); |
| +} |
| + |
| +template <class T> |
| +void ViewModel<T>::Remove(int index) { |
| + if (index == -1) |
| + return; |
| + |
| + check_index(index); |
| + entries_.erase(entries_.begin() + index); |
| +} |
| + |
| +template <class T> |
| +void ViewModel<T>::Move(int index, int target_index) { |
| + DCHECK_LT(index, static_cast<int>(entries_.size())); |
| + DCHECK_GE(index, 0); |
| + DCHECK_LT(target_index, static_cast<int>(entries_.size())); |
| + DCHECK_GE(target_index, 0); |
| + |
| + if (index == target_index) |
| + return; |
| + Entry entry(entries_[index]); |
| + entries_.erase(entries_.begin() + index); |
| + entries_.insert(entries_.begin() + target_index, entry); |
| +} |
| + |
| +template <class T> |
| +void ViewModel<T>::MoveViewOnly(int index, int target_index) { |
| + if (index == target_index) |
| + return; |
| + if (target_index < index) { |
| + T* view = entries_[index].view; |
| + for (int i = index; i > target_index; --i) |
| + entries_[i].view = entries_[i - 1].view; |
| + entries_[target_index].view = view; |
| + } else { |
| + T* view = entries_[index].view; |
| + for (int i = index; i < target_index; ++i) |
| + entries_[i].view = entries_[i + 1].view; |
| + entries_[target_index].view = view; |
| + } |
| +} |
| + |
| +template <class T> |
| +void ViewModel<T>::Clear() { |
| + Entries entries; |
| + entries.swap(entries_); |
| + for (size_t i = 0; i < entries.size(); ++i) |
| + delete entries[i].view; |
| +} |
| + |
| +template <class T> |
| +int ViewModel<T>::GetIndexOfView(const 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 views |
| #endif // UI_VIEWS_VIEW_MODEL_H_ |