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

Exposing more event data #167

Open jerbaroo opened 7 years ago

jerbaroo commented 7 years ago

Exposing more of the event data allows the user more freedom.

Take for example the mouse events,mousemove, mousedown etc. Currently the coordinates of the event relative to the source element are passed to the user: e.pageX - offset.left and e.pageY - offset.top. By exposing e.pageX and offset.left as separate items the user has more information to work with, the previous information can still be constructed.

The code in lib.js would be simplified such that each event returns the same data to Haskell, some values might be null or undefined.

In Haskell we would then convert from JSON to an event information data type:

data EventInfo = EventInfo {
    ePageX      :: Maybe Int
  , ePageY      :: Maybe Int
  , eOffsetLeft :: Maybe Int
  , eOffsetTop  :: Maybe Int
  , eWhich      :: Maybe Int
  , eValue      :: Maybe String
}

An existing event function's API would not change:

mousemove :: Element -> Event (Int,Int)
mousemove = fmap readCoordinates . domEvent "mousemove"

readCoordinates :: EventData -> (Int, Int)
readCoordinates json =
    (fromJust (ePageX e) - fromJust (eOffsetLeft e), fromJust (ePageY e) - fromJust (eOffsetTop e))
    where e = unsafeFromJSON json

However an additional function could expose more event information:

mousemove_ :: Element -> Event EventInfo
mousemove_ = fmap unsafeFromJSON . domevent "mousemove"

If there is support for this, I would be happy to work on it based on feedback 🎉