Update NoBorders and LayoutHelpers.

This commit is contained in:
David Roundy
2007-09-23 19:26:40 +00:00
parent 89f89021ab
commit f82d3dadb2
2 changed files with 41 additions and 36 deletions

View File

@@ -1,4 +1,3 @@
{-# OPTIONS -fallow-undecidable-instances #-}
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
-- | -- |
-- Module : XMonadContrib.LayoutHelpers -- Module : XMonadContrib.LayoutHelpers
@@ -15,37 +14,42 @@
module XMonadContrib.LayoutHelpers ( module XMonadContrib.LayoutHelpers (
-- * Usage -- * Usage
-- $usage -- $usage
LayoutModifier(..) LayoutModifier(..), ModifiedLayout(..)
) where ) where
import Control.Monad ( mplus )
import Graphics.X11.Xlib ( Rectangle ) import Graphics.X11.Xlib ( Rectangle )
import XMonad import XMonad
import StackSet ( Stack ) import StackSet ( Stack )
import Operations ( UnDoLayout(UnDoLayout) )
-- $usage -- $usage
-- Use LayoutHelpers to help write easy Layouts. -- Use LayoutHelpers to help write easy Layouts.
class (Show (m l a), Read (m l a), Layout l a) => LayoutModifier m l a where class (Show (m a), Read (m a)) => LayoutModifier m a where
extractLayout :: m l a -> l a modifyModify :: m a -> SomeMessage -> X (Maybe (m l))
wrapLayout :: m l a -> l a -> m l a modifyModify m mess | Just UnDoLayout <- fromMessage mess = do unhook m; return Nothing
modifyModify :: m l a -> SomeMessage -> X (Maybe (l a -> m l a)) | otherwise = return Nothing
modifyModify _ _ = return Nothing redoLayout :: m a -> Rectangle -> Stack a -> [(a, Rectangle)]
redoLayout :: m l a -> Rectangle -> Stack a -> [(a, Rectangle)] -> X ([(a, Rectangle)], Maybe (m l))
-> X ([(a, Rectangle)], Maybe (l a -> m l a))
redoLayout m _ _ wrs = do hook m; return (wrs, Nothing) redoLayout m _ _ wrs = do hook m; return (wrs, Nothing)
hook :: m l a -> X () hook :: m a -> X ()
hook _ = return () hook _ = return ()
unhook :: m a -> X ()
unhook _ = return ()
instance LayoutModifier m l a => Layout (m l) a where instance (LayoutModifier m a, Layout l a) => Layout (ModifiedLayout m l) a where
doLayout m r s = do (ws, ml') <- doLayout (extractLayout m) r s doLayout (ModifiedLayout m l) r s =
(ws', mmod') <- redoLayout m r s ws do (ws, ml') <- doLayout l r s
let ml'' = case mmod' of (ws', mm') <- redoLayout m r s ws
Just mod' -> Just $ mod' $ maybe (extractLayout m) id ml' let ml'' = case mm' of
Nothing -> wrapLayout m `fmap` ml' Just m' -> Just $ (ModifiedLayout m') $ maybe l id ml'
return (ws', ml'') Nothing -> ModifiedLayout m `fmap` ml'
modifyLayout m mess = do ml' <- modifyLayout (extractLayout m) mess return (ws', ml'')
mmod' <- modifyModify m mess modifyLayout (ModifiedLayout m l) mess =
return $ case mmod' of do ml' <- modifyLayout l mess
Just mod' -> Just $ mod' $ maybe (extractLayout m) id ml' mm' <- modifyModify m mess
Nothing -> wrapLayout m `fmap` ml' return $ case mm' of
Just m' -> Just $ (ModifiedLayout m') $ maybe l id ml'
Nothing -> (ModifiedLayout m) `fmap` ml'
data ModifiedLayout m l a = ModifiedLayout (m a) (l a) deriving ( Read, Show )

View File

@@ -18,17 +18,17 @@
module XMonadContrib.NoBorders ( module XMonadContrib.NoBorders (
-- * Usage -- * Usage
-- $usage -- $usage
noBorders, noBorders,
withBorder withBorder
) where ) where
import Control.Monad.State ( gets ) import Control.Monad.State ( gets )
import Graphics.X11.Xlib import Graphics.X11.Xlib
import XMonad import XMonad
import Operations ( UnDoLayout(UnDoLayout) ) import XMonadContrib.LayoutHelpers
import qualified StackSet as W
import {-# SOURCE #-} Config (borderWidth) import {-# SOURCE #-} Config (borderWidth)
import qualified StackSet as W
-- $usage -- $usage
-- You can use this module with the following in your Config.hs file: -- You can use this module with the following in your Config.hs file:
@@ -44,16 +44,17 @@ import {-# SOURCE #-} Config (borderWidth)
-- %layout -- prepend noBorders to default layouts above to remove their borders, like so: -- %layout -- prepend noBorders to default layouts above to remove their borders, like so:
-- %layout , noBorders full -- %layout , noBorders full
noBorders :: Layout a -> Layout a data WithBorder a = WithBorder Dimension deriving ( Read, Show )
noBorders = withBorder 0
withBorder :: Dimension -> Layout a -> Layout a instance LayoutModifier WithBorder a where
withBorder bd l = l { doLayout = \r x -> setborders bd >> doLayout l r x hook (WithBorder b) = setborders b
, modifyLayout = ml } unhook (WithBorder _) = setborders borderWidth
where ml m | Just UnDoLayout == fromMessage m
= do setborders borderWidth noBorders :: Layout l a => l a -> ModifiedLayout WithBorder l a
fmap (withBorder bd) `fmap` (modifyLayout l) m noBorders = ModifiedLayout (WithBorder 0)
| otherwise = fmap (withBorder bd) `fmap` (modifyLayout l) m
withBorder :: Layout l a => Dimension -> l a -> ModifiedLayout WithBorder l a
withBorder b = ModifiedLayout (WithBorder b)
setborders :: Dimension -> X () setborders :: Dimension -> X ()
setborders bw = withDisplay $ \d -> setborders bw = withDisplay $ \d ->