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/image/image_skia.h" |
19 #include "ui/gfx/screen.h" | 19 #include "ui/gfx/screen.h" |
20 | 20 |
21 #if defined(OS_WIN) | 21 #if defined(OS_WIN) |
22 #include "base/win/metro.h" | 22 #include "base/win/metro.h" |
| 23 #include "ui/gfx/win/dpi.h" |
23 #include <Windows.h> | 24 #include <Windows.h> |
24 #endif // defined(OS_WIN) | 25 #endif // defined(OS_WIN) |
25 | 26 |
26 namespace ui { | 27 namespace ui { |
27 | 28 |
28 namespace { | 29 namespace { |
29 | 30 |
30 bool ScaleFactorComparator(const ScaleFactor& lhs, const ScaleFactor& rhs){ | 31 bool ScaleFactorComparator(const ScaleFactor& lhs, const ScaleFactor& rhs){ |
31 return GetImageScale(lhs) < GetImageScale(rhs); | 32 return GetImageScale(lhs) < GetImageScale(rhs); |
32 } | 33 } |
(...skipping 20 matching lines...) Expand all Loading... |
53 } else if (switch_value != switches::kTouchOptimizedUIAuto) { | 54 } else if (switch_value != switches::kTouchOptimizedUIAuto) { |
54 LOG(ERROR) << "Invalid --touch-optimized-ui option: " << switch_value; | 55 LOG(ERROR) << "Invalid --touch-optimized-ui option: " << switch_value; |
55 } | 56 } |
56 } | 57 } |
57 | 58 |
58 // We use the touch layout only when we are running in Metro mode. | 59 // We use the touch layout only when we are running in Metro mode. |
59 return base::win::IsMetroProcess() && ui::IsTouchDevicePresent(); | 60 return base::win::IsMetroProcess() && ui::IsTouchDevicePresent(); |
60 } | 61 } |
61 #endif // defined(OS_WIN) | 62 #endif // defined(OS_WIN) |
62 | 63 |
63 const float kScaleFactorScales[] = {1.0f, 1.0f, 1.25f, 1.33f, 1.4f, 1.5f, 1.8f, | 64 } // namespace |
64 2.0f, 3.0f}; | 65 |
65 COMPILE_ASSERT(NUM_SCALE_FACTORS == arraysize(kScaleFactorScales), | 66 float g_kScaleFactorScales[] = {1.0f, 1.0f, 1.25f, 1.33f, 1.4f, 1.5f, 1.8f, |
| 67 2.0f, 3.0f}; |
| 68 COMPILE_ASSERT(NUM_SCALE_FACTORS == arraysize(g_kScaleFactorScales), |
66 kScaleFactorScales_incorrect_size); | 69 kScaleFactorScales_incorrect_size); |
67 | 70 |
68 } // namespace | |
69 | |
70 DisplayLayout GetDisplayLayout() { | 71 DisplayLayout GetDisplayLayout() { |
71 #if defined(OS_WIN) | 72 #if defined(OS_WIN) |
72 if (UseTouchOptimizedUI()) | 73 if (UseTouchOptimizedUI()) |
73 return LAYOUT_TOUCH; | 74 return LAYOUT_TOUCH; |
74 #endif | 75 #endif |
75 return LAYOUT_DESKTOP; | 76 return LAYOUT_DESKTOP; |
76 } | 77 } |
77 | 78 |
78 void SetSupportedScaleFactors( | 79 void SetSupportedScaleFactors( |
79 const std::vector<ui::ScaleFactor>& scale_factors) { | 80 const std::vector<ui::ScaleFactor>& scale_factors) { |
80 if (g_supported_scale_factors != NULL) | 81 if (g_supported_scale_factors != NULL) |
81 delete g_supported_scale_factors; | 82 delete g_supported_scale_factors; |
82 | 83 |
83 g_supported_scale_factors = new std::vector<ScaleFactor>(scale_factors); | 84 g_supported_scale_factors = new std::vector<ScaleFactor>(scale_factors); |
84 std::sort(g_supported_scale_factors->begin(), | 85 std::sort(g_supported_scale_factors->begin(), |
85 g_supported_scale_factors->end(), | 86 g_supported_scale_factors->end(), |
86 ScaleFactorComparator); | 87 ScaleFactorComparator); |
87 | 88 |
88 // Set ImageSkia's supported scales. | 89 // Set ImageSkia's supported scales. |
89 std::vector<float> scales; | 90 std::vector<float> scales; |
90 for (std::vector<ScaleFactor>::const_iterator it = | 91 for (std::vector<ScaleFactor>::const_iterator it = |
91 g_supported_scale_factors->begin(); | 92 g_supported_scale_factors->begin(); |
92 it != g_supported_scale_factors->end(); ++it) { | 93 it != g_supported_scale_factors->end(); ++it) { |
93 scales.push_back(GetImageScale(*it)); | 94 scales.push_back(g_kScaleFactorScales[*it]); |
94 } | 95 } |
95 gfx::ImageSkia::SetSupportedScales(scales); | 96 gfx::ImageSkia::SetSupportedScales(scales); |
96 } | 97 } |
97 | 98 |
98 const std::vector<ScaleFactor>& GetSupportedScaleFactors() { | 99 const std::vector<ScaleFactor>& GetSupportedScaleFactors() { |
99 DCHECK(g_supported_scale_factors != NULL); | 100 DCHECK(g_supported_scale_factors != NULL); |
100 return *g_supported_scale_factors; | 101 return *g_supported_scale_factors; |
101 } | 102 } |
102 | 103 |
103 ScaleFactor GetSupportedScaleFactor(float scale) { | 104 ScaleFactor GetSupportedScaleFactor(float scale) { |
104 DCHECK(g_supported_scale_factors != NULL); | 105 DCHECK(g_supported_scale_factors != NULL); |
105 ScaleFactor closest_match = SCALE_FACTOR_100P; | 106 ScaleFactor closest_match = SCALE_FACTOR_100P; |
106 float smallest_diff = std::numeric_limits<float>::max(); | 107 float smallest_diff = std::numeric_limits<float>::max(); |
107 for (size_t i = 0; i < g_supported_scale_factors->size(); ++i) { | 108 for (size_t i = 0; i < g_supported_scale_factors->size(); ++i) { |
108 ScaleFactor scale_factor = (*g_supported_scale_factors)[i]; | 109 ScaleFactor scale_factor = (*g_supported_scale_factors)[i]; |
109 float diff = std::abs(kScaleFactorScales[scale_factor] - scale); | 110 float diff = std::abs(g_kScaleFactorScales[scale_factor] - scale); |
110 if (diff < smallest_diff) { | 111 if (diff < smallest_diff) { |
111 closest_match = scale_factor; | 112 closest_match = scale_factor; |
112 smallest_diff = diff; | 113 smallest_diff = diff; |
113 } | 114 } |
114 } | 115 } |
115 DCHECK_NE(closest_match, SCALE_FACTOR_NONE); | 116 DCHECK_NE(closest_match, SCALE_FACTOR_NONE); |
116 return closest_match; | 117 return closest_match; |
117 } | 118 } |
118 | 119 |
119 float GetImageScale(ScaleFactor scale_factor) { | 120 float GetImageScale(ScaleFactor scale_factor) { |
120 return kScaleFactorScales[scale_factor]; | 121 #if defined(OS_WIN) |
| 122 if (gfx::IsHighDPIEnabled()) |
| 123 return gfx::win::GetDeviceScaleFactor(); |
| 124 #endif |
| 125 return g_kScaleFactorScales[scale_factor]; |
121 } | 126 } |
122 | 127 |
123 bool IsScaleFactorSupported(ScaleFactor scale_factor) { | 128 bool IsScaleFactorSupported(ScaleFactor scale_factor) { |
124 DCHECK(g_supported_scale_factors != NULL); | 129 DCHECK(g_supported_scale_factors != NULL); |
125 return std::find(g_supported_scale_factors->begin(), | 130 return std::find(g_supported_scale_factors->begin(), |
126 g_supported_scale_factors->end(), | 131 g_supported_scale_factors->end(), |
127 scale_factor) != g_supported_scale_factors->end(); | 132 scale_factor) != g_supported_scale_factors->end(); |
128 } | 133 } |
129 | 134 |
130 // Returns the scale factor closest to |scale| from the full list of factors. | 135 // 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. | 136 // 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. | 137 // Finding the closest match is inefficient and shouldn't be done frequently. |
133 ScaleFactor FindClosestScaleFactorUnsafe(float scale) { | 138 ScaleFactor FindClosestScaleFactorUnsafe(float scale) { |
134 float smallest_diff = std::numeric_limits<float>::max(); | 139 float smallest_diff = std::numeric_limits<float>::max(); |
135 ScaleFactor closest_match = SCALE_FACTOR_100P; | 140 ScaleFactor closest_match = SCALE_FACTOR_100P; |
136 for (int i = SCALE_FACTOR_100P; i < NUM_SCALE_FACTORS; ++i) { | 141 for (int i = SCALE_FACTOR_100P; i < NUM_SCALE_FACTORS; ++i) { |
137 const ScaleFactor scale_factor = static_cast<ScaleFactor>(i); | 142 const ScaleFactor scale_factor = static_cast<ScaleFactor>(i); |
138 float diff = std::abs(kScaleFactorScales[scale_factor] - scale); | 143 float diff = std::abs(g_kScaleFactorScales[scale_factor] - scale); |
139 if (diff < smallest_diff) { | 144 if (diff < smallest_diff) { |
140 closest_match = scale_factor; | 145 closest_match = scale_factor; |
141 smallest_diff = diff; | 146 smallest_diff = diff; |
142 } | 147 } |
143 } | 148 } |
144 return closest_match; | 149 return closest_match; |
145 } | 150 } |
146 | 151 |
147 namespace test { | 152 namespace test { |
148 | 153 |
(...skipping 25 matching lines...) Expand all Loading... |
174 gfx::Screen* screen = gfx::Screen::GetScreenFor(view); | 179 gfx::Screen* screen = gfx::Screen::GetScreenFor(view); |
175 if (screen->IsDIPEnabled()) { | 180 if (screen->IsDIPEnabled()) { |
176 gfx::Display display = screen->GetDisplayNearestWindow(view); | 181 gfx::Display display = screen->GetDisplayNearestWindow(view); |
177 return GetSupportedScaleFactor(display.device_scale_factor()); | 182 return GetSupportedScaleFactor(display.device_scale_factor()); |
178 } | 183 } |
179 return ui::SCALE_FACTOR_100P; | 184 return ui::SCALE_FACTOR_100P; |
180 } | 185 } |
181 #endif // !defined(OS_MACOSX) | 186 #endif // !defined(OS_MACOSX) |
182 | 187 |
183 } // namespace ui | 188 } // namespace ui |
OLD | NEW |