aboutsummaryrefslogtreecommitdiff
path: root/src/Rahm/Desktop/Hooks/WindowChange.hs
blob: ec8e44587d83c563392bc9b4e77c50607c58b231 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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 ()
  }