michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: this.EXPORTED_SYMBOLS = [ 'GlobalSimulatorScreen' ]; michael@0: michael@0: const Cu = Components.utils; michael@0: michael@0: Cu.import('resource://gre/modules/XPCOMUtils.jsm'); michael@0: Cu.import('resource://gre/modules/Services.jsm'); michael@0: michael@0: this.GlobalSimulatorScreen = { michael@0: mozOrientationLocked: false, michael@0: michael@0: // Actual orientation of apps michael@0: mozOrientation: 'portrait', michael@0: michael@0: // The restricted list of actual orientation that can be used michael@0: // if mozOrientationLocked is true michael@0: lockedOrientation: [], michael@0: michael@0: // The faked screen orientation michael@0: // if screenOrientation doesn't match mozOrientation due michael@0: // to lockedOrientation restriction, the app will be displayed michael@0: // on the side on desktop michael@0: screenOrientation: 'portrait', michael@0: michael@0: // Updated by screen.js michael@0: width: 0, height: 0, michael@0: michael@0: lock: function(orientation) { michael@0: this.mozOrientationLocked = true; michael@0: michael@0: // Normalize to portrait or landscape, michael@0: // i.e. the possible values of screenOrientation michael@0: function normalize(str) { michael@0: if (str.match(/^portrait/)) { michael@0: return 'portrait'; michael@0: } else if (str.match(/^landscape/)) { michael@0: return 'landscape'; michael@0: } else { michael@0: return 'portrait'; michael@0: } michael@0: } michael@0: this.lockedOrientation = orientation.map(normalize); michael@0: michael@0: this.updateOrientation(); michael@0: }, michael@0: michael@0: unlock: function() { michael@0: this.mozOrientationLocked = false; michael@0: this.updateOrientation(); michael@0: }, michael@0: michael@0: updateOrientation: function () { michael@0: let orientation = this.screenOrientation; michael@0: michael@0: // If the orientation is locked, we have to ensure ending up with a value michael@0: // of lockedOrientation. If none of lockedOrientation values matches michael@0: // the screen orientation we just choose the first locked orientation. michael@0: // This will be the precise scenario where the app is displayed on the michael@0: // side on desktop! michael@0: if (this.mozOrientationLocked && michael@0: this.lockedOrientation.indexOf(this.screenOrientation) == -1) { michael@0: orientation = this.lockedOrientation[0]; michael@0: } michael@0: michael@0: // If the actual orientation changed, michael@0: // we have to fire mozorientation DOM events michael@0: if (this.mozOrientation != orientation) { michael@0: this.mozOrientation = orientation; michael@0: michael@0: // Notify each app screen object to fire the event michael@0: Services.obs.notifyObservers(null, 'simulator-orientation-change', null); michael@0: } michael@0: michael@0: // Finally, in any case, we update the window size and orientation michael@0: // (Use wrappedJSObject trick to be able to pass a raw JS object) michael@0: Services.obs.notifyObservers({wrappedJSObject:this}, 'simulator-adjust-window-size', null); michael@0: }, michael@0: michael@0: flipScreen: function() { michael@0: if (this.screenOrientation == 'portrait') { michael@0: this.screenOrientation = 'landscape'; michael@0: } else if (this.screenOrientation == 'landscape') { michael@0: this.screenOrientation = 'portrait'; michael@0: } michael@0: this.updateOrientation(); michael@0: } michael@0: }