Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(367)

Side by Side Diff: ui/display/display_finder.cc

Issue 2730413002: Moves FindDisplayIndexContainingPoint into display (Closed)
Patch Set: comment Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/display/display_finder.h" 5 #include "ui/display/display_finder.h"
6 6
7 #include <algorithm>
7 #include <limits> 8 #include <limits>
8 9
9 #include "base/logging.h" 10 #include "base/logging.h"
10 #include "ui/display/display.h" 11 #include "ui/display/display.h"
11 #include "ui/gfx/geometry/point.h" 12 #include "ui/gfx/geometry/point.h"
12 #include "ui/gfx/geometry/rect.h" 13 #include "ui/gfx/geometry/rect.h"
13 14
14 namespace display { 15 namespace display {
15 16
16 const Display* FindDisplayNearestPoint(const std::vector<Display>& displays, 17 const Display* FindDisplayNearestPoint(const std::vector<Display>& displays,
17 const gfx::Point& point) { 18 const gfx::Point& point) {
18 DCHECK(!displays.empty()); 19 DCHECK(!displays.empty());
20 const int containing_index = FindDisplayIndexContainingPoint(displays, point);
21 if (containing_index != -1)
22 return &displays[containing_index];
23
19 int min_distance = std::numeric_limits<int>::max(); 24 int min_distance = std::numeric_limits<int>::max();
20 const Display* nearest_display = nullptr; 25 const Display* nearest_display = nullptr;
21 for (const auto& display : displays) { 26 for (const auto& display : displays) {
22 const int distance = display.bounds().ManhattanDistanceToPoint(point); 27 const int distance = display.bounds().ManhattanDistanceToPoint(point);
23 if (distance < min_distance) { 28 if (distance < min_distance) {
24 min_distance = distance; 29 min_distance = distance;
25 nearest_display = &display; 30 nearest_display = &display;
26 } 31 }
27 } 32 }
28 // There should always be at least one display that is less than INT_MAX away. 33 // There should always be at least one display that is less than INT_MAX away.
(...skipping 11 matching lines...) Expand all
40 const gfx::Rect intersect = IntersectRects(display.bounds(), rect); 45 const gfx::Rect intersect = IntersectRects(display.bounds(), rect);
41 const int area = intersect.width() * intersect.height(); 46 const int area = intersect.width() * intersect.height();
42 if (area > max_area) { 47 if (area > max_area) {
43 max_area = area; 48 max_area = area;
44 matching = &display; 49 matching = &display;
45 } 50 }
46 } 51 }
47 return matching; 52 return matching;
48 } 53 }
49 54
55 int FindDisplayIndexContainingPoint(const std::vector<Display>& displays,
56 const gfx::Point& point_in_screen) {
57 auto iter = std::find_if(displays.begin(), displays.end(),
58 [point_in_screen](const Display& display) {
59 return display.bounds().Contains(point_in_screen);
60 });
61 return iter == displays.end() ? -1 : (iter - displays.begin());
62 }
63
50 } // namespace display 64 } // namespace display
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698