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 #include "ui/views/view_model_utils.h"
9 #include "ui/views/view.h"
10 #include "ui/views/view_model.h"
16 // Used in calculating ideal bounds.
17 int primary_axis_coordinate(ViewModelUtils::Alignment alignment,
20 return alignment == ViewModelUtils::HORIZONTAL ? x : y;
26 void ViewModelUtils::SetViewBoundsToIdealBounds(const ViewModelBase& model) {
27 for (int i = 0; i < model.view_size(); ++i)
28 model.ViewAtBase(i)->SetBoundsRect(model.ideal_bounds(i));
32 bool ViewModelUtils::IsAtIdealBounds(const ViewModelBase& model) {
33 for (int i = 0; i < model.view_size(); ++i) {
34 View* view = model.ViewAtBase(i);
35 if (view->bounds() != model.ideal_bounds(i))
42 int ViewModelUtils::DetermineMoveIndex(const ViewModelBase& model,
47 int value = primary_axis_coordinate(alignment, x, y);
48 int current_index = model.GetIndexOfView(view);
49 DCHECK_NE(-1, current_index);
50 for (int i = 0; i < current_index; ++i) {
51 int mid_point = primary_axis_coordinate(
53 model.ideal_bounds(i).x() + model.ideal_bounds(i).width() / 2,
54 model.ideal_bounds(i).y() + model.ideal_bounds(i).height() / 2);
55 if (value < mid_point)
59 if (current_index + 1 == model.view_size())
62 // For indices after the current index ignore the bounds of the view being
63 // dragged. This keeps the view from bouncing around as moved.
64 int delta = primary_axis_coordinate(
66 model.ideal_bounds(current_index + 1).x() -
67 model.ideal_bounds(current_index).x(),
68 model.ideal_bounds(current_index + 1).y() -
69 model.ideal_bounds(current_index).y());
70 for (int i = current_index + 1; i < model.view_size(); ++i) {
71 const gfx::Rect& bounds(model.ideal_bounds(i));
72 int mid_point = primary_axis_coordinate(
74 bounds.x() + bounds.width() / 2 - delta,
75 bounds.y() + bounds.height() / 2 - delta);
76 if (value < mid_point)
79 return model.view_size() - 1;