{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE TypeApplications #-}
module XMonad.Hooks.StatusBar.WorkspaceScreen
(
combineWithScreen
, combineWithScreenName
, combineWithScreenNumber
, WorkspaceScreenCombiner
) where
import Graphics.X11.Xrandr
import XMonad
import XMonad.Hooks.StatusBar.PP
import XMonad.Prelude
import qualified XMonad.StackSet as W
type WorkspaceScreenCombiner = WorkspaceId -> WindowScreen -> String
screenNames :: X [Maybe String]
screenNames :: X [Maybe WorkspaceId]
screenNames = do
XConf { Display
display :: XConf -> Display
display :: Display
display, Window
theRoot :: XConf -> Window
theRoot :: Window
theRoot } <- X XConf
forall r (m :: * -> *). MonadReader r m => m r
ask
let getName :: XRRMonitorInfo -> IO (Maybe WorkspaceId)
getName XRRMonitorInfo
mi = Display -> Window -> IO (Maybe WorkspaceId)
getAtomName Display
display (XRRMonitorInfo -> Window
xrr_moninf_name XRRMonitorInfo
mi)
IO [Maybe WorkspaceId] -> X [Maybe WorkspaceId]
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io
(IO [Maybe WorkspaceId] -> X [Maybe WorkspaceId])
-> IO [Maybe WorkspaceId] -> X [Maybe WorkspaceId]
forall a b. (a -> b) -> a -> b
$ IO [Maybe WorkspaceId]
-> ([XRRMonitorInfo] -> IO [Maybe WorkspaceId])
-> Maybe [XRRMonitorInfo]
-> IO [Maybe WorkspaceId]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ([Maybe WorkspaceId] -> IO [Maybe WorkspaceId]
forall (f :: * -> *) a. Applicative f => a -> f a
pure []) ((XRRMonitorInfo -> IO (Maybe WorkspaceId))
-> [XRRMonitorInfo] -> IO [Maybe WorkspaceId]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse XRRMonitorInfo -> IO (Maybe WorkspaceId)
getName)
(Maybe [XRRMonitorInfo] -> IO [Maybe WorkspaceId])
-> IO (Maybe [XRRMonitorInfo]) -> IO [Maybe WorkspaceId]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Display -> Window -> Bool -> IO (Maybe [XRRMonitorInfo])
xrrGetMonitors Display
display Window
theRoot Bool
True
combineWithScreenName :: (WorkspaceId -> String -> String) -> PP -> X PP
combineWithScreenName :: (WorkspaceId -> WorkspaceId -> WorkspaceId) -> PP -> X PP
combineWithScreenName WorkspaceId -> WorkspaceId -> WorkspaceId
c = X WorkspaceScreenCombiner -> PP -> X PP
combineWithScreen (X WorkspaceScreenCombiner -> PP -> X PP)
-> X WorkspaceScreenCombiner -> PP -> X PP
forall a b. (a -> b) -> a -> b
$ do
[Maybe WorkspaceId]
screens <- X [Maybe WorkspaceId]
screenNames
WorkspaceScreenCombiner -> X WorkspaceScreenCombiner
forall (m :: * -> *) a. Monad m => a -> m a
return (WorkspaceScreenCombiner -> X WorkspaceScreenCombiner)
-> WorkspaceScreenCombiner -> X WorkspaceScreenCombiner
forall a b. (a -> b) -> a -> b
$ \WorkspaceId
w Screen WorkspaceId (Layout Window) Window ScreenId ScreenDetail
sc -> WorkspaceId
-> (WorkspaceId -> WorkspaceId) -> Maybe WorkspaceId -> WorkspaceId
forall b a. b -> (a -> b) -> Maybe a -> b
maybe WorkspaceId
w (WorkspaceId -> WorkspaceId -> WorkspaceId
c WorkspaceId
w) (Maybe WorkspaceId -> WorkspaceId)
-> Maybe WorkspaceId -> WorkspaceId
forall a b. (a -> b) -> a -> b
$ Maybe (Maybe WorkspaceId) -> Maybe WorkspaceId
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join ([Maybe WorkspaceId]
screens [Maybe WorkspaceId] -> Int -> Maybe (Maybe WorkspaceId)
forall a. [a] -> Int -> Maybe a
!? ScreenId -> Int
forall a b. (Integral a, Num b) => a -> b
fi (Screen WorkspaceId (Layout Window) Window ScreenId ScreenDetail
-> ScreenId
forall i l a sid sd. Screen i l a sid sd -> sid
W.screen Screen WorkspaceId (Layout Window) Window ScreenId ScreenDetail
sc))
combineWithScreenNumber :: (WorkspaceId -> String -> String) -> PP -> X PP
combineWithScreenNumber :: (WorkspaceId -> WorkspaceId -> WorkspaceId) -> PP -> X PP
combineWithScreenNumber WorkspaceId -> WorkspaceId -> WorkspaceId
c =
X WorkspaceScreenCombiner -> PP -> X PP
combineWithScreen (X WorkspaceScreenCombiner -> PP -> X PP)
-> (WorkspaceScreenCombiner -> X WorkspaceScreenCombiner)
-> WorkspaceScreenCombiner
-> PP
-> X PP
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WorkspaceScreenCombiner -> X WorkspaceScreenCombiner
forall (m :: * -> *) a. Monad m => a -> m a
return (WorkspaceScreenCombiner -> PP -> X PP)
-> WorkspaceScreenCombiner -> PP -> X PP
forall a b. (a -> b) -> a -> b
$ \WorkspaceId
w Screen WorkspaceId (Layout Window) Window ScreenId ScreenDetail
sc -> WorkspaceId -> WorkspaceId -> WorkspaceId
c WorkspaceId
w (forall a. Show a => a -> WorkspaceId
show @Int (Int -> WorkspaceId)
-> (Screen WorkspaceId (Layout Window) Window ScreenId ScreenDetail
-> Int)
-> Screen WorkspaceId (Layout Window) Window ScreenId ScreenDetail
-> WorkspaceId
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ScreenId -> Int
forall a b. (Integral a, Num b) => a -> b
fi (ScreenId -> Int)
-> (Screen WorkspaceId (Layout Window) Window ScreenId ScreenDetail
-> ScreenId)
-> Screen WorkspaceId (Layout Window) Window ScreenId ScreenDetail
-> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Screen WorkspaceId (Layout Window) Window ScreenId ScreenDetail
-> ScreenId
forall i l a sid sd. Screen i l a sid sd -> sid
W.screen (Screen WorkspaceId (Layout Window) Window ScreenId ScreenDetail
-> WorkspaceId)
-> Screen WorkspaceId (Layout Window) Window ScreenId ScreenDetail
-> WorkspaceId
forall a b. (a -> b) -> a -> b
$ Screen WorkspaceId (Layout Window) Window ScreenId ScreenDetail
sc)
combineWithScreen :: X WorkspaceScreenCombiner -> PP -> X PP
combineWithScreen :: X WorkspaceScreenCombiner -> PP -> X PP
combineWithScreen X WorkspaceScreenCombiner
xCombiner PP
pp = do
WorkspaceScreenCombiner
combiner <- X WorkspaceScreenCombiner
xCombiner
[Screen WorkspaceId (Layout Window) Window ScreenId ScreenDetail]
ss <- (WindowSet
-> X [Screen
WorkspaceId (Layout Window) Window ScreenId ScreenDetail])
-> X [Screen
WorkspaceId (Layout Window) Window ScreenId ScreenDetail]
forall a. (WindowSet -> X a) -> X a
withWindowSet ([Screen WorkspaceId (Layout Window) Window ScreenId ScreenDetail]
-> X [Screen
WorkspaceId (Layout Window) Window ScreenId ScreenDetail]
forall (m :: * -> *) a. Monad m => a -> m a
return ([Screen WorkspaceId (Layout Window) Window ScreenId ScreenDetail]
-> X [Screen
WorkspaceId (Layout Window) Window ScreenId ScreenDetail])
-> (WindowSet
-> [Screen
WorkspaceId (Layout Window) Window ScreenId ScreenDetail])
-> WindowSet
-> X [Screen
WorkspaceId (Layout Window) Window ScreenId ScreenDetail]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WindowSet
-> [Screen
WorkspaceId (Layout Window) Window ScreenId ScreenDetail]
forall i l a s sd. StackSet i l a s sd -> [Screen i l a s sd]
W.screens)
PP -> X PP
forall (m :: * -> *) a. Monad m => a -> m a
return (PP -> X PP) -> PP -> X PP
forall a b. (a -> b) -> a -> b
$ PP
pp
{ ppRename :: WorkspaceId -> WindowSpace -> WorkspaceId
ppRename = PP -> WorkspaceId -> WindowSpace -> WorkspaceId
ppRename PP
pp (WorkspaceId -> WindowSpace -> WorkspaceId)
-> (WorkspaceId -> WindowSpace -> WorkspaceId)
-> WorkspaceId
-> WindowSpace
-> WorkspaceId
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< \WorkspaceId
s WindowSpace
w ->
WorkspaceId
-> (Screen WorkspaceId (Layout Window) Window ScreenId ScreenDetail
-> WorkspaceId)
-> Maybe
(Screen WorkspaceId (Layout Window) Window ScreenId ScreenDetail)
-> WorkspaceId
forall b a. b -> (a -> b) -> Maybe a -> b
maybe WorkspaceId
s (WorkspaceScreenCombiner
combiner WorkspaceId
s) ((Screen WorkspaceId (Layout Window) Window ScreenId ScreenDetail
-> Bool)
-> [Screen
WorkspaceId (Layout Window) Window ScreenId ScreenDetail]
-> Maybe
(Screen WorkspaceId (Layout Window) Window ScreenId ScreenDetail)
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find ((WorkspaceId -> WorkspaceId -> Bool
forall a. Eq a => a -> a -> Bool
== WindowSpace -> WorkspaceId
forall i l a. Workspace i l a -> i
W.tag WindowSpace
w) (WorkspaceId -> Bool)
-> (Screen WorkspaceId (Layout Window) Window ScreenId ScreenDetail
-> WorkspaceId)
-> Screen WorkspaceId (Layout Window) Window ScreenId ScreenDetail
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WindowSpace -> WorkspaceId
forall i l a. Workspace i l a -> i
W.tag (WindowSpace -> WorkspaceId)
-> (Screen WorkspaceId (Layout Window) Window ScreenId ScreenDetail
-> WindowSpace)
-> Screen WorkspaceId (Layout Window) Window ScreenId ScreenDetail
-> WorkspaceId
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Screen WorkspaceId (Layout Window) Window ScreenId ScreenDetail
-> WindowSpace
forall i l a sid sd. Screen i l a sid sd -> Workspace i l a
W.workspace) [Screen WorkspaceId (Layout Window) Window ScreenId ScreenDetail]
ss)
}