Skip to content

Commit a8e48bd

Browse files
toastalkeijokapp
authored andcommitted
ScreenOrientation support
W3 spec for `ScreenOrientation`: https://drafts.csswg.org/cssom-view/
1 parent 3f1ee78 commit a8e48bd

File tree

7 files changed

+198
-6
lines changed

7 files changed

+198
-6
lines changed

bower.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"package.json"
1616
],
1717
"dependencies": {
18+
"purescript-aff": "^6.0.0",
1819
"purescript-web-events": "^2.0.1",
1920
"purescript-web-html": "^2.3.0",
2021
"purescript-web-geometry": "https://github.com/keijokapp/purescript-web-geometry.git"

src/Web/CSSOMView/Screen.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ exports.width = getter("width");
1414
exports.height = getter("height");
1515
exports.colorDepth = getter("colorDepth");
1616
exports.pixelDepth = getter("pixelDepth");
17+
exports.orientation = getter("orientation");

src/Web/CSSOMView/Screen.purs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
module Web.CSSOMView.Screen where
22

33
import Effect (Effect)
4+
import Web.CSSOMView.ScreenOrientation (ScreenOrientation)
45

56
foreign import data Screen :: Type
67

7-
foreign import availWidth :: Effect Int
8-
foreign import availHeight :: Effect Int
9-
foreign import width :: Effect Int
10-
foreign import height :: Effect Int
11-
foreign import colorDepth :: Effect Int
12-
foreign import pixelDepth :: Effect Int
8+
foreign import availWidth :: Screen -> Effect Int
9+
foreign import availHeight :: Screen -> Effect Int
10+
foreign import width :: Screen -> Effect Int
11+
foreign import height :: Screen -> Effect Int
12+
foreign import colorDepth :: Screen -> Effect Int
13+
foreign import pixelDepth :: Screen -> Effect Int
14+
foreign import orientation :: Screen -> Effect ScreenOrientation
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"use strict";
2+
3+
exports.lockImpl = function lock(orientation) {
4+
return function(so) {
5+
return function(onError, onSuccess) {
6+
so.lock(orientation).then(onSuccess, onError);
7+
return function canceler(cancelError, onCancelerError, onCancelerSuccess) {
8+
onCancelerSuccess();
9+
};
10+
};
11+
};
12+
};
13+
14+
exports.unlockImpl = function(so) {
15+
return so.unlock();
16+
};
17+
18+
exports.typeImpl = function(so) {
19+
return so.type;
20+
};
21+
22+
exports.angleImpl = function(so) {
23+
return so.angle;
24+
};
25+
26+
exports.onChangeImpl = function(handler, so) {
27+
so.onchange = handler;
28+
};
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
module Web.CSSOMView.ScreenOrientation
2+
( ScreenOrientation
3+
, lock
4+
, unlock
5+
, type_
6+
, angle
7+
, onChange
8+
, toEventTarget
9+
, fromEventTarget
10+
) where
11+
12+
-- https://w3c.github.io/screen-orientation/
13+
14+
import Prelude
15+
16+
import Data.Maybe (Maybe)
17+
import Effect (Effect)
18+
import Effect.Aff (Aff)
19+
import Effect.Aff.Compat (EffectFnAff, fromEffectFnAff)
20+
import Effect.Uncurried (EffectFn1, EffectFn2, runEffectFn1, runEffectFn2)
21+
import Unsafe.Coerce (unsafeCoerce)
22+
import Web.CSSOMView.ScreenOrientation.OrientationLockType (OrientationLockType)
23+
import Web.CSSOMView.ScreenOrientation.OrientationLockType as OrientationLockType
24+
import Web.CSSOMView.ScreenOrientation.OrientationType (OrientationType)
25+
import Web.CSSOMView.ScreenOrientation.OrientationType as OrientationType
26+
import Web.Event.Internal.Types (Event, EventTarget)
27+
import Web.Internal.FFI (unsafeReadProtoTagged)
28+
29+
foreign import data ScreenOrientation :: Type
30+
31+
foreign import lockImpl :: String -> ScreenOrientation -> EffectFnAff Unit
32+
33+
lock :: OrientationLockType -> ScreenOrientation -> Aff Unit
34+
lock lockType = fromEffectFnAff <<< lockImpl (OrientationLockType.print lockType)
35+
36+
foreign import unlockImpl :: EffectFn1 ScreenOrientation Unit
37+
38+
unlock ScreenOrientation -> Effect Unit
39+
unlock = runEffectFn1 unlockImpl
40+
41+
foreign import typeImpl :: EffectFn1 ScreenOrientation String
42+
43+
type_ :: ScreenOrientation -> Effect (Maybe OrientationType)
44+
type_ = map OrientationType.parse <<< runEffectFn1 typeImpl
45+
46+
foreign import angleImpl :: EffectFn1 ScreenOrientation Int
47+
48+
angle :: ScreenOrientation -> Effect Int
49+
angle = runEffectFn1 angleImpl
50+
51+
foreign import onChangeImpl :: EffectFn2 (Event -> Effect Unit) ScreenOrientation Unit
52+
53+
onChange :: (Event -> Effect Unit) -> ScreenOrientation -> Effect Unit
54+
onChange = runEffectFn2 onChangeImpl
55+
56+
toEventTarget :: ScreenOrientation -> EventTarget
57+
toEventTarget = unsafeCoerce
58+
59+
fromEventTarget :: EventTarget -> Maybe ScreenOrientation
60+
fromEventTarget = unsafeReadProtoTagged "ScreenOrientation"
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
module Web.CSSOMView.ScreenOrientation.OrientationLockType where
2+
3+
import Prelude
4+
5+
import Data.Maybe (Maybe(..))
6+
7+
data OrientationLockType
8+
= Any
9+
| Natural
10+
| Landscape
11+
| Portrait
12+
| PortraitPrimary
13+
| PortraitSecondary
14+
| LandscapePrimary
15+
| LandscapeSecondary
16+
17+
derive instance eqOrientationLockType :: Eq OrientationLockType
18+
19+
instance showOrientationLockType :: Show OrientationLockType where
20+
show = case _ of
21+
Any -> "Any"
22+
Natural -> "Natural"
23+
Landscape -> "Landscape"
24+
Portrait -> "Portrait"
25+
PortraitPrimary -> "PortraitPrimary"
26+
PortraitSecondary -> "PortraitSecondary"
27+
LandscapePrimary -> "LandscapePrimary"
28+
LandscapeSecondary -> "LandscapeSecondary"
29+
30+
parse :: String -> Maybe OrientationLockType
31+
parse = case _ of
32+
"any" -> Just Any
33+
"natural" -> Just Natural
34+
"landscape" -> Just Landscape
35+
"portrait" -> Just Portrait
36+
"portrait-primary" -> Just PortraitPrimary
37+
"portrait-secondary" -> Just PortraitSecondary
38+
"landscape-primary" -> Just LandscapePrimary
39+
"landscape-secondary" -> Just LandscapeSecondary
40+
_ -> Nothing
41+
42+
print :: OrientationLockType -> String
43+
print = case _ of
44+
Any -> "any"
45+
Natural -> "natural"
46+
Landscape -> "landscape"
47+
Portrait -> "portrait"
48+
PortraitPrimary -> "portrait-primary"
49+
PortraitSecondary -> "portrait-secondary"
50+
LandscapePrimary -> "landscape-primary"
51+
LandscapeSecondary -> "landscape-secondary"
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
module Web.CSSOMView.ScreenOrientation.OrientationType where
2+
3+
import Prelude
4+
5+
import Data.Maybe (Maybe(..))
6+
7+
data OrientationType
8+
= PortraitPrimary
9+
| PortraitSecondary
10+
| LandscapePrimary
11+
| LandscapeSecondary
12+
13+
derive instance eqOrientationType :: Eq OrientationType
14+
15+
instance showOrientationType :: Show OrientationType where
16+
show = case _ of
17+
PortraitPrimary -> "PortraitPrimary"
18+
PortraitSecondary -> "PortraitSecondary"
19+
LandscapePrimary -> "LandscapePrimary"
20+
LandscapeSecondary -> "LandscapeSecondary"
21+
22+
parse :: String -> Maybe OrientationType
23+
parse = case _ of
24+
"portrait-primary" -> Just PortraitPrimary
25+
"portrait-secondary" -> Just PortraitSecondary
26+
"landscape-primary" -> Just LandscapePrimary
27+
"landscape-secondary" -> Just LandscapeSecondary
28+
_ -> Nothing
29+
30+
print :: OrientationType -> String
31+
print = case _ of
32+
PortraitPrimary -> "portrait-primary"
33+
PortraitSecondary -> "portrait-secondary"
34+
LandscapePrimary -> "landscape-primary"
35+
LandscapeSecondary -> "landscape-secondary"
36+
37+
isPortrait :: OrientationType -> Boolean
38+
isPortrait = case _ of
39+
PortraitPrimary -> true
40+
PortraitSecondary -> true
41+
LandscapePrimary -> false
42+
LandscapeSecondary -> false
43+
44+
isLandscape :: OrientationType -> Boolean
45+
isLandscape = case _ of
46+
PortraitPrimary -> false
47+
PortraitSecondary -> false
48+
LandscapePrimary -> true
49+
LandscapeSecondary -> true

0 commit comments

Comments
 (0)