OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/base/layout.h" | 5 #include "ui/base/layout.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cmath> | 8 #include <cmath> |
9 #include <limits> | 9 #include <limits> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "build/build_config.h" | 14 #include "build/build_config.h" |
15 #include "ui/base/touch/touch_device.h" | 15 #include "ui/base/touch/touch_device.h" |
16 #include "ui/base/ui_base_switches.h" | 16 #include "ui/base/ui_base_switches.h" |
17 #include "ui/gfx/display.h" | 17 #include "ui/gfx/display.h" |
| 18 #include "ui/gfx/image/image_skia.h" |
18 #include "ui/gfx/screen.h" | 19 #include "ui/gfx/screen.h" |
19 | 20 |
20 #if defined(OS_MACOSX) && !defined(OS_IOS) | |
21 #include "base/mac/mac_util.h" | |
22 #endif | |
23 | |
24 #if defined(OS_WIN) | 21 #if defined(OS_WIN) |
25 #include "base/win/metro.h" | 22 #include "base/win/metro.h" |
26 #include "ui/gfx/win/dpi.h" | |
27 #include <Windows.h> | 23 #include <Windows.h> |
28 #endif // defined(OS_WIN) | 24 #endif // defined(OS_WIN) |
29 | 25 |
30 #if defined(OS_CHROMEOS) | |
31 #include "ui/base/resource/resource_bundle.h" | |
32 #endif | |
33 | |
34 namespace ui { | 26 namespace ui { |
35 | 27 |
36 namespace { | 28 namespace { |
37 | 29 |
38 bool ScaleFactorComparator(const ScaleFactor& lhs, const ScaleFactor& rhs){ | 30 bool ScaleFactorComparator(const ScaleFactor& lhs, const ScaleFactor& rhs){ |
39 return GetScaleFactorScale(lhs) < GetScaleFactorScale(rhs); | 31 return GetImageScale(lhs) < GetImageScale(rhs); |
40 } | 32 } |
41 | 33 |
| 34 std::vector<ScaleFactor>* g_supported_scale_factors = NULL; |
| 35 |
42 #if defined(OS_WIN) | 36 #if defined(OS_WIN) |
43 // Helper function that determines whether we want to optimize the UI for touch. | 37 // Helper function that determines whether we want to optimize the UI for touch. |
44 bool UseTouchOptimizedUI() { | 38 bool UseTouchOptimizedUI() { |
45 // If --touch-optimized-ui is specified and not set to "auto", then override | 39 // If --touch-optimized-ui is specified and not set to "auto", then override |
46 // the hardware-determined setting (eg. for testing purposes). | 40 // the hardware-determined setting (eg. for testing purposes). |
47 static bool has_touch_optimized_ui = CommandLine::ForCurrentProcess()-> | 41 static bool has_touch_optimized_ui = CommandLine::ForCurrentProcess()-> |
48 HasSwitch(switches::kTouchOptimizedUI); | 42 HasSwitch(switches::kTouchOptimizedUI); |
49 if (has_touch_optimized_ui) { | 43 if (has_touch_optimized_ui) { |
50 const std::string switch_value = CommandLine::ForCurrentProcess()-> | 44 const std::string switch_value = CommandLine::ForCurrentProcess()-> |
51 GetSwitchValueASCII(switches::kTouchOptimizedUI); | 45 GetSwitchValueASCII(switches::kTouchOptimizedUI); |
(...skipping 13 matching lines...) Expand all Loading... |
65 return base::win::IsMetroProcess() && ui::IsTouchDevicePresent(); | 59 return base::win::IsMetroProcess() && ui::IsTouchDevicePresent(); |
66 } | 60 } |
67 #endif // defined(OS_WIN) | 61 #endif // defined(OS_WIN) |
68 | 62 |
69 const float kScaleFactorScales[] = {1.0f, 1.0f, 1.25f, 1.33f, 1.4f, 1.5f, 1.8f, | 63 const float kScaleFactorScales[] = {1.0f, 1.0f, 1.25f, 1.33f, 1.4f, 1.5f, 1.8f, |
70 2.0f}; | 64 2.0f}; |
71 COMPILE_ASSERT(NUM_SCALE_FACTORS == arraysize(kScaleFactorScales), | 65 COMPILE_ASSERT(NUM_SCALE_FACTORS == arraysize(kScaleFactorScales), |
72 kScaleFactorScales_incorrect_size); | 66 kScaleFactorScales_incorrect_size); |
73 const size_t kScaleFactorScalesLength = arraysize(kScaleFactorScales); | 67 const size_t kScaleFactorScalesLength = arraysize(kScaleFactorScales); |
74 | 68 |
75 namespace { | |
76 | |
77 // Returns the scale factor closest to |scale| from the full list of factors. | |
78 // Note that it does NOT rely on the list of supported scale factors. | |
79 // Finding the closest match is inefficient and shouldn't be done frequently. | |
80 ScaleFactor FindClosestScaleFactorUnsafe(float scale) { | |
81 float smallest_diff = std::numeric_limits<float>::max(); | |
82 ScaleFactor closest_match = SCALE_FACTOR_100P; | |
83 for (int i = SCALE_FACTOR_100P; i < NUM_SCALE_FACTORS; ++i) { | |
84 const ScaleFactor scale_factor = static_cast<ScaleFactor>(i); | |
85 float diff = std::abs(kScaleFactorScales[scale_factor] - scale); | |
86 if (diff < smallest_diff) { | |
87 closest_match = scale_factor; | |
88 smallest_diff = diff; | |
89 } | |
90 } | |
91 return closest_match; | |
92 } | |
93 | |
94 } // namespace | |
95 | |
96 std::vector<ScaleFactor>& GetSupportedScaleFactorsInternal() { | |
97 static std::vector<ScaleFactor>* supported_scale_factors = | |
98 new std::vector<ScaleFactor>(); | |
99 if (supported_scale_factors->empty()) { | |
100 #if !defined(OS_IOS) | |
101 // On platforms other than iOS, 100P is always a supported scale factor. | |
102 supported_scale_factors->push_back(SCALE_FACTOR_100P); | |
103 #endif | |
104 | |
105 #if defined(OS_ANDROID) | |
106 const gfx::Display display = | |
107 gfx::Screen::GetNativeScreen()->GetPrimaryDisplay(); | |
108 const float display_density = display.device_scale_factor(); | |
109 const ScaleFactor closest = FindClosestScaleFactorUnsafe(display_density); | |
110 if (closest != SCALE_FACTOR_100P) | |
111 supported_scale_factors->push_back(closest); | |
112 #elif defined(OS_IOS) | |
113 gfx::Display display = gfx::Screen::GetNativeScreen()->GetPrimaryDisplay(); | |
114 if (display.device_scale_factor() > 1.0) { | |
115 DCHECK_EQ(2.0, display.device_scale_factor()); | |
116 supported_scale_factors->push_back(SCALE_FACTOR_200P); | |
117 } else { | |
118 supported_scale_factors->push_back(SCALE_FACTOR_100P); | |
119 } | |
120 #elif defined(OS_MACOSX) | |
121 if (base::mac::IsOSLionOrLater()) | |
122 supported_scale_factors->push_back(SCALE_FACTOR_200P); | |
123 #elif defined(OS_WIN) | |
124 // Have high-DPI resources for 140% and 180% scaling on Windows based on | |
125 // default scaling for Metro mode. Round to nearest supported scale in | |
126 // all cases. | |
127 if (gfx::IsInHighDPIMode()) { | |
128 supported_scale_factors->push_back(SCALE_FACTOR_140P); | |
129 supported_scale_factors->push_back(SCALE_FACTOR_180P); | |
130 } | |
131 #elif defined(OS_CHROMEOS) | |
132 // TODO(oshima): Include 200P only if the device support 200P | |
133 supported_scale_factors->push_back(SCALE_FACTOR_200P); | |
134 #endif | |
135 std::sort(supported_scale_factors->begin(), | |
136 supported_scale_factors->end(), | |
137 ScaleFactorComparator); | |
138 } | |
139 return *supported_scale_factors; | |
140 } | |
141 | |
142 } // namespace | 69 } // namespace |
143 | 70 |
144 DisplayLayout GetDisplayLayout() { | 71 DisplayLayout GetDisplayLayout() { |
145 #if defined(OS_WIN) | 72 #if defined(OS_WIN) |
146 if (UseTouchOptimizedUI()) | 73 if (UseTouchOptimizedUI()) |
147 return LAYOUT_TOUCH; | 74 return LAYOUT_TOUCH; |
148 #endif | 75 #endif |
149 return LAYOUT_DESKTOP; | 76 return LAYOUT_DESKTOP; |
150 } | 77 } |
151 | 78 |
152 ScaleFactor GetScaleFactorFromScale(float scale) { | 79 void SetSupportedScaleFactors( |
| 80 const std::vector<ui::ScaleFactor>& scale_factors) { |
| 81 if (g_supported_scale_factors != NULL) |
| 82 delete g_supported_scale_factors; |
| 83 |
| 84 g_supported_scale_factors = new std::vector<ScaleFactor>(scale_factors); |
| 85 std::sort(g_supported_scale_factors->begin(), |
| 86 g_supported_scale_factors->end(), |
| 87 ScaleFactorComparator); |
| 88 |
| 89 // Set ImageSkia's supported scales. |
| 90 std::vector<float> scales; |
| 91 for (std::vector<ScaleFactor>::const_iterator it = |
| 92 g_supported_scale_factors->begin(); |
| 93 it != g_supported_scale_factors->end(); ++it) { |
| 94 scales.push_back(GetImageScale(*it)); |
| 95 } |
| 96 gfx::ImageSkia::SetSupportedScales(scales); |
| 97 } |
| 98 |
| 99 const std::vector<ScaleFactor>& GetSupportedScaleFactors() { |
| 100 DCHECK(g_supported_scale_factors != NULL); |
| 101 return *g_supported_scale_factors; |
| 102 } |
| 103 |
| 104 ScaleFactor GetSupportedScaleFactor(float scale) { |
| 105 DCHECK(g_supported_scale_factors != NULL); |
153 ScaleFactor closest_match = SCALE_FACTOR_100P; | 106 ScaleFactor closest_match = SCALE_FACTOR_100P; |
154 float smallest_diff = std::numeric_limits<float>::max(); | 107 float smallest_diff = std::numeric_limits<float>::max(); |
155 const std::vector<ScaleFactor>& supported = | 108 for (size_t i = 0; i < g_supported_scale_factors->size(); ++i) { |
156 GetSupportedScaleFactorsInternal(); | 109 ScaleFactor scale_factor = (*g_supported_scale_factors)[i]; |
157 for (size_t i = 0; i < supported.size(); ++i) { | |
158 ScaleFactor scale_factor = supported[i]; | |
159 float diff = std::abs(kScaleFactorScales[scale_factor] - scale); | 110 float diff = std::abs(kScaleFactorScales[scale_factor] - scale); |
160 if (diff < smallest_diff) { | 111 if (diff < smallest_diff) { |
161 closest_match = scale_factor; | 112 closest_match = scale_factor; |
162 smallest_diff = diff; | 113 smallest_diff = diff; |
163 } | 114 } |
164 } | 115 } |
165 DCHECK_NE(closest_match, SCALE_FACTOR_NONE); | 116 DCHECK_NE(closest_match, SCALE_FACTOR_NONE); |
166 return closest_match; | 117 return closest_match; |
167 } | 118 } |
168 | 119 |
169 float GetScaleFactorScale(ScaleFactor scale_factor) { | 120 float GetImageScale(ScaleFactor scale_factor) { |
170 return kScaleFactorScales[scale_factor]; | 121 return kScaleFactorScales[scale_factor]; |
171 } | 122 } |
172 | 123 |
173 ScaleFactor GetMaxScaleFactor() { | 124 bool IsScaleFactorSupported(ScaleFactor scale_factor) { |
174 #if defined(OS_CHROMEOS) | 125 DCHECK(g_supported_scale_factors != NULL); |
175 return ResourceBundle::GetSharedInstance().max_scale_factor(); | 126 return std::find(g_supported_scale_factors->begin(), |
176 #else | 127 g_supported_scale_factors->end(), |
177 return GetSupportedScaleFactorsInternal().back(); | 128 scale_factor) != g_supported_scale_factors->end(); |
178 #endif | |
179 } | 129 } |
180 | 130 |
181 std::vector<ScaleFactor> GetSupportedScaleFactors() { | 131 // Returns the scale factor closest to |scale| from the full list of factors. |
182 return GetSupportedScaleFactorsInternal(); | 132 // Note that it does NOT rely on the list of supported scale factors. |
183 } | 133 // Finding the closest match is inefficient and shouldn't be done frequently. |
184 | 134 ScaleFactor FindClosestScaleFactorUnsafe(float scale) { |
185 bool IsScaleFactorSupported(ScaleFactor scale_factor) { | 135 float smallest_diff = std::numeric_limits<float>::max(); |
186 const std::vector<ScaleFactor>& supported = | 136 ScaleFactor closest_match = SCALE_FACTOR_100P; |
187 GetSupportedScaleFactorsInternal(); | 137 for (int i = SCALE_FACTOR_100P; i < NUM_SCALE_FACTORS; ++i) { |
188 return std::find(supported.begin(), supported.end(), scale_factor) != | 138 const ScaleFactor scale_factor = static_cast<ScaleFactor>(i); |
189 supported.end(); | 139 float diff = std::abs(kScaleFactorScales[scale_factor] - scale); |
| 140 if (diff < smallest_diff) { |
| 141 closest_match = scale_factor; |
| 142 smallest_diff = diff; |
| 143 } |
| 144 } |
| 145 return closest_match; |
190 } | 146 } |
191 | 147 |
192 namespace test { | 148 namespace test { |
193 | 149 |
194 void SetSupportedScaleFactors( | |
195 const std::vector<ui::ScaleFactor>& scale_factors) { | |
196 std::vector<ui::ScaleFactor>& supported_scale_factors = | |
197 GetSupportedScaleFactorsInternal(); | |
198 supported_scale_factors = scale_factors; | |
199 std::sort(supported_scale_factors.begin(), | |
200 supported_scale_factors.end(), | |
201 ScaleFactorComparator); | |
202 } | |
203 | |
204 ScopedSetSupportedScaleFactors::ScopedSetSupportedScaleFactors( | 150 ScopedSetSupportedScaleFactors::ScopedSetSupportedScaleFactors( |
205 const std::vector<ui::ScaleFactor>& new_scale_factors) | 151 const std::vector<ui::ScaleFactor>& new_scale_factors) { |
206 : original_scale_factors_(GetSupportedScaleFactors()) { | 152 if (g_supported_scale_factors) { |
| 153 original_scale_factors_ = |
| 154 new std::vector<ScaleFactor>(*g_supported_scale_factors); |
| 155 } else { |
| 156 original_scale_factors_ = NULL; |
| 157 } |
207 SetSupportedScaleFactors(new_scale_factors); | 158 SetSupportedScaleFactors(new_scale_factors); |
208 } | 159 } |
209 | 160 |
210 ScopedSetSupportedScaleFactors::~ScopedSetSupportedScaleFactors() { | 161 ScopedSetSupportedScaleFactors::~ScopedSetSupportedScaleFactors() { |
211 SetSupportedScaleFactors(original_scale_factors_); | 162 if (original_scale_factors_) { |
| 163 SetSupportedScaleFactors(*original_scale_factors_); |
| 164 delete original_scale_factors_; |
| 165 } else { |
| 166 delete g_supported_scale_factors; |
| 167 g_supported_scale_factors = NULL; |
| 168 } |
212 } | 169 } |
213 | 170 |
214 } // namespace test | 171 } // namespace test |
215 | 172 |
216 #if !defined(OS_MACOSX) | 173 #if !defined(OS_MACOSX) |
217 ScaleFactor GetScaleFactorForNativeView(gfx::NativeView view) { | 174 ScaleFactor GetScaleFactorForNativeView(gfx::NativeView view) { |
218 gfx::Screen* screen = gfx::Screen::GetScreenFor(view); | 175 gfx::Screen* screen = gfx::Screen::GetScreenFor(view); |
219 if (screen->IsDIPEnabled()) { | 176 if (screen->IsDIPEnabled()) { |
220 gfx::Display display = screen->GetDisplayNearestWindow(view); | 177 gfx::Display display = screen->GetDisplayNearestWindow(view); |
221 return GetScaleFactorFromScale(display.device_scale_factor()); | 178 return GetSupportedScaleFactor(display.device_scale_factor()); |
222 } | 179 } |
223 return ui::SCALE_FACTOR_100P; | 180 return ui::SCALE_FACTOR_100P; |
224 } | 181 } |
225 #endif // !defined(OS_MACOSX) | 182 #endif // !defined(OS_MACOSX) |
226 | 183 |
227 } // namespace ui | 184 } // namespace ui |
OLD | NEW |