2012-03-08 21:12:54 +01:00
|
|
|
{-# LANGUAGE DeriveDataTypeable, ExistentialQuantification, TypeSynonymInstances, MultiParamTypeClasses, FlexibleInstances, PatternGuards, DatatypeContexts #-}
|
2011-02-25 19:44:32 +01:00
|
|
|
|
|
|
|
module Storage ( StoreData
|
|
|
|
, Storage
|
|
|
|
, storage
|
|
|
|
, getStoreData
|
|
|
|
, setStoreData
|
|
|
|
, updateStoreData
|
|
|
|
) where
|
|
|
|
|
|
|
|
import XMonad
|
|
|
|
import qualified XMonad.StackSet as W
|
|
|
|
import XMonad.Layout.LayoutModifier (LayoutModifier(..), ModifiedLayout(..))
|
|
|
|
|
|
|
|
import Control.Applicative ((<$>))
|
|
|
|
import Control.Monad
|
|
|
|
|
|
|
|
import Data.IORef
|
|
|
|
import Data.Maybe
|
|
|
|
import Data.Typeable
|
|
|
|
|
|
|
|
|
|
|
|
class (Typeable d, Show d, Read d) => StoreData d
|
|
|
|
|
|
|
|
|
|
|
|
data (StoreData d) => StorageMessage d = GetStoreData (IORef (Maybe d)) | SetStoreData d
|
|
|
|
deriving Typeable
|
|
|
|
instance (StoreData d) => Message (StorageMessage d)
|
|
|
|
|
|
|
|
|
|
|
|
data (StoreData d) => Storage d a = Storage d deriving (Show, Read)
|
|
|
|
|
|
|
|
instance (StoreData d) => LayoutModifier (Storage d) a where
|
|
|
|
modifierDescription _ = "Storage"
|
|
|
|
|
|
|
|
handleMess (Storage d) m
|
2011-02-27 16:18:48 +01:00
|
|
|
| Just (GetStoreData ref) <- fromMessage m = do
|
2011-02-25 19:44:32 +01:00
|
|
|
io $ writeIORef ref $ Just d
|
|
|
|
return $ Nothing
|
|
|
|
|
2011-02-27 16:18:48 +01:00
|
|
|
| Just (SetStoreData d') <- fromMessage m = do
|
2011-02-25 19:44:32 +01:00
|
|
|
return $ Just $ Storage d'
|
|
|
|
|
|
|
|
handleMess _ _ = return Nothing
|
|
|
|
|
|
|
|
|
|
|
|
storage :: (LayoutClass l a, StoreData d) => d -> l a -> ModifiedLayout (Storage d) l a
|
|
|
|
storage def = ModifiedLayout $ Storage def
|
|
|
|
|
|
|
|
|
2011-02-26 04:21:44 +01:00
|
|
|
sendMessage' :: Message a => a -> X ()
|
|
|
|
sendMessage' a = W.workspace . W.current <$> gets windowset >>= sendMessageWithNoRefresh a
|
|
|
|
|
|
|
|
|
2011-02-25 19:44:32 +01:00
|
|
|
getStoreData :: StoreData d => X (Maybe d)
|
|
|
|
getStoreData = do
|
|
|
|
ref <- io . newIORef $ Nothing
|
2011-02-26 04:21:44 +01:00
|
|
|
sendMessage' $ GetStoreData ref
|
2011-02-25 19:44:32 +01:00
|
|
|
io . readIORef $ ref
|
|
|
|
|
|
|
|
|
|
|
|
setStoreData :: StoreData d => d -> X ()
|
|
|
|
setStoreData = broadcastMessage . SetStoreData
|
|
|
|
|
|
|
|
updateStoreData :: StoreData d => (d -> d) -> X ()
|
|
|
|
updateStoreData f = do
|
|
|
|
d <- getStoreData
|
|
|
|
when (isJust d) $ setStoreData . f $ fromJust d
|