module Rahm.Desktop.Hooks.WindowChange where import XMonad import Control.Monad import qualified XMonad.Util.ExtensibleState as XS import Data.Default import Rahm.Desktop.Common import qualified XMonad.StackSet as W newtype LastLocation = LastLocation (Maybe Location) deriving (Read, Show) instance Default LastLocation where def = LastLocation Nothing instance ExtensionClass LastLocation where initialValue = def extensionType = PersistentExtension -- Creates a log hook from the function provided. -- -- The first argument to the function is the old window, the second argument in -- the new window. -- -- If the first window is Nothing, this is the first time XMonad started. withLocationChangeHook :: (Maybe Location -> Location -> X ()) -> XConfig l -> XConfig l withLocationChangeHook fn config = config { logHook = do logHook config currentLocation <- Location <$> getCurrentWorkspace <*> withWindowSet (return . W.peek) LastLocation last <- XS.get when (last /= Just currentLocation) $ fn last currentLocation XS.put $ LastLocation $ Just currentLocation return () }