{-# LANGUAGE TypeSynonymInstances, MultiParamTypeClasses #-}
-----------------------------------------------------------------------------
-- |
-- Module      :  XMonad.Layout.SimpleFloat
-- Description :  A basic floating layout.
-- Copyright   :  (c) 2007 Andrea Rossato
-- License     :  BSD-style (see xmonad/LICENSE)
--
-- Maintainer  :  andrea.rossato@unibz.it
-- Stability   :  unstable
-- Portability :  unportable
--
-- A basic floating layout.
-----------------------------------------------------------------------------

module XMonad.Layout.SimpleFloat
    ( -- * Usage:
      -- $usage
      simpleFloat
    , simpleFloat'
    , SimpleDecoration (..)
    , SimpleFloat (..)
    , shrinkText, CustomShrink(CustomShrink)
    , Shrinker(..)
    ) where

import XMonad
import qualified XMonad.StackSet as S
import XMonad.Actions.MouseResize
import XMonad.Layout.Decoration
import XMonad.Layout.SimpleDecoration
import XMonad.Layout.WindowArranger

-- $usage
-- You can use this module with the following in your
-- @xmonad.hs@:
--
-- > import XMonad.Layout.SimpleFloat
--
-- Then edit your @layoutHook@ by adding the SimpleFloat layout:
--
-- > myLayout = simpleFloat ||| Full ||| etc..
-- > main = xmonad def { layoutHook = myLayout }
--
-- 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".

-- | A simple floating layout where every window is placed according
-- to the window's initial attributes.
--
-- This version is decorated with the 'SimpleDecoration' style.
simpleFloat :: Eq a => ModifiedLayout (Decoration SimpleDecoration DefaultShrinker)
               (ModifiedLayout MouseResize (ModifiedLayout WindowArranger SimpleFloat)) a
simpleFloat :: forall a.
Eq a =>
ModifiedLayout
  (Decoration SimpleDecoration DefaultShrinker)
  (ModifiedLayout
     MouseResize (ModifiedLayout WindowArranger SimpleFloat))
  a
simpleFloat = forall (ds :: * -> *) a s (l :: * -> *).
(DecorationStyle ds a, Shrinker s) =>
s -> Theme -> ds a -> l a -> ModifiedLayout (Decoration ds s) l a
decoration DefaultShrinker
shrinkText forall a. Default a => a
def (forall a. Bool -> SimpleDecoration a
Simple Bool
False) (forall (l :: * -> *) a. l a -> ModifiedLayout MouseResize l a
mouseResize forall a b. (a -> b) -> a -> b
$ forall (l :: * -> *) a. l a -> ModifiedLayout WindowArranger l a
windowArrangeAll forall a b. (a -> b) -> a -> b
$ forall a. Dimension -> SimpleFloat a
SF Dimension
20)

-- | Same as 'simpleFloat', but with the possibility of setting a
-- custom shrinker and a custom theme.
simpleFloat' :: (Eq a, Shrinker s) => s -> Theme ->
               ModifiedLayout (Decoration SimpleDecoration s)
               (ModifiedLayout MouseResize (ModifiedLayout WindowArranger SimpleFloat)) a
simpleFloat' :: forall a s.
(Eq a, Shrinker s) =>
s
-> Theme
-> ModifiedLayout
     (Decoration SimpleDecoration s)
     (ModifiedLayout
        MouseResize (ModifiedLayout WindowArranger SimpleFloat))
     a
simpleFloat' s
s Theme
c = forall (ds :: * -> *) a s (l :: * -> *).
(DecorationStyle ds a, Shrinker s) =>
s -> Theme -> ds a -> l a -> ModifiedLayout (Decoration ds s) l a
decoration s
s Theme
c (forall a. Bool -> SimpleDecoration a
Simple Bool
False) (forall (l :: * -> *) a. l a -> ModifiedLayout MouseResize l a
mouseResize forall a b. (a -> b) -> a -> b
$ forall (l :: * -> *) a. l a -> ModifiedLayout WindowArranger l a
windowArrangeAll forall a b. (a -> b) -> a -> b
$ forall a. Dimension -> SimpleFloat a
SF (Theme -> Dimension
decoHeight Theme
c))

newtype SimpleFloat a = SF Dimension deriving (Int -> SimpleFloat a -> ShowS
forall a. Int -> SimpleFloat a -> ShowS
forall a. [SimpleFloat a] -> ShowS
forall a. SimpleFloat a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SimpleFloat a] -> ShowS
$cshowList :: forall a. [SimpleFloat a] -> ShowS
show :: SimpleFloat a -> String
$cshow :: forall a. SimpleFloat a -> String
showsPrec :: Int -> SimpleFloat a -> ShowS
$cshowsPrec :: forall a. Int -> SimpleFloat a -> ShowS
Show, ReadPrec [SimpleFloat a]
ReadPrec (SimpleFloat a)
ReadS [SimpleFloat a]
forall a. ReadPrec [SimpleFloat a]
forall a. ReadPrec (SimpleFloat a)
forall a. Int -> ReadS (SimpleFloat a)
forall a. ReadS [SimpleFloat a]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [SimpleFloat a]
$creadListPrec :: forall a. ReadPrec [SimpleFloat a]
readPrec :: ReadPrec (SimpleFloat a)
$creadPrec :: forall a. ReadPrec (SimpleFloat a)
readList :: ReadS [SimpleFloat a]
$creadList :: forall a. ReadS [SimpleFloat a]
readsPrec :: Int -> ReadS (SimpleFloat a)
$creadsPrec :: forall a. Int -> ReadS (SimpleFloat a)
Read)
instance LayoutClass SimpleFloat Window where
    description :: SimpleFloat Window -> String
