Blink roll 174234:174238
[chromium-blink-merge.git] / ui / views / view_model.h
blobe8c84a9568edfdb311eeca8bafea671700b96eaa
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
3 // found in the LICENSE file.
5 #ifndef UI_VIEWS_VIEW_MODEL_H_
6 #define UI_VIEWS_VIEW_MODEL_H_
8 #include <vector>
10 #include "base/basictypes.h"
11 #include "base/logging.h"
12 #include "ui/gfx/rect.h"
13 #include "ui/views/views_export.h"
15 namespace views {
17 class View;
19 // 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 // 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 // you define the 'interesting' views and operate on those views.
24 class VIEWS_EXPORT ViewModel {
25 public:
26 ViewModel();
27 ~ViewModel();
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);
33 // Removes the view at the specified index. This does not actually remove the
34 // view from the view hierarchy.
35 void Remove(int index);
37 // Moves the view at |index| to |target_index|. |target_index| is in terms
38 // of the model *after* the view at |index| is removed.
39 void Move(int index, int target_index);
41 // 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
43 // exactly the same as the bounds of the view at |target_index| before
44 // invoking this.
45 void MoveViewOnly(int index, int target_index);
47 // Returns the number of views.
48 int view_size() const { return static_cast<int>(entries_.size()); }
50 // Removes and deletes all the views.
51 void Clear();
53 // Returns the view at the specified index.
54 View* view_at(int index) const {
55 check_index(index);
56 return entries_[index].view;
59 void set_ideal_bounds(int index, const gfx::Rect& bounds) {
60 check_index(index);
61 entries_[index].ideal_bounds = bounds;
64 const gfx::Rect& ideal_bounds(int index) const {
65 check_index(index);
66 return entries_[index].ideal_bounds;
69 // Returns the index of the specified view, or -1 if the view isn't in the
70 // model.
71 int GetIndexOfView(const View* view) const;
73 private:
74 struct Entry {
75 Entry() : view(NULL) {}
77 View* view;
78 gfx::Rect ideal_bounds;
80 typedef std::vector<Entry> Entries;
82 #if !defined(NDEBUG)
83 void check_index(int index) const {
84 DCHECK_LT(index, static_cast<int>(entries_.size()));
85 DCHECK_GE(index, 0);
87 #else
88 void check_index(int index) const {}
89 #endif
91 Entries entries_;
93 DISALLOW_COPY_AND_ASSIGN(ViewModel);
96 } // namespace views
98 #endif // UI_VIEWS_VIEW_MODEL_H_