gale client

root / Screen.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
{-# LANGUAGE OverlappingInstances #-}
module Screen(
    Packing(..),
    Attribute(..),
    Widget(..),
    Canvas(..),
    doRender,
    Key(..),
    RenderContext,
    newRenderContext,
    setRenderContext,
    tryDrawRenderContext,
    resizeRenderContext,
    setRenderWidget,
    keyRenderContext,
    keyCatcherWidget,
    newSVarWidget,
    dynamicWidget,
    widgetAttr,
    widgetCenter,
    widgetEmpty,
    widgetHorizontalBox,
    widgetScroll,
    widgetSimpleFrame,
    widgetText,
    widgetVerticalBox,
    stackedWidgets

    ) where

import Control.Concurrent
import Control.Exception as E
import Data.Bits
import Control.Monad

import CacheIO
import Curses
import ErrorLog
import Format
import GenUtil
import Doc.Chars
import Doc.DocLike()


data Canvas = Canvas {window :: !Window, origin :: !(Int,Int), widgetOrigin :: !(Int,Int), bounds :: !(Int,Int)  }

data Widget = Widget {
    render :: Canvas -> IO (),
    getBounds :: IO (Int,Int),
    processKey :: Key -> IO Bool
    }


data Packing = NoExpand | Expand | ExpandFill
    deriving(Show,Eq,Enum)
data Attribute = AttrBold | AttrBlink | AttrDim | AttrReverse | AttrUnderline | AttrColor String String
    deriving(Show,Eq)

instance Show Canvas where
    show c = fmtSs "<canvas:origin=%s,bounds=%s>" [show (origin c), show (bounds c)]

widgetEmpty = emptyWidget
--widgetEmptySized x y = emptySizedWidget (floor (x + 0.5)) (floor (y + 0.5))
widgetCenter w = centerWidget w
widgetSimpleFrame w = boxWidget w
widgetVerticalBox True ws = homVerticalBox ws
widgetVerticalBox False ws = verticalBox ws []
widgetHorizontalBox True ws = homHorizontalBox ws
widgetHorizontalBox False ws = horizontalBox ws []
widgetText s = staticText s
widgetAttr al w = attrWidget (foldl (flip ($)) attr0 (map ma al)) w where
    ma AttrBlink a = a `setBlink` True
    ma AttrBold a = a  `setBold` True
    ma AttrDim a = a  `setDim` True
    ma AttrReverse a = a  `setReverse` True
    ma AttrUnderline a = a `setUnderline` True
    ma _ a = a
widgetScroll = newScrollWidget


isExpand NoExpand = False
isExpand _ = True

isFill ExpandFill = True
isFill _ = False

{-
data Attribute = Attribute [String] String String
parseAttr :: String -> Attribute
parseAttr s = Attribute as fg bg where
    rs = filter (not . f . head) $ groupBy (\x y -> f x && f y) (map toLower s)
    as = filter (`elem` attributes) rs
    col x = if isJust (color x) then return x else Nothing
    fg = fromJust $ msum (map (cGet "fg") rs)  `mplus` msum (map col rs) `mplus` return "default"
    bg = fromJust $ msum (map (cGet "bg") rs) `mplus` return "default"
    f ',' = True
    f c | isSpace c = True
    f _ = False
    cGet p r | (p ++ ":") `isPrefixOf` r = col (drop (length p + 1) r)
    cGet _ _ = Nothing
    attributes = ["normal", "bold", "blink", "dim", "reverse", "underline" ]
-}

data RenderContext = RenderContext {
    needsResize :: MVar Bool,
    drawingStuff :: MVar (IO (), Key -> IO Bool, IO ())
    }

newRenderContext = do
    y <- newMVar False
    z <- newMVar (return (), \_ -> return False, return ())
    return RenderContext {needsResize = y, drawingStuff = z }


resizeRenderContext rc action = swapMVar (needsResize rc) True >>= \x -> unless x action >> return ()

tryDrawRenderContext rc = do
    y <- swapMVar (needsResize rc) False
    withMVar (drawingStuff rc) $ \(z,_,_) -> do
        erase
        when y (attemptIO endWin >> refresh)
        z
        refresh

setRenderContext rc dr = do
    modifyMVar_ (drawingStuff rc)  $ \(_,_,final) -> do
    final
    --touchRenderContext rc
    return (dr,\_ -> return False, return ())

setRenderWidget rc w = do
    modifyMVar_ (drawingStuff rc)  $ \(_,_,final) -> do
    final
    --touchRenderContext rc
    return (wdr, processKey w, return (){-, nf-}) where
        wdr = do
            c <- newCanvas
            {- eannM ("doRender " ++ show c) $ -}
            render w c

keyRenderContext :: RenderContext -> Key -> IO Bool
keyRenderContext _ KeyResize = return True
keyRenderContext rc k = do
    (_,pks,_) <- readMVar (drawingStuff rc)
    pks k

doRender w = do
    erase
    c <- newCanvas
    {- eannM ("doRender " ++ show c) $ -}
    render w c
    refresh

newCanvas = do
    (ys,xs) <- scrSize
    return $  (Canvas {window = stdScr, origin = (0,0), widgetOrigin = (0,0), bounds = (xs,ys)})




-------------------
-- Drawing Routines
-------------------

canvasTranslate :: Canvas -> (Int,Int) -> Canvas
-- canvasTranslate canvas@(Canvas {origin = (x,y), bounds = (xb,yb)}) (xt,yt) = assert (xt > 0 && yt > 0) $ canvas {origin = (x + xt, y + yt), bounds = (max (xb - xt) 0, max (yb - yt) 0)}
canvasTranslate canvas@(Canvas {origin = (x,y), bounds = (xb,yb), widgetOrigin = (wx,wy)}) (xt,yt) = canvas {origin = (x + xt, y + yt), widgetOrigin = (wx, wy), bounds = (max (xb - xt) 0, max (yb - yt) 0)}


canvasWTranslate :: Canvas -> (Int,Int) -> Canvas
canvasWTranslate canvas@(Canvas {widgetOrigin = (x,y)}) (xt,yt) = canvas {widgetOrigin = (x + xt, y + yt)}

withAttr :: Canvas -> Attr -> IO a -> IO a
withAttr canvas attr action = E.bracket (wAttrGet w) (wAttrSet w) f where
    f (a,p) = wAttrSet w (a .|. attr,p) >> action
    w = window canvas


drawString :: Canvas -> String -> IO ()
drawString canvas s = {- eannM (fmtSs "drawString %s %s" [(show canvas), s]) $ -} ds ml yb (origin canvas) where
        ds (s:ls) yb (x,y) | yb > 0 = tryIO (mvWAddStr (window canvas) y x (take xb s)) >> ds ls (yb - 1) (x,y + 1)
        ds _ _ _ = return ()
        (xb,yb) = bounds canvas
        ls = lines s
        (wx,wy) = widgetOrigin canvas
        ml = map (mv ' ' wx) (mv "" wy ls)
        mv _ 0 ss = ss
        mv _ n ss | n > 0 = drop n ss
        mv ec n ss = replicate (0 - n) ec ++ ss


boundCanvas :: Maybe Int -> Maybe Int -> Canvas -> Canvas
boundCanvas mx my canvas = (canvas {bounds = (nx,ny)}) where
    (cx,cy) = bounds canvas
    nx = maybe cx (min cx) mx
    ny = maybe cy (min cy) my


-- rendering routines

renderCentered :: Widget -> Canvas -> IO ()
renderCentered w canvas = do
        (xs,ys) <- getBounds w
        let (xs',ys') =  bounds canvas
            f cs ws | ws >= cs = 0
            f cs ws = (cs - ws) `div` 2
            nc = (canvasTranslate canvas (f xs' xs,f ys' ys))
        renderChild w nc {bounds=(min xs $ fst $ bounds nc ,min ys $ snd $ bounds nc)}


renderChild :: Widget -> Canvas -> IO ()
renderChild _ (Canvas {bounds = (xb,yb)}) | xb == 0 || yb == 0 = return ()
renderChild w c = render w c

----------
-- Widgets
----------



emptyWidget = Widget {
    render = const (return ()),
    getBounds = return (0,0),
    processKey = \_ -> return False
    }


childrenWidget ws = emptyWidget {processKey = pk} where
    pk k = tk ws k
    tk [] _ = return False
    tk (w:ws) k = do
        b <- processKey w k
        if b then return True else tk ws k
-- layout widgets

-- | centers child widget, causes child to not use any extra space.
centerWidget :: Widget -> Widget
centerWidget w = w {render = rst} where
    rst canvas = do
        (xs,ys) <- getBounds w
        let (xs',ys') =  bounds canvas
        renderChild w (canvasTranslate canvas (f xs' xs,f ys' ys)) {bounds=(min xs $ fst $ bounds canvas ,min ys $ snd $ bounds canvas)} where
            f cs ws | ws >= cs = 0
            f cs ws = (cs - ws) `div` 2


-- General Widgets


newScrollWidget :: Widget -> IO Widget
newScrollWidget w = do
    yr <- newMVar 0
    let rst canvas = do
            y <- readMVar yr
            renderChild w (canvasWTranslate canvas (0,y))
        pk (KeyChar 'j') = modifyMVar_ yr (\x -> return $ x + 1) >> keyDone
        pk KeyDown = modifyMVar_ yr (\x -> return $ x + 1) >> keyDone
        pk (KeyChar 'k') = modifyMVar_ yr (\x -> return $ x - 1) >> keyDone
        pk KeyUp = modifyMVar_ yr (\x -> return $ x - 1) >> keyDone
        pk KeyHome = swapMVar yr 0 >> keyDone
        pk (KeyChar 'g') = swapMVar yr 0 >> keyDone
        pk KeyNPage = modifyMVar_ yr (\x -> return $ x + 25) >> keyDone
        pk KeyPPage = modifyMVar_ yr (\x -> return $ x - 25) >> keyDone
        pk k = processKey w k
        keyDone = modifyMVar_ yr (\x -> return  (max 0 x)) >> return True
    return w {processKey = pk, render = rst}


-- | draw box around child widget
boxWidget :: Widget -> Widget
boxWidget w = w {render = rst, getBounds = gb} where
    gb = getBounds w >>= \(x,y) -> return (x + 2, y + 2)
    rst canvas@(Canvas {bounds = (xb,yb)}) = do
        -- let tb = '+' : (replicate (xb - 2) '-' ++ "+")
        let tb = ulCorner ++ (replicate (xb - 2) hLine ++ urCorner)
            bb = llCorner ++ (replicate (xb - 2) hLine ++ lrCorner)
            bc c@(Canvas {bounds = (xb,yb)}) = c {bounds = (max (xb - 1) 0, max (yb - 1) 0)}
        renderChild w (canvasTranslate (bc canvas) (1,1))
        drawString canvas tb
        drawString (canvasTranslate canvas (0,yb - 1)) bb
        sequence_ [drawString (canvasTranslate canvas (xt,yt)) (vLine :: String) | yt <- [1..yb - 2], xt <- [0,xb - 1]]


{-
widgetMinBBox :: Maybe Int -> Maybe Int -> Widget -> Widget
widgetMaxBBox :: Maybe Int -> Maybe Int -> Widget -> Widget
widgetExactBBox :: Maybe Int -> Maybe Int -> Widget -> Widget
-}

staticText s = emptyWidget {render = rst, getBounds = return bounds } where
    bounds = (maximum (0:map length ls),length ls)
    ls = lines s
    rst canvas = drawString canvas s





verticalBox :: [(Packing,Widget)] -> [(Packing,Widget)] -> Widget
verticalBox starts ends = (childrenWidget aw) {render = rd, getBounds = gb} where
    awpb = starts ++ ends
    awp = if any isExpand $ fsts awpb then awpb else starts ++ [(Expand,emptyWidget)] ++ ends
    aw = snds awp
    gb = do
        (xs,ys) <- fmap unzip $ mapM getBounds aw
        return (maximum xs, sum ys)
    rd canvas = do
        let (_,yb) = bounds canvas
        --let ywo = snd $ widgetOrigin $ canvas
        (_,ys) <- gb
        let ne = length (filter isExpand $ fsts awp)
            el = if yb > ys then splitSpace (yb - ys) ne else replicate ne 0
            f ((e:es),canvas) (ps,w) | isExpand ps = do
                (_,wby) <- getBounds w
                if isFill ps then
                    renderChild w (boundCanvas Nothing (Just (wby + e)) canvas)
                        else renderCentered w (boundCanvas Nothing (Just (wby + e)) canvas)
                return (es,canvasTranslate canvas (0,wby + e))
            f (es,canvas) (_,w) = do
                (_,wby) <- getBounds w
                --renderCentered w (boundCanvas Nothing (Just wby) canvas)
                renderChild w (boundCanvas Nothing (Just wby) canvas)
                return (es,canvasTranslate canvas (0,wby))
        foldlM_ f (el,canvas) awp


horizontalBox :: [(Packing,Widget)] -> [(Packing,Widget)] -> Widget
horizontalBox starts ends = (childrenWidget aw) {render = rd, getBounds = gb } where
    awpb = starts ++ ends
    awp = if any isExpand $ fsts awpb then awpb else starts ++ [(Expand,emptyWidget)] ++ ends
    aw = snds awp
    gb = do
        (xs,ys) <- fmap unzip $ mapM getBounds aw
        return (sum xs, maximum ys)
    rd canvas = do
        let (xb,_) = bounds canvas
        (xs,_) <- gb
        let ne = length (filter isExpand $ fsts awp)
            el = if xb > xs then splitSpace (xb - xs) ne else replicate ne 0
            f ((e:es),canvas) (ps,w) | isExpand ps = do
                (wbx,_) <- getBounds w
                if isFill ps then
                    renderChild w (boundCanvas (Just (wbx + e)) Nothing canvas)
                        else renderCentered w (boundCanvas (Just (wbx + e)) Nothing canvas)
                return (es,canvasTranslate canvas (wbx + e,0))
            f (es,canvas) (_,w) = do
                (wbx,_) <- getBounds w
                --renderCentered w (boundCanvas (Just wbx) Nothing canvas)
                renderChild w (boundCanvas (Just wbx) Nothing canvas)
                return (es,canvasTranslate canvas (wbx,0))
        foldlM_ f (el,canvas) awp

homVerticalBox :: [(Packing,Widget)] -> Widget
homVerticalBox awp = (childrenWidget aw) {render = rd, getBounds = gb } where
    aw = snds awp
    gb = do
        (xs,ys) <- fmap unzip $ mapM getBounds aw
        return (maximum xs, (maximum ys) * length aw)
    rd canvas = do
        let (_,yb) = bounds canvas
            el = splitSpace yb (length aw)
            f ([],_) _ = error "no space to split"
            f ((e:es),canvas) (ps,w)  = do
                if isFill ps then
                    renderChild w (boundCanvas Nothing (Just e) canvas)
                        else renderCentered w (boundCanvas Nothing (Just e) canvas)
                return (es,canvasTranslate canvas (0,e))
        foldlM_ f (el,canvas) awp


homHorizontalBox :: [(Packing,Widget)] -> Widget
homHorizontalBox awp = (childrenWidget aw) {render = rd, getBounds = gb } where
    aw = snds awp
    gb = do
        (xs,ys) <- fmap unzip $ mapM getBounds aw
        return (maximum xs * length aw, maximum ys)
    rd canvas = do
        let (xb,_) = bounds canvas
            el = splitSpace xb (length aw)
            f ([],_) _ = error "no space to split"
            f ((e:es),canvas) (ps,w)  = do
                if isFill ps then
                    renderChild w (boundCanvas (Just e) Nothing canvas)
                        else renderCentered w (boundCanvas (Just e) Nothing canvas)
                return (es,canvasTranslate canvas (e,0))
        foldlM_ f (el,canvas) awp

splitSpace m n = f (m `mod` n) $ replicate n (m `div` n) where
    f 0 xs = xs
    f n (x:xs) = (x + 1) : f (n - 1) xs
    f n' [] = errorf "splitSpace %i %i (f %i []): this can't happen." [fi m, fi n, fi n']



newSVarWidget :: (Readable c ) => c a -> (a -> Widget) -> Widget
newSVarWidget sv f = emptyWidget {render = rw, getBounds = gb} where
    rw canvas = do
        v <- readVal sv
        render (f v) canvas
    gb = do
        v <- readVal sv
        getBounds (f v)

dynamicWidget :: IO Widget -> Widget
dynamicWidget w = emptyWidget {render = rw, getBounds = gb, processKey = pk {- , processEvent = pe -} } where
    rw canvas = do
        v <- w
        render v canvas
    gb = w >>= getBounds
    pk k = w >>= \x -> processKey x k
--    pe a b = w >>= \x -> processEvent x a b



stackedWidgets :: [Widget] -> Widget
stackedWidgets [] = widgetEmpty
stackedWidgets ws@(w:_) = widgetEmpty { render = rnd, processKey = \k -> processKey w k, getBounds = gb  } where
    rnd canvas = mapM_ (flip render canvas) (reverse ws)
    gb = do
        bs <- mapM getBounds ws
        return $ (liftT2 (maximum, maximum)) (unzip bs)

{-
newAlternate :: Widget -> IO (Widget, Alternate)
newAlternate w = do
    sig <- newSignal
    final <- connect (changedSignal w) (\() -> signal sig ())
    r <- newMVar (w,final)
    let w = emptyWidget {render = rw, getBounds = gb, processKey = pk }
        rw canvas = do
            (w,_) <- readMVar r
            renderChild w canvas
        gb = do
            (w,_) <- readMVar r
            getBounds w
        pk k = do
            (w,_) <- readMVar r
            processKey w k
    return (w,Alternate r sig)


switchAlternate :: Alternate -> Widget -> IO ()
switchAlternate (Alternate mv sig) w = (modifyMVar_ mv $ \(_,final) ->
        (final >> connect (changedSignal w) (\() -> signal sig ()) >>= \nf -> return (w,nf)))
            >> signal sig ()
-}

keyCatcherWidget :: (Curses.Key -> IO Bool) -> Widget -> Widget
keyCatcherWidget kr w = w {processKey = kr'} where
    kr' k = do
        b <- processKey w k
        if b then return True else (kr k)

attrWidget :: Attr -> Widget -> Widget
attrWidget a w = w {render = nr} where
    nr canvas = Screen.withAttr canvas a (renderChild w  canvas)

{-
-- data Event = KeyEvent Key | MouseEvent MouseEvent

--data Justification = Justified | LeftJustified | RightJustified


data InlineBox = InlineBoxText String | InlineBoxAttr [Attr] [InlineBox] | InlineBoxChoice InlineBox InlineBox |
    InlineBoxLB | InlineBoxRight [InlineBox] -- | InlineBoxAction (Event -> IO Bool) [InlineBox]




inlineBoxWidget :: [InlineBox] -> Widget
inlineBoxWidget xs = emptyWidget {render = rnd, getBounds = gb } where
    rnd canvas = undefined
    gb = return (0,0)

-}