Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(23)

Side by Side Diff: Source/devtools/front_end/emulation/EmulatedDevices.js

Issue 1202493002: [DevTools] Refactor emulated devices modes. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 /** 5 /**
6 * @constructor 6 * @constructor
7 */ 7 */
8 WebInspector.EmulatedDevice = function() 8 WebInspector.EmulatedDevice = function()
9 { 9 {
10 /** @type {string} */ 10 /** @type {string} */
(...skipping 15 matching lines...) Expand all
26 26
27 /** @type {string} */ 27 /** @type {string} */
28 this._show = WebInspector.EmulatedDevice._Show.Default; 28 this._show = WebInspector.EmulatedDevice._Show.Default;
29 /** @type {boolean} */ 29 /** @type {boolean} */
30 this._showByDefault = true; 30 this._showByDefault = true;
31 31
32 /** @type {?Runtime.Extension} */ 32 /** @type {?Runtime.Extension} */
33 this._extension = null; 33 this._extension = null;
34 } 34 }
35 35
36 /** @typedef {!{title: string, orientation: string, pageRect: !WebInspector.Geom etry.Rect, images: ?WebInspector.EmulatedDevice.Images}} */ 36 /** @typedef {!{title: string, orientation: string, insets: !Insets, images: ?We bInspector.EmulatedDevice.Images}} */
37 WebInspector.EmulatedDevice.Mode; 37 WebInspector.EmulatedDevice.Mode;
38 38
39 /** @typedef {!{width: number, height: number, outlineInsets: ?WebInspector.Geom etry.Insets, outlineImages: ?WebInspector.EmulatedDevice.Images}} */ 39 /** @typedef {!{width: number, height: number, outlineInsets: ?Insets, outlineIm ages: ?WebInspector.EmulatedDevice.Images}} */
40 WebInspector.EmulatedDevice.Orientation; 40 WebInspector.EmulatedDevice.Orientation;
41 41
42 WebInspector.EmulatedDevice.Horizontal = "horizontal"; 42 WebInspector.EmulatedDevice.Horizontal = "horizontal";
43 WebInspector.EmulatedDevice.Vertical = "vertical"; 43 WebInspector.EmulatedDevice.Vertical = "vertical";
44 44
45 WebInspector.EmulatedDevice.Type = { 45 WebInspector.EmulatedDevice.Type = {
46 Phone: "phone", 46 Phone: "phone",
47 Tablet: "tablet", 47 Tablet: "tablet",
48 Notebook: "notebook", 48 Notebook: "notebook",
49 Desktop: "desktop", 49 Desktop: "desktop",
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 function parseIntValue(object, key) 96 function parseIntValue(object, key)
97 { 97 {
98 var value = /** @type {number} */ (parseValue(object, key, "number") ); 98 var value = /** @type {number} */ (parseValue(object, key, "number") );
99 if (value !== Math.abs(value)) 99 if (value !== Math.abs(value))
100 throw new Error("Emulated device value '" + key + "' must be int eger"); 100 throw new Error("Emulated device value '" + key + "' must be int eger");
101 return value; 101 return value;
102 } 102 }
103 103
104 /** 104 /**
105 * @param {*} json 105 * @param {*} json
106 * @return {!WebInspector.Geometry.Rect} 106 * @return {!Insets}
107 */ 107 */
108 function parseIntRect(json) 108 function parseInsets(json)
109 { 109 {
110 var result = {}; 110 return new Insets(parseIntValue(json, "left"), parseIntValue(json, " top"), parseIntValue(json, "right"), parseIntValue(json, "bottom"));
111 result.top = parseIntValue(json, "top");
112 result.left = parseIntValue(json, "left");
113 result.width = parseIntValue(json, "width");
114 result.height = parseIntValue(json, "height");
115 return /** @type {!WebInspector.Geometry.Rect} */ (result);
116 } 111 }
117 112
118 /** 113 /**
119 * @param {*} json
120 * @return {?WebInspector.Geometry.Insets}
121 */
122 function parseIntInsets(json)
123 {
124 if (json === null)
125 return null;
126 var result = {};
127 result.top = parseIntValue(json, "top");
128 result.left = parseIntValue(json, "left");
129 return /** @type {?WebInspector.Geometry.Insets} */ (result);
130 }
131
132 /**
133 * @param {*} json 114 * @param {*} json
134 * @return {!WebInspector.EmulatedDevice.Images} 115 * @return {!WebInspector.EmulatedDevice.Images}
135 */ 116 */
136 function parseImages(json) 117 function parseImages(json)
137 { 118 {
138 if (!Array.isArray(json)) 119 if (!Array.isArray(json))
139 throw new Error("Emulated device images is not an array"); 120 throw new Error("Emulated device images is not an array");
140 var result = new WebInspector.EmulatedDevice.Images(); 121 var result = new WebInspector.EmulatedDevice.Images();
141 for (var i = 0; i < json.length; ++i) { 122 for (var i = 0; i < json.length; ++i) {
142 var src = /** @type {string} */ (parseValue(json[i], "src", "str ing")); 123 var src = /** @type {string} */ (parseValue(json[i], "src", "str ing"));
(...skipping 14 matching lines...) Expand all
157 var result = {}; 138 var result = {};
158 139
159 result.width = parseIntValue(json, "width"); 140 result.width = parseIntValue(json, "width");
160 if (result.width < 0 || result.width > WebInspector.OverridesSupport .MaxDeviceSize) 141 if (result.width < 0 || result.width > WebInspector.OverridesSupport .MaxDeviceSize)
161 throw new Error("Emulated device has wrong width: " + result.wid th); 142 throw new Error("Emulated device has wrong width: " + result.wid th);
162 143
163 result.height = parseIntValue(json, "height"); 144 result.height = parseIntValue(json, "height");
164 if (result.height < 0 || result.height > WebInspector.OverridesSuppo rt.MaxDeviceSize) 145 if (result.height < 0 || result.height > WebInspector.OverridesSuppo rt.MaxDeviceSize)
165 throw new Error("Emulated device has wrong height: " + result.he ight); 146 throw new Error("Emulated device has wrong height: " + result.he ight);
166 147
167 result.outlineInsets = parseIntInsets(parseValue(json["outline"], "i nsets", "object", null)); 148 var outlineInsets = parseValue(json["outline"], "insets", "object", null);
168 if (result.outlineInsets) { 149 if (outlineInsets) {
150 result.outlineInsets = parseInsets(outlineInsets);
169 if (result.outlineInsets.left < 0 || result.outlineInsets.top < 0) 151 if (result.outlineInsets.left < 0 || result.outlineInsets.top < 0)
170 throw new Error("Emulated device has wrong outline insets"); 152 throw new Error("Emulated device has wrong outline insets");
171 result.outlineImages = parseImages(parseValue(json["outline"], " images", "object")); 153 result.outlineImages = parseImages(parseValue(json["outline"], " images", "object"));
172 } 154 }
173 155
174 return /** @type {!WebInspector.EmulatedDevice.Orientation} */ (resu lt); 156 return /** @type {!WebInspector.EmulatedDevice.Orientation} */ (resu lt);
175 } 157 }
176 158
177 var result = new WebInspector.EmulatedDevice(); 159 var result = new WebInspector.EmulatedDevice();
178 result.title = /** @type {string} */ (parseValue(json, "title", "string" )); 160 result.title = /** @type {string} */ (parseValue(json, "title", "string" ));
(...skipping 21 matching lines...) Expand all
200 if (!Array.isArray(modes)) 182 if (!Array.isArray(modes))
201 throw new Error("Emulated device modes must be an array"); 183 throw new Error("Emulated device modes must be an array");
202 result.modes = []; 184 result.modes = [];
203 for (var i = 0; i < modes.length; ++i) { 185 for (var i = 0; i < modes.length; ++i) {
204 var mode = {}; 186 var mode = {};
205 mode.title = /** @type {string} */ (parseValue(modes[i], "title", "s tring")); 187 mode.title = /** @type {string} */ (parseValue(modes[i], "title", "s tring"));
206 mode.orientation = /** @type {string} */ (parseValue(modes[i], "orie ntation", "string")); 188 mode.orientation = /** @type {string} */ (parseValue(modes[i], "orie ntation", "string"));
207 if (mode.orientation !== WebInspector.EmulatedDevice.Vertical && mod e.orientation !== WebInspector.EmulatedDevice.Horizontal) 189 if (mode.orientation !== WebInspector.EmulatedDevice.Vertical && mod e.orientation !== WebInspector.EmulatedDevice.Horizontal)
208 throw new Error("Emulated device mode has wrong orientation '" + mode.orientation + "'"); 190 throw new Error("Emulated device mode has wrong orientation '" + mode.orientation + "'");
209 var orientation = result.orientationByName(mode.orientation); 191 var orientation = result.orientationByName(mode.orientation);
210 mode.pageRect = parseIntRect(parseValue(modes[i], "page-rect", "obje ct")); 192 mode.insets = parseInsets(parseValue(modes[i], "insets", "object"));
211 if (mode.pageRect.top < 0 || mode.pageRect.left < 0 || mode.pageRect .width < 0 || mode.pageRect.height < 0 || 193 if (mode.insets.top < 0 || mode.insets.left < 0 || mode.insets.right < 0 || mode.insets.bottom < 0 ||
212 mode.pageRect.top + mode.pageRect.height > orientation.height || mode.pageRect.left + mode.pageRect.width > orientation.width) { 194 mode.insets.top + mode.insets.bottom > orientation.height || mod e.insets.left + mode.insets.right > orientation.width) {
213 throw new Error("Emulated device mode '" + mode.title + "'has wr ong page rect"); 195 throw new Error("Emulated device mode '" + mode.title + "'has wr ong mode insets");
214 } 196 }
215 if (modes[i].hasOwnProperty("images")) 197 if (modes[i].hasOwnProperty("images"))
216 mode.images = parseImages(parseValue(modes[i], "images", "object ")); 198 mode.images = parseImages(parseValue(modes[i], "images", "object "));
217 result.modes.push(mode); 199 result.modes.push(mode);
218 } 200 }
219 201
220 result._showByDefault = /** @type {boolean} */ (parseValue(json, "show-b y-default", "boolean", true)); 202 result._showByDefault = /** @type {boolean} */ (parseValue(json, "show-b y-default", "boolean", true));
221 result._show = /** @type {string} */ (parseValue(json, "show", "string", WebInspector.EmulatedDevice._Show.Default)); 203 result._show = /** @type {string} */ (parseValue(json, "show", "string", WebInspector.EmulatedDevice._Show.Default));
222 204
223 result.createImplicitModes();
224 return result; 205 return result;
225 } catch (e) { 206 } catch (e) {
226 WebInspector.console.error("Failed to update emulated device list. " + S tring(e)); 207 WebInspector.console.error("Failed to update emulated device list. " + S tring(e));
227 return null; 208 return null;
228 } 209 }
229 } 210 }
230 211
231 /** 212 /**
232 * @param {!WebInspector.OverridesSupport.Device} device 213 * @param {!WebInspector.OverridesSupport.Device} device
233 * @param {string} title 214 * @param {string} title
234 * @param {string=} type 215 * @param {string=} type
235 * @return {!WebInspector.EmulatedDevice} 216 * @return {!WebInspector.EmulatedDevice}
236 */ 217 */
237 WebInspector.EmulatedDevice.fromOverridesDevice = function(device, title, type) 218 WebInspector.EmulatedDevice.fromOverridesDevice = function(device, title, type)
238 { 219 {
239 var result = new WebInspector.EmulatedDevice(); 220 var result = new WebInspector.EmulatedDevice();
240 result.title = title; 221 result.title = title;
241 result.type = type || WebInspector.EmulatedDevice.Type.Unknown; 222 result.type = type || WebInspector.EmulatedDevice.Type.Unknown;
242 result.vertical.width = device.width; 223 result.vertical.width = device.width;
243 result.vertical.height = device.height; 224 result.vertical.height = device.height;
244 result.horizontal.width = device.height; 225 result.horizontal.width = device.height;
245 result.horizontal.height = device.width; 226 result.horizontal.height = device.width;
246 result.deviceScaleFactor = device.deviceScaleFactor; 227 result.deviceScaleFactor = device.deviceScaleFactor;
247 result.userAgent = device.userAgent; 228 result.userAgent = device.userAgent;
248 result.capabilities = []; 229 result.capabilities = [];
249 if (device.touch) 230 if (device.touch)
250 result.capabilities.push(WebInspector.EmulatedDevice.Capability.Touch); 231 result.capabilities.push(WebInspector.EmulatedDevice.Capability.Touch);
251 if (device.mobile) 232 if (device.mobile)
252 result.capabilities.push(WebInspector.EmulatedDevice.Capability.Mobile); 233 result.capabilities.push(WebInspector.EmulatedDevice.Capability.Mobile);
253 result.createImplicitModes();
254 return result; 234 return result;
255 } 235 }
256 236
257 /** 237 /**
258 * @param {!WebInspector.EmulatedDevice} device1 238 * @param {!WebInspector.EmulatedDevice} device1
259 * @param {!WebInspector.EmulatedDevice} device2 239 * @param {!WebInspector.EmulatedDevice} device2
260 * @return {number} 240 * @return {number}
261 */ 241 */
262 WebInspector.EmulatedDevice.compareByTitle = function(device1, device2) 242 WebInspector.EmulatedDevice.compareByTitle = function(device1, device2)
263 { 243 {
(...skipping 10 matching lines...) Expand all
274 }, 254 },
275 255
276 /** 256 /**
277 * @param {?Runtime.Extension} extension 257 * @param {?Runtime.Extension} extension
278 */ 258 */
279 setExtension: function(extension) 259 setExtension: function(extension)
280 { 260 {
281 this._extension = extension; 261 this._extension = extension;
282 }, 262 },
283 263
284 createImplicitModes: function()
285 {
286 // TODO(dgozman): this whole method should be removed once we have modes for all devices.
287 if (this.modes.length)
288 return;
289 this.modes.push({title: "", orientation: WebInspector.EmulatedDevice.Hor izontal, pageRect: {top: 0, left: 0, width: this.horizontal.width, height: this. horizontal.height}, images: null});
290 if (this.type === WebInspector.EmulatedDevice.Type.Phone || this.type == = WebInspector.EmulatedDevice.Type.Tablet || this.type === WebInspector.Emulated Device.Type.Unknown)
291 this.modes.push({title: "", orientation: WebInspector.EmulatedDevice .Vertical, pageRect: {top: 0, left: 0, width: this.vertical.width, height: this. vertical.height}, images: null});
292 },
293
294 /** 264 /**
295 * @param {string} orientation 265 * @param {string} orientation
296 * @return {!Array.<!WebInspector.EmulatedDevice.Mode>} 266 * @return {!Array.<!WebInspector.EmulatedDevice.Mode>}
297 */ 267 */
298 modesForOrientation: function(orientation) 268 modesForOrientation: function(orientation)
299 { 269 {
300 var result = []; 270 var result = [];
301 for (var index = 0; index < this.modes.length; index++) { 271 for (var index = 0; index < this.modes.length; index++) {
302 if (this.modes[index].orientation === orientation) 272 if (this.modes[index].orientation === orientation)
303 result.push(this.modes[index]); 273 result.push(this.modes[index]);
(...skipping 15 matching lines...) Expand all
319 json["screen"] = {}; 289 json["screen"] = {};
320 json["screen"]["device-pixel-ratio"] = this.deviceScaleFactor; 290 json["screen"]["device-pixel-ratio"] = this.deviceScaleFactor;
321 json["screen"]["vertical"] = this._orientationToJSON(this.vertical); 291 json["screen"]["vertical"] = this._orientationToJSON(this.vertical);
322 json["screen"]["horizontal"] = this._orientationToJSON(this.horizontal); 292 json["screen"]["horizontal"] = this._orientationToJSON(this.horizontal);
323 293
324 json["modes"] = []; 294 json["modes"] = [];
325 for (var i = 0; i < this.modes.length; ++i) { 295 for (var i = 0; i < this.modes.length; ++i) {
326 var mode = {}; 296 var mode = {};
327 mode["title"] = this.modes[i].title; 297 mode["title"] = this.modes[i].title;
328 mode["orientation"] = this.modes[i].orientation; 298 mode["orientation"] = this.modes[i].orientation;
329 mode["page-rect"] = this.modes[i].pageRect; 299 mode["insets"] = {};
300 mode["insets"]["left"] = this.modes[i].insets.left;
301 mode["insets"]["top"] = this.modes[i].insets.top;
302 mode["insets"]["right"] = this.modes[i].insets.right;
303 mode["insets"]["bottom"] = this.modes[i].insets.bottom;
330 if (this.modes[i].images) 304 if (this.modes[i].images)
331 mode["images"] = this.modes[i].images._toJSON(); 305 mode["images"] = this.modes[i].images._toJSON();
332 json["modes"].push(mode); 306 json["modes"].push(mode);
333 } 307 }
334 308
335 json["show-by-default"] = this._showByDefault; 309 json["show-by-default"] = this._showByDefault;
336 json["show"] = this._show; 310 json["show"] = this._show;
337 311
338 return json; 312 return json;
339 }, 313 },
340 314
341 /** 315 /**
342 * @param {!WebInspector.EmulatedDevice.Orientation} orientation 316 * @param {!WebInspector.EmulatedDevice.Orientation} orientation
343 * @return {*} 317 * @return {*}
344 */ 318 */
345 _orientationToJSON: function(orientation) 319 _orientationToJSON: function(orientation)
346 { 320 {
347 var json = {}; 321 var json = {};
348 json["width"] = orientation.width; 322 json["width"] = orientation.width;
349 json["height"] = orientation.height; 323 json["height"] = orientation.height;
350 if (orientation.outlineInsets) { 324 if (orientation.outlineInsets) {
351 json["outline"] = {}; 325 json["outline"] = {};
352 json["outline"]["insets"] = orientation.outlineInsets; 326 json["outline"]["insets"] = {};
327 json["outline"]["insets"]["left"] = orientation.outlineInsets.left;
328 json["outline"]["insets"]["top"] = orientation.outlineInsets.top;
329 json["outline"]["insets"]["right"] = orientation.outlineInsets.right ;
330 json["outline"]["insets"]["bottom"] = orientation.outlineInsets.bott om;
353 json["outline"]["images"] = orientation.outlineImages._toJSON(); 331 json["outline"]["images"] = orientation.outlineImages._toJSON();
354 } 332 }
355 return json; 333 return json;
356 }, 334 },
357 335
358 /** 336 /**
359 * @param {!WebInspector.EmulatedDevice.Mode} mode 337 * @param {!WebInspector.EmulatedDevice.Mode} mode
360 * @return {!WebInspector.OverridesSupport.Device} 338 * @return {!WebInspector.OverridesSupport.Device}
361 */ 339 */
362 modeToOverridesDevice: function(mode) 340 modeToOverridesDevice: function(mode)
363 { 341 {
364 var result = {}; 342 var result = {};
365 result.width = mode.pageRect.width; 343 var orientation = this.orientationByName(mode.orientation);
366 result.height = mode.pageRect.height; 344 result.width = orientation.width - mode.insets.left - mode.insets.right;
345 result.height = orientation.height - mode.insets.top - mode.insets.botto m;
367 result.deviceScaleFactor = this.deviceScaleFactor; 346 result.deviceScaleFactor = this.deviceScaleFactor;
368 result.userAgent = this.userAgent; 347 result.userAgent = this.userAgent;
369 result.touch = this.touch(); 348 result.touch = this.touch();
370 result.mobile = this.mobile(); 349 result.mobile = this.mobile();
371 return result; 350 return result;
372 }, 351 },
373 352
374 /** 353 /**
375 * @param {string} name 354 * @param {string} name
376 * @return {!WebInspector.EmulatedDevice.Orientation} 355 * @return {!WebInspector.EmulatedDevice.Orientation}
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
505 * @param {!Array.<*>} jsonArray 484 * @param {!Array.<*>} jsonArray
506 * @return {!Array.<!WebInspector.EmulatedDevice>} 485 * @return {!Array.<!WebInspector.EmulatedDevice>}
507 */ 486 */
508 _listFromJSONV1: function(jsonArray) 487 _listFromJSONV1: function(jsonArray)
509 { 488 {
510 var result = []; 489 var result = [];
511 if (!Array.isArray(jsonArray)) 490 if (!Array.isArray(jsonArray))
512 return result; 491 return result;
513 for (var i = 0; i < jsonArray.length; ++i) { 492 for (var i = 0; i < jsonArray.length; ++i) {
514 var device = WebInspector.EmulatedDevice.fromJSONV1(jsonArray[i]); 493 var device = WebInspector.EmulatedDevice.fromJSONV1(jsonArray[i]);
515 if (device) 494 if (device) {
516 result.push(device); 495 result.push(device);
496 if (!device.modes.length) {
497 device.modes.push({title: "", orientation: WebInspector.Emul atedDevice.Horizontal, insets: new Insets(0, 0, 0, 0), images: null});
498 device.modes.push({title: "", orientation: WebInspector.Emul atedDevice.Vertical, insets: new Insets(0, 0, 0, 0), images: null});
499 }
500 }
517 } 501 }
518 return result; 502 return result;
519 }, 503 },
520 504
521 /** 505 /**
522 * @return {!Array.<!WebInspector.EmulatedDevice>} 506 * @return {!Array.<!WebInspector.EmulatedDevice>}
523 */ 507 */
524 standard: function() 508 standard: function()
525 { 509 {
526 return this._standard; 510 return this._standard;
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
581 if (deviceById.has(title)) 565 if (deviceById.has(title))
582 to[i].copyShowFrom(/** @type {!WebInspector.EmulatedDevice} */ ( deviceById.get(title))); 566 to[i].copyShowFrom(/** @type {!WebInspector.EmulatedDevice} */ ( deviceById.get(title)));
583 } 567 }
584 }, 568 },
585 569
586 __proto__: WebInspector.Object.prototype 570 __proto__: WebInspector.Object.prototype
587 } 571 }
588 572
589 /** @type {!WebInspector.EmulatedDevicesList} */ 573 /** @type {!WebInspector.EmulatedDevicesList} */
590 WebInspector.emulatedDevicesList; 574 WebInspector.emulatedDevicesList;
OLDNEW
« no previous file with comments | « Source/devtools/front_end/emulation/DevicesSettingsTab.js ('k') | Source/devtools/front_end/emulation/ResponsiveDesignView.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698