Hi,
I would like to move the API discussion from bug 673922 to a more public
place so we can increase the number of people able to participate. Thus,
it goes without saying that feedbacks are more than welcome.
** Ideas from the bug **
There were various proposals in the bug and amongst the ideas, there were:
1. allowing to set <meta name="viewport"
content="orientation=landscape/portrait orientation-lock=yes/no">;
2. access this trough navigator.viewport;
3. having a beforeorientationchange event that could be canceled with
.preventDefault();
4. JS API with window.lockOrientation() window.unlockOrientation().
I think allowing to set the screen orientation in the viewport meta tag
might be a bit odd. Also, the proposed API seems inadequate and having
only orientation=landscape/portrait should be enough. It's actually what
CSS Device Adaptation [2] has specified.
In one hand I believe having a declarative way would be nice because it
would allow webapps to easily set the orientation and also might prevent
screen flickering if the orientation is set after the first paint. In
the other hand, it might also be nice to keep this API contained in a JS
API. We can easily assume using JS APIs wouldn't bother too much
potential users of this API given that it doesn't aim static websites.
As far as I understand it (2) should be handled by CSS Device Adaptation
[2] specification. And again, allowing changes to the screen orientation
via navigator.viewport.orientation might be confusing.
Regarding (3), we definitely need an event but beforeorientationchange
and allowing to cancel it doesn't seem very useful. If we have a JS API
that allows a webapp to specify which are the allowed orientations, why
would an app block some orientations? In addition, such an event might
make the orientation change slower.
Unless there is an obvious use case I think we shouldn't add this to the
API (at least the first version).
Finally, we definitely need a JS API that might look like (4) proposal
but not in the global scope.
** Proposal **
partial interface DOMScreen {
// Returns the current screen orientation.
readonly attribute DOMString orientation;
// Returns whether locking the screen to |orientation| is allowed.
// Should return false if the |orientation| isn't known by the device
// or if the application can't lock the screen orientation.
boolean orientationAllowed(in DOMString orientation);
// Locks the screen to |orientation| or to the current orientation if
// no value is passed.
// Throws if the application isn't allowed to lock orientation or if
// |orientation| isn't known by the device.
// If this changes the screen orientation, an 'orientationchange'
// event will be fired.
void lockOrientation([optional] in DOMString orientation);
// Unlock the screen orientation.
// Throws if the screen orientation isn't currently locked.
// If this changes the screen orientation, an 'orientationchange'
// event will be fired.
void unlockOrientation();
attribute Function onorientationchange;
};
There are four possible orientations:
- landscape-primary
- landscape-secondary
- portrait-primary
- portrait-secondary
(Feel free to bikeshed the names. Using
landscape-reverse/portrait-reverse or portrait-upsidedown would be ok
but it seems hard to find a better name for *-primary.)
In addition, lockOrientation will accept the following orientations:
- landscape, means any landscape-* orientation;
- portrait, means any portrait-* orientation.
That way, applications will be able to allow the screen to have
different orientations. However, I don't think we need something like
allowOrientations(in DOMString[] orientation) like someone proposed in
the bug because I don't see any reason why an app would like to do that.
If it happens to be needed, we can always add this later.
** Alternatives **
My first idea was an interface with only |attribute DOMString
orientation;| where the authors would be able to set and get the
orientation. That way seems to be more JS style but that would have
created very weird situations like those:
// This might look like a no-op but it's actually locking the screen.
screen.orientation = screen.orientation;
// Setting would be async.
screen.orientation = "portrait-secondary";
alert(screen.orientation); // might be anything, will be
"portrait-secondary" when "orientationchange" event will fire.
// Unlocking would require a special value:
screen.orientation = "default";
// Which means after 'orientationchange' event, screen.orientation
wouldn't be "default" but any value.
** Open questions **
- Maybe we should use a ScreenLockRequest objects when lockOrientation()
and unlockOrientation() are called. That might make the API a bit more
heavy but might make things a bit more clearer too...
- Should we have orientation in the viewport meta tag? If we don't we
need to be sure we can easily set the orientation before the first paint.
** Notes **
CSS3 Media Queries [3] allows authors to use orientation based on
width/height comparison.
[1]
https://bugzilla.mozilla.org/show_bug.cgi?id=673922
[2]
http://dev.w3.org/csswg/css-device-adapt/
[3]
http://www.w3.org/TR/css3-mediaqueries/
Thank you for reading,
--
Mounir