on d41a656 help sign in
56 lines 1.9 kB history raw
1Elm.Native.Window = {};
2Elm.Native.Window.make = function(elm) {
4 elm.Native = elm.Native || {};
5 elm.Native.Window = elm.Native.Window || {};
6 if (elm.Native.Window.values) return elm.Native.Window.values;
8 var Signal = Elm.Signal.make(elm);
9 var Tuple2 = Elm.Native.Utils.make(elm).Tuple2;
11 function getWidth() { return elm.node.clientWidth; }
12 function getHeight() {
13 if (elm.display === ElmRuntime.Display.FULLSCREEN) {
14 return window.innerHeight;
15 }
16 return elm.node.clientHeight;
17 }
19 var dimensions = Signal.constant(Tuple2(getWidth(), getHeight()));
20 dimensions.defaultNumberOfKids = 2;
22 // Do not move width and height into Elm. By setting the default number of kids,
23 // the resize listener can be detached.
24 var width = A2(Signal.lift, function(p){return p._0;}, dimensions);
25 width.defaultNumberOfKids = 0;
27 var height = A2(Signal.lift, function(p){return p._1;}, dimensions);
28 height.defaultNumberOfKids = 0;
30 function resizeIfNeeded() {
31 // Do not trigger event if the dimensions have not changed.
32 // This should be most of the time.
33 var w = getWidth();
34 var h = getHeight();
35 if (dimensions.value._0 === w && dimensions.value._1 === h) return;
37 setTimeout(function () {
38 // Check again to see if the dimensions have changed.
39 // It is conceivable that the dimensions have changed
40 // again while some other event was being processed.
41 var w = getWidth();
42 var h = getHeight();
43 if (dimensions.value._0 === w && dimensions.value._1 === h) return;
44 elm.notify(dimensions.id, Tuple2(w,h));
45 }, 0);
46 }
47 elm.addListener([dimensions.id], window, 'resize', resizeIfNeeded);
49 return elm.Native.Window.values = {
50 dimensions:dimensions,
51 width:width,
52 height:height,
53 resizeIfNeeded:resizeIfNeeded
54 };
56};