module Rahm.Desktop.Hooks.WindowChange where import Control.Monad (when) import Data.Default (Default (..)) import Rahm.Desktop.Common ( Location (Location), getCurrentWorkspace, ) import qualified Rahm.Desktop.StackSet as W (peek) import XMonad ( ExtensionClass (..), StateExtension (PersistentExtension), X, XConfig (logHook), withWindowSet, ) import qualified XMonad.Util.ExtensibleState as XS (get, put) 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 () }