-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
Buttons.hs
75 lines (57 loc) · 2.22 KB
/
Buttons.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import Control.Monad
import Control.Concurrent (threadDelay)
import Paths
import qualified Graphics.UI.Threepenny as UI
import Graphics.UI.Threepenny.Core
{-----------------------------------------------------------------------------
Buttons
------------------------------------------------------------------------------}
main :: IO ()
main = do
static <- getStaticDir
startGUI defaultConfig { jsStatic = Just static } setup
setup :: Window -> UI ()
setup w = void $ do
return w # set title "Buttons"
UI.addStyleSheet w "buttons.css"
buttons <- mkButtons
getBody w #+
[UI.div #. "wrap" #+ (greet ++ map element buttons ++ [viewSource])]
greet :: [UI Element]
greet =
[ UI.h1 #+ [string "Hello, Haskell!"]
, UI.div #+ [string "Try the buttons below, they hover and click."]
]
mkButton :: String -> UI (Element, Element)
mkButton title = do
button <- UI.button #. "button" #+ [string title]
view <- UI.p #+ [element button]
return (button, view)
mkButtons :: UI [Element]
mkButtons = do
list <- UI.ul #. "buttons-list"
(button1, view1) <- mkButton button1Title
on UI.hover button1 $ \_ -> do
element button1 # set text (button1Title ++ " [hover]")
on UI.leave button1 $ \_ -> do
element button1 # set text button1Title
on UI.click button1 $ \_ -> do
element button1 # set text (button1Title ++ " [pressed]")
liftIO $ threadDelay $ 1000 * 1000 * 1
element list #+ [UI.li # set html "<b>Delayed</b> result!"]
(button2, view2) <- mkButton button2Title
on UI.hover button2 $ \_ -> do
element button2 # set text (button2Title ++ " [hover]")
on UI.leave button2 $ \_ -> do
element button2 # set text button2Title
on UI.click button2 $ \_ -> do
element button2 # set text (button2Title ++ " [pressed]")
element list #+ [UI.li # set html "Zap! Quick result!"]
return [list, view1, view2]
where button1Title = "Click me, I delay a bit"
button2Title = "Click me, I work immediately"
viewSource :: UI Element
viewSource = UI.p #+
[UI.anchor #. "view-source" # set UI.href url #+ [string "View source code"]]
where
url = samplesURL ++ "Buttons.hs"