{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeSynonymInstances #-}

-----------------------------------------------------------------------------
-- |
-- Module      :  XMonad.Layout.VoidBorders
-- Description :  Set borders to 0 for all windows in the workspace.
-- Copyright   :  Wilson Sales <spoonm@spoonm.org>
-- License     :  BSD-style (see LICENSE)
--
-- Maintainer  :  <spoonm@spoonm.org>
-- Stability   :  unstable
-- Portability :  unportable
--
-- Modifies a layout to set borders to 0 for all windows in the workspace.
-- Unlike XMonad.Layout.NoBorders, this modifier will not restore the window
-- border if the windows are moved to a different workspace or the layout is
-- changed.
--
-- This modifier's primary use is to eliminate the "border flash" you get
-- while switching workspaces with the `noBorders` modifier. It won't return
-- the borders to their original width, however.
--
-----------------------------------------------------------------------------

module XMonad.Layout.VoidBorders ( -- * Usage
                                   -- $usage
                                   voidBorders
                                 ) where

import XMonad
import XMonad.Layout.LayoutModifier
import XMonad.StackSet (integrate)

-- $usage
-- You can use this module with the following in your ~\/.xmonad/xmonad.hs
-- file:
--
-- > import XMonad.Layout.VoidBorders
--
-- and modify the layouts to call 'voidBorders' on the layouts you want to
-- remove borders from windows:
--
-- > layoutHook = ... ||| voidBorders Full ||| ...
--
-- For more detailed instructions on editing the layoutHook see:
--
-- "XMonad.Doc.Extending#Editing_the_layout_hook"

data VoidBorders a = VoidBorders deriving (ReadPrec [VoidBorders a]
ReadPrec (VoidBorders a)
Int -> ReadS (VoidBorders a)
ReadS [VoidBorders a]
(Int -> ReadS (VoidBorders a))
-> ReadS [VoidBorders a]
-> ReadPrec (VoidBorders a)
-> ReadPrec [VoidBorders a]
-> Read (VoidBorders a)
forall a. ReadPrec [VoidBorders a]
forall a. ReadPrec (VoidBorders a)
forall a. Int -> ReadS (VoidBorders a)
forall a. ReadS [VoidBorders a]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [VoidBorders a]
$creadListPrec :: forall a. ReadPrec [VoidBorders a]
readPrec :: ReadPrec (VoidBorders a)
$creadPrec :: forall a. ReadPrec (VoidBorders a)
readList :: ReadS [VoidBorders a]
$creadList :: forall a. ReadS [VoidBorders a]
readsPrec :: Int -> ReadS (VoidBorders a)
$creadsPrec :: forall a. Int -> ReadS (VoidBorders a)
Read, Int -> VoidBorders a -> ShowS
[VoidBorders a] -> ShowS
VoidBorders a -> String
(Int -> VoidBorders a -> ShowS)
-> (VoidBorders a -> String)
-> ([VoidBorders a] -> ShowS)
-> Show (VoidBorders a)
forall a. Int -> VoidBorders a -> ShowS
forall a. [VoidBorders a] -> ShowS
forall a. VoidBorders a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [VoidBorders a] -> ShowS
$cshowList :: forall a. [VoidBorders a] -> ShowS
show :: VoidBorders a -> String
$cshow :: forall a. VoidBorders a -> String
showsPrec :: Int -> VoidBorders a -> ShowS
$cshowsPrec :: forall a. Int -> VoidBorders a -> ShowS
Show)

voidBorders :: l Window -> ModifiedLayout VoidBorders l Window
voidBorders :: l Window -> ModifiedLayout VoidBorders l Window
voidBorders = VoidBorders Window
-> l Window -> ModifiedLayout VoidBorders l Window
forall (m :: * -> *) (l :: * -> *) a.
m a -> l a -> ModifiedLayout m l a
ModifiedLayout VoidBorders Window
forall a. VoidBorders a
VoidBorders

instance LayoutModifier VoidBorders Window where
  modifierDescription :: VoidBorders Window -> String
modifierDescription = String -> VoidBorders Window -> String
forall a b. a -> b -> a
const String
"VoidBorders"

  redoLayout :: VoidBorders Window
-> Rectangle
-> Maybe (Stack Window)
-> [(Window, Rectangle)]
-> X ([(Window, Rectangle)], Maybe (VoidBorders Window))
redoLayout VoidBorders Window
VoidBorders Rectangle
_ Maybe (Stack Window)
Nothing [(Window, Rectangle)]
wrs = ([(Window, Rectangle)], Maybe (VoidBorders Window))
-> X ([(Window, Rectangle)], Maybe (VoidBorders Window))
forall (m :: * -> *) a. Monad m => a -> m a
return ([(Window, Rectangle)]
wrs, Maybe (VoidBorders Window)
forall a. Maybe a
Nothing)
  redoLayout VoidBorders Window
VoidBorders Rectangle
_ (Just Stack Window
s) [(Window, Rectangle)]
wrs = do
    (Window -> X ()) -> [Window] -> X ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Window -> X ()
setZeroBorder ([Window] -> X ()) -> [Window] -> X ()
forall a b. (a -> b) -> a -> b
$ Stack Window -> [Window]
forall a. Stack a -> [a]
integrate Stack Window
s
    ([(Window, Rectangle)], Maybe (VoidBorders Window))
-> X ([(Window, Rectangle)], Maybe (VoidBorders Window))
forall (m :: * -> *) a. Monad m => a -> m a
return ([(Window, Rectangle)]
wrs, Maybe (VoidBorders Window)
forall a. Maybe a
Nothing)

-- | Sets border width to 0 for every window from the specified layout.
setZeroBorder :: Window -> X ()
setZeroBorder :: Window -> X ()
setZeroBorder Window
w = (Display -> X ()) -> X ()
forall a. (Display -> X a) -> X a
withDisplay ((Display -> X ()) -> X ()) -> (Display -> X ()) -> X ()
forall a b. (a -> b) -> a -> b
$ \Display
d -> IO () -> X ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io (IO () -> X ()) -> IO () -> X ()
forall a b. (a -> b) -> a -> b
$ Display -> Window -> Dimension -> IO ()
setWindowBorderWidth Display
d Window
w Dimension
0