kBlockedAllowID should not have IDS_BLOCKED_COOKIES_UNBLOCK.
[chromium-blink-merge.git] / ash / display / display_util.cc
blob90d2f95a497a9fd3109b77981dec3371b017b383
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 "ash/display/display_util.h"
7 #include <algorithm>
9 #include "ash/display/display_info.h"
10 #include "ash/display/display_manager.h"
11 #include "ash/host/ash_window_tree_host.h"
12 #include "ash/shell.h"
13 #include "ui/aura/env.h"
14 #include "ui/aura/window_tree_host.h"
15 #include "ui/gfx/geometry/point.h"
16 #include "ui/gfx/geometry/rect.h"
17 #include "ui/wm/core/coordinate_conversion.h"
19 #if defined(OS_CHROMEOS)
20 #include "base/sys_info.h"
21 #endif
23 namespace ash {
24 namespace {
26 // List of value UI Scale values. Scales for 2x are equivalent to 640,
27 // 800, 1024, 1280, 1440, 1600 and 1920 pixel width respectively on
28 // 2560 pixel width 2x density display. Please see crbug.com/233375
29 // for the full list of resolutions.
30 const float kUIScalesFor2x[] =
31 {0.5f, 0.625f, 0.8f, 1.0f, 1.125f, 1.25f, 1.5f, 2.0f};
32 const float kUIScalesFor1_25x[] = {0.5f, 0.625f, 0.8f, 1.0f, 1.25f };
33 const float kUIScalesFor1280[] = {0.5f, 0.625f, 0.8f, 1.0f, 1.125f };
34 const float kUIScalesFor1366[] = {0.5f, 0.6f, 0.75f, 1.0f, 1.125f };
36 std::vector<float> GetScalesForDisplay(const DisplayMode& native_mode) {
37 #define ASSIGN_ARRAY(v, a) v.assign(a, a + arraysize(a))
39 std::vector<float> ret;
40 if (native_mode.device_scale_factor == 2.0f) {
41 ASSIGN_ARRAY(ret, kUIScalesFor2x);
42 return ret;
43 } else if (native_mode.device_scale_factor == 1.25f) {
44 ASSIGN_ARRAY(ret, kUIScalesFor1_25x);
45 return ret;
47 switch (native_mode.size.width()) {
48 case 1280:
49 ASSIGN_ARRAY(ret, kUIScalesFor1280);
50 break;
51 case 1366:
52 ASSIGN_ARRAY(ret, kUIScalesFor1366);
53 break;
54 default:
55 ASSIGN_ARRAY(ret, kUIScalesFor1280);
56 #if defined(OS_CHROMEOS)
57 if (base::SysInfo::IsRunningOnChromeOS())
58 NOTREACHED() << "Unknown resolution:" << native_mode.size.ToString();
59 #endif
61 return ret;
64 struct ScaleComparator {
65 explicit ScaleComparator(float s) : scale(s) {}
67 bool operator()(const DisplayMode& mode) const {
68 const float kEpsilon = 0.0001f;
69 return std::abs(scale - mode.ui_scale) < kEpsilon;
71 float scale;
74 void ConvertPointFromScreenToNative(aura::WindowTreeHost* host,
75 gfx::Point* point) {
76 ::wm::ConvertPointFromScreen(host->window(), point);
77 host->ConvertPointToNativeScreen(point);
80 } // namespace
82 std::vector<DisplayMode> CreateInternalDisplayModeList(
83 const DisplayMode& native_mode) {
84 std::vector<DisplayMode> display_mode_list;
86 float native_ui_scale = (native_mode.device_scale_factor == 1.25f)
87 ? 1.0f
88 : native_mode.device_scale_factor;
89 for (float ui_scale : GetScalesForDisplay(native_mode)) {
90 DisplayMode mode = native_mode;
91 mode.ui_scale = ui_scale;
92 mode.native = (ui_scale == native_ui_scale);
93 display_mode_list.push_back(mode);
95 return display_mode_list;
98 // static
99 float GetNextUIScale(const DisplayInfo& info, bool up) {
100 ScaleComparator comparator(info.configured_ui_scale());
101 const std::vector<DisplayMode>& modes = info.display_modes();
102 for (auto iter = modes.begin(); iter != modes.end(); ++iter) {
103 if (comparator(*iter)) {
104 if (up && (iter + 1) != modes.end())
105 return (iter + 1)->ui_scale;
106 if (!up && iter != modes.begin())
107 return (iter - 1)->ui_scale;
108 return info.configured_ui_scale();
111 // Fallback to 1.0f if the |scale| wasn't in the list.
112 return 1.0f;
115 bool HasDisplayModeForUIScale(const DisplayInfo& info, float ui_scale) {
116 ScaleComparator comparator(ui_scale);
117 const std::vector<DisplayMode>& modes = info.display_modes();
118 return std::find_if(modes.begin(), modes.end(), comparator) != modes.end();
121 void ComputeBoundary(const gfx::Display& primary_display,
122 const gfx::Display& secondary_display,
123 DisplayLayout::Position position,
124 gfx::Rect* primary_edge_in_screen,
125 gfx::Rect* secondary_edge_in_screen) {
126 const gfx::Rect& primary = primary_display.bounds();
127 const gfx::Rect& secondary = secondary_display.bounds();
128 switch (position) {
129 case DisplayLayout::TOP:
130 case DisplayLayout::BOTTOM: {
131 int left = std::max(primary.x(), secondary.x());
132 int right = std::min(primary.right(), secondary.right());
133 if (position == DisplayLayout::TOP) {
134 primary_edge_in_screen->SetRect(left, primary.y(), right - left, 1);
135 secondary_edge_in_screen->SetRect(left, secondary.bottom() - 1,
136 right - left, 1);
137 } else {
138 primary_edge_in_screen->SetRect(left, primary.bottom() - 1,
139 right - left, 1);
140 secondary_edge_in_screen->SetRect(left, secondary.y(), right - left, 1);
142 break;
144 case DisplayLayout::LEFT:
145 case DisplayLayout::RIGHT: {
146 int top = std::max(primary.y(), secondary.y());
147 int bottom = std::min(primary.bottom(), secondary.bottom());
148 if (position == DisplayLayout::LEFT) {
149 primary_edge_in_screen->SetRect(primary.x(), top, 1, bottom - top);
150 secondary_edge_in_screen->SetRect(secondary.right() - 1, top, 1,
151 bottom - top);
152 } else {
153 primary_edge_in_screen->SetRect(primary.right() - 1, top, 1,
154 bottom - top);
155 secondary_edge_in_screen->SetRect(secondary.y(), top, 1, bottom - top);
157 break;
162 gfx::Rect GetNativeEdgeBounds(AshWindowTreeHost* ash_host,
163 const gfx::Rect& bounds_in_screen) {
164 aura::WindowTreeHost* host = ash_host->AsWindowTreeHost();
165 gfx::Rect native_bounds = host->GetBounds();
166 native_bounds.Inset(ash_host->GetHostInsets());
167 gfx::Point start_in_native = bounds_in_screen.origin();
168 gfx::Point end_in_native = bounds_in_screen.bottom_right();
170 ConvertPointFromScreenToNative(host, &start_in_native);
171 ConvertPointFromScreenToNative(host, &end_in_native);
173 if (std::abs(start_in_native.x() - end_in_native.x()) <
174 std::abs(start_in_native.y() - end_in_native.y())) {
175 // vertical in native
176 int x = std::abs(native_bounds.x() - start_in_native.x()) <
177 std::abs(native_bounds.right() - start_in_native.x())
178 ? native_bounds.x()
179 : native_bounds.right() - 1;
180 return gfx::Rect(x, std::min(start_in_native.y(), end_in_native.y()), 1,
181 std::abs(end_in_native.y() - start_in_native.y()));
182 } else {
183 // horizontal in native
184 int y = std::abs(native_bounds.y() - start_in_native.y()) <
185 std::abs(native_bounds.bottom() - start_in_native.y())
186 ? native_bounds.y()
187 : native_bounds.bottom() - 1;
188 return gfx::Rect(std::min(start_in_native.x(), end_in_native.x()), y,
189 std::abs(end_in_native.x() - start_in_native.x()), 1);
193 // Moves the cursor to the point inside the root that is closest to
194 // the point_in_screen, which is outside of the root window.
195 void MoveCursorTo(AshWindowTreeHost* ash_host,
196 const gfx::Point& point_in_screen,
197 bool update_last_location_now) {
198 aura::WindowTreeHost* host = ash_host->AsWindowTreeHost();
199 gfx::Point point_in_native = point_in_screen;
200 ::wm::ConvertPointFromScreen(host->window(), &point_in_native);
201 host->ConvertPointToNativeScreen(&point_in_native);
203 // now fit the point inside the native bounds.
204 gfx::Rect native_bounds = host->GetBounds();
205 gfx::Point native_origin = native_bounds.origin();
206 native_bounds.Inset(ash_host->GetHostInsets());
207 // Shrink further so that the mouse doesn't warp on the
208 // edge. The right/bottom needs to be shrink by 2 to subtract
209 // the 1 px from width/height value.
210 native_bounds.Inset(1, 1, 2, 2);
212 // Ensure that |point_in_native| is inside the |native_bounds|.
213 point_in_native.SetToMax(native_bounds.origin());
214 point_in_native.SetToMin(native_bounds.bottom_right());
216 gfx::Point point_in_host = point_in_native;
218 point_in_host.Offset(-native_origin.x(), -native_origin.y());
219 host->MoveCursorToHostLocation(point_in_host);
221 if (update_last_location_now) {
222 gfx::Point new_point_in_screen = point_in_native;
223 if (Shell::GetInstance()->display_manager()->IsInUnifiedMode()) {
224 // TODO(oshima): Do not use ConvertPointFromNativeScreen because
225 // the mirroring display has a transform that should not be applied here.
226 gfx::Point origin = host->GetBounds().origin();
227 new_point_in_screen.Offset(-origin.x(), -origin.y());
228 } else {
229 host->ConvertPointFromNativeScreen(&new_point_in_screen);
231 ::wm::ConvertPointToScreen(host->window(), &new_point_in_screen);
232 aura::Env::GetInstance()->set_last_mouse_location(new_point_in_screen);
236 int FindDisplayIndexContainingPoint(const std::vector<gfx::Display>& displays,
237 const gfx::Point& point_in_screen) {
238 auto iter = std::find_if(displays.begin(), displays.end(),
239 [point_in_screen](const gfx::Display& display) {
240 return display.bounds().Contains(point_in_screen);
242 return iter == displays.end() ? -1 : (iter - displays.begin());
245 } // namespace ash