Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
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 | this.EXPORTED_SYMBOLS = [ 'GlobalSimulatorScreen' ]; |
michael@0 | 6 | |
michael@0 | 7 | const Cu = Components.utils; |
michael@0 | 8 | |
michael@0 | 9 | Cu.import('resource://gre/modules/XPCOMUtils.jsm'); |
michael@0 | 10 | Cu.import('resource://gre/modules/Services.jsm'); |
michael@0 | 11 | |
michael@0 | 12 | this.GlobalSimulatorScreen = { |
michael@0 | 13 | mozOrientationLocked: false, |
michael@0 | 14 | |
michael@0 | 15 | // Actual orientation of apps |
michael@0 | 16 | mozOrientation: 'portrait', |
michael@0 | 17 | |
michael@0 | 18 | // The restricted list of actual orientation that can be used |
michael@0 | 19 | // if mozOrientationLocked is true |
michael@0 | 20 | lockedOrientation: [], |
michael@0 | 21 | |
michael@0 | 22 | // The faked screen orientation |
michael@0 | 23 | // if screenOrientation doesn't match mozOrientation due |
michael@0 | 24 | // to lockedOrientation restriction, the app will be displayed |
michael@0 | 25 | // on the side on desktop |
michael@0 | 26 | screenOrientation: 'portrait', |
michael@0 | 27 | |
michael@0 | 28 | // Updated by screen.js |
michael@0 | 29 | width: 0, height: 0, |
michael@0 | 30 | |
michael@0 | 31 | lock: function(orientation) { |
michael@0 | 32 | this.mozOrientationLocked = true; |
michael@0 | 33 | |
michael@0 | 34 | // Normalize to portrait or landscape, |
michael@0 | 35 | // i.e. the possible values of screenOrientation |
michael@0 | 36 | function normalize(str) { |
michael@0 | 37 | if (str.match(/^portrait/)) { |
michael@0 | 38 | return 'portrait'; |
michael@0 | 39 | } else if (str.match(/^landscape/)) { |
michael@0 | 40 | return 'landscape'; |
michael@0 | 41 | } else { |
michael@0 | 42 | return 'portrait'; |
michael@0 | 43 | } |
michael@0 | 44 | } |
michael@0 | 45 | this.lockedOrientation = orientation.map(normalize); |
michael@0 | 46 | |
michael@0 | 47 | this.updateOrientation(); |
michael@0 | 48 | }, |
michael@0 | 49 | |
michael@0 | 50 | unlock: function() { |
michael@0 | 51 | this.mozOrientationLocked = false; |
michael@0 | 52 | this.updateOrientation(); |
michael@0 | 53 | }, |
michael@0 | 54 | |
michael@0 | 55 | updateOrientation: function () { |
michael@0 | 56 | let orientation = this.screenOrientation; |
michael@0 | 57 | |
michael@0 | 58 | // If the orientation is locked, we have to ensure ending up with a value |
michael@0 | 59 | // of lockedOrientation. If none of lockedOrientation values matches |
michael@0 | 60 | // the screen orientation we just choose the first locked orientation. |
michael@0 | 61 | // This will be the precise scenario where the app is displayed on the |
michael@0 | 62 | // side on desktop! |
michael@0 | 63 | if (this.mozOrientationLocked && |
michael@0 | 64 | this.lockedOrientation.indexOf(this.screenOrientation) == -1) { |
michael@0 | 65 | orientation = this.lockedOrientation[0]; |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | // If the actual orientation changed, |
michael@0 | 69 | // we have to fire mozorientation DOM events |
michael@0 | 70 | if (this.mozOrientation != orientation) { |
michael@0 | 71 | this.mozOrientation = orientation; |
michael@0 | 72 | |
michael@0 | 73 | // Notify each app screen object to fire the event |
michael@0 | 74 | Services.obs.notifyObservers(null, 'simulator-orientation-change', null); |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | // Finally, in any case, we update the window size and orientation |
michael@0 | 78 | // (Use wrappedJSObject trick to be able to pass a raw JS object) |
michael@0 | 79 | Services.obs.notifyObservers({wrappedJSObject:this}, 'simulator-adjust-window-size', null); |
michael@0 | 80 | }, |
michael@0 | 81 | |
michael@0 | 82 | flipScreen: function() { |
michael@0 | 83 | if (this.screenOrientation == 'portrait') { |
michael@0 | 84 | this.screenOrientation = 'landscape'; |
michael@0 | 85 | } else if (this.screenOrientation == 'landscape') { |
michael@0 | 86 | this.screenOrientation = 'portrait'; |
michael@0 | 87 | } |
michael@0 | 88 | this.updateOrientation(); |
michael@0 | 89 | } |
michael@0 | 90 | } |