aboutsummaryrefslogtreecommitdiff
path: root/src/Rahm/Desktop/SwapMaster.hs
blob: f95303d21a4317373cbc93c236ba3935930c0dba (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
52
53
54
55
56
57
58
59
60
61
62
{- Swap window with the master, but save it. -}
module Rahm.Desktop.SwapMaster (swapMaster) where

import Control.Monad (void)
import Control.Monad.State (gets)
import Control.Monad.Trans (lift)
import Control.Monad.Trans.Maybe (MaybeT (..))
import Data.Map (Map)
import qualified Data.Map as Map
import Rahm.Desktop.Common (runMaybeT_)
import qualified Rahm.Desktop.StackSet as W
  ( currentTag,
    focusMaster,
    masterWindow,
    peek,
    swapWindows,
    tag,
  )
import XMonad
  ( ExtensionClass (..),
    Window,
    X (..),
    windows,
    windowset,
  )
import qualified XMonad.Util.ExtensibleState as XS (get, modify, put)

newtype LastWindow = LastWindow
  { lastWindows :: Map String Window
  }
  deriving (Show, Read)

instance ExtensionClass LastWindow where
  initialValue = LastWindow mempty

swapMaster :: X ()
swapMaster =
  runMaybeT_ $ do
    ss <- gets windowset
    let ct = W.currentTag ss

    (focused, master) <-
      hoist $ do
        a <- W.peek ss
        b <- W.masterWindow ss
        return (a, b)

    lift $ do
      st <- lastWindows <$> XS.get
      windows . W.swapWindows $
        case focused == master of
          True
            | (Just lw) <- Map.lookup ct st ->
              [(focused, lw)]
          False -> [(master, focused)]
          _ -> []

      XS.modify $ mlw (Map.insert (W.currentTag ss) master)
      windows W.focusMaster
  where
    mlw fn (LastWindow l) = LastWindow (fn l)
    hoist = MaybeT . return