browser/metro/modules/View.jsm

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4 "use strict";
michael@0 5
michael@0 6 this.EXPORTED_SYMBOLS = ["View"];
michael@0 7
michael@0 8 Components.utils.import("resource://gre/modules/PlacesUtils.jsm");
michael@0 9 Components.utils.import("resource:///modules/colorUtils.jsm");
michael@0 10 Components.utils.import("resource://gre/modules/Services.jsm");
michael@0 11 Components.utils.import("resource://gre/modules/Task.jsm");
michael@0 12
michael@0 13 // --------------------------------
michael@0 14 // module helpers
michael@0 15 //
michael@0 16
michael@0 17 function makeURI(aURL, aOriginCharset, aBaseURI) {
michael@0 18 return Services.io.newURI(aURL, aOriginCharset, aBaseURI);
michael@0 19 }
michael@0 20
michael@0 21 // --------------------------------
michael@0 22
michael@0 23
michael@0 24 // --------------------------------
michael@0 25 // View prototype for shared functionality
michael@0 26
michael@0 27 function View(aSet) {
michael@0 28 this._set = aSet;
michael@0 29 this._set.controller = this;
michael@0 30 this._window = aSet.ownerDocument.defaultView;
michael@0 31 this._maxTiles = 8;
michael@0 32 this._tilePrefName = "unknown";
michael@0 33
michael@0 34 this.onResize = () => this._adjustDOMforViewState();
michael@0 35 this._window.addEventListener("resize", this.onResize);
michael@0 36
michael@0 37 ColorUtils.init();
michael@0 38 this._adjustDOMforViewState();
michael@0 39 }
michael@0 40
michael@0 41 View.prototype = {
michael@0 42 set maxTiles(aVal) {
michael@0 43 this._maxTiles = aVal;
michael@0 44 },
michael@0 45
michael@0 46 get maxTiles() {
michael@0 47 return this._maxTiles;
michael@0 48 },
michael@0 49
michael@0 50 set showing(aFlag) {
michael@0 51 // 'vbox' must be defined on objects that inherit from us
michael@0 52 this.vbox.setAttribute("hidden", aFlag ? "false" : "true");
michael@0 53 },
michael@0 54
michael@0 55 set tilePrefName(aStr) {
michael@0 56 // Should be called once on init by objects that inherit from us
michael@0 57 this._tilePrefName = aStr;
michael@0 58 this._maxTiles = Services.prefs.getIntPref(this._tilePrefName);
michael@0 59 Services.prefs.addObserver(this._tilePrefName, this, false);
michael@0 60 },
michael@0 61
michael@0 62 destruct: function () {
michael@0 63 this._window.removeEventListener("resize", this.onResize);
michael@0 64 if (this._tilePrefName != "unknown") {
michael@0 65 Services.prefs.removeObserver(this._tilePrefName, this);
michael@0 66 }
michael@0 67 },
michael@0 68
michael@0 69 _adjustDOMforViewState: function _adjustDOMforViewState(aState) {
michael@0 70 let grid = this._set;
michael@0 71 if (!grid) {
michael@0 72 return;
michael@0 73 }
michael@0 74 if (!aState) {
michael@0 75 aState = grid.getAttribute("viewstate");
michael@0 76 }
michael@0 77 switch (aState) {
michael@0 78 case "snapped":
michael@0 79 grid.setAttribute("nocontext", true);
michael@0 80 grid.selectNone();
michael@0 81 grid.disableCrossSlide();
michael@0 82 break;
michael@0 83 case "portrait":
michael@0 84 grid.removeAttribute("nocontext");
michael@0 85 grid.setAttribute("vertical", true);
michael@0 86 grid.enableCrossSlide();
michael@0 87 break;
michael@0 88 default:
michael@0 89 grid.removeAttribute("nocontext");
michael@0 90 grid.removeAttribute("vertical");
michael@0 91 grid.enableCrossSlide();
michael@0 92 }
michael@0 93 if ("arrangeItems" in grid) {
michael@0 94 grid.arrangeItems();
michael@0 95 }
michael@0 96 },
michael@0 97
michael@0 98 _updateFavicon: function pv__updateFavicon(aItem, aUri) {
michael@0 99 if ("string" == typeof aUri) {
michael@0 100 aUri = makeURI(aUri);
michael@0 101 }
michael@0 102 PlacesUtils.favicons.getFaviconURLForPage(aUri, this._gotIcon.bind(this, aItem));
michael@0 103 },
michael@0 104
michael@0 105 _gotIcon: function pv__gotIcon(aItem, aIconUri) {
michael@0 106 if (!aIconUri) {
michael@0 107 aItem.removeAttribute("iconURI");
michael@0 108 if (aItem.refresh) {
michael@0 109 aItem.refresh();
michael@0 110 }
michael@0 111 return;
michael@0 112 }
michael@0 113 if ("string" == typeof aIconUri) {
michael@0 114 aIconUri = makeURI(aIconUri);
michael@0 115 }
michael@0 116 let faviconURL = (PlacesUtils.favicons.getFaviconLinkForIcon(aIconUri)).spec;
michael@0 117 aItem.iconSrc = faviconURL;
michael@0 118
michael@0 119 let xpFaviconURI = makeURI(faviconURL.replace("moz-anno:favicon:",""));
michael@0 120 Task.spawn(function() {
michael@0 121 let colorInfo = yield ColorUtils.getForegroundAndBackgroundIconColors(xpFaviconURI);
michael@0 122 if (!(colorInfo && colorInfo.background && colorInfo.foreground)) {
michael@0 123 return;
michael@0 124 }
michael@0 125 let { background, foreground } = colorInfo;
michael@0 126 aItem.style.color = foreground; //color text
michael@0 127 aItem.setAttribute("customColor", background);
michael@0 128 let matteColor = 0xffffff; // white
michael@0 129 let alpha = 0.04; // the tint weight
michael@0 130 let [,r,g,b] = background.match(/rgb\((\d+),(\d+),(\d+)/);
michael@0 131 // get the rgb value that represents this color at given opacity over a white matte
michael@0 132 let tintColor = ColorUtils.addRgbColors(matteColor, ColorUtils.createDecimalColorWord(r,g,b,alpha));
michael@0 133 aItem.setAttribute("tintColor", ColorUtils.convertDecimalToRgbColor(tintColor));
michael@0 134 // when bound, use the setter to propogate the color change through the tile
michael@0 135 if ('color' in aItem) {
michael@0 136 aItem.color = background;
michael@0 137 }
michael@0 138 });
michael@0 139 },
michael@0 140
michael@0 141 refreshView: function () {
michael@0 142 },
michael@0 143
michael@0 144 observe: function (aSubject, aTopic, aState) {
michael@0 145 switch (aTopic) {
michael@0 146 case "nsPref:changed": {
michael@0 147 if (aState == this._tilePrefName) {
michael@0 148 let count = Services.prefs.getIntPref(this._tilePrefName);
michael@0 149 this.maxTiles = count;
michael@0 150 this.showing = this.maxTiles > 0;
michael@0 151 this.refreshView();
michael@0 152 }
michael@0 153 break;
michael@0 154 }
michael@0 155 }
michael@0 156 }
michael@0 157 };

mercurial