├── .gitignore ├── LICENSE ├── README.md ├── elm-package.json └── src ├── Native └── Window.js └── Window.elm /.gitignore: -------------------------------------------------------------------------------- 1 | elm-stuff -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016, Evan Czaplicki 2 | 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without 6 | modification, are permitted provided that the following conditions are met: 7 | 8 | * Redistributions of source code must retain the above copyright 9 | notice, this list of conditions and the following disclaimer. 10 | 11 | * Redistributions in binary form must reproduce the above 12 | copyright notice, this list of conditions and the following 13 | disclaimer in the documentation and/or other materials provided 14 | with the distribution. 15 | 16 | * Neither the name of Evan Czaplicki nor the names of other 17 | contributors may be used to endorse or promote products derived 18 | from this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Moved to [`elm/browser`](https://github.com/elm/browser) 2 | -------------------------------------------------------------------------------- /elm-package.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "1.0.1", 3 | "summary": "Get the dimensions of the window / screen size.", 4 | "repository": "http://github.com/elm-lang/window.git", 5 | "license": "BSD3", 6 | "source-directories": [ 7 | "src" 8 | ], 9 | "exposed-modules": [ 10 | "Window" 11 | ], 12 | "native-modules": true, 13 | "dependencies": { 14 | "elm-lang/core": "5.0.0 <= v < 6.0.0", 15 | "elm-lang/dom": "1.1.1 <= v < 2.0.0" 16 | }, 17 | "elm-version": "0.18.0 <= v < 0.19.0" 18 | } 19 | -------------------------------------------------------------------------------- /src/Native/Window.js: -------------------------------------------------------------------------------- 1 | var _elm_lang$window$Native_Window = function() 2 | { 3 | 4 | var size = _elm_lang$core$Native_Scheduler.nativeBinding(function(callback) { 5 | callback(_elm_lang$core$Native_Scheduler.succeed({ 6 | width: window.innerWidth, 7 | height: window.innerHeight 8 | })); 9 | }); 10 | 11 | return { 12 | size: size 13 | }; 14 | 15 | }(); -------------------------------------------------------------------------------- /src/Window.elm: -------------------------------------------------------------------------------- 1 | effect module Window where { subscription = MySub } exposing 2 | ( Size 3 | , size, width, height 4 | , resizes 5 | ) 6 | 7 | {-| Your application lives in some sort of window. This library helps you 8 | figure out how big that window is. 9 | 10 | # Window Size 11 | @docs Size, size, width, height, resizes 12 | 13 | -} 14 | 15 | import Dom.LowLevel as Dom 16 | import Json.Decode as Json 17 | import Native.Window 18 | import Process 19 | import Task exposing (Task) 20 | 21 | 22 | 23 | {-| The size of the window in pixels. 24 | -} 25 | type alias Size = 26 | { width : Int 27 | , height : Int 28 | } 29 | 30 | 31 | {-| Get the current window size. 32 | -} 33 | size : Task x Size 34 | size = 35 | Native.Window.size 36 | 37 | 38 | {-| Get the current window width. 39 | -} 40 | width : Task x Int 41 | width = 42 | Task.map .width size 43 | 44 | 45 | {-| Get the current window height. 46 | -} 47 | height : Task x Int 48 | height = 49 | Task.map .height size 50 | 51 | 52 | {-| Subscribe to any changes in window size. 53 | -} 54 | resizes : (Size -> msg) -> Sub msg 55 | resizes tagger = 56 | subscription (MySub tagger) 57 | 58 | 59 | 60 | -- SUBSCRIPTIONS 61 | 62 | 63 | type MySub msg 64 | = MySub (Size -> msg) 65 | 66 | 67 | subMap : (a -> b) -> MySub a -> MySub b 68 | subMap func (MySub tagger) = 69 | MySub (tagger >> func) 70 | 71 | 72 | 73 | -- EFFECT MANAGER 74 | 75 | 76 | type alias State msg = 77 | Maybe 78 | { subs : List (MySub msg) 79 | , pid : Process.Id 80 | } 81 | 82 | 83 | init : Task Never (State msg) 84 | init = 85 | Task.succeed Nothing 86 | 87 | 88 | (&>) task1 task2 = 89 | Task.andThen (\_ -> task2) task1 90 | 91 | 92 | onEffects : Platform.Router msg Size -> List (MySub msg) -> State msg -> Task Never (State msg) 93 | onEffects router newSubs oldState = 94 | case (oldState, newSubs) of 95 | (Nothing, []) -> 96 | Task.succeed Nothing 97 | 98 | (Just {pid}, []) -> 99 | Process.kill pid 100 | &> Task.succeed Nothing 101 | 102 | (Nothing, _) -> 103 | Process.spawn (Dom.onWindow "resize" (Json.succeed ()) (\_ -> Task.andThen (Platform.sendToSelf router) size)) 104 | |> Task.andThen (\pid -> Task.succeed (Just { subs = newSubs, pid = pid })) 105 | 106 | (Just {pid}, _) -> 107 | Task.succeed (Just { subs = newSubs, pid = pid }) 108 | 109 | 110 | onSelfMsg : Platform.Router msg Size -> Size -> State msg -> Task Never (State msg) 111 | onSelfMsg router dimensions state = 112 | case state of 113 | Nothing -> 114 | Task.succeed state 115 | 116 | Just {subs} -> 117 | let 118 | send (MySub tagger) = 119 | Platform.sendToApp router (tagger dimensions) 120 | in 121 | Task.sequence (List.map send subs) 122 | &> Task.succeed state 123 | 124 | --------------------------------------------------------------------------------