description SimpleFloat Window
_ = String
"Float"
    doLayout :: SimpleFloat Window
-> Rectangle
-> Stack Window
-> X ([(Window, Rectangle)], Maybe (SimpleFloat Window))
doLayout (SF Dimension
i) Rectangle
sc (S.Stack Window
w [Window]
l [Window]
r) = do
        [(Window, Rectangle)]
wrs <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (Dimension -> Rectangle -> Window -> X (Window, Rectangle)
getSize Dimension
i Rectangle
sc) (Window
w forall a. a -> [a] -> [a]
: forall a. [a] -> [a]
reverse [Window]
l forall a. [a] -> [a] -> [a]
++ [Window]
r)
        forall (m :: * -> *) a. Monad m => a -> m a
return ([(Window, Rectangle)]
wrs, forall a. Maybe a
Nothing)

getSize :: Dimension -> Rectangle -> Window -> X (Window,Rectangle)
getSize :: Dimension -> Rectangle -> Window -> X (Window, Rectangle)
getSize Dimension
i (Rectangle Position
rx Position
ry Dimension
_ Dimension
_) Window
w = do
  Display
d  <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks XConf -> Display
display
  Dimension
bw <- 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)
  WindowAttributes
wa <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
io forall a b. (a -> b) -> a -> b
$ Display -> Window -> IO WindowAttributes
getWindowAttributes Display
d Window
w
  let ny :: Position
ny = Position
ry forall a. Num a => a -> a -> a
+ forall a b. (Integral a, Num b) => a -> b
fi Dimension
i
      x :: Position
x  =  forall a. Ord a => a -> a -> a
max Position
rx forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fi forall a b. (a -> b) -> a -> b
$ WindowAttributes -> CInt
wa_x WindowAttributes
wa
      y :: Position
y  =  forall a. Ord a => a -> a -> a
max Position
ny forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fi forall a b. (a -> b) -> a -> b
$ WindowAttributes -> CInt
wa_y WindowAttributes
wa
      wh :: Dimension
wh = forall a b. (Integral a, Num b) => a -> b
fi (WindowAttributes -> CInt
wa_width  WindowAttributes
wa) forall a. Num a => a -> a -> a
+ (Dimension
bw forall a. Num a => a -> a -> a
* Dimension
2)
      ht :: Dimension
ht = forall a b. (Integral a, Num b) => a -> b
fi (WindowAttributes -> CInt
wa_height WindowAttributes
wa) forall a. Num a => a -> a -> a
+ (Dimension
bw forall a. Num a => a -> a -> a
* Dimension
2)
  forall (m :: * -> *) a. Monad m => a -> m a
return (Window
w, Position -> Position -> Dimension -> Dimension -> Rectangle
Rectangle Position
x Position
y Dimension
wh Dimension
ht)