-- Common ways to select workspaces module Rahm.Desktop.Workspaces ( getPopulatedWorkspaces, next, prev, lastWorkspaceId, firstWorkspaceId, windowsInCurrentWorkspace, getOrderedScreens, accompanyingWorkspace, adjacentWorkspaceNotVisible, adjacentWorkspace, viewAdjacent, viewAdjacentTo, withScreen, workspaceWithWindow, getWorkspaceToTheRight, getWorkspaceToTheLeft, getWorkspaceAbove, getWorkspaceBelow, getNextWorkspaceInOrder, getPrevWorkspaceInOrder, WorkspaceState (..), ) where import Control.Arrow (Arrow ((&&&))) import Control.Monad.Trans (lift, MonadIO (liftIO)) import Control.Monad.Trans.Maybe (MaybeT (MaybeT)) import Data.Char (chr, isDigit, isUpper, ord, toLower, toUpper) import Data.Int (Int32) import Data.List (find, sort, sortBy, sortOn, (\\)) import Data.List.Safe ((!!)) import Data.Maybe (fromMaybe, listToMaybe, mapMaybe) import Rahm.Desktop.Common (getCurrentWorkspace, gotoWorkspace, runMaybeT_) import Rahm.Desktop.Geometry import Rahm.Desktop.Logger import qualified Rahm.Desktop.StackSet as W import XMonad ( Rectangle (Rectangle), ScreenDetail (SD), ScreenId, Window, WindowSet, WorkspaceId, X, windows, withWindowSet, ) import Prelude hiding ((!!)) import System.IO (stderr, hPutStrLn) newtype Selector = Selector (forall a. (a -> Bool) -> [a] -> Maybe a) data WorkspaceState = Current | Hidden | Visible deriving (Ord, Eq, Enum) -- Returns all the workspaces that are either visible, current or Hidden but -- have windows and that workspace's state. -- -- In other words, filters out workspaces that have no windows and are not -- visible. -- -- This function will sort the result by the workspace tag. getPopulatedWorkspaces :: W.StackSet String l a sid sd -> [(WorkspaceState, W.Workspace String l a)] getPopulatedWorkspaces (W.StackSet (W.Screen cur _ _) vis hi _) = filter ((/= "*") . W.tag . snd) $ sortOn (W.tag . snd) $ mapMaybe (\w@(W.Workspace _ _ s) -> fmap (const (Hidden, w)) s) hi ++ map (\(W.Screen w _ _) -> (Visible, w)) vis ++ [(Current, cur)] next :: Selector next = Selector $ \f l -> select f l l where select f (x : y : xs) _ | f x = Just y select f [x] (y : _) | f x = Just y select f (x : xs) orig = select f xs orig select f _ _ = Nothing prev :: Selector prev = Selector $ \f l -> let (Selector fn) = next in fn f (reverse l) lastWorkspaceId :: X WorkspaceId lastWorkspaceId = W.tag . snd . last <$> withWindowSet (return . getPopulatedWorkspaces) firstWorkspaceId :: X WorkspaceId firstWorkspaceId = W.tag . snd . head <$> withWindowSet (return . getPopulatedWorkspaces) windowsInCurrentWorkspace :: X [Window] windowsInCurrentWorkspace = withWindowSet $ \(W.StackSet (W.Screen (W.Workspace _ _ s) _ _) _ _ _) -> do return $ W.integrate' s getOrderedScreens :: W.StackSet wid l a ScreenId ScreenDetail -> [(Bool, W.Screen wid l a ScreenId ScreenDetail)] -- ^ Returns a list of screens ordered top to bottom, left to right (based on center point). getOrderedScreens windowSet = flip sortBy screens $ \(b1, sc1) (b2, sc2) -> let (SD (Rectangle x1 y1 w1 h1)) = W.screenDetail sc1 (SD (Rectangle x2 y2 w2 h2)) = W.screenDetail sc2 xCenter1 = fromIntegral x1 + (fromIntegral w1 / 2) yCenter1 = fromIntegral y1 + (fromIntegral h1 / 2) xCenter2 = fromIntegral x2 + (fromIntegral w2 / 2) yCenter2 = fromIntegral y2 + (fromIntegral h2 / 2) in case yCenter1 `compare` yCenter2 of EQ -> xCenter1 `compare` xCenter2 other -> other where screens = (True, W.current windowSet) : map (False,) (W.visible windowSet) accompanyingWorkspace :: WorkspaceId -> WorkspaceId accompanyingWorkspace [s] | isDigit s = show (fl (ord s - ord '0')) where fl x | even x = x + 1 fl x = x - 1 accompanyingWorkspace [s] = return $ if isUpper s then toLower s else toUpper s accompanyingWorkspace s = s adjacentWorkspaceNotVisible :: Selector -> WorkspaceId -> X WorkspaceId adjacentWorkspaceNotVisible (Selector selector) from = withWindowSet $ \ss -> let tags = sort $ W.tag . snd <$> filter (\x -> fst x /= Visible) ( getPopulatedWorkspaces ss ) in return $ fromMaybe from $ selector (== from) tags adjacentWorkspace :: Selector -> WorkspaceId -> X WorkspaceId adjacentWorkspace (Selector selector) from = withWindowSet $ \ss -> let tags = sort $ W.tag . snd <$> getPopulatedWorkspaces ss in return $ fromMaybe from $ selector (== from) tags getNextWorkspaceInOrder :: WorkspaceId -> X WorkspaceId getNextWorkspaceInOrder from = withWindowSet $ \ss -> let screens = getOrderedScreens ss tags = map (W.tag . W.workspace . snd) screens tagsWithHidden = map (W.tag . snd) $ getPopulatedWorkspaces ss orderedTags = filter (\t -> t `elem` tagsWithHidden) tags getIndex [] _ = Nothing getIndex (x : xs) target = if x == target then if null xs then Just $ head orderedTags -- wrap around else Just $ head xs else getIndex xs target in return $ fromMaybe from $ getIndex orderedTags from getPrevWorkspaceInOrder :: WorkspaceId -> X WorkspaceId getPrevWorkspaceInOrder from = withWindowSet $ \ss -> let screens = getOrderedScreens ss tags = map (W.tag . W.workspace . snd) screens tagsWithHidden = map (W.tag . snd) $ getPopulatedWorkspaces ss orderedTags = filter (\t -> t `elem` tagsWithHidden) tags reverseTags = reverse orderedTags getIndex [] _ = Nothing getIndex (x : xs) target = if x == target then if null xs then Just $ head reverseTags -- wrap around else Just $ head xs else getIndex xs target in return $ fromMaybe from $ getIndex reverseTags from viewAdjacent :: Selector -> X () viewAdjacent sel = gotoWorkspace =<< (adjacentWorkspaceNotVisible sel =<< getCurrentWorkspace) viewAdjacentTo :: X (Maybe WorkspaceId) -> Selector -> X () viewAdjacentTo wsM (Selector sel) = runMaybeT_ $ do lift $ logs Debug "viewAdjacentTo" tag <- MaybeT wsM lift $ logs Debug "from: %s" tag ws <- MaybeT $ withWindowSet $ \ws -> let vis = map (W.tag . W.workspace) (W.screens ws) allW = sort $ map (W.tag . snd) (getPopulatedWorkspaces ws) final = allW \\ (vis \\ [tag]) in return $ sel (== tag) final lift $ logs Debug "to: %s" ws lift $ windows $ W.switchWorkspaces tag ws adjacentScreen :: Selector -> X WorkspaceId adjacentScreen (Selector f) = do (screens, current) <- withWindowSet $ return . (getOrderedScreens &&& W.current) return $ W.tag $ W.workspace $ maybe current snd (f fst screens) withScreen :: (WorkspaceId -> WindowSet -> WindowSet) -> Int -> X () withScreen fn n = do windows $ \windowSet -> case map snd (getOrderedScreens windowSet) !! n of Nothing -> windowSet Just screen -> fn (W.tag $ W.workspace screen) windowSet workspaceWithWindow :: Window -> X (Maybe WorkspaceId) workspaceWithWindow wid = withWindowSet $ \(W.StackSet c v h _) -> return $ W.tag <$> find (\(W.Workspace _ _ stack) -> wid `elem` W.integrate' stack) (map W.workspace (c : v) ++ h) getScreenRectangles :: X [(GeomRectangle, WorkspaceId)] getScreenRectangles = withWindowSet $ \(W.StackSet cur vis _ _) -> do return $ ofScreen cur : map ofScreen vis where ofScreen (W.Screen (W.tag -> t) _ (SD rect)) = (toGeom rect, t) toGeom (Rectangle x y w h) = GeomRectangle (fromIntegral x) (fromIntegral y) (fromIntegral w) (fromIntegral h) getScreenRect :: WorkspaceId -> X (Maybe GeomRectangle) getScreenRect w = do rects <- getScreenRectangles return $ listToMaybe [r | (r, w') <- rects, w' == w] getWorkspaceToTheRight :: WorkspaceId -> MaybeT X WorkspaceId getWorkspaceToTheRight w = do refRect <- lift $ getScreenRect w rects <- lift getScreenRectangles MaybeT $ return (snd <$> closestInDirection rects w E) getWorkspaceToTheLeft :: WorkspaceId -> MaybeT X WorkspaceId getWorkspaceToTheLeft w = do refRect <- lift $ getScreenRect w rects <- lift getScreenRectangles MaybeT $ return (snd <$> closestInDirection rects w W) getWorkspaceAbove :: WorkspaceId -> MaybeT X WorkspaceId getWorkspaceAbove w = do refRect <- lift $ getScreenRect w rects <- lift getScreenRectangles MaybeT $ return (snd <$> closestInDirection rects w N) getWorkspaceBelow :: WorkspaceId -> MaybeT X WorkspaceId getWorkspaceBelow w = do refRect <- lift $ getScreenRect w rects <- lift getScreenRectangles MaybeT $ return (snd <$> closestInDirection rects w S)