Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / base / layout.cc
blobe49c24bed9b7802c22dd0776e3e956e8997d5f4a
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 "ui/gfx/win/dpi.h"
24 #include <Windows.h>
25 #endif // defined(OS_WIN)
27 namespace ui {
29 namespace {
31 std::vector<ScaleFactor>* g_supported_scale_factors = NULL;
33 const float kScaleFactorScales[] = {1.0f, 1.0f, 1.25f, 1.33f, 1.4f, 1.5f, 1.8f,
34 2.0f, 2.5f, 3.0f};
35 static_assert(NUM_SCALE_FACTORS == arraysize(kScaleFactorScales),
36 "kScaleFactorScales has incorrect size");
38 } // namespace
40 void SetSupportedScaleFactors(
41 const std::vector<ui::ScaleFactor>& scale_factors) {
42 if (g_supported_scale_factors != NULL)
43 delete g_supported_scale_factors;
45 g_supported_scale_factors = new std::vector<ScaleFactor>(scale_factors);
46 std::sort(g_supported_scale_factors->begin(),
47 g_supported_scale_factors->end(),
48 [](ScaleFactor lhs, ScaleFactor rhs) {
49 return GetScaleForScaleFactor(lhs) < GetScaleForScaleFactor(rhs);
50 });
52 // Set ImageSkia's supported scales.
53 std::vector<float> scales;
54 for (std::vector<ScaleFactor>::const_iterator it =
55 g_supported_scale_factors->begin();
56 it != g_supported_scale_factors->end(); ++it) {
57 scales.push_back(kScaleFactorScales[*it]);
59 gfx::ImageSkia::SetSupportedScales(scales);
62 const std::vector<ScaleFactor>& GetSupportedScaleFactors() {
63 DCHECK(g_supported_scale_factors != NULL);
64 return *g_supported_scale_factors;
67 ScaleFactor GetSupportedScaleFactor(float scale) {
68 DCHECK(g_supported_scale_factors != NULL);
69 ScaleFactor closest_match = SCALE_FACTOR_100P;
70 float smallest_diff = std::numeric_limits<float>::max();
71 for (size_t i = 0; i < g_supported_scale_factors->size(); ++i) {
72 ScaleFactor scale_factor = (*g_supported_scale_factors)[i];
73 float diff = std::abs(kScaleFactorScales[scale_factor] - scale);
74 if (diff < smallest_diff) {
75 closest_match = scale_factor;
76 smallest_diff = diff;
79 DCHECK_NE(closest_match, SCALE_FACTOR_NONE);
80 return closest_match;
83 float GetImageScale(ScaleFactor scale_factor) {
84 #if defined(OS_WIN)
85 return gfx::GetDPIScale();
86 #else
87 return GetScaleForScaleFactor(scale_factor);
88 #endif
91 float GetScaleForScaleFactor(ScaleFactor scale_factor) {
92 return kScaleFactorScales[scale_factor];
95 bool IsSupportedScale(float scale) {
96 for (auto scale_factor_idx : *g_supported_scale_factors) {
97 if (kScaleFactorScales[scale_factor_idx] == scale)
98 return true;
100 return false;
103 namespace test {
105 ScopedSetSupportedScaleFactors::ScopedSetSupportedScaleFactors(
106 const std::vector<ui::ScaleFactor>& new_scale_factors) {
107 if (g_supported_scale_factors) {
108 original_scale_factors_ =
109 new std::vector<ScaleFactor>(*g_supported_scale_factors);
110 } else {
111 original_scale_factors_ = NULL;
113 SetSupportedScaleFactors(new_scale_factors);
116 ScopedSetSupportedScaleFactors::~ScopedSetSupportedScaleFactors() {
117 if (original_scale_factors_) {
118 SetSupportedScaleFactors(*original_scale_factors_);
119 delete original_scale_factors_;
120 } else {
121 delete g_supported_scale_factors;
122 g_supported_scale_factors = NULL;
126 } // namespace test
128 #if !defined(OS_MACOSX)
129 float GetScaleFactorForNativeView(gfx::NativeView view) {
130 gfx::Screen* screen = gfx::Screen::GetScreenFor(view);
131 gfx::Display display = screen->GetDisplayNearestWindow(view);
132 return display.device_scale_factor();
134 #endif // !defined(OS_MACOSX)
136 } // namespace ui