summaryrefslogtreecommitdiffstats
path: root/b2g/components/GlobalSimulatorScreen.jsm
blob: 2895aef9642d6991f3d8baa288aa889fd8227b95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

this.EXPORTED_SYMBOLS = [ 'GlobalSimulatorScreen' ];

const Cu = Components.utils;

Cu.import('resource://gre/modules/XPCOMUtils.jsm');
Cu.import('resource://gre/modules/Services.jsm');

this.GlobalSimulatorScreen = {
  mozOrientationLocked: false,

  // Actual orientation of apps
  mozOrientation: 'portrait',

  // The restricted list of actual orientation that can be used
  // if mozOrientationLocked is true
  lockedOrientation: [],

  // The faked screen orientation
  // if screenOrientation doesn't match mozOrientation due
  // to lockedOrientation restriction, the app will be displayed
  // on the side on desktop
  screenOrientation: 'portrait',

  // Updated by screen.js
  width: 0, height: 0,

  lock: function(orientation) {
    this.mozOrientationLocked = true;

    // Normalize to portrait or landscape,
    // i.e. the possible values of screenOrientation
    function normalize(str) {
      if (str.match(/^portrait/)) {
        return 'portrait';
      } else if (str.match(/^landscape/)) {
        return 'landscape';
      } else {
        return 'portrait';
      }
    }
    this.lockedOrientation = orientation.map(normalize);

    this.updateOrientation();
  },

  unlock: function() {
    this.mozOrientationLocked = false;
    this.updateOrientation();
  },

  updateOrientation: function () {
    let orientation = this.screenOrientation;

    // If the orientation is locked, we have to ensure ending up with a value
    // of lockedOrientation. If none of lockedOrientation values matches
    // the screen orientation we just choose the first locked orientation.
    // This will be the precise scenario where the app is displayed on the
    // side on desktop!
    if (this.mozOrientationLocked &&
        this.lockedOrientation.indexOf(this.screenOrientation) == -1) {
      orientation = this.lockedOrientation[0];
    }

    // If the actual orientation changed,
    // we have to fire mozorientation DOM events
    if (this.mozOrientation != orientation) {
      this.mozOrientation = orientation;

      // Notify each app screen object to fire the event
      Services.obs.notifyObservers(null, 'simulator-orientation-change', null);
    }

    // Finally, in any case, we update the window size and orientation
    // (Use wrappedJSObject trick to be able to pass a raw JS object)
    Services.obs.notifyObservers({wrappedJSObject:this}, 'simulator-adjust-window-size', null);
  },

  flipScreen: function() {
    if (this.screenOrientation == 'portrait') {
      this.screenOrientation = 'landscape';
    } else if (this.screenOrientation == 'landscape') {
      this.screenOrientation = 'portrait';
    }
    this.updateOrientation();
  }
}