{-# LANGUAGE ScopedTypeVariables, GeneralizedNewtypeDeriving,
FlexibleInstances, MultiParamTypeClasses, FlexibleContexts #-}
module XMonad.Util.WindowState (
get,
put,
StateQuery(..),
runStateQuery,
catchQuery ) where
import XMonad hiding (get, put, modify)
import Control.Monad.Reader(ReaderT(..))
import Control.Monad.State.Class
import Data.Typeable (typeOf)
newtype StateQuery s a = StateQuery {
forall s a. StateQuery s a -> Query a
getQuery :: Query a
} deriving (forall {s}. Applicative (StateQuery s)
forall a. a -> StateQuery s a
forall s a. a -> StateQuery s a
forall a b. StateQuery s a -> StateQuery s b -> StateQuery s b
forall a b.
StateQuery s a -> (a -> StateQuery s b) -> StateQuery s b
forall s a b. StateQuery s a -> StateQuery s b -> StateQuery s b
forall s a b.
StateQuery s a -> (a -> StateQuery s b) -> StateQuery s b
forall (m :: * -> *).
Applicative m
-> (forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
return :: forall a. a -> StateQuery s a
$creturn :: forall s a. a -> StateQuery s a
>> :: forall a b. StateQuery s a -> StateQuery s b -> StateQuery s b
$c>> :: forall s a b. StateQuery s a -> StateQuery s b -> StateQuery s b
>>= :: forall a b.
StateQuery s a -> (a -> StateQuery s b) -> StateQuery s b
$c>>= :: forall s a b.
StateQuery s a -> (a -> StateQuery s b) -> StateQuery s b
Monad, forall s. Monad (StateQuery s)
forall a. IO a -> StateQuery s a
forall s a. IO a -> StateQuery s a
forall (m :: * -> *).
Monad m -> (forall a. IO a -> m a) -> MonadIO m
liftIO :: forall a. IO a -> StateQuery s a
$cliftIO :: forall s a. IO a -> StateQuery s a
MonadIO, forall {s}. Functor (StateQuery s)
forall a. a -> StateQuery s a
forall s a. a -> StateQuery s a
forall a b. StateQuery s a -> StateQuery s b -> StateQuery s a
forall a b. StateQuery s a -> StateQuery s b -> StateQuery s b
forall a b.
StateQuery s (a -> b) -> StateQuery s a -> StateQuery s b
forall s a b. StateQuery s a -> StateQuery s b -> StateQuery s a
forall s a b. StateQuery s a -> StateQuery s b -> StateQuery s b
forall s a b.
StateQuery s (a -> b) -> StateQuery s a -> StateQuery s b
forall a b c.
(a -> b -> c) -> StateQuery s a -> StateQuery s b -> StateQuery s c
forall s a b c.
(a -> b -> c) -> StateQuery s a -> StateQuery s b -> StateQuery s c
forall (f :: * -> *).
Functor f
-> (forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
<* :: forall a b. StateQuery s a -> StateQuery s b -> StateQuery s a
$c<* :: forall s a b. StateQuery s a -> StateQuery s b -> StateQuery s a
*> :: forall a b. StateQuery s a -> StateQuery s b -> StateQuery s b
$c*> :: forall s a b. StateQuery s a -> StateQuery s b -> StateQuery s b
liftA2 :: forall a b c.
(a -> b -> c) -> StateQuery s a -> StateQuery s b -> StateQuery s c
$cliftA2 :: forall s a b c.
(a -> b -> c) -> StateQuery s a -> StateQuery s b -> StateQuery s c
<*> :: forall a b.
StateQuery s (a -> b) -> StateQuery s a -> StateQuery s b
$c<*> :: forall s a b.
StateQuery s (a -> b) -> StateQuery s a -> StateQuery s b
pure :: forall a. a -> StateQuery s a
$cpure :: forall s a. a -> StateQuery s a
Applicative, forall a b. a -> StateQuery s b -> StateQuery s a
forall a b. (a -> b) -> StateQuery s a -> StateQuery s b
forall s a b. a -> StateQuery s b -> StateQuery s a
forall s a b. (a -> b) -> StateQuery s a -> StateQuery s b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> StateQuery s b -> StateQuery s a
$c<$ :: forall s a b. a -> StateQuery s b -> StateQuery s a
fmap :: forall a b. (a -> b) -> StateQuery s a -> StateQuery s b
$cfmap :: forall s a b. (a -> b) -> StateQuery s a -> StateQuery s b
Functor)
packIntoQuery :: (Window -> X a) -> Query a
packIntoQuery :: forall a. (Window -> X a) -> Query a
packIntoQuery = forall a. ReaderT Window X a -> Query a
Query forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
ReaderT
runStateQuery :: StateQuery s a -> Window -> X a
runStateQuery :: forall s a. StateQuery s a -> Window -> X a
runStateQuery = forall a. Query a -> Window -> X a
runQuery forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s a. StateQuery s a -> Query a
getQuery
catchQuery :: Query a -> Query (Maybe a)
catchQuery :: forall a. Query a -> Query (Maybe a)
catchQuery Query a
q = forall a. (Window -> X a) -> Query a
packIntoQuery forall a b. (a -> b) -> a -> b
$ \Window
win -> forall a. X a -> X (Maybe a)
userCode forall a b. (a -> b) -> a -> b
$ forall a. Query a -> Window -> X a
runQuery Query a
q Window
win
instance (Show s, Read s, Typeable s) => MonadState (Maybe s) (StateQuery s) where
get :: StateQuery s (Maybe s)
get = forall s a. Query a -> StateQuery s a
StateQuery forall a b. (a -> b) -> a -> b
$ Read s => String -> Maybe s
read' forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe s -> Query String
get' forall a. HasCallStack => a
undefined where
get' :: Maybe s -> Query String
get' :: Maybe s -> Query String
get' Maybe s
x = String -> Query String
stringProperty (forall a. Typeable a => a -> String
typePropertyName Maybe s
x)
read' :: (Read s) => String -> Maybe s
read' :: Read s => String -> Maybe s
read' String
"" = forall a. Maybe a
Nothing
read' String
s = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a. Read a => String -> a
read String
s
put :: Maybe s -> StateQuery s ()
put = forall s a. Query a -> StateQuery s a
StateQuery forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (Window -> X a) -> Query a
packIntoQuery forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall {a}. (Show a, Typeable a) => Maybe a -> Window -> X ()
setWindowProperty' where
setWindowProperty' :: Maybe a -> Window -> X ()
setWindowProperty' Maybe a
val = String -> String -> Window -> X ()
setWindowProperty String
prop String
strValue where
prop :: String
prop = forall a. Typeable a => a -> String
typePropertyName Maybe a
val
strValue :: String
strValue = forall b a. b -> (a -> b) -> Maybe a -> b
maybe String
"" forall a. Show a => a -> String
show Maybe a
val
typePropertyName :: (Typeable a) => a -> String
typePropertyName :: forall a. Typeable a => a -> String
typePropertyName a
x = String
"_XMONAD_WINSTATE__" forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show (forall a. Typeable a => a -> TypeRep
typeOf a
x)
type PropertyName = String
setWindowProperty :: PropertyName -> String -> Window -> X ()
setWindowProperty :: String -> String -> Window -> X ()
setWindowProperty String
prop String
val Window
win = forall a. (Display -> X a) -> X a
withDisplay forall a b. (a -> b) -> a -> b
$ \Display
d -> forall (m :: * -> *) a. MonadIO m => IO a -> m a
io forall a b. (a -> b) -> a -> b
$
Display -> String -> Bool -> IO Window
internAtom Display
d String
prop Bool
False forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
Display -> Window -> String -> Window -> IO ()
setTextProperty Display
d Window
win String
val