{-# 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", the 'voidBorders' modifier will not
-- restore the window border if the windows are moved to a different workspace
-- or the layout is changed. There is, however, a companion 'normalBorders'
-- modifier which explicitly restores the border.
--
-- This modifier's primary use is to eliminate the "border flash" you get
-- while switching workspaces with the "XMonad.Layout.NoBorders" modifier.
--
-----------------------------------------------------------------------------

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

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

-- $usage
-- You can use this module with the following in your @xmonad.hs@
-- file:
--
-- > import XMonad.Layout.VoidBorders
--
-- and modify the layouts to call 'voidBorders' on the layouts you want to
-- remove borders from windows, and 'normalBorders' on the layouts you want
-- to keep borders for:
--
-- > layoutHook = ... ||| voidBorders Full ||| normalBorders Tall ...
--
-- For more detailed instructions on editing the layoutHook see
-- <https://xmonad.org/TUTORIAL.html#customizing-xmonad the tutorial> and
-- "XMonad.Doc.Extending#Editing_the_layout_hook".

data VoidBorders a = VoidBorders deriving (ReadPrec [VoidBorders a]
ReadPrec (VoidBorders a)
ReadS [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
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)

instance LayoutModifier VoidBorders Window where
  modifierDescription :: VoidBorders Window -> String
modifierDescription = 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 = forall (m :: * -> *) a. Monad m => a -> m a
return ([(Window, Rectangle)]
wrs, forall a. Maybe a
Nothing)
  redoLayout VoidBorders Window
VoidBorders Rectangle
_ (Just Stack Window
s) [(Window, Rectangle)]
wrs = do
    forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Window -> X ()
setZeroBorder forall a b. (a -> b) -> a -> b
$ forall a. Stack a -> [a]
integrate Stack Window
s
    forall (m :: * -> *) a. Monad m => a -> m a
return ([(Window, Rectangle)]
wrs, forall a. Maybe a
Nothing)

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

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

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

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

normalBorders :: l Window -> ModifiedLayout NormalBorders l Window
normalBorders :: forall (l :: * -> *).
l Window -> ModifiedLayout NormalBorders l Window
normalBorders = forall (m :: * -> *) (l :: * -> *) a.
m a -> l a -> ModifiedLayout m l a
ModifiedLayout forall a. NormalBorders a
NormalBorders

-- | Sets border width to 0 for every window from the specified layout.
setZeroBorder :: Window -> X ()
setZeroBorder :: Window -> X ()
setZeroBorder Window
w = Window -> Dimension -> X ()
setBorders Window
w Dimension
0

-- | Resets the border to the value read from the current configuration.
resetBorders :: Window -> X ()
resetBorders :: Window -> X ()
resetBorders Window
w = forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (forall (l :: * -> *). XConfig l -> Dimension
borderWidth forall b c a. (b -> c) -> (a -> b) -> a -> c
. XConf -> XConfig Layout
config) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Window -> Dimension -> X ()
setBorders Window
w

setBorders :: Window -> Dimension -> X ()
setBorders :: Window -> Dimension -> X ()
setBorders Window
w Dimension
bw = forall a. (Display -> X a) -> X a
withDisplay forall a b. (a -> b) -> a -> b
$ \Display
d -> forall (m :: * -> *) a. MonadIO m => IO a -> m a
io forall a b. (a -> b) -> a -> b
$ Display -> Window -> Dimension -> IO ()
setWindowBorderWidth Display
d Window
w Dimension
bw