{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses #-}
-----------------------------------------------------------------------------
-- |
-- Module      :  XMonad.Layout.Column
-- Description :  Layout that places all windows in one column.
-- Copyright   :  (c) 2009 Ilya Portnov
-- License     :  BSD3-style (see LICENSE)
--
-- Maintainer  :  Ilya Portnov <portnov84@rambler.ru>
-- Stability   :  unstable
-- Portability :  unportable
--
-- Provides Column layout that places all windows in one column. Windows
-- heights are calculated from the equation: H1/H2 = H2/H3 = ... = q, where q is
-- given. With Shrink/Expand messages you can change the q value.
--
-----------------------------------------------------------------------------

module XMonad.Layout.Column (
                             -- * Usage
                             -- $usage
                             Column (..)
                            ) where
import XMonad
import qualified XMonad.StackSet as W

-- $usage
-- This module defines layout named Column. It places all windows in one
-- column. Windows heights are calculated from the equation: H1/H2 = H2/H3 = ... =
-- q, where `q' is given (thus, windows heights are members of geometric
-- progression). With Shrink/Expand messages one can change the `q' value.
--
-- You can use this module by adding following in your @xmonad.hs@:
--
-- > import XMonad.Layout.Column
--
-- Then add layouts to your layoutHook:
--
-- > myLayoutHook = Column 1.6 ||| ...
--
-- In this example, each next window will have height 1.6 times less then
-- previous window.

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

instance LayoutClass Column a where
    pureLayout :: Column a -> Rectangle -> Stack a -> [(a, Rectangle)]
pureLayout = forall a. Column a -> Rectangle -> Stack a -> [(a, Rectangle)]
columnLayout
    pureMessage :: Column a -> SomeMessage -> Maybe (Column a)
pureMessage = forall a. Column a -> SomeMessage -> Maybe (Column a)
columnMessage

columnMessage :: Column a -> SomeMessage -> Maybe (Column a)
columnMessage :: forall a. Column a -> SomeMessage -> Maybe (Column a)
columnMessage (Column Float
q) SomeMessage
m = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall {a}. Resize -> Column a
resize (forall m. Message m => SomeMessage -> Maybe m
fromMessage SomeMessage
m)
    where resize :: Resize -> Column a
resize Resize
Shrink = forall a. Float -> Column a
Column (Float
qforall a. Num a => a -> a -> a
-Float
0.1)
          resize Resize
Expand = forall a. Float -> Column a
Column (Float
qforall a. Num a => a -> a -> a
+Float
0.1)

columnLayout :: Column a -> Rectangle -> W.Stack a -> [(a,Rectangle)]
columnLayout :: forall a. Column a -> Rectangle -> Stack a -> [(a, Rectangle)]
columnLayout (Column Float
q) Rectangle
rect Stack a
stack = forall a b. [a] -> [b] -> [(a, b)]
zip [a]
ws [Rectangle]
rects
    where ws :: [a]
ws = forall a. Stack a -> [a]
W.integrate Stack a
stack
          n :: Int
n = forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
ws
          heights :: [Dimension]
heights = forall a b. (a -> b) -> [a] -> [b]
map (Int -> Rectangle -> Float -> Int -> Dimension
xn Int
n Rectangle
rect Float
q) [Int
1..Int
n]
          ys :: [Position]
ys = [forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum forall a b. (a -> b) -> a -> b
$ forall a. Int -> [a] -> [a]
take Int
k [Dimension]
heights | Int
k <- [Int
0..Int
nforall a. Num a => a -> a -> a
-Int
1]]
          rects :: [Rectangle]
rects = forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (forall a b c. ((a, b) -> c) -> a -> b -> c
curry (Rectangle -> (Dimension, Position) -> Rectangle
mkRect Rectangle
rect)) [Dimension]
heights [Position]
ys

mkRect :: Rectangle -> (Dimension,Position) -> Rectangle
mkRect :: Rectangle -> (Dimension, Position) -> Rectangle
mkRect (Rectangle Position
xs Position
ys Dimension
ws Dimension
_) (Dimension
h,Position
y) = Position -> Position -> Dimension -> Dimension -> Rectangle
Rectangle Position
xs (Position
ysforall a. Num a => a -> a -> a
+forall a b. (Integral a, Num b) => a -> b
fromIntegral Position
y) Dimension
ws Dimension
h

xn :: Int -> Rectangle -> Float -> Int -> Dimension
xn :: Int -> Rectangle -> Float -> Int -> Dimension
xn Int
n (Rectangle Position
_ Position
_ Dimension
_ Dimension
h) Float
q Int
k = if Float
qforall a. Eq a => a -> a -> Bool
==Float
1 then
                                  Dimension
h forall a. Integral a => a -> a -> a
`div` forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n
                               else
                                  forall a b. (RealFrac a, Integral b) => a -> b
round (forall a b. (Integral a, Num b) => a -> b
fromIntegral Dimension
hforall a. Num a => a -> a -> a
*Float
qforall a b. (Num a, Integral b) => a -> b -> a
^(Int
nforall a. Num a => a -> a -> a
-Int
k)forall a. Num a => a -> a -> a
*(Float
1forall a. Num a => a -> a -> a
-Float
q)forall a. Fractional a => a -> a -> a
/(Float
1forall a. Num a => a -> a -> a
-Float
qforall a b. (Num a, Integral b) => a -> b -> a
^Int
n))