summaryrefslogtreecommitdiffstats
path: root/lib/Phi/Panel.hs
blob: b15f6abab7a55f69e3ae020d8a9b3f6f2fd3f49d (plain)
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
{-# LANGUAGE ExistentialQuantification #-}

module Phi.Panel ( Position(..)
                 , Panel(..)
                 , PanelClass(..)
                 , (<~>)
                 , PanelConfig(..)
                 , defaultPanelConfig
                 , separator
                 ) where

import Data.Function
import Data.Monoid

data Position = Top | Bottom

class PanelClass a where
  minSize :: a -> Int
  
  weight :: a -> Float
  weight _ = 0

data Panel = forall a. PanelClass a => Panel a | CompoundPanel [Panel]

instance Monoid Panel where
  mempty = CompoundPanel []
  mappend a b = makePanel $ (toList a) ++ (toList b)
    where
      toList (Panel p) = [Panel p]
      toList (CompoundPanel panels) = panels
      
      makePanel [p] = p
      makePanel panels = CompoundPanel panels

instance PanelClass Panel where
  minSize (Panel p) = minSize p
  minSize (CompoundPanel panels) = sum $ map minSize panels
  
  weight (Panel p) = weight p
  weight (CompoundPanel panels) = sum $ map weight panels

(<~>) :: Panel -> Panel -> Panel
(<~>) = mappend


data PanelConfig = PanelConfig { panelPosition :: Position
                               , panelSize     :: Int
                               , panelContent  :: Panel
                               }

defaultPanelConfig :: PanelConfig
defaultPanelConfig = PanelConfig { panelPosition = Top, panelSize = 24, panelContent = mempty }

data Separator = Separator Int Float

instance PanelClass Separator where
  minSize (Separator s _) = s
  weight (Separator _ w) = w

separator :: Int -> Float -> Panel
separator s w = Panel $ Separator s w