on 8a6f4d7 help sign in
reflex-vty/src-bin/example.hs
212 lines 8.0 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
30import Data.Tree
32-- Unlimited Stack
33 -- Parent provides orientation and maximum cross-dimension size
34 -- Each child takes as much main-dimension space as it wants and reports what it took
35 -- Parent offsets each child so that it does not overlap with other children
36 -- If parent runs of out space, parent provides a scroll bar
38data Example = Example_TextEditor
39 | Example_Todo
40 deriving (Show, Read, Eq, Ord, Enum, Bounded)
42div' :: (Integral a, Applicative f) => f a ->f a -> f a
43div' = liftA2 div
45main :: IO ()
46main = mainWidget $ do
47 inp <- input
49 w <- displayWidth
50 h <- displayHeight
51 let buttons = do
52 text $ pure "Select an example. Esc will bring you back here. Ctrl+c to quit."
53 let w' = fmap (`div`6) w
54 h' = fmap (`div`6) h
55 region1 = DynRegion w' h' w' h'
56 region2 = DynRegion (2 * w') h' w' h'
57 todo' <- pane region1 (pure True) $ textButtonStatic def "Todo List"
58 editor <- pane region2 (pure True) $ textButtonStatic def "Text Editor"
59 return $ leftmost
60 [ Left Example_TextEditor <$ editor
61 , Left Example_Todo <$ todo'
62 ]
63 escapable w = do
64 void w
65 i <- input
66 return $ fforMaybe i $ \case
67 V.EvKey V.KEsc [] -> Just $ Right ()
68 _ -> Nothing
69 rec out <- networkHold buttons $ ffor (switch (current out)) $ \case
70 Left Example_TextEditor -> escapable testBoxes
71 Left Example_Todo -> escapable taskList
72 Right () -> buttons
74 return $ fforMaybe inp $ \case
75 V.EvKey (V.KChar 'c') [V.MCtrl] -> Just ()
76 _ -> Nothing
78taskList
79 :: (Reflex t, MonadHold t m, MonadFix m, Adjustable t m, NotReady t m, PostBuild t m)
80 => VtyWidget t m ()
81taskList = do
82 let btn = textButtonStatic def "Add another task"
83 inp <- input
84 rec let todos' = todos [Todo "Find reflex-vty" True, Todo "Become functional reactive" False, Todo "Make vty apps" False] $ leftmost
85 [ () <$ e
86 , fforMaybe inp $ \case
87 V.EvKey V.KEnter [] -> Just ()
88 _ -> Nothing
89 ]
90 (m, (e, _)) <- splitV (pure (subtract 6)) (pure (True, True)) todos' $ do
91 splitV (pure (subtract 3)) (pure (True, True)) btn (display $ current m)
92 return ()
94testBoxes :: (Reflex t, MonadHold t m, MonadFix m) => VtyWidget t m ()
95testBoxes = do
96 dw <- displayWidth
97 dh <- displayHeight
98 let region1 = DynRegion (div' dw 6) (div' dh 6) (div' dw 2) (div' dh 2)
99 region2 = DynRegion (div' dw 4) (div' dh 4) (2 * div' dw 3) (2 * div' dh 3)
100 pane region1 (constDyn False) . boxStatic singleBoxStyle $ debugInput
101 _ <- pane region2 (constDyn True) . boxStatic singleBoxStyle $
102 splitVDrag (hRule doubleBoxStyle) (boxStatic roundedBoxStyle $ multilineTextInput def) (boxStatic roundedBoxStyle dragTest)
103 return ()
105debugFocus :: (Reflex t, Monad m) => VtyWidget t m ()
106debugFocus = do
107 f <- focus
108 text $ T.pack . show <$> current f
110debugInput :: (Reflex t, MonadHold t m) => VtyWidget t m ()
111debugInput = do
112 lastEvent <- hold "No event yet" . fmap show =<< input
113 text $ T.pack <$> lastEvent
115dragTest :: (Reflex t, MonadHold t m, MonadFix m) => VtyWidget t m ()
116dragTest = do
117 lastEvent <- hold "No event yet" . fmap show =<< drag V.BLeft
118 text $ T.pack <$> lastEvent
120testStringBox :: (Reflex t, Monad m) => VtyWidget t m ()
121testStringBox = boxStatic singleBoxStyle .
122 text . pure . T.pack . take 500 $ cycle ('\n' : ['a'..'z'])
124data Todo = Todo
125 { _todo_label :: Text
126 , _todo_done :: Bool
127 }
128 deriving (Show, Read, Eq, Ord)
130data TodoOutput t = TodoOutput
131 { _todoOutput_todo :: Dynamic t Todo
132 , _todoOutput_delete :: Event t ()
133 , _todoOutput_height :: Dynamic t Int
134 }
136instance Reflex t => Switchable t (TodoOutput t) where
137 switching t0 e = TodoOutput
138 <$> switching (_todoOutput_todo t0) (_todoOutput_todo <$> e)
139 <*> switching (_todoOutput_delete t0) (_todoOutput_delete <$> e)
140 <*> switching (_todoOutput_height t0) (_todoOutput_height <$> e)
142todo
143 :: (MonadHold t m, MonadFix m, Reflex t)
144 => Todo
145 -> VtyWidget t m (TodoOutput t)
146todo t0 = do
147 w <- displayWidth
148 rec let checkboxWidth = 3
149 checkboxRegion = DynRegion 0 0 checkboxWidth 1
150 labelHeight = _textInput_lines ti
151 labelWidth = w - 1 - checkboxWidth
152 labelLeft = checkboxWidth + 1
153 labelTop = constDyn 0
154 labelRegion = DynRegion labelLeft labelTop labelWidth labelHeight
155 value <- pane checkboxRegion (pure True) $ checkbox def $ _todo_done t0
156 (ti, d) <- pane labelRegion (pure True) $ do
157 i <- input
158 v <- textInput $ def { _textInputConfig_initialValue = TZ.fromText $ _todo_label t0 }
159 let deleteSelf = attachWithMaybe backspaceOnEmpty (current $ _textInput_value v) i
160 return (v, deleteSelf)
161 return $ TodoOutput
162 { _todoOutput_todo = Todo <$> (_textInput_value ti) <*> value
163 , _todoOutput_delete = d
164 , _todoOutput_height = _textInput_lines ti
165 }
166 where
167 backspaceOnEmpty v = \case
168 V.EvKey V.KBS _ | T.null v -> Just ()
169 _ -> Nothing
171todos
172 :: forall t m. (MonadHold t m, MonadFix m, Reflex t, Adjustable t m, NotReady t m, PostBuild t m)
173 => [Todo]
174 -> Event t ()
175 -> VtyWidget t m (Dynamic t (Map Int Todo))
176todos todos0 newTodo = do
177 let todosMap0 = Map.fromList $ zip [0..] todos0
178 w <- displayWidth
179 rec listOut <- listHoldWithKey todosMap0 updates $ \row t -> do
180 let reg = zipDynWith (\w' ts ->
181 let l = Map.size $ Map.takeWhileAntitone (<row) ts
182 in Region 0 l w' 1) w todosMap
183 pane (DynRegion 0 (fromMaybe 0 . Map.lookup row <$> offsets) w (fromMaybe 1 . Map.lookup row <$> heights))
184 (fmap (==row) selected) $ do
185 e <- mouseUp
186 r <- todo t
187 return (row <$ e, r)
188 let heights :: Dynamic t (Map Int Int) = joinDynThroughMap $ fmap (fmap (_todoOutput_height . snd)) listOut
189 offsets :: Dynamic t (Map Int Int) = ffor heights $ \m ->
190 let l = Map.toList m
191 o = scanl (+) 0 $ map snd l
192 in Map.fromList $ zipWith (\(k, _) o' -> (k, o')) l o
193 let selectionClick = switch . current $ fmap (leftmost . Map.elems . fmap fst) listOut
194 selected <- holdDyn 0 $ leftmost
195 [ selectionClick
196 , fmapMaybe (fmap fst . Map.lookupMax) insert
197 , selectOnDelete
198 ]
199 let todosMap = joinDynThroughMap $ fmap (fmap (_todoOutput_todo . snd)) listOut
200 todoDelete = switch . current $
201 leftmost . Map.elems . Map.mapWithKey (\k -> (k <$) . _todoOutput_delete . snd) <$> listOut
202 selectOnDelete = attachWithMaybe
203 (\m k -> let (before, after) = Map.split k m
204 in fmap fst $ Map.lookupMax before <|> Map.lookupMin after)
205 (current todosMap)
206 todoDelete
207 insert = ffor (tag (current todosMap) newTodo) $ \m -> case Map.lookupMax m of
208 Nothing -> Map.singleton 0 $ Just $ Todo "" False
209 Just (k, _) -> Map.singleton (k+1) $ Just $ Todo "" False
210 delete = ffor todoDelete $ \k -> Map.singleton k Nothing
211 updates = leftmost [insert, delete]
212 return todosMap