summaryrefslogtreecommitdiffstats
path: root/components/mus/view_coordinate_conversions.cc
blob: 445d76210b2e06d8284d03b0c92f6b5b5cba8db6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/mus/view_coordinate_conversions.h"

#include "components/mus/server_view.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/point_conversions.h"
#include "ui/gfx/geometry/point_f.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/vector2d.h"
#include "ui/gfx/geometry/vector2d_f.h"

namespace mus {

namespace {

gfx::Vector2dF CalculateOffsetToAncestor(const ServerView* view,
                                         const ServerView* ancestor) {
  DCHECK(ancestor->Contains(view));
  gfx::Vector2d result;
  for (const ServerView* v = view; v != ancestor; v = v->parent())
    result += v->bounds().OffsetFromOrigin();
  return gfx::Vector2dF(result.x(), result.y());
}

}  // namespace

gfx::Point ConvertPointBetweenViews(const ServerView* from,
                                    const ServerView* to,
                                    const gfx::Point& point) {
  return gfx::ToFlooredPoint(
      ConvertPointFBetweenViews(from, to, gfx::PointF(point.x(), point.y())));
}

gfx::PointF ConvertPointFBetweenViews(const ServerView* from,
                                      const ServerView* to,
                                      const gfx::PointF& point) {
  DCHECK(from);
  DCHECK(to);
  if (from == to)
    return point;

  if (from->Contains(to)) {
    const gfx::Vector2dF offset(CalculateOffsetToAncestor(to, from));
    return point - offset;
  }
  DCHECK(to->Contains(from));
  const gfx::Vector2dF offset(CalculateOffsetToAncestor(from, to));
  return point + offset;
}

gfx::Rect ConvertRectBetweenViews(const ServerView* from,
                                  const ServerView* to,
                                  const gfx::Rect& rect) {
  DCHECK(from);
  DCHECK(to);
  if (from == to)
    return rect;

  const gfx::Point top_left(ConvertPointBetweenViews(from, to, rect.origin()));
  const gfx::Point bottom_right(gfx::ToCeiledPoint(ConvertPointFBetweenViews(
      from, to, gfx::PointF(rect.right(), rect.bottom()))));
  return gfx::Rect(top_left.x(), top_left.y(), bottom_right.x() - top_left.x(),
                   bottom_right.y() - top_left.y());
}

}  // namespace mus