module XMonad.Util.Loggers.NamedScratchpad (
nspTrackStartup
,nspTrackHook
,nspActiveIcon
,nspActive
,nspActive') where
import XMonad.Core
import Graphics.X11.Xlib (Window)
import Graphics.X11.Xlib.Extras (Event(..))
import XMonad.Util.Loggers (Logger)
import XMonad.Util.NamedScratchpad (NamedScratchpad(..))
import qualified XMonad.Util.ExtensibleState as XS
import XMonad.Prelude (All (..), chr, foldM, forM)
import qualified Data.IntMap as M
import qualified XMonad.StackSet as W (allWindows)
newtype NSPTrack = NSPTrack [Maybe Window]
instance ExtensionClass NSPTrack where
initialValue :: NSPTrack
initialValue = [Maybe Window] -> NSPTrack
NSPTrack []
nspTrackStartup :: [NamedScratchpad] -> X ()
nspTrackStartup :: [NamedScratchpad] -> X ()
nspTrackStartup [NamedScratchpad]
ns = do
let ns'i :: IntMap (Maybe a)
ns'i = [(Key, Maybe a)] -> IntMap (Maybe a)
forall a. [(Key, a)] -> IntMap a
M.fromList ([(Key, Maybe a)] -> IntMap (Maybe a))
-> [(Key, Maybe a)] -> IntMap (Maybe a)
forall a b. (a -> b) -> a -> b
$ [Key] -> [Maybe a] -> [(Key, Maybe a)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Key
0..] ([Maybe a] -> [(Key, Maybe a)]) -> [Maybe a] -> [(Key, Maybe a)]
forall a b. (a -> b) -> a -> b
$ (NamedScratchpad -> Maybe a) -> [NamedScratchpad] -> [Maybe a]
forall a b. (a -> b) -> [a] -> [b]
map (Maybe a -> NamedScratchpad -> Maybe a
forall a b. a -> b -> a
const Maybe a
forall a. Maybe a
Nothing) [NamedScratchpad]
ns
IntMap (Maybe Window)
ns' <- (WindowSet -> X (IntMap (Maybe Window)))
-> X (IntMap (Maybe Window))
forall a. (WindowSet -> X a) -> X a
withWindowSet ((WindowSet -> X (IntMap (Maybe Window)))
-> X (IntMap (Maybe Window)))
-> (WindowSet -> X (IntMap (Maybe Window)))
-> X (IntMap (Maybe Window))
forall a b. (a -> b) -> a -> b
$ (IntMap (Maybe Window) -> Window -> X (IntMap (Maybe Window)))
-> IntMap (Maybe Window) -> [Window] -> X (IntMap (Maybe Window))
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM ([NamedScratchpad]
-> IntMap (Maybe Window) -> Window -> X (IntMap (Maybe Window))
isSp [NamedScratchpad]
ns) IntMap (Maybe Window)
forall a. IntMap (Maybe a)
ns'i ([Window] -> X (IntMap (Maybe Window)))
-> (WindowSet -> [Window])
-> WindowSet
-> X (IntMap (Maybe Window))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WindowSet -> [Window]
forall a i l s sd. Eq a => StackSet i l a s sd -> [a]
W.allWindows
NSPTrack -> X ()
forall a (m :: * -> *). (ExtensionClass a, XLike m) => a -> m ()
XS.put ([Maybe Window] -> NSPTrack
NSPTrack (((Key, Maybe Window) -> Maybe Window)
-> [(Key, Maybe Window)] -> [Maybe Window]
forall a b. (a -> b) -> [a] -> [b]
map (Key, Maybe Window) -> Maybe Window
forall a b. (a, b) -> b
snd ([(Key, Maybe Window)] -> [Maybe Window])
-> [(Key, Maybe Window)] -> [Maybe Window]
forall a b. (a -> b) -> a -> b
$ IntMap (Maybe Window) -> [(Key, Maybe Window)]
forall a. IntMap a -> [(Key, a)]
M.toAscList IntMap (Maybe Window)
ns'))
isSp :: [NamedScratchpad] -> M.IntMap (Maybe Window) -> Window -> X (M.IntMap (Maybe Window))
isSp :: [NamedScratchpad]
-> IntMap (Maybe Window) -> Window -> X (IntMap (Maybe Window))
isSp [NamedScratchpad]
ns IntMap (Maybe Window)
ws Window
w = do
Maybe Key
n <- Query (Maybe Key) -> Window -> X (Maybe Key)
forall a. Query a -> Window -> X a
runQuery ([NamedScratchpad] -> Query (Maybe Key)
scratchpadWindow [NamedScratchpad]
ns) Window
w
IntMap (Maybe Window) -> X (IntMap (Maybe Window))
forall (m :: * -> *) a. Monad m => a -> m a
return (IntMap (Maybe Window) -> X (IntMap (Maybe Window)))
-> IntMap (Maybe Window) -> X (IntMap (Maybe Window))
forall a b. (a -> b) -> a -> b
$ case Maybe Key
n of
Maybe Key
Nothing -> IntMap (Maybe Window)
ws
Just Key
n' -> Key
-> Maybe Window -> IntMap (Maybe Window) -> IntMap (Maybe Window)
forall a. Key -> a -> IntMap a -> IntMap a
M.insert Key
n' (Window -> Maybe Window
forall a. a -> Maybe a
Just Window
w) IntMap (Maybe Window)
ws
scratchpadWindow :: [NamedScratchpad] -> Query (Maybe Int)
scratchpadWindow :: [NamedScratchpad] -> Query (Maybe Key)
scratchpadWindow [NamedScratchpad]
ns = (Maybe Key -> (Key, NamedScratchpad) -> Query (Maybe Key))
-> Maybe Key -> [(Key, NamedScratchpad)] -> Query (Maybe Key)
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM Maybe Key -> (Key, NamedScratchpad) -> Query (Maybe Key)
sp' Maybe Key
forall a. Maybe a
Nothing ([Key] -> [NamedScratchpad] -> [(Key, NamedScratchpad)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Key
0..] [NamedScratchpad]
ns)
where sp' :: Maybe Int -> (Int,NamedScratchpad) -> Query (Maybe Int)
sp' :: Maybe Key -> (Key, NamedScratchpad) -> Query (Maybe Key)
sp' r :: Maybe Key
r@(Just Key
_) (Key, NamedScratchpad)
_ = Maybe Key -> Query (Maybe Key)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Key
r
sp' Maybe Key
Nothing (Key
n,NS String
_ String
_ Query Bool
q ManageHook
_) = Query Bool
q Query Bool -> (Bool -> Query (Maybe Key)) -> Query (Maybe Key)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Bool
p -> Maybe Key -> Query (Maybe Key)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe Key -> Query (Maybe Key)) -> Maybe Key -> Query (Maybe Key)
forall a b. (a -> b) -> a -> b
$ if Bool
p then Key -> Maybe Key
forall a. a -> Maybe a
Just Key
n else Maybe Key
forall a. Maybe a
Nothing
nspTrackHook :: [NamedScratchpad] -> Event -> X All
nspTrackHook :: [NamedScratchpad] -> Event -> X All
nspTrackHook [NamedScratchpad]
_ DestroyWindowEvent{ev_window :: Event -> Window
ev_window = Window
w} = do
(NSPTrack -> NSPTrack) -> X ()
forall a (m :: * -> *).
(ExtensionClass a, XLike m) =>
(a -> a) -> m ()
XS.modify ((NSPTrack -> NSPTrack) -> X ()) -> (NSPTrack -> NSPTrack) -> X ()
forall a b. (a -> b) -> a -> b
$ \(NSPTrack [Maybe Window]
ws) -> [Maybe Window] -> NSPTrack
NSPTrack ([Maybe Window] -> NSPTrack) -> [Maybe Window] -> NSPTrack
forall a b. (a -> b) -> a -> b
$ (Maybe Window -> Maybe Window) -> [Maybe Window] -> [Maybe Window]
forall a b. (a -> b) -> [a] -> [b]
map (\Maybe Window
sw -> if Maybe Window
sw Maybe Window -> Maybe Window -> Bool
forall a. Eq a => a -> a -> Bool
== Window -> Maybe Window
forall a. a -> Maybe a
Just Window
w then Maybe Window
forall a. Maybe a
Nothing else Maybe Window
sw) [Maybe Window]
ws
All -> X All
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> All
All Bool
True)
nspTrackHook [NamedScratchpad]
ns ConfigureRequestEvent{ev_window :: Event -> Window
ev_window = Window
w} = do
NSPTrack [Maybe Window]
ws <- X NSPTrack
forall a (m :: * -> *). (ExtensionClass a, XLike m) => m a
XS.get
[Maybe Window]
ws' <- [(Integer, Maybe Window, NamedScratchpad)]
-> ((Integer, Maybe Window, NamedScratchpad) -> X (Maybe Window))
-> X [Maybe Window]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM ([Integer]
-> [Maybe Window]
-> [NamedScratchpad]
-> [(Integer, Maybe Window, NamedScratchpad)]
forall a b c. [a] -> [b] -> [c] -> [(a, b, c)]
zip3 [Integer
0 :: Integer ..] [Maybe Window]
ws [NamedScratchpad]
ns) (((Integer, Maybe Window, NamedScratchpad) -> X (Maybe Window))
-> X [Maybe Window])
-> ((Integer, Maybe Window, NamedScratchpad) -> X (Maybe Window))
-> X [Maybe Window]
forall a b. (a -> b) -> a -> b
$ \(Integer
_,Maybe Window
w',NS String
_ String
_ Query Bool
q ManageHook
_) -> do
Bool
p <- Query Bool -> Window -> X Bool
forall a. Query a -> Window -> X a
runQuery Query Bool
q Window
w
Maybe Window -> X (Maybe Window)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe Window -> X (Maybe Window))
-> Maybe Window -> X (Maybe Window)
forall a b. (a -> b) -> a -> b
$ if Bool
p then Window -> Maybe Window
forall a. a -> Maybe a
Just Window
w else Maybe Window
w'
NSPTrack -> X ()
forall a (m :: * -> *). (ExtensionClass a, XLike m) => a -> m ()
XS.put (NSPTrack -> X ()) -> NSPTrack -> X ()
forall a b. (a -> b) -> a -> b
$ [Maybe Window] -> NSPTrack
NSPTrack [Maybe Window]
ws'
All -> X All
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> All
All Bool
True)
nspTrackHook [NamedScratchpad]
_ Event
_ = All -> X All
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> All
All Bool
True)
nspActiveIcon :: [Char] -> (String -> String) -> (String -> String) -> Logger
nspActiveIcon :: String -> (String -> String) -> (String -> String) -> Logger
nspActiveIcon String
icns String -> String
act String -> String
inact = do
NSPTrack [Maybe Window]
ws <- X NSPTrack
forall a (m :: * -> *). (ExtensionClass a, XLike m) => m a
XS.get
Maybe String -> Logger
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe String -> Logger) -> Maybe String -> Logger
forall a b. (a -> b) -> a -> b
$ if [Maybe Window] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Maybe Window]
ws
then Maybe String
forall a. Maybe a
Nothing
else let icon' :: Key -> Char
icon' Key
n = if Key
n Key -> Key -> Bool
forall a. Ord a => a -> a -> Bool
< String -> Key
forall (t :: * -> *) a. Foldable t => t a -> Key
length String
icns then String
icns String -> Key -> Char
forall a. [a] -> Key -> a
!! Key
n else Char
'\NUL'
icon :: Key -> String
icon Key
n = let c :: Char
c = Key -> Char
icon' Key
n
in [if Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'\NUL' then Key -> Char
chr (Key
0x2460 Key -> Key -> Key
forall a. Num a => a -> a -> a
+ Key
n) else Char
c]
ckact :: Key -> Maybe a -> String
ckact Key
n Maybe a
w = let icn :: String
icn = Key -> String
icon Key
n
in case Maybe a
w of
Maybe a
Nothing -> String -> String
inact String
icn
Just a
_ -> String -> String
act String
icn
s :: String
s = [String] -> String
unwords ([String] -> String) -> [String] -> String
forall a b. (a -> b) -> a -> b
$ (Key -> Maybe Window -> String)
-> [Key] -> [Maybe Window] -> [String]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith Key -> Maybe Window -> String
forall a. Key -> Maybe a -> String
ckact [Key
0..] [Maybe Window]
ws
in String -> Maybe String
forall a. a -> Maybe a
Just String
s
nspActive :: [String] -> (String -> String) -> (String -> String) -> Logger
nspActive :: [String] -> (String -> String) -> (String -> String) -> Logger
nspActive [String]
icns String -> String
act String -> String
inact = do
NSPTrack [Maybe Window]
ws <- X NSPTrack
forall a (m :: * -> *). (ExtensionClass a, XLike m) => m a
XS.get
Maybe String -> Logger
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe String -> Logger) -> Maybe String -> Logger
forall a b. (a -> b) -> a -> b
$ if [Maybe Window] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Maybe Window]
ws
then Maybe String
forall a. Maybe a
Nothing
else let ckact :: Key -> Maybe a -> String
ckact Key
n Maybe a
w = let icn :: String
icn = [String]
icns [String] -> Key -> String
forall a. [a] -> Key -> a
!! Key
n
in case Maybe a
w of
Maybe a
Nothing -> String -> String
inact String
icn
Just a
_ -> String -> String
act String
icn
s :: String
s = [String] -> String
unwords ([String] -> String) -> [String] -> String
forall a b. (a -> b) -> a -> b
$ (Key -> Maybe Window -> String)
-> [Key] -> [Maybe Window] -> [String]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith Key -> Maybe Window -> String
forall a. Key -> Maybe a -> String
ckact [Key
0..] [Maybe Window]
ws
in String -> Maybe String
forall a. a -> Maybe a
Just String
s
nspActive' :: [NamedScratchpad] -> (String -> String) -> (String -> String) -> Logger
nspActive' :: [NamedScratchpad]
-> (String -> String) -> (String -> String) -> Logger
nspActive' [NamedScratchpad]
ns = [String] -> (String -> String) -> (String -> String) -> Logger
nspActive ((NamedScratchpad -> String) -> [NamedScratchpad] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map NamedScratchpad -> String
name [NamedScratchpad]
ns)