aboutsummaryrefslogtreecommitdiffstats
path: root/Magnifier.hs
blob: e2d0c80124cb49ecc504d51bb7a340d0fff63184 (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
65
66
{-# OPTIONS_GHC -fglasgow-exts #-}
-----------------------------------------------------------------------------
-- |
-- Module      :  XMonadContrib.Magnifier
-- Copyright   :  (c) Peter De Wachter 2007
-- License     :  BSD-style (see xmonad/LICENSE)
-- 
-- Maintainer  :  Peter De Wachter <pdewacht@gmail.com>
-- Stability   :  unstable
-- Portability :  unportable
--
-- Screenshot  :  <http://caladan.rave.org/magnifier.png>
--
-- This layout hack increases the size of the window that has focus.
--
-----------------------------------------------------------------------------


module XMonadContrib.Magnifier (
    -- * Usage
    -- $usage
    magnifier, magnifier') where

import Graphics.X11.Xlib
import XMonad
import StackSet

-- $usage
-- > import XMonadContrib.Magnifier
-- > defaultLayouts = [ magnifier tiled , magnifier $ mirror tiled ]

-- | Increase the size of the window that has focus, unless it is the master window.
magnifier :: Eq a => Layout a -> Layout a
magnifier l = l { doLayout = \r s -> unlessMaster applyMagnifier r s `fmap` doLayout l r s
                , modifyLayout = \x -> fmap magnifier `fmap` modifyLayout l x }

-- | Increase the size of the window that has focus, even if it is the master window.
magnifier' :: Eq a => Layout a -> Layout a
magnifier' l = l { doLayout = \r s -> applyMagnifier r s `fmap` doLayout l r s
                 , modifyLayout = \x -> fmap magnifier' `fmap` modifyLayout l x }


type DoLayout = Eq a => Rectangle -> Stack a -> [(a, Rectangle)] -> [(a, Rectangle)]

unlessMaster :: DoLayout -> DoLayout
unlessMaster f r s = if null (up s) then id else f r s

applyMagnifier :: DoLayout
applyMagnifier r s = reverse . foldr accumulate []
    where accumulate (w,wr) ws | w == focus s = ws ++ [(w, shrink r $ magnify wr)]
                               | otherwise    = (w,wr) : ws

magnify :: Rectangle -> Rectangle
magnify (Rectangle x y w h) = Rectangle x' y' w' h'
    where x' = x - fromIntegral (w' - w) `div` 2
          y' = y - fromIntegral (h' - h) `div` 2
          w' = round $ fromIntegral w * zoom
          h' = round $ fromIntegral h * zoom
          zoom = 1.5 :: Double

shrink :: Rectangle -> Rectangle -> Rectangle
shrink (Rectangle sx sy sw sh) (Rectangle x y w h) = Rectangle x' y' w' h'
    where x' = max sx x 
          y' = max sy y
          w' = min w (fromIntegral sx + sw - fromIntegral x')
          h' = min h (fromIntegral sy + sh - fromIntegral y')