module Graphics.UI.Threepenny.Events (
valueChange, selectionChange, checkedChange,
click, contextmenu, mousemove, mousedown, mouseup,
hover, leave,
focus, blur,
KeyCode, keyup, keydown, keypress,
roundCoordinates
) where
import Graphics.UI.Threepenny.Attributes
import Graphics.UI.Threepenny.Core
silence :: Event a -> Event ()
silence :: forall a. Event a -> Event ()
silence = (a -> ()) -> Event a -> Event ()
forall a b. (a -> b) -> Event a -> Event b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (() -> a -> ()
forall a b. a -> b -> a
const ())
valueChange :: Element -> Event String
valueChange :: Element -> Event String
valueChange Element
el = Element
-> (EventData -> UI String) -> Event EventData -> Event String
forall t b. Element -> (t -> UI b) -> Event t -> Event b
unsafeMapUI Element
el (UI String -> EventData -> UI String
forall a b. a -> b -> a
const (UI String -> EventData -> UI String)
-> UI String -> EventData -> UI String
forall a b. (a -> b) -> a -> b
$ ReadWriteAttr Element String String -> Element -> UI String
forall x i o. ReadWriteAttr x i o -> x -> UI o
get ReadWriteAttr Element String String
value Element
el) (String -> Element -> Event EventData
domEvent String
"keydown" Element
el)
unsafeMapUI :: Element -> (t -> UI b) -> Event t -> Event b
unsafeMapUI :: forall t b. Element -> (t -> UI b) -> Event t -> Event b
unsafeMapUI Element
el t -> UI b
f = (t -> IO b) -> Event t -> Event b
forall a b. (a -> IO b) -> Event a -> Event b
unsafeMapIO (\t
a -> Element -> IO Window
getWindow Element
el IO Window -> (Window -> IO b) -> IO b
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Window
w -> Window -> UI b -> IO b
forall a. Window -> UI a -> IO a
runUI Window
w (t -> UI b
f t
a))
selectionChange :: Element -> Event (Maybe Int)
selectionChange :: Element -> Event (Maybe Int)
selectionChange Element
el = Element -> (() -> UI (Maybe Int)) -> Event () -> Event (Maybe Int)
forall t b. Element -> (t -> UI b) -> Event t -> Event b
unsafeMapUI Element
el (UI (Maybe Int) -> () -> UI (Maybe Int)
forall a b. a -> b -> a
const (UI (Maybe Int) -> () -> UI (Maybe Int))
-> UI (Maybe Int) -> () -> UI (Maybe Int)
forall a b. (a -> b) -> a -> b
$ ReadWriteAttr Element (Maybe Int) (Maybe Int)
-> Element -> UI (Maybe Int)
forall x i o. ReadWriteAttr x i o -> x -> UI o
get ReadWriteAttr Element (Maybe Int) (Maybe Int)
selection Element
el) (Element -> Event ()
click Element
el)
checkedChange :: Element -> Event Bool
checkedChange :: Element -> Event Bool
checkedChange Element
el = Element -> (() -> UI Bool) -> Event () -> Event Bool
forall t b. Element -> (t -> UI b) -> Event t -> Event b
unsafeMapUI Element
el (UI Bool -> () -> UI Bool
forall a b. a -> b -> a
const (UI Bool -> () -> UI Bool) -> UI Bool -> () -> UI Bool
forall a b. (a -> b) -> a -> b
$ ReadWriteAttr Element Bool Bool -> Element -> UI Bool
forall x i o. ReadWriteAttr x i o -> x -> UI o
get ReadWriteAttr Element Bool Bool
checked Element
el) (Element -> Event ()
click Element
el)
click :: Element -> Event ()
click :: Element -> Event ()
click = Event EventData -> Event ()
forall a. Event a -> Event ()
silence (Event EventData -> Event ())
-> (Element -> Event EventData) -> Element -> Event ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Element -> Event EventData
domEvent String
"click"
contextmenu :: Element -> Event (Double,Double)
= (EventData -> (Double, Double))
-> Event EventData -> Event (Double, Double)
forall a b. (a -> b) -> Event a -> Event b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap EventData -> (Double, Double)
readCoordinates (Event EventData -> Event (Double, Double))
-> (Element -> Event EventData)
-> Element
-> Event (Double, Double)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Element -> Event EventData
domEvent String
"contextmenu"
hover :: Element -> Event ()
hover :: Element -> Event ()
hover = Event EventData -> Event ()
forall a. Event a -> Event ()
silence (Event EventData -> Event ())
-> (Element -> Event EventData) -> Element -> Event ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Element -> Event EventData
domEvent String
"mouseenter"
mousemove :: Element -> Event (Double,Double)
mousemove :: Element -> Event (Double, Double)
mousemove = (EventData -> (Double, Double))
-> Event EventData -> Event (Double, Double)
forall a b. (a -> b) -> Event a -> Event b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap EventData -> (Double, Double)
readCoordinates (Event EventData -> Event (Double, Double))
-> (Element -> Event EventData)
-> Element
-> Event (Double, Double)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Element -> Event EventData
domEvent String
"mousemove"
readCoordinates :: EventData -> (Double,Double)
readCoordinates :: EventData -> (Double, Double)
readCoordinates EventData
json = (Double
x,Double
y)
where [Double
x,Double
y] = EventData -> [Double]
forall a. FromJSON a => EventData -> a
unsafeFromJSON EventData
json
roundCoordinates :: (Double,Double) -> (Int,Int)
roundCoordinates :: (Double, Double) -> (Int, Int)
roundCoordinates (Double
x,Double
y) = (Double -> Int
forall b. Integral b => Double -> b
forall a b. (RealFrac a, Integral b) => a -> b
round Double
x, Double -> Int
forall b. Integral b => Double -> b
forall a b. (RealFrac a, Integral b) => a -> b
round Double
y)
mousedown :: Element -> Event (Double,Double)
mousedown :: Element -> Event (Double, Double)
mousedown = (EventData -> (Double, Double))
-> Event EventData -> Event (Double, Double)
forall a b. (a -> b) -> Event a -> Event b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap EventData -> (Double, Double)
readCoordinates (Event EventData -> Event (Double, Double))
-> (Element -> Event EventData)
-> Element
-> Event (Double, Double)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Element -> Event EventData
domEvent String
"mousedown"
mouseup :: Element -> Event (Double,Double)
mouseup :: Element -> Event (Double, Double)
mouseup = (EventData -> (Double, Double))
-> Event EventData -> Event (Double, Double)
forall a b. (a -> b) -> Event a -> Event b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap EventData -> (Double, Double)
readCoordinates (Event EventData -> Event (Double, Double))
-> (Element -> Event EventData)
-> Element
-> Event (Double, Double)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Element -> Event EventData
domEvent String
"mouseup"
leave :: Element -> Event ()
leave :: Element -> Event ()
leave = Event EventData -> Event ()
forall a. Event a -> Event ()
silence (Event EventData -> Event ())
-> (Element -> Event EventData) -> Element -> Event ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Element -> Event EventData
domEvent String
"mouseleave"
focus :: Element -> Event ()
focus :: Element -> Event ()
focus = Event EventData -> Event ()
forall a. Event a -> Event ()
silence (Event EventData -> Event ())
-> (Element -> Event EventData) -> Element -> Event ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Element -> Event EventData
domEvent String
"focus"
blur :: Element -> Event ()
blur :: Element -> Event ()
blur = Event EventData -> Event ()
forall a. Event a -> Event ()
silence (Event EventData -> Event ())
-> (Element -> Event EventData) -> Element -> Event ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Element -> Event EventData
domEvent String
"blur"
type KeyCode = Int
keydown :: Element -> Event KeyCode
keydown :: Element -> Event Int
keydown = (EventData -> Int) -> Event EventData -> Event Int
forall a b. (a -> b) -> Event a -> Event b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap EventData -> Int
forall a. FromJSON a => EventData -> a
unsafeFromJSON (Event EventData -> Event Int)
-> (Element -> Event EventData) -> Element -> Event Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Element -> Event EventData
domEvent String
"keydown"
keyup :: Element -> Event KeyCode
keyup :: Element -> Event Int
keyup = (EventData -> Int) -> Event EventData -> Event Int
forall a b. (a -> b) -> Event a -> Event b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap EventData -> Int
forall a. FromJSON a => EventData -> a
unsafeFromJSON (Event EventData -> Event Int)
-> (Element -> Event EventData) -> Element -> Event Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Element -> Event EventData
domEvent String
"keyup"
keypress :: Element -> Event Char
keypress :: Element -> Event Char
keypress = (EventData -> Char) -> Event EventData -> Event Char
forall a b. (a -> b) -> Event a -> Event b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Int -> Char
forall a. Enum a => Int -> a
toEnum (Int -> Char) -> (EventData -> Int) -> EventData -> Char
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Int
forall a. Read a => String -> a
read (String -> Int) -> (EventData -> String) -> EventData -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [String] -> String
forall a. HasCallStack => [a] -> a
head ([String] -> String)
-> (EventData -> [String]) -> EventData -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. EventData -> [String]
forall a. FromJSON a => EventData -> a
unsafeFromJSON) (Event EventData -> Event Char)
-> (Element -> Event EventData) -> Element -> Event Char
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Element -> Event EventData
domEvent String
"keypress"