Roll icu to 241739 and drop a version from icudt*dat
[chromium-blink-merge.git] / ui / base / layout.cc
blob2c1fb51a3952706e1f6d77a0b2f29a46c2508124
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
3 // found in the LICENSE file.
5 #include "ui/base/layout.h"
7 #include <algorithm>
8 #include <cmath>
9 #include <limits>
11 #include "base/basictypes.h"
12 #include "base/command_line.h"
13 #include "base/logging.h"
14 #include "build/build_config.h"
15 #include "ui/base/touch/touch_device.h"
16 #include "ui/base/ui_base_switches.h"
17 #include "ui/gfx/display.h"
18 #include "ui/gfx/image/image_skia.h"
19 #include "ui/gfx/screen.h"
21 #if defined(OS_WIN)
22 #include "base/win/metro.h"
23 #include <Windows.h>
24 #endif // defined(OS_WIN)
26 namespace ui {
28 namespace {
30 bool ScaleFactorComparator(const ScaleFactor& lhs, const ScaleFactor& rhs){
31 return GetImageScale(lhs) < GetImageScale(rhs);
34 std::vector<ScaleFactor>* g_supported_scale_factors = NULL;
36 #if defined(OS_WIN)
37 // Helper function that determines whether we want to optimize the UI for touch.
38 bool UseTouchOptimizedUI() {
39 // If --touch-optimized-ui is specified and not set to "auto", then override
40 // the hardware-determined setting (eg. for testing purposes).
41 static bool has_touch_optimized_ui = CommandLine::ForCurrentProcess()->
42 HasSwitch(switches::kTouchOptimizedUI);
43 if (has_touch_optimized_ui) {
44 const std::string switch_value = CommandLine::ForCurrentProcess()->
45 GetSwitchValueASCII(switches::kTouchOptimizedUI);
47 // Note that simply specifying the switch is the same as enabled.
48 if (switch_value.empty() ||
49 switch_value == switches::kTouchOptimizedUIEnabled) {
50 return true;
51 } else if (switch_value == switches::kTouchOptimizedUIDisabled) {
52 return false;
53 } else if (switch_value != switches::kTouchOptimizedUIAuto) {
54 LOG(ERROR) << "Invalid --touch-optimized-ui option: " << switch_value;
58 // We use the touch layout only when we are running in Metro mode.
59 return base::win::IsMetroProcess() && ui::IsTouchDevicePresent();
61 #endif // defined(OS_WIN)
63 const float kScaleFactorScales[] = {1.0f, 1.0f, 1.25f, 1.33f, 1.4f, 1.5f, 1.8f,
64 2.0f, 3.0f};
65 COMPILE_ASSERT(NUM_SCALE_FACTORS == arraysize(kScaleFactorScales),
66 kScaleFactorScales_incorrect_size);
68 } // namespace
70 DisplayLayout GetDisplayLayout() {
71 #if defined(OS_WIN)
72 if (UseTouchOptimizedUI())
73 return LAYOUT_TOUCH;
74 #endif
75 return LAYOUT_DESKTOP;
78 void SetSupportedScaleFactors(
79 const std::vector<ui::ScaleFactor>& scale_factors) {
80 if (g_supported_scale_factors != NULL)
81 delete g_supported_scale_factors;
83 g_supported_scale_factors = new std::vector<ScaleFactor>(scale_factors);
84 std::sort(g_supported_scale_factors->begin(),
85 g_supported_scale_factors->end(),
86 ScaleFactorComparator);
88 // Set ImageSkia's supported scales.
89 std::vector<float> scales;
90 for (std::vector<ScaleFactor>::const_iterator it =
91 g_supported_scale_factors->begin();
92 it != g_supported_scale_factors->end(); ++it) {
93 scales.push_back(GetImageScale(*it));
95 gfx::ImageSkia::SetSupportedScales(scales);
98 const std::vector<ScaleFactor>& GetSupportedScaleFactors() {
99 DCHECK(g_supported_scale_factors != NULL);
100 return *g_supported_scale_factors;
103 ScaleFactor GetSupportedScaleFactor(float scale) {
104 DCHECK(g_supported_scale_factors != NULL);
105 ScaleFactor closest_match = SCALE_FACTOR_100P;
106 float smallest_diff = std::numeric_limits<float>::max();
107 for (size_t i = 0; i < g_supported_scale_factors->size(); ++i) {
108 ScaleFactor scale_factor = (*g_supported_scale_factors)[i];
109 float diff = std::abs(kScaleFactorScales[scale_factor] - scale);
110 if (diff < smallest_diff) {
111 closest_match = scale_factor;
112 smallest_diff = diff;
115 DCHECK_NE(closest_match, SCALE_FACTOR_NONE);
116 return closest_match;
119 float GetImageScale(ScaleFactor scale_factor) {
120 return kScaleFactorScales[scale_factor];
123 bool IsScaleFactorSupported(ScaleFactor scale_factor) {
124 DCHECK(g_supported_scale_factors != NULL);
125 return std::find(g_supported_scale_factors->begin(),
126 g_supported_scale_factors->end(),
127 scale_factor) != g_supported_scale_factors->end();
130 // Returns the scale factor closest to |scale| from the full list of factors.
131 // Note that it does NOT rely on the list of supported scale factors.
132 // Finding the closest match is inefficient and shouldn't be done frequently.
133 ScaleFactor FindClosestScaleFactorUnsafe(float scale) {
134 float smallest_diff = std::numeric_limits<float>::max();
135 ScaleFactor closest_match = SCALE_FACTOR_100P;
136 for (int i = SCALE_FACTOR_100P; i < NUM_SCALE_FACTORS; ++i) {
137 const ScaleFactor scale_factor = static_cast<ScaleFactor>(i);
138 float diff = std::abs(kScaleFactorScales[scale_factor] - scale);
139 if (diff < smallest_diff) {
140 closest_match = scale_factor;
141 smallest_diff = diff;
144 return closest_match;
147 namespace test {
149 ScopedSetSupportedScaleFactors::ScopedSetSupportedScaleFactors(
150 const std::vector<ui::ScaleFactor>& new_scale_factors) {
151 if (g_supported_scale_factors) {
152 original_scale_factors_ =
153 new std::vector<ScaleFactor>(*g_supported_scale_factors);
154 } else {
155 original_scale_factors_ = NULL;
157 SetSupportedScaleFactors(new_scale_factors);
160 ScopedSetSupportedScaleFactors::~ScopedSetSupportedScaleFactors() {
161 if (original_scale_factors_) {
162 SetSupportedScaleFactors(*original_scale_factors_);
163 delete original_scale_factors_;
164 } else {
165 delete g_supported_scale_factors;
166 g_supported_scale_factors = NULL;
170 } // namespace test
172 #if !defined(OS_MACOSX)
173 ScaleFactor GetScaleFactorForNativeView(gfx::NativeView view) {
174 gfx::Screen* screen = gfx::Screen::GetScreenFor(view);
175 if (screen->IsDIPEnabled()) {
176 gfx::Display display = screen->GetDisplayNearestWindow(view);
177 return GetSupportedScaleFactor(display.device_scale_factor());
179 return ui::SCALE_FACTOR_100P;
181 #endif // !defined(OS_MACOSX)
183 } // namespace ui