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" | |
19 #include "ui/gfx/screen.h" | 18 #include "ui/gfx/screen.h" |
20 | 19 |
| 20 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 21 #include "base/mac/mac_util.h" |
| 22 #endif |
| 23 |
21 #if defined(OS_WIN) | 24 #if defined(OS_WIN) |
22 #include "base/win/metro.h" | 25 #include "base/win/metro.h" |
| 26 #include "ui/gfx/win/dpi.h" |
23 #include <Windows.h> | 27 #include <Windows.h> |
24 #endif // defined(OS_WIN) | 28 #endif // defined(OS_WIN) |
25 | 29 |
| 30 #if defined(OS_CHROMEOS) |
| 31 #include "ui/base/resource/resource_bundle.h" |
| 32 #endif |
| 33 |
26 namespace ui { | 34 namespace ui { |
27 | 35 |
28 namespace { | 36 namespace { |
29 | 37 |
30 bool ScaleFactorComparator(const ScaleFactor& lhs, const ScaleFactor& rhs){ | 38 bool ScaleFactorComparator(const ScaleFactor& lhs, const ScaleFactor& rhs){ |
31 return GetImageScale(lhs) < GetImageScale(rhs); | 39 return GetScaleFactorScale(lhs) < GetScaleFactorScale(rhs); |
32 } | 40 } |
33 | 41 |
34 std::vector<ScaleFactor>* g_supported_scale_factors = NULL; | |
35 | |
36 #if defined(OS_WIN) | 42 #if defined(OS_WIN) |
37 // Helper function that determines whether we want to optimize the UI for touch. | 43 // Helper function that determines whether we want to optimize the UI for touch. |
38 bool UseTouchOptimizedUI() { | 44 bool UseTouchOptimizedUI() { |
39 // If --touch-optimized-ui is specified and not set to "auto", then override | 45 // If --touch-optimized-ui is specified and not set to "auto", then override |
40 // the hardware-determined setting (eg. for testing purposes). | 46 // the hardware-determined setting (eg. for testing purposes). |
41 static bool has_touch_optimized_ui = CommandLine::ForCurrentProcess()-> | 47 static bool has_touch_optimized_ui = CommandLine::ForCurrentProcess()-> |
42 HasSwitch(switches::kTouchOptimizedUI); | 48 HasSwitch(switches::kTouchOptimizedUI); |
43 if (has_touch_optimized_ui) { | 49 if (has_touch_optimized_ui) { |
44 const std::string switch_value = CommandLine::ForCurrentProcess()-> | 50 const std::string switch_value = CommandLine::ForCurrentProcess()-> |
45 GetSwitchValueASCII(switches::kTouchOptimizedUI); | 51 GetSwitchValueASCII(switches::kTouchOptimizedUI); |
(...skipping 13 matching lines...) Expand all Loading... |
59 return base::win::IsMetroProcess() && ui::IsTouchDevicePresent(); | 65 return base::win::IsMetroProcess() && ui::IsTouchDevicePresent(); |
60 } | 66 } |
61 #endif // defined(OS_WIN) | 67 #endif // defined(OS_WIN) |
62 | 68 |
63 const float kScaleFactorScales[] = {1.0f, 1.0f, 1.25f, 1.33f, 1.4f, 1.5f, 1.8f, | 69 const float kScaleFactorScales[] = {1.0f, 1.0f, 1.25f, 1.33f, 1.4f, 1.5f, 1.8f, |
64 2.0f}; | 70 2.0f}; |
65 COMPILE_ASSERT(NUM_SCALE_FACTORS == arraysize(kScaleFactorScales), | 71 COMPILE_ASSERT(NUM_SCALE_FACTORS == arraysize(kScaleFactorScales), |
66 kScaleFactorScales_incorrect_size); | 72 kScaleFactorScales_incorrect_size); |
67 const size_t kScaleFactorScalesLength = arraysize(kScaleFactorScales); | 73 const size_t kScaleFactorScalesLength = arraysize(kScaleFactorScales); |
68 | 74 |
| 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 |
69 } // namespace | 142 } // namespace |
70 | 143 |
71 DisplayLayout GetDisplayLayout() { | 144 DisplayLayout GetDisplayLayout() { |
72 #if defined(OS_WIN) | 145 #if defined(OS_WIN) |
73 if (UseTouchOptimizedUI()) | 146 if (UseTouchOptimizedUI()) |
74 return LAYOUT_TOUCH; | 147 return LAYOUT_TOUCH; |
75 #endif | 148 #endif |
76 return LAYOUT_DESKTOP; | 149 return LAYOUT_DESKTOP; |
77 } | 150 } |
78 | 151 |
79 void SetSupportedScaleFactors( | 152 ScaleFactor GetScaleFactorFromScale(float scale) { |
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); | |
106 ScaleFactor closest_match = SCALE_FACTOR_100P; | 153 ScaleFactor closest_match = SCALE_FACTOR_100P; |
107 float smallest_diff = std::numeric_limits<float>::max(); | 154 float smallest_diff = std::numeric_limits<float>::max(); |
108 for (size_t i = 0; i < g_supported_scale_factors->size(); ++i) { | 155 const std::vector<ScaleFactor>& supported = |
109 ScaleFactor scale_factor = (*g_supported_scale_factors)[i]; | 156 GetSupportedScaleFactorsInternal(); |
| 157 for (size_t i = 0; i < supported.size(); ++i) { |
| 158 ScaleFactor scale_factor = supported[i]; |
110 float diff = std::abs(kScaleFactorScales[scale_factor] - scale); | 159 float diff = std::abs(kScaleFactorScales[scale_factor] - scale); |
111 if (diff < smallest_diff) { | 160 if (diff < smallest_diff) { |
112 closest_match = scale_factor; | 161 closest_match = scale_factor; |
113 smallest_diff = diff; | 162 smallest_diff = diff; |
114 } | 163 } |
115 } | 164 } |
116 DCHECK_NE(closest_match, SCALE_FACTOR_NONE); | 165 DCHECK_NE(closest_match, SCALE_FACTOR_NONE); |
117 return closest_match; | 166 return closest_match; |
118 } | 167 } |
119 | 168 |
120 float GetImageScale(ScaleFactor scale_factor) { | 169 float GetScaleFactorScale(ScaleFactor scale_factor) { |
121 return kScaleFactorScales[scale_factor]; | 170 return kScaleFactorScales[scale_factor]; |
122 } | 171 } |
123 | 172 |
124 bool IsScaleFactorSupported(ScaleFactor scale_factor) { | 173 ScaleFactor GetMaxScaleFactor() { |
125 DCHECK(g_supported_scale_factors != NULL); | 174 #if defined(OS_CHROMEOS) |
126 return std::find(g_supported_scale_factors->begin(), | 175 return ResourceBundle::GetSharedInstance().max_scale_factor(); |
127 g_supported_scale_factors->end(), | 176 #else |
128 scale_factor) != g_supported_scale_factors->end(); | 177 return GetSupportedScaleFactorsInternal().back(); |
| 178 #endif |
129 } | 179 } |
130 | 180 |
131 // Returns the scale factor closest to |scale| from the full list of factors. | 181 std::vector<ScaleFactor> GetSupportedScaleFactors() { |
132 // Note that it does NOT rely on the list of supported scale factors. | 182 return GetSupportedScaleFactorsInternal(); |
133 // Finding the closest match is inefficient and shouldn't be done frequently. | 183 } |
134 ScaleFactor FindClosestScaleFactorUnsafe(float scale) { | 184 |
135 float smallest_diff = std::numeric_limits<float>::max(); | 185 bool IsScaleFactorSupported(ScaleFactor scale_factor) { |
136 ScaleFactor closest_match = SCALE_FACTOR_100P; | 186 const std::vector<ScaleFactor>& supported = |
137 for (int i = SCALE_FACTOR_100P; i < NUM_SCALE_FACTORS; ++i) { | 187 GetSupportedScaleFactorsInternal(); |
138 const ScaleFactor scale_factor = static_cast<ScaleFactor>(i); | 188 return std::find(supported.begin(), supported.end(), scale_factor) != |
139 float diff = std::abs(kScaleFactorScales[scale_factor] - scale); | 189 supported.end(); |
140 if (diff < smallest_diff) { | |
141 closest_match = scale_factor; | |
142 smallest_diff = diff; | |
143 } | |
144 } | |
145 return closest_match; | |
146 } | 190 } |
147 | 191 |
148 namespace test { | 192 namespace test { |
149 | 193 |
| 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 |
150 ScopedSetSupportedScaleFactors::ScopedSetSupportedScaleFactors( | 204 ScopedSetSupportedScaleFactors::ScopedSetSupportedScaleFactors( |
151 const std::vector<ui::ScaleFactor>& new_scale_factors) { | 205 const std::vector<ui::ScaleFactor>& new_scale_factors) |
152 if (g_supported_scale_factors) { | 206 : original_scale_factors_(GetSupportedScaleFactors()) { |
153 original_scale_factors_ = | |
154 new std::vector<ScaleFactor>(*g_supported_scale_factors); | |
155 } else { | |
156 original_scale_factors_ = NULL; | |
157 } | |
158 SetSupportedScaleFactors(new_scale_factors); | 207 SetSupportedScaleFactors(new_scale_factors); |
159 } | 208 } |
160 | 209 |
161 ScopedSetSupportedScaleFactors::~ScopedSetSupportedScaleFactors() { | 210 ScopedSetSupportedScaleFactors::~ScopedSetSupportedScaleFactors() { |
162 if (original_scale_factors_) { | 211 SetSupportedScaleFactors(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 } | |
169 } | 212 } |
170 | 213 |
171 } // namespace test | 214 } // namespace test |
172 | 215 |
173 #if !defined(OS_MACOSX) | 216 #if !defined(OS_MACOSX) |
174 ScaleFactor GetScaleFactorForNativeView(gfx::NativeView view) { | 217 ScaleFactor GetScaleFactorForNativeView(gfx::NativeView view) { |
175 gfx::Screen* screen = gfx::Screen::GetScreenFor(view); | 218 gfx::Screen* screen = gfx::Screen::GetScreenFor(view); |
176 if (screen->IsDIPEnabled()) { | 219 if (screen->IsDIPEnabled()) { |
177 gfx::Display display = screen->GetDisplayNearestWindow(view); | 220 gfx::Display display = screen->GetDisplayNearestWindow(view); |
178 return GetSupportedScaleFactor(display.device_scale_factor()); | 221 return GetScaleFactorFromScale(display.device_scale_factor()); |
179 } | 222 } |
180 return ui::SCALE_FACTOR_100P; | 223 return ui::SCALE_FACTOR_100P; |
181 } | 224 } |
182 #endif // !defined(OS_MACOSX) | 225 #endif // !defined(OS_MACOSX) |
183 | 226 |
184 } // namespace ui | 227 } // namespace ui |
OLD | NEW |