----------------------------------------------------------------------------
-- |
-- Module      :  XMonad.Hooks.CurrentWorkspaceOnTop
-- Description :  Ensure that windows on the current workspace are on top.
-- Copyright   :  (c) Jan Vornberger 2009
-- License     :  BSD3-style (see LICENSE)
--
-- Maintainer  :  jan.vornberger@informatik.uni-oldenburg.de
-- Stability   :  unstable
-- Portability :  not portable
--
-- Ensures that the windows of the current workspace are always in front
-- of windows that are located on other visible screens. This becomes important
-- if you use decoration and drag windows from one screen to another. Using this
-- module, the dragged window will always be in front of other windows.
--
-----------------------------------------------------------------------------

module XMonad.Hooks.CurrentWorkspaceOnTop (
    -- * Usage
    -- $usage
    currentWorkspaceOnTop
    ) where

import qualified Data.List.NonEmpty as NE (nonEmpty)
import qualified Data.Map as M
import XMonad
import XMonad.Prelude (NonEmpty ((:|)), when)
import qualified XMonad.StackSet as S
import qualified XMonad.Util.ExtensibleState as XS

-- $usage
-- You can use this module with the following in your @xmonad.hs@:
--
-- > import XMonad.Hooks.CurrentWorkspaceOnTop
-- >
-- > main = xmonad $ def {
-- >    ...
-- >    logHook = currentWorkspaceOnTop
-- >    ...
-- >  }
--

newtype CWOTState = CWOTS String

instance ExtensionClass CWOTState where
  initialValue :: CWOTState
initialValue = [Char] -> CWOTState
CWOTS [Char]
""

currentWorkspaceOnTop :: X ()
currentWorkspaceOnTop :: X ()
currentWorkspaceOnTop = (Display -> X ()) -> X ()
forall a. (Display -> X a) -> X a
withDisplay ((Display -> X ()) -> X ()) -> (Display -> X ()) -> X ()
forall a b. (a -> b) -> a -> b
$ \Display
d -> do
    ws <- (XState
 -> StackSet [Char] (Layout Window) Window ScreenId ScreenDetail)
-> X (StackSet [Char] (Layout Window) Window ScreenId ScreenDetail)
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets XState
-> StackSet [Char] (Layout Window) Window ScreenId ScreenDetail
windowset
    (CWOTS lastTag) <- XS.get
    let curTag = Workspace [Char] (Layout Window) Window -> [Char]
forall i l a. Workspace i l a -> i
S.tag (Workspace [Char] (Layout Window) Window -> [Char])
-> (StackSet [Char] (Layout Window) Window ScreenId ScreenDetail
    -> Workspace [Char] (Layout Window) Window)
-> StackSet [Char] (Layout Window) Window ScreenId ScreenDetail
-> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Screen [Char] (Layout Window) Window ScreenId ScreenDetail
-> Workspace [Char] (Layout Window) Window
forall i l a sid sd. Screen i l a sid sd -> Workspace i l a
S.workspace (Screen [Char] (Layout Window) Window ScreenId ScreenDetail
 -> Workspace [Char] (Layout Window) Window)
-> (StackSet [Char] (Layout Window) Window ScreenId ScreenDetail
    -> Screen [Char] (Layout Window) Window ScreenId ScreenDetail)
-> StackSet [Char] (Layout Window) Window ScreenId ScreenDetail
-> Workspace [Char] (Layout Window) Window
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StackSet [Char] (Layout Window) Window ScreenId ScreenDetail
-> Screen [Char] (Layout Window) Window ScreenId ScreenDetail
forall i l a sid sd. StackSet i l a sid sd -> Screen i l a sid sd
S.current (StackSet [Char] (Layout Window) Window ScreenId ScreenDetail
 -> [Char])
-> StackSet [Char] (Layout Window) Window ScreenId ScreenDetail
-> [Char]
forall a b. (a -> b) -> a -> b
$ StackSet [Char] (Layout Window) Window ScreenId ScreenDetail
ws
    when (curTag /= lastTag) $ do
        -- the following is more or less a reimplementation of what's happening in "XMonad.Operation"
        let s = StackSet [Char] (Layout Window) Window ScreenId ScreenDetail
-> Screen [Char] (Layout Window) Window ScreenId ScreenDetail
forall i l a sid sd. StackSet i l a sid sd -> Screen i l a sid sd
S.current StackSet [Char] (Layout Window) Window ScreenId ScreenDetail
ws
            wsp = Screen [Char] (Layout Window) Window ScreenId ScreenDetail
