b2g/components/GlobalSimulatorScreen.jsm

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/b2g/components/GlobalSimulatorScreen.jsm	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,90 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +this.EXPORTED_SYMBOLS = [ 'GlobalSimulatorScreen' ];
     1.9 +
    1.10 +const Cu = Components.utils;
    1.11 +
    1.12 +Cu.import('resource://gre/modules/XPCOMUtils.jsm');
    1.13 +Cu.import('resource://gre/modules/Services.jsm');
    1.14 +
    1.15 +this.GlobalSimulatorScreen = {
    1.16 +  mozOrientationLocked: false,
    1.17 +
    1.18 +  // Actual orientation of apps
    1.19 +  mozOrientation: 'portrait',
    1.20 +
    1.21 +  // The restricted list of actual orientation that can be used
    1.22 +  // if mozOrientationLocked is true
    1.23 +  lockedOrientation: [],
    1.24 +
    1.25 +  // The faked screen orientation
    1.26 +  // if screenOrientation doesn't match mozOrientation due
    1.27 +  // to lockedOrientation restriction, the app will be displayed
    1.28 +  // on the side on desktop
    1.29 +  screenOrientation: 'portrait',
    1.30 +
    1.31 +  // Updated by screen.js
    1.32 +  width: 0, height: 0,
    1.33 +
    1.34 +  lock: function(orientation) {
    1.35 +    this.mozOrientationLocked = true;
    1.36 +
    1.37 +    // Normalize to portrait or landscape,
    1.38 +    // i.e. the possible values of screenOrientation
    1.39 +    function normalize(str) {
    1.40 +      if (str.match(/^portrait/)) {
    1.41 +        return 'portrait';
    1.42 +      } else if (str.match(/^landscape/)) {
    1.43 +        return 'landscape';
    1.44 +      } else {
    1.45 +        return 'portrait';
    1.46 +      }
    1.47 +    }
    1.48 +    this.lockedOrientation = orientation.map(normalize);
    1.49 +
    1.50 +    this.updateOrientation();
    1.51 +  },
    1.52 +
    1.53 +  unlock: function() {
    1.54 +    this.mozOrientationLocked = false;
    1.55 +    this.updateOrientation();
    1.56 +  },
    1.57 +
    1.58 +  updateOrientation: function () {
    1.59 +    let orientation = this.screenOrientation;
    1.60 +
    1.61 +    // If the orientation is locked, we have to ensure ending up with a value
    1.62 +    // of lockedOrientation. If none of lockedOrientation values matches
    1.63 +    // the screen orientation we just choose the first locked orientation.
    1.64 +    // This will be the precise scenario where the app is displayed on the
    1.65 +    // side on desktop!
    1.66 +    if (this.mozOrientationLocked &&
    1.67 +        this.lockedOrientation.indexOf(this.screenOrientation) == -1) {
    1.68 +      orientation = this.lockedOrientation[0];
    1.69 +    }
    1.70 +
    1.71 +    // If the actual orientation changed,
    1.72 +    // we have to fire mozorientation DOM events
    1.73 +    if (this.mozOrientation != orientation) {
    1.74 +      this.mozOrientation = orientation;
    1.75 +
    1.76 +      // Notify each app screen object to fire the event
    1.77 +      Services.obs.notifyObservers(null, 'simulator-orientation-change', null);
    1.78 +    }
    1.79 +
    1.80 +    // Finally, in any case, we update the window size and orientation
    1.81 +    // (Use wrappedJSObject trick to be able to pass a raw JS object)
    1.82 +    Services.obs.notifyObservers({wrappedJSObject:this}, 'simulator-adjust-window-size', null);
    1.83 +  },
    1.84 +
    1.85 +  flipScreen: function() {
    1.86 +    if (this.screenOrientation == 'portrait') {
    1.87 +      this.screenOrientation = 'landscape';
    1.88 +    } else if (this.screenOrientation == 'landscape') {
    1.89 +      this.screenOrientation = 'portrait';
    1.90 +    }
    1.91 +    this.updateOrientation();
    1.92 +  }
    1.93 +}

mercurial