Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / components / view_manager / view_coordinate_conversions.cc
blob8237875fb6ea078be2a9db9e66f31e2e38cab711
1 // Copyright 2014 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 "components/view_manager/view_coordinate_conversions.h"
7 #include "components/view_manager/server_view.h"
8 #include "ui/gfx/geometry/point.h"
9 #include "ui/gfx/geometry/point_conversions.h"
10 #include "ui/gfx/geometry/point_f.h"
11 #include "ui/gfx/geometry/rect.h"
12 #include "ui/gfx/geometry/vector2d.h"
13 #include "ui/gfx/geometry/vector2d_f.h"
15 namespace view_manager {
17 namespace {
19 gfx::Vector2dF CalculateOffsetToAncestor(const ServerView* view,
20 const ServerView* ancestor) {
21 DCHECK(ancestor->Contains(view));
22 gfx::Vector2d result;
23 for (const ServerView* v = view; v != ancestor; v = v->parent())
24 result += v->bounds().OffsetFromOrigin();
25 return gfx::Vector2dF(result.x(), result.y());
28 } // namespace
30 gfx::Point ConvertPointBetweenViews(const ServerView* from,
31 const ServerView* to,
32 const gfx::Point& point) {
33 return gfx::ToFlooredPoint(
34 ConvertPointFBetweenViews(from, to, gfx::PointF(point.x(), point.y())));
37 gfx::PointF ConvertPointFBetweenViews(const ServerView* from,
38 const ServerView* to,
39 const gfx::PointF& point) {
40 DCHECK(from);
41 DCHECK(to);
42 if (from == to)
43 return point;
45 if (from->Contains(to)) {
46 const gfx::Vector2dF offset(CalculateOffsetToAncestor(to, from));
47 return point - offset;
49 DCHECK(to->Contains(from));
50 const gfx::Vector2dF offset(CalculateOffsetToAncestor(from, to));
51 return point + offset;
54 gfx::Rect ConvertRectBetweenViews(const ServerView* from,
55 const ServerView* to,
56 const gfx::Rect& rect) {
57 DCHECK(from);
58 DCHECK(to);
59 if (from == to)
60 return rect;
62 const gfx::Point top_left(ConvertPointBetweenViews(from, to, rect.origin()));
63 const gfx::Point bottom_right(gfx::ToCeiledPoint(ConvertPointFBetweenViews(
64 from, to, gfx::PointF(rect.right(), rect.bottom()))));
65 return gfx::Rect(top_left.x(), top_left.y(), bottom_right.x() - top_left.x(),
66 bottom_right.y() - top_left.y());
69 } // namespace view_manager