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