diff --git a/js/lib.js b/js/lib.js index c94c3cfb..7373dddd 100644 --- a/js/lib.js +++ b/js/lib.js @@ -46,6 +46,13 @@ Haskell.on = function (el, eventType, fun) { fun(e.keyCode); return true; }); + } else if(eventType === 'resize') { + window.onresize = function(e) { + var t = e.target; + fun([t.innerWidth, t.innerHeight]); + return true; + }; + fun([window.innerWidth, window.innerHeight]); } else { $(el).on(eventType, function(e) { fun(e.which ? [e.which.toString()] : e.detail || []); diff --git a/src/Graphics/UI/Threepenny/Events.hs b/src/Graphics/UI/Threepenny/Events.hs index 33b5de14..83768ed5 100644 --- a/src/Graphics/UI/Threepenny/Events.hs +++ b/src/Graphics/UI/Threepenny/Events.hs @@ -8,12 +8,13 @@ module Graphics.UI.Threepenny.Events ( -- * Standard DOM events click, contextmenu, mousemove, mousedown, mouseup, hover, leave, - focus, blur, + focus, blur, resize, resize', KeyCode, keyup, keydown, keypress, ) where import Graphics.UI.Threepenny.Attributes import Graphics.UI.Threepenny.Core +import System.IO.Unsafe (unsafePerformIO) silence = fmap (const ()) @@ -87,6 +88,14 @@ focus = silence . domEvent "focus" blur :: Element -> Event () blur = silence . domEvent "blur" +-- | Element reports its window's resize. +-- Note that there should only be at most one +-- 'resize' event registered. +resize :: Element -> Event (Int,Int) +resize = fmap readCoordinates . domEvent "resize" +resize' :: Window -> Event (Int,Int) +resize' w = fmap readCoordinates $ domEvent "resize" e -- do fmap readCoordinates . + where e = unsafePerformIO (runUI w $ getBody w) type KeyCode = Int