[SyncFS] Build indexes from FileTracker entries on disk.
[chromium-blink-merge.git] / ui / base / layout.cc
blobaa3aecf55bd625d891f75339c8971406370f7e2f
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 bool ScaleFactorComparator(const ScaleFactor& lhs, const ScaleFactor& rhs){
32 return GetScaleForScaleFactor(lhs) < GetScaleForScaleFactor(rhs);
35 std::vector<ScaleFactor>* g_supported_scale_factors = NULL;
37 const float kScaleFactorScales[] = {1.0f, 1.0f, 1.25f, 1.33f, 1.4f, 1.5f, 1.8f,
38 2.0f, 2.5f, 3.0f};
39 COMPILE_ASSERT(NUM_SCALE_FACTORS == arraysize(kScaleFactorScales),
40 kScaleFactorScales_incorrect_size);
42 } // namespace
44 void SetSupportedScaleFactors(
45 const std::vector<ui::ScaleFactor>& scale_factors) {
46 if (g_supported_scale_factors != NULL)
47 delete g_supported_scale_factors;
49 g_supported_scale_factors = new std::vector<ScaleFactor>(scale_factors);
50 std::sort(g_supported_scale_factors->begin(),
51 g_supported_scale_factors->end(),
52 ScaleFactorComparator);
54 // Set ImageSkia's supported scales.
55 std::vector<float> scales;
56 for (std::vector<ScaleFactor>::const_iterator it =
57 g_supported_scale_factors->begin();
58 it != g_supported_scale_factors->end(); ++it) {
59 scales.push_back(kScaleFactorScales[*it]);
61 gfx::ImageSkia::SetSupportedScales(scales);
64 const std::vector<ScaleFactor>& GetSupportedScaleFactors() {
65 DCHECK(g_supported_scale_factors != NULL);
66 return *g_supported_scale_factors;
69 ScaleFactor GetSupportedScaleFactor(float scale) {
70 DCHECK(g_supported_scale_factors != NULL);
71 ScaleFactor closest_match = SCALE_FACTOR_100P;
72 float smallest_diff = std::numeric_limits<float>::max();
73 for (size_t i = 0; i < g_supported_scale_factors->size(); ++i) {
74 ScaleFactor scale_factor = (*g_supported_scale_factors)[i];
75 float diff = std::abs(kScaleFactorScales[scale_factor] - scale);
76 if (diff < smallest_diff) {
77 closest_match = scale_factor;
78 smallest_diff = diff;
81 DCHECK_NE(closest_match, SCALE_FACTOR_NONE);
82 return closest_match;
85 float GetImageScale(ScaleFactor scale_factor) {
86 #if defined(OS_WIN)
87 if (gfx::IsHighDPIEnabled())
88 return gfx::win::GetDeviceScaleFactor();
89 #endif
90 return GetScaleForScaleFactor(scale_factor);
93 float GetScaleForScaleFactor(ScaleFactor scale_factor) {
94 return kScaleFactorScales[scale_factor];
97 namespace test {
99 ScopedSetSupportedScaleFactors::ScopedSetSupportedScaleFactors(
100 const std::vector<ui::ScaleFactor>& new_scale_factors) {
101 if (g_supported_scale_factors) {
102 original_scale_factors_ =
103 new std::vector<ScaleFactor>(*g_supported_scale_factors);
104 } else {
105 original_scale_factors_ = NULL;
107 SetSupportedScaleFactors(new_scale_factors);
110 ScopedSetSupportedScaleFactors::~ScopedSetSupportedScaleFactors() {
111 if (original_scale_factors_) {
112 SetSupportedScaleFactors(*original_scale_factors_);
113 delete original_scale_factors_;
114 } else {
115 delete g_supported_scale_factors;
116 g_supported_scale_factors = NULL;
120 } // namespace test
122 #if !defined(OS_MACOSX)
123 float GetScaleFactorForNativeView(gfx::NativeView view) {
124 gfx::Screen* screen = gfx::Screen::GetScreenFor(view);
125 if (screen->IsDIPEnabled()) {
126 gfx::Display display = screen->GetDisplayNearestWindow(view);
127 return display.device_scale_factor();
129 return 1.0f;
131 #endif // !defined(OS_MACOSX)
133 } // namespace ui