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"
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"
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
);
43 } else if (native_mode
.device_scale_factor
== 1.25f
) {
44 ASSIGN_ARRAY(ret
, kUIScalesFor1_25x
);
47 switch (native_mode
.size
.width()) {
49 ASSIGN_ARRAY(ret
, kUIScalesFor1280
);
52 ASSIGN_ARRAY(ret
, kUIScalesFor1366
);
55 ASSIGN_ARRAY(ret
, kUIScalesFor1280
);
56 #if defined(OS_CHROMEOS)
57 if (base::SysInfo::IsRunningOnChromeOS())
58 NOTREACHED() << "Unknown resolution:" << native_mode
.size
.ToString();
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
;
74 void ConvertPointFromScreenToNative(aura::WindowTreeHost
* host
,
76 ::wm::ConvertPointFromScreen(host
->window(), point
);
77 host
->ConvertPointToNativeScreen(point
);
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
)
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
;
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.
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();
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,
138 primary_edge_in_screen
->SetRect(left
, primary
.bottom() - 1,
140 secondary_edge_in_screen
->SetRect(left
, secondary
.y(), right
- left
, 1);
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,
153 primary_edge_in_screen
->SetRect(primary
.right() - 1, top
, 1,
155 secondary_edge_in_screen
->SetRect(secondary
.y(), top
, 1, bottom
- top
);
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());
168 bool vertical
= bounds_in_screen
.width() < bounds_in_screen
.height();
169 gfx::Point start_in_native
;
170 gfx::Point end_in_native
;
173 start_in_native
= bounds_in_screen
.origin();
174 end_in_native
= start_in_native
;
175 end_in_native
.set_y(bounds_in_screen
.bottom());
177 start_in_native
= bounds_in_screen
.origin();
178 end_in_native
= start_in_native
;
179 end_in_native
.set_x(bounds_in_screen
.right());
182 ConvertPointFromScreenToNative(host
, &start_in_native
);
183 ConvertPointFromScreenToNative(host
, &end_in_native
);
185 // vertical in native
186 int x
= std::abs(native_bounds
.x() - start_in_native
.x()) <
187 std::abs(native_bounds
.right() - start_in_native
.x())
189 : native_bounds
.right() - 1;
190 return gfx::Rect(x
, std::min(start_in_native
.y(), end_in_native
.y()), 1,
191 end_in_native
.y() - start_in_native
.y());
193 // horizontal in native
194 int y
= std::abs(native_bounds
.y() - start_in_native
.y()) <
195 std::abs(native_bounds
.bottom() - start_in_native
.y())
197 : native_bounds
.bottom() - 1;
198 return gfx::Rect(std::min(start_in_native
.x(), end_in_native
.x()), y
,
199 end_in_native
.x() - start_in_native
.x(), 1);
203 // Moves the cursor to the point inside the root that is closest to
204 // the point_in_screen, which is outside of the root window.
205 void MoveCursorTo(AshWindowTreeHost
* ash_host
,
206 const gfx::Point
& point_in_screen
,
207 bool update_last_location_now
) {
208 aura::WindowTreeHost
* host
= ash_host
->AsWindowTreeHost();
209 gfx::Point point_in_native
= point_in_screen
;
210 ::wm::ConvertPointFromScreen(host
->window(), &point_in_native
);
211 host
->ConvertPointToNativeScreen(&point_in_native
);
213 // now fit the point inside the native bounds.
214 gfx::Rect native_bounds
= host
->GetBounds();
215 gfx::Point native_origin
= native_bounds
.origin();
216 native_bounds
.Inset(ash_host
->GetHostInsets());
217 // Shrink further so that the mouse doesn't warp on the
218 // edge. The right/bottom needs to be shrink by 2 to subtract
219 // the 1 px from width/height value.
220 native_bounds
.Inset(1, 1, 2, 2);
222 // Ensure that |point_in_native| is inside the |native_bounds|.
223 point_in_native
.SetToMax(native_bounds
.origin());
224 point_in_native
.SetToMin(native_bounds
.bottom_right());
226 gfx::Point point_in_host
= point_in_native
;
228 point_in_host
.Offset(-native_origin
.x(), -native_origin
.y());
229 host
->MoveCursorToHostLocation(point_in_host
);
231 if (update_last_location_now
) {
232 gfx::Point new_point_in_screen
= point_in_native
;
233 if (Shell::GetInstance()->display_manager()->IsInUnifiedMode()) {
234 // TODO(oshima): Do not use ConvertPointFromNativeScreen because
235 // the mirroring display has a transform that should not be applied here.
236 gfx::Point origin
= host
->GetBounds().origin();
237 new_point_in_screen
.Offset(-origin
.x(), -origin
.y());
239 host
->ConvertPointFromNativeScreen(&new_point_in_screen
);
241 ::wm::ConvertPointToScreen(host
->window(), &new_point_in_screen
);
242 aura::Env::GetInstance()->set_last_mouse_location(new_point_in_screen
);
246 int FindDisplayIndexContainingPoint(const std::vector
<gfx::Display
>& displays
,
247 const gfx::Point
& point_in_screen
) {
248 auto iter
= std::find_if(displays
.begin(), displays
.end(),
249 [point_in_screen
](const gfx::Display
& display
) {
250 return display
.bounds().Contains(point_in_screen
);
252 return iter
== displays
.end() ? -1 : (iter
- displays
.begin());