-> Workspace [Char] (Layout Window) Window
forall i l a sid sd. Screen i l a sid sd -> Workspace i l a
S.workspace Screen [Char] (Layout Window) Window ScreenId ScreenDetail
s
            viewrect = ScreenDetail -> Rectangle
screenRect (ScreenDetail -> Rectangle) -> ScreenDetail -> Rectangle
forall a b. (a -> b) -> a -> b
$ Screen [Char] (Layout Window) Window ScreenId ScreenDetail
-> ScreenDetail
forall i l a sid sd. Screen i l a sid sd -> sd
S.screenDetail Screen [Char] (Layout Window) Window ScreenId ScreenDetail
s
            tmpStack = Workspace [Char] (Layout Window) Window -> Maybe (Stack Window)
forall i l a. Workspace i l a -> Maybe (Stack a)
S.stack Workspace [Char] (Layout Window) Window
wsp Maybe (Stack Window)
-> (Stack Window -> Maybe (Stack Window)) -> Maybe (Stack Window)
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Window -> Bool) -> Stack Window -> Maybe (Stack Window)
forall a. (a -> Bool) -> Stack a -> Maybe (Stack a)
S.filter (Window -> Map Window RationalRect -> Bool
forall k a. Ord k => k -> Map k a -> Bool
`M.notMember` StackSet [Char] (Layout Window) Window ScreenId ScreenDetail
-> Map Window RationalRect
forall i l a sid sd. StackSet i l a sid sd -> Map a RationalRect
S.floating StackSet [Char] (Layout Window) Window ScreenId ScreenDetail
ws)
        (rs, ml') <- runLayout wsp { S.stack = tmpStack } viewrect
        updateLayout curTag ml'
        let this = [Char]
-> StackSet [Char] (Layout Window) Window ScreenId ScreenDetail
-> StackSet [Char] (Layout Window) Window ScreenId ScreenDetail
forall s i l a sd.
(Eq s, Eq i) =>
i -> StackSet i l a s sd -> StackSet i l a s sd
S.view [Char]
curTag StackSet [Char] (Layout Window) Window ScreenId ScreenDetail
ws
            fltWins = (Window -> Bool) -> [Window] -> [Window]
forall a. (a -> Bool) -> [a] -> [a]
filter (Window -> Map Window RationalRect -> Bool
forall k a. Ord k => k -> Map k a -> Bool
`M.member` StackSet [Char] (Layout Window) Window ScreenId ScreenDetail
-> Map Window RationalRect
forall i l a sid sd. StackSet i l a sid sd -> Map a RationalRect
S.floating StackSet [Char] (Layout Window) Window ScreenId ScreenDetail
ws) ([Window] -> [Window]) -> [Window] -> [Window]
forall a b. (a -> b) -> a -> b
$ StackSet [Char] (Layout Window) Window ScreenId ScreenDetail
-> [Window]
forall i l a s sd. StackSet i l a s sd -> [a]
S.index StackSet [Char] (Layout Window) Window ScreenId ScreenDetail
this
            wins = [Window]
fltWins [Window] -> [Window] -> [Window]
forall a. [a] -> [a] -> [a]
++ ((Window, Rectangle) -> Window)
-> [(Window, Rectangle)] -> [Window]
forall a b. (a -> b) -> [a] -> [b]
map (Window, Rectangle) -> Window
forall a b. (a, b) -> a
fst [(Window, Rectangle)]
rs  -- order: first all floating windows, then the order the layout returned
        -- end of reimplementation

        case NE.nonEmpty wins of
            Maybe (NonEmpty Window)
Nothing         -> () -> X ()
forall a. a -> X a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
            Just (Window
w :| [Window]
ws') -> do
                IO () -> X ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io (IO () -> X ()) -> IO () -> X ()
forall a b. (a -> b) -> a -> b
$ Display -> Window -> IO ()
raiseWindow Display
d Window
w            -- raise first window of current workspace to the very top,
                IO () -> X ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io (IO () -> X ()) -> IO () -> X ()
forall a b. (a -> b) -> a -> b
$ Display -> [Window] -> IO ()
restackWindows Display
d (Window
w Window -> [Window] -> [Window]
forall a. a -> [a] -> [a]
: [Window]
ws') -- then use restackWindows to let all other windows from the workspace follow
        XS.put(CWOTS curTag)