-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
BarTab.hs
77 lines (60 loc) · 2.12 KB
/
BarTab.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
75
76
77
{-# LANGUAGE TypeSynonymInstances, FlexibleInstances #-}
import Control.Applicative
import Control.Monad
import Data.IORef
import Data.Maybe
import qualified Graphics.UI.Threepenny as UI
import Graphics.UI.Threepenny.Core
-- | Main entry point.
main :: IO ()
main = startGUI defaultConfig setup
setup :: Window -> UI ()
setup w = do
-- active elements
return w # set title "BarTab"
elAdd <- UI.button # set UI.text "Add"
elRemove <- UI.button # set UI.text "Remove"
elResult <- UI.span
inputs <- liftIO $ newIORef []
-- functionality
let
displayTotal = void $ do
xs <- mapM (get value) =<< liftIO (readIORef inputs)
element elResult # set UI.text (showNumber . sum $ map readNumber xs)
redoLayout :: UI ()
redoLayout = void $ do
layout <- mkLayout =<< liftIO (readIORef inputs)
getBody w # set children [layout]
displayTotal
mkLayout :: [Element] -> UI Element
mkLayout xs = column $
[row [element elAdd, element elRemove]
,UI.hr]
++ map element xs ++
[UI.hr
,row [UI.span # set text "Sum: ", element elResult]
]
addInput :: UI ()
addInput = do
elInput <- UI.input # set value "0"
on (domEvent "livechange") elInput $ \_ -> displayTotal
liftIO $ modifyIORef inputs (elInput:)
removeInput :: UI ()
removeInput = liftIO $ modifyIORef inputs (drop 1)
on UI.click elAdd $ \_ -> addInput >> redoLayout
on UI.click elRemove $ \_ -> removeInput >> redoLayout
addInput >> redoLayout
{-----------------------------------------------------------------------------
Functionality
------------------------------------------------------------------------------}
type Number = Maybe Double
instance Num Number where
(+) = liftA2 (+)
(-) = liftA2 (-)
(*) = liftA2 (*)
abs = fmap abs
signum = fmap signum
fromInteger = pure . fromInteger
readNumber :: String -> Number
readNumber s = listToMaybe [x | (x,"") <- reads s]
showNumber = maybe "--" show