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

Side by Side Diff: Source/devtools/front_end/OverridesSupport.js

Issue 73203003: Refactor how font scale factor appears in the emulation panel (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: getElementsByTagName("input")[0] -> firstChild Created 7 years, 1 month 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
« no previous file with comments | « no previous file | Source/devtools/front_end/OverridesView.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2012 Google Inc. All rights reserved. 2 * Copyright (C) 2012 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 59
60 WebInspector.OverridesSupport.Events = { 60 WebInspector.OverridesSupport.Events = {
61 OverridesWarningUpdated: "OverridesWarningUpdated", 61 OverridesWarningUpdated: "OverridesWarningUpdated",
62 } 62 }
63 63
64 /** 64 /**
65 * @constructor 65 * @constructor
66 * @param {number} width 66 * @param {number} width
67 * @param {number} height 67 * @param {number} height
68 * @param {number} deviceScaleFactor 68 * @param {number} deviceScaleFactor
69 * @param {number} fontScaleFactor
70 * @param {boolean} textAutosizing 69 * @param {boolean} textAutosizing
70 * @param {boolean} useAndroidFontMetrics
71 */ 71 */
72 WebInspector.OverridesSupport.DeviceMetrics = function(width, height, deviceScal eFactor, fontScaleFactor, textAutosizing) 72 WebInspector.OverridesSupport.DeviceMetrics = function(width, height, deviceScal eFactor, textAutosizing, useAndroidFontMetrics)
73 { 73 {
74 this.width = width; 74 this.width = width;
75 this.height = height; 75 this.height = height;
76 this.deviceScaleFactor = deviceScaleFactor; 76 this.deviceScaleFactor = deviceScaleFactor;
77 this.fontScaleFactor = fontScaleFactor;
78 this.textAutosizing = textAutosizing; 77 this.textAutosizing = textAutosizing;
78 this.useAndroidFontMetrics = useAndroidFontMetrics;
79 } 79 }
80 80
81 /** 81 /**
82 * Android computes a font scale factor fudged for improved legibility. This val ue is
83 * not computed on non-Android builds so we duplicate the Android-specific logic here.
84 * For a description of this algorithm, see:
85 * chrome/browser/chrome_content_browser_client.cc, GetFontScaleMultiplier(. ..)
86 *
87 * @param {number} width
88 * @param {number} height
89 * @param {number} deviceScaleFactor
90 * @return {number} fontScaleFactor for Android.
91 */
92 // FIXME(crbug.com/313410): Unify this calculation with the c++ side so we don't duplicate this logic.
93 WebInspector.OverridesSupport.DeviceMetrics._computeFontScaleFactorForAndroid = function(width, height, deviceScaleFactor)
94 {
95 var minWidth = Math.min(width, height) / deviceScaleFactor;
96
97 var kMinFSM = 1.05;
98 var kWidthForMinFSM = 320;
99 var kMaxFSM = 1.3;
100 var kWidthForMaxFSM = 800;
101
102 if (minWidth <= kWidthForMinFSM)
103 return kMinFSM;
104 if (minWidth >= kWidthForMaxFSM)
105 return kMaxFSM;
106
107 // The font scale multiplier varies linearly between kMinFSM and kMaxFSM.
108 var ratio = (minWidth - kWidthForMinFSM) / (kWidthForMaxFSM - kWidthForMinFS M);
109 var fontScaleFactor = ratio * (kMaxFSM - kMinFSM) + kMinFSM;
110 return Math.round(fontScaleFactor * 1000) / 1000;
111 }
112
113 /**
114 * @return {WebInspector.OverridesSupport.DeviceMetrics} 82 * @return {WebInspector.OverridesSupport.DeviceMetrics}
115 */ 83 */
116 WebInspector.OverridesSupport.DeviceMetrics.parseSetting = function(value) 84 WebInspector.OverridesSupport.DeviceMetrics.parseSetting = function(value)
117 { 85 {
118 var width = 0; 86 var width = 0;
119 var height = 0; 87 var height = 0;
120 var deviceScaleFactor = 1; 88 var deviceScaleFactor = 1;
121 var fontScaleFactor = 1;
122 var textAutosizing = false; 89 var textAutosizing = false;
90 var useAndroidFontMetrics = false;
123 if (value) { 91 if (value) {
124 var splitMetrics = value.split("x"); 92 var splitMetrics = value.split("x");
125 if (splitMetrics.length === 5) { 93 if (splitMetrics.length === 5) {
126 width = parseInt(splitMetrics[0], 10); 94 width = parseInt(splitMetrics[0], 10);
127 height = parseInt(splitMetrics[1], 10); 95 height = parseInt(splitMetrics[1], 10);
128 deviceScaleFactor = parseFloat(splitMetrics[2]); 96 deviceScaleFactor = parseFloat(splitMetrics[2]);
129 fontScaleFactor = parseFloat(splitMetrics[3]); 97 useAndroidFontMetrics = splitMetrics[3] == 1;
130 if (fontScaleFactor == 0)
131 fontScaleFactor = WebInspector.OverridesSupport.DeviceMetrics._c omputeFontScaleFactorForAndroid(width, height, deviceScaleFactor);
132 textAutosizing = splitMetrics[4] == 1; 98 textAutosizing = splitMetrics[4] == 1;
133 } 99 }
134 } 100 }
135 return new WebInspector.OverridesSupport.DeviceMetrics(width, height, device ScaleFactor, fontScaleFactor, textAutosizing); 101 return new WebInspector.OverridesSupport.DeviceMetrics(width, height, device ScaleFactor, textAutosizing, useAndroidFontMetrics);
136 } 102 }
137 103
138 /** 104 /**
139 * @return {?WebInspector.OverridesSupport.DeviceMetrics} 105 * @return {?WebInspector.OverridesSupport.DeviceMetrics}
140 */ 106 */
141 WebInspector.OverridesSupport.DeviceMetrics.parseUserInput = function(widthStrin g, heightString, deviceScaleFactorString, fontScaleFactorString, textAutosizing) 107 WebInspector.OverridesSupport.DeviceMetrics.parseUserInput = function(widthStrin g, heightString, deviceScaleFactorString, textAutosizing, useAndroidFontMetrics)
142 { 108 {
143 function isUserInputValid(value, isInteger) 109 function isUserInputValid(value, isInteger)
144 { 110 {
145 if (!value) 111 if (!value)
146 return true; 112 return true;
147 return isInteger ? /^[0]*[1-9][\d]*$/.test(value) : /^[0]*([1-9][\d]*(\. \d+)?|\.\d+)$/.test(value); 113 return isInteger ? /^[0]*[1-9][\d]*$/.test(value) : /^[0]*([1-9][\d]*(\. \d+)?|\.\d+)$/.test(value);
148 } 114 }
149 115
150 if (!widthString ^ !heightString) 116 if (!widthString ^ !heightString)
151 return null; 117 return null;
152 118
153 var isWidthValid = isUserInputValid(widthString, true); 119 var isWidthValid = isUserInputValid(widthString, true);
154 var isHeightValid = isUserInputValid(heightString, true); 120 var isHeightValid = isUserInputValid(heightString, true);
155 var isDeviceScaleFactorValid = isUserInputValid(deviceScaleFactorString, fal se); 121 var isDeviceScaleFactorValid = isUserInputValid(deviceScaleFactorString, fal se);
156 var isFontScaleFactorValid = isUserInputValid(fontScaleFactorString, false);
157 122
158 if (!isWidthValid && !isHeightValid && !isDeviceScaleFactorValid && !isFontS caleFactorValid) 123 if (!isWidthValid && !isHeightValid && !isDeviceScaleFactorValid)
159 return null; 124 return null;
160 125
161 var width = isWidthValid ? parseInt(widthString || "0", 10) : -1; 126 var width = isWidthValid ? parseInt(widthString || "0", 10) : -1;
162 var height = isHeightValid ? parseInt(heightString || "0", 10) : -1; 127 var height = isHeightValid ? parseInt(heightString || "0", 10) : -1;
163 var deviceScaleFactor = isDeviceScaleFactorValid ? parseFloat(deviceScaleFac torString) : -1; 128 var deviceScaleFactor = isDeviceScaleFactorValid ? parseFloat(deviceScaleFac torString) : -1;
164 var fontScaleFactor = isFontScaleFactorValid ? parseFloat(fontScaleFactorStr ing) : -1;
165 129
166 return new WebInspector.OverridesSupport.DeviceMetrics(width, height, device ScaleFactor, fontScaleFactor, textAutosizing); 130 return new WebInspector.OverridesSupport.DeviceMetrics(width, height, device ScaleFactor, textAutosizing, useAndroidFontMetrics);
167 } 131 }
168 132
169 WebInspector.OverridesSupport.DeviceMetrics.prototype = { 133 WebInspector.OverridesSupport.DeviceMetrics.prototype = {
170 /** 134 /**
171 * @return {boolean} 135 * @return {boolean}
172 */ 136 */
173 isValid: function() 137 isValid: function()
174 { 138 {
175 return this.isWidthValid() && this.isHeightValid() && this.isDeviceScale FactorValid() && this.isFontScaleFactorValid(); 139 return this.isWidthValid() && this.isHeightValid() && this.isDeviceScale FactorValid();
176 }, 140 },
177 141
178 /** 142 /**
179 * @return {boolean} 143 * @return {boolean}
180 */ 144 */
181 isWidthValid: function() 145 isWidthValid: function()
182 { 146 {
183 return this.width >= 0; 147 return this.width >= 0;
184 }, 148 },
185 149
186 /** 150 /**
187 * @return {boolean} 151 * @return {boolean}
188 */ 152 */
189 isHeightValid: function() 153 isHeightValid: function()
190 { 154 {
191 return this.height >= 0; 155 return this.height >= 0;
192 }, 156 },
193 157
194 /** 158 /**
195 * @return {boolean} 159 * @return {boolean}
196 */ 160 */
197 isDeviceScaleFactorValid: function() 161 isDeviceScaleFactorValid: function()
198 { 162 {
199 return this.deviceScaleFactor > 0; 163 return this.deviceScaleFactor > 0;
200 }, 164 },
201 165
202 /** 166 /**
203 * @return {boolean} 167 * @return {boolean}
204 */ 168 */
205 isFontScaleFactorValid: function() 169 isUseAndroidFontMetricsDisabled: function()
206 { 170 {
207 return this.fontScaleFactor > 0; 171 return !this.textAutosizing;
208 }, 172 },
209 173
210 /** 174 /**
211 * @return {boolean}
212 */
213 isTextAutosizingValid: function()
214 {
215 return true;
216 },
217
218 /**
219 * @return {string} 175 * @return {string}
220 */ 176 */
221 toSetting: function() 177 toSetting: function()
222 { 178 {
223 if (!this.isValid()) 179 if (!this.isValid())
224 return ""; 180 return "";
225 181
226 return this.width && this.height ? this.width + "x" + this.height + "x" + this.deviceScaleFactor + "x" + this.fontScaleFactor + "x" + (this.textAutosizi ng ? "1" : "0") : ""; 182 return this.width && this.height ? this.width + "x" + this.height + "x" + this.deviceScaleFactor + "x" + (this.useAndroidFontMetrics ? "1" : "0") + "x" + (this.textAutosizing ? "1" : "0") : "";
227 }, 183 },
228 184
229 /** 185 /**
230 * @return {string} 186 * @return {string}
231 */ 187 */
232 widthToInput: function() 188 widthToInput: function()
233 { 189 {
234 return this.isWidthValid() && this.width ? String(this.width) : ""; 190 return this.isWidthValid() && this.width ? String(this.width) : "";
235 }, 191 },
236 192
237 /** 193 /**
238 * @return {string} 194 * @return {string}
239 */ 195 */
240 heightToInput: function() 196 heightToInput: function()
241 { 197 {
242 return this.isHeightValid() && this.height ? String(this.height) : ""; 198 return this.isHeightValid() && this.height ? String(this.height) : "";
243 }, 199 },
244 200
245 /** 201 /**
246 * @return {string} 202 * @return {string}
247 */ 203 */
248 deviceScaleFactorToInput: function() 204 deviceScaleFactorToInput: function()
249 { 205 {
250 return this.isDeviceScaleFactorValid() && this.deviceScaleFactor ? Strin g(this.deviceScaleFactor) : ""; 206 return this.isDeviceScaleFactorValid() && this.deviceScaleFactor ? Strin g(this.deviceScaleFactor) : "";
251 }, 207 },
252 208
253 /** 209 /**
254 * @return {string} 210 * Compute the font scale factor.
211 *
212 * Android uses a device scale adjustment for fonts used in text autosizing for improved
213 * legibility. This function computes this adjusted value if useAndroidFontM etrics is true.
214 *
215 * For a description of the Android device scale adjustment algorithm, see:
216 * chrome/browser/chrome_content_browser_client.cc, GetFontScaleMultipli er(...)
217 *
218 * @return {number} font scale factor for Android if useAndroidFontMetrics, or 1.
255 */ 219 */
256 fontScaleFactorToInput: function() 220 fontScaleFactor: function()
257 { 221 {
258 return this.isFontScaleFactorValid() && this.fontScaleFactor ? String(th is.fontScaleFactor) : ""; 222 if (this.useAndroidFontMetrics && this.isValid()) {
223 var minWidth = Math.min(this.width, this.height) / this.deviceScaleF actor;
224
225 var kMinFSM = 1.05;
226 var kWidthForMinFSM = 320;
227 var kMaxFSM = 1.3;
228 var kWidthForMaxFSM = 800;
229
230 if (minWidth <= kWidthForMinFSM)
231 return kMinFSM;
232 if (minWidth >= kWidthForMaxFSM)
233 return kMaxFSM;
234
235 // The font scale multiplier varies linearly between kMinFSM and kMa xFSM.
236 var ratio = (minWidth - kWidthForMinFSM) / (kWidthForMaxFSM - kWidth ForMinFSM);
237
238 return ratio * (kMaxFSM - kMinFSM) + kMinFSM;
239 }
240
241 return 1;
259 } 242 }
260 } 243 }
261 244
262 /** 245 /**
263 * @constructor 246 * @constructor
264 * @param {number} latitude 247 * @param {number} latitude
265 * @param {number} longitude 248 * @param {number} longitude
266 */ 249 */
267 WebInspector.OverridesSupport.GeolocationPosition = function(latitude, longitude , error) 250 WebInspector.OverridesSupport.GeolocationPosition = function(latitude, longitude , error)
268 { 251 {
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after
461 444
462 var dipWidth = Math.round(metrics.width / metrics.deviceScaleFactor); 445 var dipWidth = Math.round(metrics.width / metrics.deviceScaleFactor);
463 var dipHeight = Math.round(metrics.height / metrics.deviceScaleFactor); 446 var dipHeight = Math.round(metrics.height / metrics.deviceScaleFactor);
464 447
465 // Disable override without checks. 448 // Disable override without checks.
466 if (dipWidth && dipHeight && WebInspector.isInspectingDevice()) { 449 if (dipWidth && dipHeight && WebInspector.isInspectingDevice()) {
467 this._updateWarningMessage(WebInspector.UIString("Screen emulation o n the device is not available.")); 450 this._updateWarningMessage(WebInspector.UIString("Screen emulation o n the device is not available."));
468 return; 451 return;
469 } 452 }
470 453
471 PageAgent.setDeviceMetricsOverride(dipWidth, dipHeight, metrics.deviceSc aleFactor, WebInspector.settings.emulateViewport.get(), WebInspector.settings.de viceFitWindow.get(), metrics.textAutosizing, metrics.fontScaleFactor, apiCallbac k.bind(this)); 454 PageAgent.setDeviceMetricsOverride(dipWidth, dipHeight, metrics.deviceSc aleFactor, WebInspector.settings.emulateViewport.get(), WebInspector.settings.de viceFitWindow.get(), metrics.textAutosizing, metrics.fontScaleFactor(), apiCallb ack.bind(this));
472 455
473 /** 456 /**
474 * param {?Protocol.Error} error 457 * param {?Protocol.Error} error
475 */ 458 */
476 function apiCallback(error) 459 function apiCallback(error)
477 { 460 {
478 if (error) { 461 if (error) {
479 this._updateWarningMessage(WebInspector.UIString("Screen emulati on is not available on this page.")); 462 this._updateWarningMessage(WebInspector.UIString("Screen emulati on is not available on this page."));
480 return; 463 return;
481 } 464 }
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
566 }, 549 },
567 550
568 __proto__: WebInspector.Object.prototype 551 __proto__: WebInspector.Object.prototype
569 } 552 }
570 553
571 554
572 /** 555 /**
573 * @type {WebInspector.OverridesSupport} 556 * @type {WebInspector.OverridesSupport}
574 */ 557 */
575 WebInspector.overridesSupport; 558 WebInspector.overridesSupport;
OLDNEW
« no previous file with comments | « no previous file | Source/devtools/front_end/OverridesView.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698