Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / ash / display / display_layout.cc
blob9816f21b694d54909c03da804e908d282f6150f7
1 // Copyright (c) 2013 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 "ash/display/display_layout.h"
7 #include "ash/ash_switches.h"
8 #include "ash/display/display_manager.h"
9 #include "ash/display/display_pref_util.h"
10 #include "ash/shell.h"
11 #include "base/json/json_value_converter.h"
12 #include "base/logging.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_piece.h"
15 #include "base/strings/stringprintf.h"
16 #include "base/values.h"
17 #include "ui/gfx/display.h"
19 namespace ash {
20 namespace {
22 // The maximum value for 'offset' in DisplayLayout in case of outliers. Need
23 // to change this value in case to support even larger displays.
24 const int kMaxValidOffset = 10000;
26 // Persistent key names
27 const char kPositionKey[] = "position";
28 const char kOffsetKey[] = "offset";
29 const char kMirroredKey[] = "mirrored";
30 const char kDefaultUnifiedKey[] = "default_unified";
31 const char kPrimaryIdKey[] = "primary-id";
33 typedef std::map<DisplayLayout::Position, std::string> PositionToStringMap;
35 const PositionToStringMap* GetPositionToStringMap() {
36 static const PositionToStringMap* map = CreateToStringMap(
37 DisplayLayout::TOP, "top",
38 DisplayLayout::BOTTOM, "bottom",
39 DisplayLayout::RIGHT, "right",
40 DisplayLayout::LEFT, "left");
41 return map;
44 bool GetPositionFromString(const base::StringPiece& position,
45 DisplayLayout::Position* field) {
46 if (ReverseFind(GetPositionToStringMap(), position, field))
47 return true;
48 LOG(ERROR) << "Invalid position value:" << position;
49 return false;
52 std::string GetStringFromPosition(DisplayLayout::Position position) {
53 const PositionToStringMap* map = GetPositionToStringMap();
54 PositionToStringMap::const_iterator iter = map->find(position);
55 return iter != map->end() ? iter->second : std::string("unknown");
58 bool GetDisplayIdFromString(const base::StringPiece& position, int64* field) {
59 return base::StringToInt64(position, field);
62 } // namespace
64 ////////////////////////////////////////////////////////////////////////////////
65 // DisplayLayout
67 // static
68 DisplayLayout DisplayLayout::FromInts(int position, int offsets) {
69 return DisplayLayout(static_cast<Position>(position), offsets);
72 DisplayLayout::DisplayLayout()
73 : position(RIGHT),
74 offset(0),
75 mirrored(false),
76 #if defined(OS_CHROMEOS)
77 default_unified(true),
78 #else
79 default_unified(false),
80 #endif
81 primary_id(gfx::Display::kInvalidDisplayID) {
84 DisplayLayout::DisplayLayout(DisplayLayout::Position position, int offset)
85 : position(position),
86 offset(offset),
87 mirrored(false),
88 #if defined(OS_CHROMEOS)
89 default_unified(true),
90 #else
91 default_unified(false),
92 #endif
93 primary_id(gfx::Display::kInvalidDisplayID) {
94 DCHECK_LE(TOP, position);
95 DCHECK_GE(LEFT, position);
97 // Set the default value to |position| in case position is invalid. DCHECKs
98 // above doesn't stop in Release builds.
99 if (TOP > position || LEFT < position)
100 this->position = RIGHT;
102 DCHECK_GE(kMaxValidOffset, abs(offset));
105 DisplayLayout DisplayLayout::Invert() const {
106 Position inverted_position = RIGHT;
107 switch (position) {
108 case TOP:
109 inverted_position = BOTTOM;
110 break;
111 case BOTTOM:
112 inverted_position = TOP;
113 break;
114 case RIGHT:
115 inverted_position = LEFT;
116 break;
117 case LEFT:
118 inverted_position = RIGHT;
119 break;
121 DisplayLayout ret = DisplayLayout(inverted_position, -offset);
122 ret.primary_id = primary_id;
123 return ret;
126 // static
127 bool DisplayLayout::ConvertFromValue(const base::Value& value,
128 DisplayLayout* layout) {
129 base::JSONValueConverter<DisplayLayout> converter;
130 return converter.Convert(value, layout);
133 // static
134 bool DisplayLayout::ConvertToValue(const DisplayLayout& layout,
135 base::Value* value) {
136 base::DictionaryValue* dict_value = NULL;
137 if (!value->GetAsDictionary(&dict_value) || dict_value == NULL)
138 return false;
140 const std::string position_str = GetStringFromPosition(layout.position);
141 dict_value->SetString(kPositionKey, position_str);
142 dict_value->SetInteger(kOffsetKey, layout.offset);
143 dict_value->SetBoolean(kMirroredKey, layout.mirrored);
144 dict_value->SetBoolean(kDefaultUnifiedKey, layout.default_unified);
145 dict_value->SetString(kPrimaryIdKey, base::Int64ToString(layout.primary_id));
146 return true;
149 std::string DisplayLayout::ToString() const {
150 const std::string position_str = GetStringFromPosition(position);
151 bool unified =
152 default_unified &&
153 Shell::GetInstance()->display_manager()->unified_desktop_enabled();
155 return base::StringPrintf("%s, %d%s%s", position_str.c_str(), offset,
156 mirrored ? ", mirrored" : "",
157 unified ? ", unified" : "");
160 // static
161 void DisplayLayout::RegisterJSONConverter(
162 base::JSONValueConverter<DisplayLayout>* converter) {
163 converter->RegisterCustomField<Position>(
164 kPositionKey, &DisplayLayout::position, &GetPositionFromString);
165 converter->RegisterIntField(kOffsetKey, &DisplayLayout::offset);
166 converter->RegisterBoolField(kMirroredKey, &DisplayLayout::mirrored);
167 converter->RegisterBoolField(kDefaultUnifiedKey,
168 &DisplayLayout::default_unified);
169 converter->RegisterCustomField<int64>(
170 kPrimaryIdKey, &DisplayLayout::primary_id, &GetDisplayIdFromString);
173 } // namespace ash