HeinrichApfelmus / threepenny-gui

GUI framework that uses the web browser as a display.
https://heinrichapfelmus.github.io/threepenny-gui/
Other
437 stars 77 forks source link

Events on windows? #230

Open valyagolev opened 5 years ago

valyagolev commented 5 years ago

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

bradrn commented 5 years ago

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

valyagolev commented 5 years ago

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

bradrn commented 5 years ago

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

valyagolev commented 5 years ago

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

bradrn commented 5 years ago

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

valyagolev commented 5 years ago

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

HeinrichApfelmus commented 4 years ago

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.