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

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

Issue 2730413002: Moves FindDisplayIndexContainingPoint into display (Closed)
Patch Set: cleanup 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
« no previous file with comments | « ui/display/display_finder.h ('k') | ui/display/manager/display_manager.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 using Displays = std::vector<Display>;
18
19 const Display* FindDisplayNearestPoint(const Displays& displays,
17 const gfx::Point& point) { 20 const gfx::Point& point) {
18 DCHECK(!displays.empty()); 21 DCHECK(!displays.empty());
22 auto iter = FindDisplayContainingPoint(displays, point);
23 if (iter != displays.end())
24 return &(*iter);
25
19 int min_distance = std::numeric_limits<int>::max(); 26 int min_distance = std::numeric_limits<int>::max();
20 const Display* nearest_display = nullptr; 27 const Display* nearest_display = nullptr;
21 for (const auto& display : displays) { 28 for (const auto& display : displays) {
22 const int distance = display.bounds().ManhattanDistanceToPoint(point); 29 const int distance = display.bounds().ManhattanDistanceToPoint(point);
23 if (distance < min_distance) { 30 if (distance < min_distance) {
24 min_distance = distance; 31 min_distance = distance;
25 nearest_display = &display; 32 nearest_display = &display;
26 } 33 }
27 } 34 }
28 // There should always be at least one display that is less than INT_MAX away. 35 // There should always be at least one display that is less than INT_MAX away.
29 DCHECK(nearest_display); 36 DCHECK(nearest_display);
30 return nearest_display; 37 return nearest_display;
31 } 38 }
32 39
33 const Display* FindDisplayWithBiggestIntersection( 40 const Display* FindDisplayWithBiggestIntersection(const Displays& displays,
34 const std::vector<Display>& displays, 41 const gfx::Rect& rect) {
35 const gfx::Rect& rect) {
36 DCHECK(!displays.empty()); 42 DCHECK(!displays.empty());
37 int max_area = 0; 43 int max_area = 0;
38 const Display* matching = nullptr; 44 const Display* matching = nullptr;
39 for (const auto& display : displays) { 45 for (const auto& display : displays) {
40 const gfx::Rect intersect = IntersectRects(display.bounds(), rect); 46 const gfx::Rect intersect = IntersectRects(display.bounds(), rect);
41 const int area = intersect.width() * intersect.height(); 47 const int area = intersect.width() * intersect.height();
42 if (area > max_area) { 48 if (area > max_area) {
43 max_area = area; 49 max_area = area;
44 matching = &display; 50 matching = &display;
45 } 51 }
46 } 52 }
47 return matching; 53 return matching;
48 } 54 }
49 55
56 Displays::const_iterator FindDisplayContainingPoint(
57 const Displays& displays,
58 const gfx::Point& point_in_screen) {
59 return std::find_if(displays.begin(), displays.end(),
60 [point_in_screen](const Display& display) {
61 return display.bounds().Contains(point_in_screen);
62 });
63 }
64
50 } // namespace display 65 } // namespace display
OLDNEW
« no previous file with comments | « ui/display/display_finder.h ('k') | ui/display/manager/display_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698