nixos-config/system/desktop/home-manager/xmonad/TabbedFix.hs

108 lines
3.6 KiB
Haskell
Raw Normal View History

2019-10-24 02:20:38 +02:00
{-# LANGUAGE MultiParamTypeClasses #-}
2019-11-10 18:13:03 +01:00
{-# LANGUAGE TypeSynonymInstances #-}
2019-10-24 02:20:38 +02:00
module TabbedFix (historyLayout, runAllPending) where
2019-11-10 18:13:03 +01:00
import Control.DeepSeq (force)
import qualified Data.List as L
import XMonad
import XMonad.Layout.LayoutModifier (LayoutModifier,
ModifiedLayout (ModifiedLayout),
modifyLayout)
import qualified XMonad.StackSet as W
import qualified XMonad.Util.ExtensibleState as ES
newtype PendingActions =
PendingActions [X ()]
2019-10-24 02:20:38 +02:00
instance ExtensionClass PendingActions where
initialValue = PendingActions []
addAction :: X () -> X ()
addAction x = ES.modify (\(PendingActions xs) -> PendingActions (x:xs))
runAllPending :: X ()
runAllPending = do
PendingActions actions <- ES.get
ES.put (PendingActions [])
sequence_ actions
newtype FocusHistory = FocusHistory {
getFocusHistory :: [Window]
} deriving (Read, Show, Typeable)
instance ExtensionClass FocusHistory where
initialValue = FocusHistory []
extensionType = PersistentExtension
data FocusLayout a = FocusLayout deriving (Read,Show,Typeable)
historyLayout :: l Window -> ModifiedLayout FocusLayout l Window
historyLayout = ModifiedLayout FocusLayout
instance LayoutModifier FocusLayout Window where
2019-11-10 18:13:03 +01:00
modifyLayout _ workspace rectangle = do
oldWindow <- getFocused
2019-10-24 02:20:38 +02:00
history <- getFocusHistory <$> ES.get
2019-11-10 18:13:03 +01:00
newWindow <- windowHistoryHook oldWindow
case newWindow of
Nothing -> runLayout workspace rectangle
Just window -> do
let oldstack = W.stack workspace
2019-10-24 02:20:38 +02:00
let mw = L.find (`elem` W.integrate' oldstack) history
2019-11-10 18:13:03 +01:00
let newstack =
if window `elem` (W.integrate' oldstack)
then until ((window ==) . W.focus) W.focusUp' <$> oldstack
else case mw of
Just window' ->
until ((window' ==) . W.focus) W.focusUp' <$> oldstack
Nothing -> oldstack
modifyWindowSet (W.focusWindow window)
2019-10-24 02:20:38 +02:00
addAction $ do
2019-11-10 18:13:03 +01:00
maybe (return ()) makeBorderNormal oldWindow
2019-10-24 02:20:38 +02:00
windows id
2019-11-10 18:13:03 +01:00
runLayout workspace {W.stack = newstack} rectangle
2019-10-24 02:20:38 +02:00
2019-11-10 18:13:03 +01:00
windowHistoryHook :: Maybe Window -> X (Maybe Window)
2019-10-24 02:20:38 +02:00
windowHistoryHook Nothing = return Nothing
2019-11-10 18:13:03 +01:00
windowHistoryHook (Just window') = do
history <- getFocusHistory <$> ES.get
currentWindowSet <- gets $ W.index . windowset
withWindowSet $ \allWindows ->
case history of
2019-10-24 02:20:38 +02:00
[] -> do
2019-11-10 18:13:03 +01:00
ES.put $ FocusHistory [window']
2019-10-24 02:20:38 +02:00
return Nothing
(prev:xs)
2019-11-10 18:13:03 +01:00
| prev == window' -> return Nothing
2019-10-24 02:20:38 +02:00
-- Previous focus was removed from ws, focus on previous existing window in current ws
2019-11-10 18:13:03 +01:00
| prev `notElem` currentWindowSet -> do
let history' = filter (`W.member` allWindows) xs
ES.put (FocusHistory $ force history')
return $ L.find (`elem` currentWindowSet) history'
2019-10-24 02:20:38 +02:00
-- Add current focus to history
2019-11-10 18:13:03 +01:00
| otherwise -> do
ES.put $ FocusHistory $ force $ window' : L.delete window' history
return Nothing
2019-10-24 02:20:38 +02:00
makeBorderRed :: Window -> X ()
makeBorderRed w =
withDisplay $ \d -> io $ do
setWindowBorder d w 0xff0000
-- wz1000: palo: btw, you will need to change the color in makeBorderNormal to your unfocused border color
2019-11-10 18:13:03 +01:00
makeBorderNormal :: Window -> X ()
2019-10-24 02:20:38 +02:00
makeBorderNormal w =
withDisplay $ \d -> io $ do
setWindowBorder d w 0x2b2b2b
2019-11-10 18:13:03 +01:00
makeBorderFocused :: Window -> X ()
2019-10-24 02:20:38 +02:00
makeBorderFocused w =
withDisplay $ \d -> io $ do
setWindowBorder d w 0xcccccc
getFocused :: X (Maybe Window)
getFocused = withWindowSet (return . W.peek)