aboutsummaryrefslogtreecommitdiffstats
path: root/Grid.hs
blob: 361b56689c94646dab8e82edba8636311c687f85 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
{-# LANGUAGE FlexibleInstances #-}
-----------------------------------------------------------------------------
-- |
-- Module      :  XMonadContrib.Grid
-- Copyright   :  (c) Lukas Mai
-- License     :  BSD-style (see LICENSE)
--
-- Maintainer  :  <l.mai@web.de>
-- Stability   :  unstable
-- Portability :  unportable
--
-- A simple layout that attempts to put all windows in a square grid.
--
-----------------------------------------------------------------------------

module XMonadContrib.Grid (
    -- * Usage
    -- $usage
	Grid(..)
) where

import XMonad
import StackSet
import Graphics.X11.Xlib.Types

-- $usage
-- Put the following in your Config.hs file:
--
-- > import XMonadContrib.Grid
-- > ...
-- > defaultLayouts = [ ...
-- >                  , Layout Grid
-- >                  ]

-- %import XMonadContrib.Grid
-- %layout , Layout Grid

data Grid a = Grid deriving (Read, Show)

instance LayoutClass Grid a where
    pureLayout Grid r s = arrange r (integrate s)

arrange :: Rectangle -> [a] -> [(a, Rectangle)]
arrange (Rectangle rx ry rw rh) st = zip st rectangles
	where
	nwins = length st
	ncols = ceiling . (sqrt :: Double -> Double) . fromIntegral $ nwins
	mincs = nwins `div` ncols
	extrs = nwins - ncols * mincs
	chop :: Int -> Dimension -> [(Position, Dimension)]
	chop n m = ((0, m - k * fromIntegral (pred n)) :) . map (flip (,) k) . tail . reverse . take n . tail . iterate (subtract k') $ m'
		where
		k :: Dimension
		k = m `div` fromIntegral n
		m' = fromIntegral m
		k' :: Position
		k' = fromIntegral k
	xcoords = chop ncols rw
	ycoords = chop mincs rh
	ycoords' = chop (succ mincs) rh
	(xbase, xext) = splitAt (ncols - extrs) xcoords
	rectangles = combine ycoords xbase ++ combine ycoords' xext
		where
		combine ys xs = [Rectangle (rx + x) (ry + y) w h | (x, w) <- xs, (y, h) <- ys]