OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 cr.define('cr.icon', function() { | 5 cr.define('cr.icon', function() { |
6 /** | 6 /** |
7 * @return {!Array<number>} The scale factors supported by this platform for | 7 * @return {!Array<number>} The scale factors supported by this platform for |
8 * webui resources. | 8 * webui resources. |
9 */ | 9 */ |
10 function getSupportedScaleFactors() { | 10 function getSupportedScaleFactors() { |
11 var supportedScaleFactors = []; | 11 var supportedScaleFactors = []; |
12 if (cr.isMac || cr.isChromeOS || cr.isWindows || cr.isLinux) { | 12 if (cr.isMac || cr.isChromeOS || cr.isWindows || cr.isLinux) { |
13 // All desktop platforms support zooming which also updates the | 13 // All desktop platforms support zooming which also updates the |
14 // renderer's device scale factors (a.k.a devicePixelRatio), and | 14 // renderer's device scale factors (a.k.a devicePixelRatio), and |
15 // these platforms has high DPI assets for 2.0x. Use 1x and 2x in | 15 // these platforms has high DPI assets for 2.0x. Use 1x and 2x in |
16 // image-set on these platforms so that the renderer can pick the | 16 // image-set on these platforms so that the renderer can pick the |
17 // closest image for the current device scale factor. | 17 // closest image for the current device scale factor. |
18 supportedScaleFactors.push(1); | 18 supportedScaleFactors.push(1); |
19 supportedScaleFactors.push(2); | 19 supportedScaleFactors.push(2); |
20 } else { | 20 } else { |
21 // For other platforms that use fixed device scale factor, use | 21 // For other platforms that use fixed device scale factor, use |
22 // the window's device pixel ratio. | 22 // the window's device pixel ratio. |
23 // TODO(oshima): Investigate if Android/iOS need to use image-set. | 23 // TODO(oshima): Investigate if Android/iOS need to use image-set. |
24 supportedScaleFactors.push(window.devicePixelRatio); | 24 supportedScaleFactors.push(window.devicePixelRatio); |
25 } | 25 } |
26 return supportedScaleFactors; | 26 return supportedScaleFactors; |
27 } | 27 } |
28 | 28 |
29 /** | 29 /** |
30 * Returns the URL of the image, or an image set of URLs for the profile | |
31 * avatar. Default avatars have resources available for multiple scalefactors, | |
32 * whereas the GAIA profile image only comes in one size. | |
33 * | |
34 * @param {string} path The path of the image. | |
35 * @return {string} The url, or an image set of URLs of the avatar image. | |
36 */ | |
37 function getProfileAvatarIcon(path) { | |
38 var chromeThemePath = 'chrome://theme'; | |
39 var isDefaultAvatar = | |
40 (path.slice(0, chromeThemePath.length) == chromeThemePath); | |
41 return isDefaultAvatar ? imageset(path + '@scalefactorx'): url(path); | |
42 } | |
43 | |
44 /** | |
45 * Generates a CSS -webkit-image-set for a chrome:// url. | 30 * Generates a CSS -webkit-image-set for a chrome:// url. |
46 * An entry in the image set is added for each of getSupportedScaleFactors(). | 31 * An entry in the image set is added for each of getSupportedScaleFactors(). |
47 * The scale-factor-specific url is generated by replacing the first instance | 32 * The scale-factor-specific url is generated by replacing the first instance |
48 * of 'scalefactor' in |path| with the numeric scale factor. | 33 * of 'scalefactor' in |path| with the numeric scale factor. |
34 * | |
49 * @param {string} path The URL to generate an image set for. | 35 * @param {string} path The URL to generate an image set for. |
50 * 'scalefactor' should be a substring of |path|. | 36 * 'scalefactor' should be a substring of |path|. |
51 * @return {string} The CSS -webkit-image-set. | 37 * @return {string} The CSS -webkit-image-set. |
52 */ | 38 */ |
53 function imageset(path) { | 39 function getImageSet(path) { |
54 var supportedScaleFactors = getSupportedScaleFactors(); | 40 var supportedScaleFactors = getSupportedScaleFactors(); |
55 | 41 |
56 var replaceStartIndex = path.indexOf('scalefactor'); | 42 var replaceStartIndex = path.indexOf('scalefactor'); |
57 if (replaceStartIndex < 0) | 43 if (replaceStartIndex < 0) |
58 return url(path); | 44 return url(path); |
59 | 45 |
60 var s = ''; | 46 var s = ''; |
61 for (var i = 0; i < supportedScaleFactors.length; ++i) { | 47 for (var i = 0; i < supportedScaleFactors.length; ++i) { |
62 var scaleFactor = supportedScaleFactors[i]; | 48 var scaleFactor = supportedScaleFactors[i]; |
63 var pathWithScaleFactor = path.substr(0, replaceStartIndex) + | 49 var pathWithScaleFactor = path.substr(0, replaceStartIndex) + |
64 scaleFactor + path.substr(replaceStartIndex + 'scalefactor'.length); | 50 scaleFactor + path.substr(replaceStartIndex + 'scalefactor'.length); |
65 | 51 |
66 s += url(pathWithScaleFactor) + ' ' + scaleFactor + 'x'; | 52 s += url(pathWithScaleFactor) + ' ' + scaleFactor + 'x'; |
67 | 53 |
68 if (i != supportedScaleFactors.length - 1) | 54 if (i != supportedScaleFactors.length - 1) |
69 s += ', '; | 55 s += ', '; |
70 } | 56 } |
71 return '-webkit-image-set(' + s + ')'; | 57 return '-webkit-image-set(' + s + ')'; |
72 } | 58 } |
73 | 59 |
74 /** | 60 /** |
61 * Returns the URL of the image, or an image set of URLs for the provided | |
62 * path. Resources in chrome://theme have multiple supported scale factors. | |
michaelpg
2016/09/02 20:00:38
opt nit: single space after period for consistency
| |
63 * | |
64 * @param {string} path The path of the image. | |
65 * @return {string} The url, or an image set of URLs. | |
66 */ | |
67 function getImage(path) { | |
68 var chromeThemePath = 'chrome://theme'; | |
69 var isChromeThemeUrl = | |
70 (path.slice(0, chromeThemePath.length) == chromeThemePath); | |
71 return isChromeThemeUrl ? getImageSet(path + '@scalefactorx') : url(path); | |
72 } | |
73 | |
74 /** | |
75 * A regular expression for identifying favicon URLs. | 75 * A regular expression for identifying favicon URLs. |
76 * @const {!RegExp} | 76 * @const {!RegExp} |
77 */ | 77 */ |
78 var FAVICON_URL_REGEX = /\.ico$/i; | 78 var FAVICON_URL_REGEX = /\.ico$/i; |
79 | 79 |
80 /** | 80 /** |
81 * Creates a CSS -webkit-image-set for a favicon request. | 81 * Creates a CSS -webkit-image-set for a favicon request. |
82 * | |
82 * @param {string} url Either the URL of the original page or of the favicon | 83 * @param {string} url Either the URL of the original page or of the favicon |
83 * itself. | 84 * itself. |
84 * @param {number=} opt_size Optional preferred size of the favicon. | 85 * @param {number=} opt_size Optional preferred size of the favicon. |
85 * @param {string=} opt_type Optional type of favicon to request. Valid values | 86 * @param {string=} opt_type Optional type of favicon to request. Valid values |
86 * are 'favicon' and 'touch-icon'. Default is 'favicon'. | 87 * are 'favicon' and 'touch-icon'. Default is 'favicon'. |
87 * @return {string} -webkit-image-set for the favicon. | 88 * @return {string} -webkit-image-set for the favicon. |
88 */ | 89 */ |
89 function getFaviconImageSet(url, opt_size, opt_type) { | 90 function getFavicon(url, opt_size, opt_type) { |
90 var size = opt_size || 16; | 91 var size = opt_size || 16; |
91 var type = opt_type || 'favicon'; | 92 var type = opt_type || 'favicon'; |
92 | 93 |
93 return imageset( | 94 return getImageSet( |
94 'chrome://' + type + '/size/' + size + '@scalefactorx/' + | 95 'chrome://' + type + '/size/' + size + '@scalefactorx/' + |
95 // Note: Literal 'iconurl' must match |kIconURLParameter| in | 96 // Note: Literal 'iconurl' must match |kIconURLParameter| in |
96 // components/favicon_base/favicon_url_parser.cc. | 97 // components/favicon_base/favicon_url_parser.cc. |
97 (FAVICON_URL_REGEX.test(url) ? 'iconurl/' : '') + url); | 98 (FAVICON_URL_REGEX.test(url) ? 'iconurl/' : '') + url); |
98 } | 99 } |
99 | 100 |
100 return { | 101 return { |
101 getSupportedScaleFactors: getSupportedScaleFactors, | 102 getImage: getImage, |
102 getProfileAvatarIcon: getProfileAvatarIcon, | 103 getFavicon: getFavicon, |
103 getFaviconImageSet: getFaviconImageSet, | |
104 }; | 104 }; |
105 }); | 105 }); |
OLD | NEW |