Add a way to update the modifier in X.L.LayoutModifier

This patch adds the possibility to update the state of a layout modifier when
modifying the underlying layout before it is run(i.e. using modifyLayout). 
The modified state is also passed to the subsequent call of redoLayout, whose 
return takes precedence if both functions return modified states of the layout 
modifier.
This commit is contained in:
Daniel Schoepe 2009-08-22 21:39:58 +00:00
parent b21208dad7
commit 2ee34742ca

View File

@ -29,6 +29,8 @@ module XMonad.Layout.LayoutModifier (
LayoutModifier(..), ModifiedLayout(..) LayoutModifier(..), ModifiedLayout(..)
) where ) where
import Control.Monad
import XMonad import XMonad
import XMonad.StackSet ( Stack, Workspace (..) ) import XMonad.StackSet ( Stack, Workspace (..) )
@ -106,6 +108,22 @@ class (Show (m a), Read (m a)) => LayoutModifier m a where
-> X ([(a, Rectangle)], Maybe (l a)) -> X ([(a, Rectangle)], Maybe (l a))
modifyLayout _ w r = runLayout w r modifyLayout _ w r = runLayout w r
-- | Similar to 'modifyLayout', but this function also allows you
-- update the state of your layout modifier(the second value in the
-- outer tuple).
--
-- If both 'modifyLayoutWithUpdate' and 'redoLayout' return a
-- modified state of the layout modifier, 'redoLayout' takes
-- precedence. If this function returns a modified state, this
-- state will internally be used in the subsequent call to
-- 'redoLayout' as well.
modifyLayoutWithUpdate :: (LayoutClass l a) =>
m a
-> Workspace WorkspaceId (l a) a
-> Rectangle
-> X (([(a,Rectangle)], Maybe (l a)), Maybe (m a))
modifyLayoutWithUpdate m w r = flip (,) Nothing `fmap` modifyLayout m w r
-- | 'handleMess' allows you to spy on messages to the underlying -- | 'handleMess' allows you to spy on messages to the underlying
-- layout, in order to have an effect in the X monad, or alter -- layout, in order to have an effect in the X monad, or alter
-- the layout modifier state in some way (by returning @Just -- the layout modifier state in some way (by returning @Just
@ -234,9 +252,9 @@ class (Show (m a), Read (m a)) => LayoutModifier m a where
-- semantics of a 'LayoutModifier' applied to an underlying layout. -- semantics of a 'LayoutModifier' applied to an underlying layout.
instance (LayoutModifier m a, LayoutClass l a) => LayoutClass (ModifiedLayout m l) a where instance (LayoutModifier m a, LayoutClass l a) => LayoutClass (ModifiedLayout m l) a where
runLayout (Workspace i (ModifiedLayout m l) ms) r = runLayout (Workspace i (ModifiedLayout m l) ms) r =
do (ws, ml') <- modifyLayout m (Workspace i l ms) r do ((ws, ml'),mm') <- modifyLayoutWithUpdate m (Workspace i l ms) r
(ws', mm') <- redoLayout m r ms ws (ws', mm'') <- redoLayout (maybe m id mm') r ms ws
let ml'' = case mm' of let ml'' = case mm'' `mplus` mm' of
Just m' -> Just $ (ModifiedLayout m') $ maybe l id ml' Just m' -> Just $ (ModifiedLayout m') $ maybe l id ml'
Nothing -> ModifiedLayout m `fmap` ml' Nothing -> ModifiedLayout m `fmap` ml'
return (ws', ml'') return (ws', ml'')