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