on 4780fe7 help sign in
reflex-vty/src-bin/example.hs
217 lines 8.1 kB history raw
1{-# LANGUAGE FlexibleContexts #-}
2{-# LANGUAGE FlexibleInstances #-}
3{-# LANGUAGE GADTs #-}
4{-# LANGUAGE LambdaCase #-}
5{-# LANGUAGE MultiParamTypeClasses #-}
6{-# LANGUAGE OverloadedStrings #-}
7{-# LANGUAGE RankNTypes #-}
8{-# LANGUAGE RecursiveDo #-}
9{-# LANGUAGE ScopedTypeVariables #-}
10{-# LANGUAGE TupleSections #-}
11{-# OPTIONS_GHC -threaded #-}
13import Control.Applicative
14import Control.Monad
15import Control.Monad.Fix
16import Data.Map (Map)
17import Data.Maybe
18import qualified Data.Map as Map
19import Data.Text (Text)
20import qualified Data.Text as T
21import qualified Data.Text.Zipper as TZ
22import qualified Graphics.Vty as V
23import Reflex
24import Reflex.Class.Orphans
25import Reflex.Network
26import Reflex.Class.Switchable
27import Reflex.NotReady.Class
28import Reflex.Vty
30data Example = Example_TextEditor
31 | Example_Todo
32 | Example_Stack
33 deriving (Show, Read, Eq, Ord, Enum, Bounded)
35div' :: (Integral a, Applicative f) => f a -> f a -> f a
36div' = liftA2 div
38main :: IO ()
39main = mainWidget $ do
40 inp <- input
42 w <- displayWidth
43 h <- displayHeight
45 row $ {- sized (div' w 2) $ col $ -} do
46 sized 5 $ textButtonStatic def "A"
47 sized 5 $ textButtonStatic def "B"
48 TextInput value lines <- sized 10 $ multilineTextInput def
49 TextInput value' lines' <- sized 10 $ multilineTextInput def
50 sized 10 $ textButton def ("\n" <> current value)
51 sized 5 $ textButtonStatic def "E"
52 sized 1 $ text "asdf"
53 sized 1 $ text "zxcv"
55 {-
56 let buttons = do
57 text $ pure "Select an example. Esc will bring you back here. Ctrl+c to quit."
58 let w' = fmap (`div`6) w
59 h' = fmap (`div`6) h
60 region1 = DynRegion w' h' w' h'
61 region2 = DynRegion (2 * w') h' w' h'
62 todo' <- pane region1 (pure True) $ textButtonStatic def "Todo List"
63 editor <- pane region2 (pure True) $ textButtonStatic def "Text Editor"
64 return $ leftmost
65 [ Left Example_TextEditor <$ editor
66 , Left Example_Todo <$ todo'
67 ]
68 escapable w = do
69 void w
70 i <- input
71 return $ fforMaybe i $ \case
72 V.EvKey V.KEsc [] -> Just $ Right ()
73 _ -> Nothing
74 rec out <- networkHold buttons $ ffor (switch (current out)) $ \case
75 Left Example_TextEditor -> escapable testBoxes
76 Left Example_Todo -> escapable taskList
77 Right () -> buttons
78 -}
79 return $ fforMaybe inp $ \case
80 V.EvKey (V.KChar 'c') [V.MCtrl] -> Just ()
81 _ -> Nothing
83taskList
84 :: (Reflex t, MonadHold t m, MonadFix m, Adjustable t m, NotReady t m, PostBuild t m)
85 => VtyWidget t m ()
86taskList = do
87 let btn = textButtonStatic def "Add another task"
88 inp <- input
89 rec let todos' = todos [Todo "Find reflex-vty" True, Todo "Become functional reactive" False, Todo "Make vty apps" False] $ leftmost
90 [ () <$ e
91 , fforMaybe inp $ \case
92 V.EvKey V.KEnter [] -> Just ()
93 _ -> Nothing
94 ]
95 (m, (e, _)) <- splitV (pure (subtract 6)) (pure (True, True)) todos' $ do
96 splitV (pure (subtract 3)) (pure (True, True)) btn (display $ current m)
97 return ()
99testBoxes :: (Reflex t, MonadHold t m, MonadFix m) => VtyWidget t m ()
100testBoxes = do
101 dw <- displayWidth
102 dh <- displayHeight
103 let region1 = DynRegion (div' dw 6) (div' dh 6) (div' dw 2) (div' dh 2)
104 region2 = DynRegion (div' dw 4) (div' dh 4) (2 * div' dw 3) (2 * div' dh 3)
105 pane region1 (constDyn False) . boxStatic singleBoxStyle $ debugInput
106 _ <- pane region2 (constDyn True) . boxStatic singleBoxStyle $
107 splitVDrag (hRule doubleBoxStyle) (boxStatic roundedBoxStyle $ multilineTextInput def) (boxStatic roundedBoxStyle dragTest)
108 return ()
110debugFocus :: (Reflex t, Monad m) => VtyWidget t m ()
111debugFocus = do
112 f <- focus
113 text $ T.pack . show <$> current f
115debugInput :: (Reflex t, MonadHold t m) => VtyWidget t m ()
116debugInput = do
117 lastEvent <- hold "No event yet" . fmap show =<< input
118 text $ T.pack <$> lastEvent
120dragTest :: (Reflex t, MonadHold t m, MonadFix m) => VtyWidget t m ()
121dragTest = do
122 lastEvent <- hold "No event yet" . fmap show =<< drag V.BLeft
123 text $ T.pack <$> lastEvent
125testStringBox :: (Reflex t, Monad m) => VtyWidget t m ()
126testStringBox = boxStatic singleBoxStyle .
127 text . pure . T.pack . take 500 $ cycle ('\n' : ['a'..'z'])
129data Todo = Todo
130 { _todo_label :: Text
131 , _todo_done :: Bool
132 }
133 deriving (Show, Read, Eq, Ord)
135data TodoOutput t = TodoOutput
136 { _todoOutput_todo :: Dynamic t Todo
137 , _todoOutput_delete :: Event t ()
138 , _todoOutput_height :: Dynamic t Int
139 }
141instance Reflex t => Switchable t (TodoOutput t) where
142 switching t0 e = TodoOutput
143 <$> switching (_todoOutput_todo t0) (_todoOutput_todo <$> e)
144 <*> switching (_todoOutput_delete t0) (_todoOutput_delete <$> e)
145 <*> switching (_todoOutput_height t0) (_todoOutput_height <$> e)
147todo
148 :: (MonadHold t m, MonadFix m, Reflex t)
149 => Todo
150 -> VtyWidget t m (TodoOutput t)
151todo t0 = do
152 w <- displayWidth
153 rec let checkboxWidth = 3
154 checkboxRegion = DynRegion 0 0 checkboxWidth 1
155 labelHeight = _textInput_lines ti
156 labelWidth = w - 1 - checkboxWidth
157 labelLeft = checkboxWidth + 1
158 labelTop = constDyn 0
159 labelRegion = DynRegion labelLeft labelTop labelWidth labelHeight
160 value <- pane checkboxRegion (pure True) $ checkbox def $ _todo_done t0
161 (ti, d) <- pane labelRegion (pure True) $ do
162 i <- input
163 v <- textInput $ def { _textInputConfig_initialValue = TZ.fromText $ _todo_label t0 }
164 let deleteSelf = attachWithMaybe backspaceOnEmpty (current $ _textInput_value v) i
165 return (v, deleteSelf)
166 return $ TodoOutput
167 { _todoOutput_todo = Todo <$> (_textInput_value ti) <*> value
168 , _todoOutput_delete = d
169 , _todoOutput_height = _textInput_lines ti
170 }
171 where
172 backspaceOnEmpty v = \case
173 V.EvKey V.KBS _ | T.null v -> Just ()
174 _ -> Nothing
176todos
177 :: forall t m. (MonadHold t m, MonadFix m, Reflex t, Adjustable t m, NotReady t m, PostBuild t m)
178 => [Todo]
179 -> Event t ()
180 -> VtyWidget t m (Dynamic t (Map Int Todo))
181todos todos0 newTodo = do
182 let todosMap0 = Map.fromList $ zip [0..] todos0
183 w <- displayWidth
184 rec listOut <- listHoldWithKey todosMap0 updates $ \row t -> do
185 let reg = zipDynWith (\w' ts ->
186 let l = Map.size $ Map.takeWhileAntitone (<row) ts
187 in Region 0 l w' 1) w todosMap
188 pane (DynRegion 0 (fromMaybe 0 . Map.lookup row <$> offsets) w (fromMaybe 1 . Map.lookup row <$> heights))
189 (fmap (==row) selected) $ do
190 e <- mouseUp
191 r <- todo t
192 return (row <$ e, r)
193 let heights :: Dynamic t (Map Int Int) = joinDynThroughMap $ fmap (fmap (_todoOutput_height . snd)) listOut
194 offsets :: Dynamic t (Map Int Int) = ffor heights $ \m ->
195 let l = Map.toList m
196 o = scanl (+) 0 $ map snd l
197 in Map.fromList $ zipWith (\(k, _) o' -> (k, o')) l o
198 let selectionClick = switch . current $ fmap (leftmost . Map.elems . fmap fst) listOut
199 selected <- holdDyn 0 $ leftmost
200 [ selectionClick
201 , fmapMaybe (fmap fst . Map.lookupMax) insert
202 , selectOnDelete
203 ]
204 let todosMap = joinDynThroughMap $ fmap (fmap (_todoOutput_todo . snd)) listOut
205 todoDelete = switch . current $
206 leftmost . Map.elems . Map.mapWithKey (\k -> (k <$) . _todoOutput_delete . snd) <$> listOut
207 selectOnDelete = attachWithMaybe
208 (\m k -> let (before, after) = Map.split k m
209 in fmap fst $ Map.lookupMax before <|> Map.lookupMin after)
210 (current todosMap)
211 todoDelete
212 insert = ffor (tag (current todosMap) newTodo) $ \m -> case Map.lookupMax m of
213 Nothing -> Map.singleton 0 $ Just $ Todo "" False
214 Just (k, _) -> Map.singleton (k+1) $ Just $ Todo "" False
215 delete = ffor todoDelete $ \k -> Map.singleton k Nothing
216 updates = leftmost [insert, delete]
217 return todosMap