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

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/emulation/DeviceModeModel.js

Issue 1575203003: [DevTools] More Device Mode polish. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 11 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
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 * @param {function()} updateCallback 7 * @param {function()} updateCallback
8 * @implements {WebInspector.TargetManager.Observer} 8 * @implements {WebInspector.TargetManager.Observer}
9 */ 9 */
10 WebInspector.DeviceModeModel = function(updateCallback) 10 WebInspector.DeviceModeModel = function(updateCallback)
(...skipping 29 matching lines...) Expand all
40 this._touchEnabled = false; 40 this._touchEnabled = false;
41 /** @type {string} */ 41 /** @type {string} */
42 this._touchConfiguration = ""; 42 this._touchConfiguration = "";
43 /** @type {string} */ 43 /** @type {string} */
44 this._screenOrientation = ""; 44 this._screenOrientation = "";
45 /** @type {number} */ 45 /** @type {number} */
46 this._fitScale = 1; 46 this._fitScale = 1;
47 47
48 /** @type {?WebInspector.Target} */ 48 /** @type {?WebInspector.Target} */
49 this._target = null; 49 this._target = null;
50 /** @type {?function()} */
51 this._onTargetAvailable = null;
50 WebInspector.targetManager.observeTargets(this, WebInspector.Target.Type.Pag e); 52 WebInspector.targetManager.observeTargets(this, WebInspector.Target.Type.Pag e);
51 } 53 }
52 54
53 /** @enum {string} */ 55 /** @enum {string} */
54 WebInspector.DeviceModeModel.Type = { 56 WebInspector.DeviceModeModel.Type = {
55 None: "None", 57 None: "None",
56 Responsive: "Responsive", 58 Responsive: "Responsive",
57 Device: "Device" 59 Device: "Device"
58 } 60 }
59 61
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 this._calculateAndEmulate(false); 95 this._calculateAndEmulate(false);
94 }, 96 },
95 97
96 /** 98 /**
97 * @param {!WebInspector.DeviceModeModel.Type} type 99 * @param {!WebInspector.DeviceModeModel.Type} type
98 * @param {?WebInspector.EmulatedDevice} device 100 * @param {?WebInspector.EmulatedDevice} device
99 * @param {?WebInspector.EmulatedDevice.Mode} mode 101 * @param {?WebInspector.EmulatedDevice.Mode} mode
100 */ 102 */
101 emulate: function(type, device, mode) 103 emulate: function(type, device, mode)
102 { 104 {
105 var resetScrollAndPageScale = this._type !== type || this._device !== de vice || this._mode !== mode;
103 this._type = type; 106 this._type = type;
104 107
105 if (type === WebInspector.DeviceModeModel.Type.Device) { 108 if (type === WebInspector.DeviceModeModel.Type.Device) {
106 console.assert(device && mode, "Must pass device and mode for device emulation"); 109 console.assert(device && mode, "Must pass device and mode for device emulation");
107 this._device = device; 110 this._device = device;
108 this._mode = mode; 111 this._mode = mode;
109 } else { 112 } else {
110 this._device = null; 113 this._device = null;
111 this._mode = null; 114 this._mode = null;
112 } 115 }
113 116
114 if (type !== WebInspector.DeviceModeModel.Type.None) 117 if (type !== WebInspector.DeviceModeModel.Type.None)
115 WebInspector.userMetrics.actionTaken(WebInspector.UserMetrics.Action .DeviceModeEnabled); 118 WebInspector.userMetrics.actionTaken(WebInspector.UserMetrics.Action .DeviceModeEnabled);
116 this._calculateAndEmulate(true); 119 this._calculateAndEmulate(resetScrollAndPageScale);
117 }, 120 },
118 121
119 /** 122 /**
120 * @return {?WebInspector.EmulatedDevice} 123 * @return {?WebInspector.EmulatedDevice}
121 */ 124 */
122 device: function() 125 device: function()
123 { 126 {
124 return this._device; 127 return this._device;
125 }, 128 },
126 129
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
261 /** 264 /**
262 * @override 265 * @override
263 * @param {!WebInspector.Target} target 266 * @param {!WebInspector.Target} target
264 */ 267 */
265 targetAdded: function(target) 268 targetAdded: function(target)
266 { 269 {
267 if (!this._target) { 270 if (!this._target) {
268 this._target = target; 271 this._target = target;
269 var domModel = WebInspector.DOMModel.fromTarget(this._target); 272 var domModel = WebInspector.DOMModel.fromTarget(this._target);
270 domModel.addEventListener(WebInspector.DOMModel.Events.InspectModeWi llBeToggled, this._inspectModeWillBeToggled, this); 273 domModel.addEventListener(WebInspector.DOMModel.Events.InspectModeWi llBeToggled, this._inspectModeWillBeToggled, this);
274 if (this._onTargetAvailable) {
275 var callback = this._onTargetAvailable;
276 this._onTargetAvailable = null;
277 callback();
278 }
271 } 279 }
272 }, 280 },
273 281
274 /** 282 /**
275 * @param {!WebInspector.Event} event 283 * @param {!WebInspector.Event} event
276 */ 284 */
277 _inspectModeWillBeToggled: function(event) 285 _inspectModeWillBeToggled: function(event)
278 { 286 {
279 var inspectModeEnabled = /** @type {boolean} */ (event.data); 287 var inspectModeEnabled = /** @type {boolean} */ (event.data);
280 if (inspectModeEnabled) { 288 if (inspectModeEnabled) {
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
323 _deviceScaleFactorSettingChanged: function() 331 _deviceScaleFactorSettingChanged: function()
324 { 332 {
325 this._calculateAndEmulate(false); 333 this._calculateAndEmulate(false);
326 }, 334 },
327 335
328 /** 336 /**
329 * @param {boolean} resetScrollAndPageScale 337 * @param {boolean} resetScrollAndPageScale
330 */ 338 */
331 _calculateAndEmulate: function(resetScrollAndPageScale) 339 _calculateAndEmulate: function(resetScrollAndPageScale)
332 { 340 {
341 if (!this._target) {
342 this._onTargetAvailable = this._calculateAndEmulate.bind(this, reset ScrollAndPageScale);
343 return;
344 }
345
333 if (this._type === WebInspector.DeviceModeModel.Type.Device) { 346 if (this._type === WebInspector.DeviceModeModel.Type.Device) {
334 var orientation = this._device.orientationByName(this._mode.orientat ion); 347 var orientation = this._device.orientationByName(this._mode.orientat ion);
335 var screenWidth = orientation.width; 348 this._calculateFitScale(orientation.width, orientation.height);
336 var screenHeight = orientation.height; 349 this._applyDeviceMetrics(new Size(orientation.width, orientation.hei ght), this._mode.insets, this._scaleSetting.get(), this._device.deviceScaleFacto r, this._device.mobile(), resetScrollAndPageScale);
337 this._applyDeviceMetrics(new Size(screenWidth, screenHeight), this._ mode.insets, this._scaleSetting.get(), this._device.deviceScaleFactor, this._dev ice.mobile(), resetScrollAndPageScale);
338 this._applyUserAgent(this._device.userAgent); 350 this._applyUserAgent(this._device.userAgent);
339 this._applyTouch(this._device.touch(), this._device.mobile()); 351 this._applyTouch(this._device.touch(), this._device.mobile());
340 this._applyScreenOrientation(this._mode.orientation == WebInspector. EmulatedDevice.Horizontal ? "landscapePrimary" : "portraitPrimary"); 352 this._applyScreenOrientation(this._mode.orientation == WebInspector. EmulatedDevice.Horizontal ? "landscapePrimary" : "portraitPrimary");
341 } else if (this._type === WebInspector.DeviceModeModel.Type.None) { 353 } else if (this._type === WebInspector.DeviceModeModel.Type.None) {
354 this._calculateFitScale(this._availableSize.width, this._availableSi ze.height);
342 this._applyDeviceMetrics(this._availableSize, new Insets(0, 0, 0, 0) , 1, 0, false, resetScrollAndPageScale); 355 this._applyDeviceMetrics(this._availableSize, new Insets(0, 0, 0, 0) , 1, 0, false, resetScrollAndPageScale);
343 this._applyUserAgent(""); 356 this._applyUserAgent("");
344 this._applyTouch(false, false); 357 this._applyTouch(false, false);
345 this._applyScreenOrientation(""); 358 this._applyScreenOrientation("");
346 } else if (this._type === WebInspector.DeviceModeModel.Type.Responsive) { 359 } else if (this._type === WebInspector.DeviceModeModel.Type.Responsive) {
347 var screenWidth = this._widthSetting.get() || this._preferredSize.wi dth / (this._scaleSetting.get() || 1); 360 var screenWidth = this._widthSetting.get() || this._preferredSize.wi dth / (this._scaleSetting.get() || 1);
348 var screenHeight = this._heightSetting.get() || this._preferredSize. height / (this._scaleSetting.get() || 1); 361 var screenHeight = this._heightSetting.get() || this._preferredSize. height / (this._scaleSetting.get() || 1);
349 var mobile = this._uaSetting.get() === WebInspector.DeviceModeModel. UA.Mobile; 362 var mobile = this._uaSetting.get() === WebInspector.DeviceModeModel. UA.Mobile;
350 var defaultDeviceScaleFactor = mobile ? WebInspector.DeviceModeModel ._defaultMobileScaleFactor : 0; 363 var defaultDeviceScaleFactor = mobile ? WebInspector.DeviceModeModel ._defaultMobileScaleFactor : 0;
364 this._calculateFitScale(this._widthSetting.get(), this._heightSettin g.get());
351 this._applyDeviceMetrics(new Size(screenWidth, screenHeight), new In sets(0, 0, 0, 0), this._scaleSetting.get(), this._deviceScaleFactorSetting.get() || defaultDeviceScaleFactor, mobile, resetScrollAndPageScale); 365 this._applyDeviceMetrics(new Size(screenWidth, screenHeight), new In sets(0, 0, 0, 0), this._scaleSetting.get(), this._deviceScaleFactorSetting.get() || defaultDeviceScaleFactor, mobile, resetScrollAndPageScale);
352 this._applyUserAgent(mobile ? WebInspector.DeviceModeModel._defaultM obileUserAgent : ""); 366 this._applyUserAgent(mobile ? WebInspector.DeviceModeModel._defaultM obileUserAgent : "");
353 this._applyTouch(this._uaSetting.get() !== WebInspector.DeviceModeMo del.UA.Desktop, mobile); 367 this._applyTouch(this._uaSetting.get() !== WebInspector.DeviceModeMo del.UA.Desktop, mobile);
354 this._applyScreenOrientation(screenHeight >= screenWidth ? "portrait Primary" : "landscapePrimary"); 368 this._applyScreenOrientation(screenHeight >= screenWidth ? "portrait Primary" : "landscapePrimary");
355 } 369 }
356 this._updateCallback.call(null); 370 this._updateCallback.call(null);
357 }, 371 },
358 372
359 /** 373 /**
360 * @param {number} screenWidth 374 * @param {number} screenWidth
361 * @param {number} screenHeight 375 * @param {number} screenHeight
362 * @return {number}
363 */ 376 */
364 _calculateFitScale: function(screenWidth, screenHeight) 377 _calculateFitScale: function(screenWidth, screenHeight)
365 { 378 {
366 var scale = Math.min(screenWidth ? this._preferredSize.width / screenWid th: 1, screenHeight ? this._preferredSize.height / screenHeight : 1); 379 var scale = Math.min(screenWidth ? this._preferredSize.width / screenWid th: 1, screenHeight ? this._preferredSize.height / screenHeight : 1);
367 return Math.min(scale, 1); 380 this._fitScale = Math.min(scale, 1);
368 }, 381 },
369 382
370 /** 383 /**
371 * @param {string} userAgent 384 * @param {string} userAgent
372 */ 385 */
373 _applyUserAgent: function(userAgent) 386 _applyUserAgent: function(userAgent)
374 { 387 {
375 WebInspector.multitargetNetworkManager.setUserAgentOverride(userAgent); 388 WebInspector.multitargetNetworkManager.setUserAgentOverride(userAgent);
376 }, 389 },
377 390
378 /** 391 /**
379 * @param {!Size} screenSize 392 * @param {!Size} screenSize
380 * @param {!Insets} insets 393 * @param {!Insets} insets
381 * @param {number} scale 394 * @param {number} scale
382 * @param {number} deviceScaleFactor 395 * @param {number} deviceScaleFactor
383 * @param {boolean} mobile 396 * @param {boolean} mobile
384 * @param {boolean} resetScrollAndPageScale 397 * @param {boolean} resetScrollAndPageScale
385 */ 398 */
386 _applyDeviceMetrics: function(screenSize, insets, scale, deviceScaleFactor, mobile, resetScrollAndPageScale) 399 _applyDeviceMetrics: function(screenSize, insets, scale, deviceScaleFactor, mobile, resetScrollAndPageScale)
387 { 400 {
388 screenSize.width = Math.max(1, Math.floor(screenSize.width)); 401 screenSize.width = Math.max(1, Math.floor(screenSize.width));
389 screenSize.height = Math.max(1, Math.floor(screenSize.height)); 402 screenSize.height = Math.max(1, Math.floor(screenSize.height));
390 this._fitScale = this._calculateFitScale(screenSize.width, screenSize.he ight);
391 403
392 var pageWidth = screenSize.width - insets.left - insets.right; 404 var pageWidth = screenSize.width - insets.left - insets.right;
393 var pageHeight = screenSize.height - insets.top - insets.bottom; 405 var pageHeight = screenSize.height - insets.top - insets.bottom;
394 var positionX = insets.left; 406 var positionX = insets.left;
395 var positionY = insets.top; 407 var positionY = insets.top;
396 408
397 this._appliedDeviceSize = screenSize; 409 this._appliedDeviceSize = screenSize;
398 this._screenRect = new WebInspector.Rect( 410 this._screenRect = new WebInspector.Rect(
399 Math.max(0, (this._availableSize.width - screenSize.width * scale) / 2), 411 Math.max(0, (this._availableSize.width - screenSize.width * scale) / 2),
400 0, 412 0,
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
498 if (!this._target || orientation === this._screenOrientation) 510 if (!this._target || orientation === this._screenOrientation)
499 return; 511 return;
500 512
501 this._screenOrientation = orientation; 513 this._screenOrientation = orientation;
502 if (!this._screenOrientation) 514 if (!this._screenOrientation)
503 this._target.screenOrientationAgent().clearScreenOrientationOverride (); 515 this._target.screenOrientationAgent().clearScreenOrientationOverride ();
504 else 516 else
505 this._target.screenOrientationAgent().setScreenOrientationOverride(t his._screenOrientation === "landscapePrimary" ? 90 : 0, /** @type {!ScreenOrient ationAgent.OrientationType} */ (this._screenOrientation)); 517 this._target.screenOrientationAgent().setScreenOrientationOverride(t his._screenOrientation === "landscapePrimary" ? 90 : 0, /** @type {!ScreenOrient ationAgent.OrientationType} */ (this._screenOrientation));
506 } 518 }
507 } 519 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698