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_
10 #include "base/basictypes.h"
11 #include "base/logging.h"
12 #include "ui/gfx/rect.h"
13 #include "ui/views/views_export.h"
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 {
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
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.
53 // Returns the view at the specified index.
54 View* view_at(int index) const {
56 return entries_[index].view;
59 void set_ideal_bounds(int index, const gfx::Rect& bounds) {
61 entries_[index].ideal_bounds = bounds;
64 const gfx::Rect& ideal_bounds(int index) const {
66 return entries_[index].ideal_bounds;
69 // Returns the index of the specified view, or -1 if the view isn't in the
71 int GetIndexOfView(const View* view) const;
75 Entry() : view(NULL) {}
78 gfx::Rect ideal_bounds;
80 typedef std::vector<Entry> Entries;
83 void check_index(int index) const {
84 DCHECK_LT(index, static_cast<int>(entries_.size()));
88 void check_index(int index) const {}
93 DISALLOW_COPY_AND_ASSIGN(ViewModel);
98 #endif // UI_VIEWS_VIEW_MODEL_H_