aboutsummaryrefslogtreecommitdiff
path: root/src/Rahm/Desktop/Hooks/WindowChange.hs
blob: a7d403ef95ffca2bbaa9ffe225fb97c3717b122e (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
44
45
46
47
48
49
50
51
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 ()
    }