browser/components/shell/content/setDesktopBackground.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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
michael@0 5 var Ci = Components.interfaces;
michael@0 6
michael@0 7 var gSetBackground = {
michael@0 8 #ifndef XP_MACOSX
michael@0 9 _position : "",
michael@0 10 _backgroundColor : 0,
michael@0 11 #else
michael@0 12 _position : "STRETCH",
michael@0 13 #endif
michael@0 14 _screenWidth : 0,
michael@0 15 _screenHeight : 0,
michael@0 16 _image : null,
michael@0 17 _canvas : null,
michael@0 18
michael@0 19 get _shell()
michael@0 20 {
michael@0 21 return Components.classes["@mozilla.org/browser/shell-service;1"]
michael@0 22 .getService(Ci.nsIShellService);
michael@0 23 },
michael@0 24
michael@0 25 load: function ()
michael@0 26 {
michael@0 27 this._canvas = document.getElementById("screen");
michael@0 28 this._screenWidth = screen.width;
michael@0 29 this._screenHeight = screen.height;
michael@0 30 #ifdef XP_MACOSX
michael@0 31 document.documentElement.getButton("accept").hidden = true;
michael@0 32 #endif
michael@0 33 if (this._screenWidth / this._screenHeight >= 1.6)
michael@0 34 document.getElementById("monitor").setAttribute("aspectratio", "16:10");
michael@0 35
michael@0 36 #ifdef XP_WIN
michael@0 37 // hide fill + fit options if <win7 since don't work
michael@0 38 var version = Components.classes["@mozilla.org/system-info;1"]
michael@0 39 .getService(Ci.nsIPropertyBag2)
michael@0 40 .getProperty("version");
michael@0 41 var isWindows7OrHigher = (parseFloat(version) >= 6.1);
michael@0 42 if (!isWindows7OrHigher) {
michael@0 43 document.getElementById("fillPosition").hidden = true;
michael@0 44 document.getElementById("fitPosition").hidden = true;
michael@0 45 }
michael@0 46 #endif
michael@0 47
michael@0 48 // make sure that the correct dimensions will be used
michael@0 49 setTimeout(function(self) {
michael@0 50 self.init(window.arguments[0]);
michael@0 51 }, 0, this);
michael@0 52 },
michael@0 53
michael@0 54 init: function (aImage)
michael@0 55 {
michael@0 56 this._image = aImage;
michael@0 57
michael@0 58 // set the size of the coordinate space
michael@0 59 this._canvas.width = this._canvas.clientWidth;
michael@0 60 this._canvas.height = this._canvas.clientHeight;
michael@0 61
michael@0 62 var ctx = this._canvas.getContext("2d");
michael@0 63 ctx.scale(this._canvas.clientWidth / this._screenWidth, this._canvas.clientHeight / this._screenHeight);
michael@0 64
michael@0 65 #ifndef XP_MACOSX
michael@0 66 this._initColor();
michael@0 67 #else
michael@0 68 // Make sure to reset the button state in case the user has already
michael@0 69 // set an image as their desktop background.
michael@0 70 var setDesktopBackground = document.getElementById("setDesktopBackground");
michael@0 71 setDesktopBackground.hidden = false;
michael@0 72 var bundle = document.getElementById("backgroundBundle");
michael@0 73 setDesktopBackground.label = bundle.getString("DesktopBackgroundSet");
michael@0 74 setDesktopBackground.disabled = false;
michael@0 75
michael@0 76 document.getElementById("showDesktopPreferences").hidden = true;
michael@0 77 #endif
michael@0 78 this.updatePosition();
michael@0 79 },
michael@0 80
michael@0 81 #ifndef XP_MACOSX
michael@0 82 _initColor: function ()
michael@0 83 {
michael@0 84 var color = this._shell.desktopBackgroundColor;
michael@0 85
michael@0 86 const rMask = 4294901760;
michael@0 87 const gMask = 65280;
michael@0 88 const bMask = 255;
michael@0 89 var r = (color & rMask) >> 16;
michael@0 90 var g = (color & gMask) >> 8;
michael@0 91 var b = (color & bMask);
michael@0 92 this.updateColor(this._rgbToHex(r, g, b));
michael@0 93
michael@0 94 var colorpicker = document.getElementById("desktopColor");
michael@0 95 colorpicker.color = this._backgroundColor;
michael@0 96 },
michael@0 97
michael@0 98 updateColor: function (aColor)
michael@0 99 {
michael@0 100 this._backgroundColor = aColor;
michael@0 101 this._canvas.style.backgroundColor = aColor;
michael@0 102 },
michael@0 103
michael@0 104 // Converts a color string in the format "#RRGGBB" to an integer.
michael@0 105 _hexStringToLong: function (aString)
michael@0 106 {
michael@0 107 return parseInt(aString.substring(1,3), 16) << 16 |
michael@0 108 parseInt(aString.substring(3,5), 16) << 8 |
michael@0 109 parseInt(aString.substring(5,7), 16);
michael@0 110 },
michael@0 111
michael@0 112 _rgbToHex: function (aR, aG, aB)
michael@0 113 {
michael@0 114 return "#" + [aR, aG, aB].map(function(aInt) aInt.toString(16).replace(/^(.)$/, "0$1"))
michael@0 115 .join("").toUpperCase();
michael@0 116 },
michael@0 117 #else
michael@0 118 observe: function (aSubject, aTopic, aData)
michael@0 119 {
michael@0 120 if (aTopic == "shell:desktop-background-changed") {
michael@0 121 document.getElementById("setDesktopBackground").hidden = true;
michael@0 122 document.getElementById("showDesktopPreferences").hidden = false;
michael@0 123
michael@0 124 Components.classes["@mozilla.org/observer-service;1"]
michael@0 125 .getService(Ci.nsIObserverService)
michael@0 126 .removeObserver(this, "shell:desktop-background-changed");
michael@0 127 }
michael@0 128 },
michael@0 129
michael@0 130 showDesktopPrefs: function()
michael@0 131 {
michael@0 132 this._shell.openApplication(Ci.nsIMacShellService.APPLICATION_DESKTOP);
michael@0 133 },
michael@0 134 #endif
michael@0 135
michael@0 136 setDesktopBackground: function ()
michael@0 137 {
michael@0 138 #ifndef XP_MACOSX
michael@0 139 document.persist("menuPosition", "value");
michael@0 140 this._shell.desktopBackgroundColor = this._hexStringToLong(this._backgroundColor);
michael@0 141 #else
michael@0 142 Components.classes["@mozilla.org/observer-service;1"]
michael@0 143 .getService(Ci.nsIObserverService)
michael@0 144 .addObserver(this, "shell:desktop-background-changed", false);
michael@0 145
michael@0 146 var bundle = document.getElementById("backgroundBundle");
michael@0 147 var setDesktopBackground = document.getElementById("setDesktopBackground");
michael@0 148 setDesktopBackground.disabled = true;
michael@0 149 setDesktopBackground.label = bundle.getString("DesktopBackgroundDownloading");
michael@0 150 #endif
michael@0 151 this._shell.setDesktopBackground(this._image,
michael@0 152 Ci.nsIShellService["BACKGROUND_" + this._position]);
michael@0 153 },
michael@0 154
michael@0 155 updatePosition: function ()
michael@0 156 {
michael@0 157 var ctx = this._canvas.getContext("2d");
michael@0 158 ctx.clearRect(0, 0, this._screenWidth, this._screenHeight);
michael@0 159
michael@0 160 #ifndef XP_MACOSX
michael@0 161 this._position = document.getElementById("menuPosition").value;
michael@0 162 #endif
michael@0 163
michael@0 164 switch (this._position) {
michael@0 165 case "TILE":
michael@0 166 ctx.save();
michael@0 167 ctx.fillStyle = ctx.createPattern(this._image, "repeat");
michael@0 168 ctx.fillRect(0, 0, this._screenWidth, this._screenHeight);
michael@0 169 ctx.restore();
michael@0 170 break;
michael@0 171 case "STRETCH":
michael@0 172 ctx.drawImage(this._image, 0, 0, this._screenWidth, this._screenHeight);
michael@0 173 break;
michael@0 174 case "CENTER":
michael@0 175 var x = (this._screenWidth - this._image.naturalWidth) / 2;
michael@0 176 var y = (this._screenHeight - this._image.naturalHeight) / 2;
michael@0 177 ctx.drawImage(this._image, x, y);
michael@0 178 break;
michael@0 179 case "FILL":
michael@0 180 //Try maxing width first, overflow height
michael@0 181 var widthRatio = this._screenWidth / this._image.naturalWidth;
michael@0 182 var width = this._image.naturalWidth * widthRatio;
michael@0 183 var height = this._image.naturalHeight * widthRatio;
michael@0 184 if (height < this._screenHeight) {
michael@0 185 //height less than screen, max height and overflow width
michael@0 186 var heightRatio = this._screenHeight / this._image.naturalHeight;
michael@0 187 width = this._image.naturalWidth * heightRatio;
michael@0 188 height = this._image.naturalHeight * heightRatio;
michael@0 189 }
michael@0 190 var x = (this._screenWidth - width) / 2;
michael@0 191 var y = (this._screenHeight - height) / 2;
michael@0 192 ctx.drawImage(this._image, x, y, width, height);
michael@0 193 break;
michael@0 194 case "FIT":
michael@0 195 //Try maxing width first, top and bottom borders
michael@0 196 var widthRatio = this._screenWidth / this._image.naturalWidth;
michael@0 197 var width = this._image.naturalWidth * widthRatio;
michael@0 198 var height = this._image.naturalHeight * widthRatio;
michael@0 199 var x = 0;
michael@0 200 var y = (this._screenHeight - height) / 2;
michael@0 201 if (height > this._screenHeight) {
michael@0 202 //height overflow, maximise height, side borders
michael@0 203 var heightRatio = this._screenHeight / this._image.naturalHeight;
michael@0 204 width = this._image.naturalWidth * heightRatio;
michael@0 205 height = this._image.naturalHeight * heightRatio;
michael@0 206 x = (this._screenWidth - width) / 2;
michael@0 207 y = 0;
michael@0 208 }
michael@0 209 ctx.drawImage(this._image, x, y, width, height);
michael@0 210 break;
michael@0 211 }
michael@0 212 }
michael@0 213 };

mercurial