threepenny-gui icon indicating copy to clipboard operation
threepenny-gui copied to clipboard

Events on windows?

Open valyagolev opened this issue 6 years ago • 7 comments

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'.

valyagolev avatar Oct 14 '18 21:10 valyagolev

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).

bradrn avatar Oct 14 '18 22:10 bradrn

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

valyagolev avatar Oct 14 '18 22:10 valyagolev

@va1en0k What's keyPressHandler? I can't see it anywhere in threepenny-gui.

bradrn avatar Oct 14 '18 22:10 bradrn

it's your event handler, a function a -> UI ()

valyagolev avatar Oct 14 '18 22:10 valyagolev

That makes sense. But did you mean a -> IO ()? That's what GHC is giving me for the type.

bradrn avatar Oct 14 '18 22:10 bradrn

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

valyagolev avatar Oct 14 '18 22:10 valyagolev

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.

HeinrichApfelmus avatar Jun 02 '20 21:06 HeinrichApfelmus