threepenny-gui
threepenny-gui copied to clipboard
Events on windows?
It seems that there's no way to add an event to Window, nor to pretend that Window is an Element.
It'd be nice to be able to use some Window events such as 'keydown' or 'resize'.
I think this would also go some way towards solving #228, which seems to require handling the onload
event (see https://github.com/HeinrichApfelmus/threepenny-gui/issues/228#issuecomment-421757012).
just in case someone needs something now, my temporary, very hacky solution is along the lines of
let keyPressHandler code = ...
let handler e = runUI window $ keyPressHandler e
window <- askWindow
exported <- ffiExport handler
runFunction (ffi "window.addEventListener('keydown', ({code}) =>{ %1(code); }, false);" exported)
I think maybe adding a class like EventTarget
would be a good solution. or maybe, for simplicity, a way to pretend that Window
is an Element
... I'll see if I could hack a PR together
@va1en0k What's keyPressHandler
? I can't see it anywhere in threepenny-gui
.
it's your event handler, a function a -> UI ()
That makes sense. But did you mean a -> IO ()
? That's what GHC is giving me for the type.
sorry, yes. in this case it's going to be String -> IO ()
because I pick the code from the event, but you'll have to tweak it to your needs
Uhm, the "keydown" and "resize" events should go on the document body, which can be obtained with getBody
? So attaching a keydown
to getBody
already works.
That said, the "resize" event currently does not have special support.