{-# LANGUAGE MultiParamTypeClasses, TypeSynonymInstances #-}

-----------------------------------------------------------------------------
-- |
-- Module      :  XMonad.Layout.MosaicAlt
-- Description :  An alternative version of "XMonad.Layout.Mosaic".
-- Copyright   :  (c) 2007 James Webb
-- License     :  BSD-style (see xmonad/LICENSE)
--
-- Maintainer  :  xmonad#jwebb,sygneca,com
-- Stability   :  unstable
-- Portability :  unportable
--
-- A layout which gives each window a specified amount of screen space
-- relative to the others. Compared to the 'Mosaic' layout, this one
-- divides the space in a more balanced way.
--
-----------------------------------------------------------------------------

module XMonad.Layout.MosaicAlt (
        -- * Usage:
        -- $usage
        MosaicAlt(..)
        , shrinkWindowAlt
        , expandWindowAlt
        , tallWindowAlt
        , wideWindowAlt
        , resetAlt

        , Params, Param
        , HandleWindowAlt
    ) where

import XMonad
import qualified XMonad.StackSet as W
import qualified Data.Map as M
import XMonad.Prelude ( sortBy )
import Data.Ratio

-- $usage
-- You can use this module with the following in your @xmonad.hs@:
--
-- > import XMonad.Layout.MosaicAlt
-- > import qualified Data.Map as M
--
-- Then edit your @layoutHook@ by adding the MosaicAlt layout:
--
-- > myLayout = MosaicAlt M.empty ||| 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".
--
-- In the key-bindings, do something like:
--
-- >     , ((modm .|. shiftMask  , xK_a    ), withFocused (sendMessage . expandWindowAlt))
-- >     , ((modm .|. shiftMask  , xK_z    ), withFocused (sendMessage . shrinkWindowAlt))
-- >     , ((modm .|. shiftMask  , xK_s    ), withFocused (sendMessage . tallWindowAlt))
-- >     , ((modm .|. shiftMask  , xK_d    ), withFocused (sendMessage . wideWindowAlt))
-- >     , ((modm .|. controlMask, xK_space), sendMessage resetAlt)
-- >     ...
--
-- For detailed instruction on editing the key binding see:
--
-- <https://xmonad.org/TUTORIAL.html#customizing-xmonad the tutorial>.

data HandleWindowAlt =
    ShrinkWindowAlt Window
    | ExpandWindowAlt Window
    | TallWindowAlt Window
    | WideWindowAlt Window
    | ResetAlt
    deriving ( HandleWindowAlt -> HandleWindowAlt -> Bool
(HandleWindowAlt -> HandleWindowAlt -> Bool)
-> (HandleWindowAlt -> HandleWindowAlt -> Bool)
-> Eq HandleWindowAlt
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: HandleWindowAlt -> HandleWindowAlt -> Bool
== :: HandleWindowAlt -> HandleWindowAlt -> Bool
$c/= :: HandleWindowAlt -> HandleWindowAlt -> Bool
/= :: HandleWindowAlt -> HandleWindowAlt -> Bool
Eq )
instance Message HandleWindowAlt
shrinkWindowAlt, expandWindowAlt :: Window -> HandleWindowAlt
tallWindowAlt, wideWindowAlt :: Window -> HandleWindowAlt
shrinkWindowAlt :: Word64 -> HandleWindowAlt
shrinkWindowAlt = Word64 -> HandleWindowAlt
ShrinkWindowAlt
expandWindowAlt :: Word64 -> HandleWindowAlt
expandWindowAlt = Word64 -> HandleWindowAlt
ExpandWindowAlt
tallWindowAlt :: Word64 -> HandleWindowAlt
tallWindowAlt = Word64 -> HandleWindowAlt
TallWindowAlt
wideWindowAlt :: Word64 -> HandleWindowAlt
wideWindowAlt = Word64 -> HandleWindowAlt
WideWindowAlt
resetAlt :: HandleWindowAlt
resetAlt :: HandleWindowAlt
resetAlt = HandleWindowAlt
ResetAlt

data Param = Param { Param -> Ratio Integer
area, Param -> Ratio Integer
aspect :: Rational } deriving ( Int -> Param -> ShowS
[Param] -> ShowS
Param -> String
(Int -> Param -> ShowS)
-> (Param -> String) -> ([Param] -> ShowS) -> Show Param
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Param -> ShowS
showsPrec :: Int -> Param -> ShowS
$cshow :: Param -> String
show :: Param -> String
$cshowList :: [Param] -> ShowS
showList :: [Param] -> ShowS
Show, ReadPrec [Param]
ReadPrec Param
Int -> ReadS Param
ReadS [Param]
(Int -> ReadS Param)
-> ReadS [Param]
-> ReadPrec Param
-> ReadPrec [Param]
-> Read Param
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS Param
readsPrec :: Int -> ReadS Param
$creadList :: ReadS [Param]
readList :: ReadS [Param]
$creadPrec :: ReadPrec Param
readPrec :: ReadPrec Param
$creadListPrec :: ReadPrec [Param]
readListPrec :: ReadPrec [Param]
Read )
type Params = M.Map Window Param
newtype MosaicAlt a = MosaicAlt Params deriving ( Int -> MosaicAlt a -> ShowS
[MosaicAlt a] -> ShowS
MosaicAlt a -> String
(Int -> MosaicAlt a -> ShowS)
-> (MosaicAlt a -> String)
-> ([MosaicAlt a] -> ShowS)
-> Show (MosaicAlt a)
forall a. Int -> MosaicAlt a -> ShowS
forall a. [MosaicAlt a] -> ShowS
forall a. MosaicAlt a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Int -> MosaicAlt a -> ShowS
showsPrec :: Int -> MosaicAlt a -> ShowS
$cshow :: forall a. MosaicAlt a -> String
show :: MosaicAlt a -> String
$cshowList :: forall a. [MosaicAlt a] -> ShowS
showList :: [MosaicAlt a] -> ShowS
Show, ReadPrec [MosaicAlt a]
ReadPrec (MosaicAlt a)
Int -> ReadS (MosaicAlt a)
ReadS [MosaicAlt a]
(Int -> ReadS (MosaicAlt a))
-> ReadS [MosaicAlt a]
-> ReadPrec (MosaicAlt a)
-> ReadPrec [MosaicAlt a]
-> Read (MosaicAlt a)
forall a. ReadPrec [MosaicAlt a]
forall a. ReadPrec (MosaicAlt a)
forall a. Int -> ReadS (MosaicAlt a)
forall a. ReadS [MosaicAlt a]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: forall a. Int -> ReadS (MosaicAlt a)
readsPrec :: Int -> ReadS (MosaicAlt a)
$creadList :: forall a. ReadS [MosaicAlt a]
readList :: ReadS [MosaicAlt a]
$creadPrec :: forall a. ReadPrec (MosaicAlt a)
readPrec :: ReadPrec (MosaicAlt a)
$creadListPrec :: forall a. ReadPrec [MosaicAlt a]
readListPrec :: ReadPrec [MosaicAlt a]
Read )

instance LayoutClass MosaicAlt Window where
    description :: MosaicAlt Word64 -> String
description MosaicAlt Word64
_ = String
"MosaicAlt"
    doLayout :: MosaicAlt Word64
-> Rectangle
-> Stack Word64
-> X ([(Word64, Rectangle)], Maybe (MosaicAlt Word64))
doLayout (MosaicAlt Map Word64 Param
params) Rectangle
rect Stack Word64
stack =
            ([(Word64, Rectangle)], Maybe (MosaicAlt Word64))
-> X ([(Word64, Rectangle)], Maybe (MosaicAlt Word64))
forall a. a -> X a
forall (m :: * -> *) a. Monad m => a -> m a
return (Rectangle
-> Stack Word64 -> Map Word64 Param -> [(Word64, Rectangle)]
arrange Rectangle
rect Stack Word64
stack Map Word64 Param
params', MosaicAlt Word64 -> Maybe (MosaicAlt Word64)
forall a. a -> Maybe a
Just (MosaicAlt Word64 -> Maybe (MosaicAlt Word64))
-> MosaicAlt Word64 -> Maybe (MosaicAlt Word64)
forall a b. (a -> b) -> a -> b
$ Map Word64 Param -> MosaicAlt Word64
forall a. Map Word64 Param -> MosaicAlt a
MosaicAlt Map Word64 Param
params')
        where
            params' :: Map Word64 Param
params' = [Word64] -> Map Word64 Param -> Map Word64 Param
forall {k}. Ord k => [k] -> Map k Param -> Map k Param
ins (Stack Word64 -> [Word64]
forall a. Stack a -> [a]
W.up Stack Word64
stack) (Map Word64 Param -> Map Word64 Param)
-> Map Word64 Param -> Map Word64 Param
forall a b. (a -> b) -> a -> b
$ [Word64] -> Map Word64 Param -> Map Word64 Param
forall {k}. Ord k => [k] -> Map k Param -> Map k Param
ins (Stack Word64 -> [Word64]
forall a. Stack a -> [a]
W.down Stack Word64
stack) (Map Word64 Param -> Map Word64 Param)
-> Map Word64 Param -> Map Word64 Param
forall a b. (a -> b) -> a -> b
$ [Word64] -> Map Word64 Param -> Map Word64 Param
forall {k}. Ord k => [k] -> Map k Param -> Map k Param
ins [Stack Word64 -> Word64
forall a. Stack a -> a
W.focus Stack Word64
stack] Map Word64 Param
params
            ins :: [k] -> Map k Param -> Map k Param
ins [k]
wins Map k Param
as = (Map k Param -> Map k Param -> Map k Param)
-> Map k Param -> [Map k Param] -> Map k Param
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl Map k Param -> Map k Param -> Map k Param
forall k a. Ord k => Map k a -> Map k a -> Map k a
M.union Map k Param
as ([Map k Param] -> Map k Param) -> [Map k Param] -> Map k Param
forall a b. (a -> b) -> a -> b
$ (k -> Map k Param) -> [k] -> [Map k Param]
forall a b. (a -> b) -> [a] -> [b]
map (k -> Param -> Map k Param
forall k a. k -> a -> Map k a
`M.singleton` Ratio Integer -> Ratio Integer -> Param
Param Ratio Integer
1 Ratio Integer
1.5) [k]
wins

    handleMessage :: MosaicAlt Word64 -> SomeMessage -> X (Maybe (MosaicAlt Word64))
handleMessage (MosaicAlt Map Word64 Param
params) SomeMessage
msg = Maybe (MosaicAlt Word64) -> X (Maybe (MosaicAlt Word64))
forall a. a -> X a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (MosaicAlt Word64) -> X (Maybe (MosaicAlt Word64)))
-> Maybe (MosaicAlt Word64) -> X (Maybe (MosaicAlt Word64))
forall a b. (a -> b) -> a -> b
$ case SomeMessage -> Maybe HandleWindowAlt
forall m. Message m => SomeMessage -> Maybe m
fromMessage SomeMessage
msg of
        Just (ShrinkWindowAlt Word64
w) -> MosaicAlt Word64 -> Maybe (MosaicAlt Word64)
forall a. a -> Maybe a
Just (MosaicAlt Word64 -> Maybe (MosaicAlt Word64))
-> MosaicAlt Word64 -> Maybe (MosaicAlt Word64)
forall a b. (a -> b) -> a -> b
$ Map Word64 Param -> MosaicAlt Word64
forall a. Map Word64 Param -> MosaicAlt a
MosaicAlt (Map Word64 Param -> MosaicAlt Word64)
-> Map Word64 Param -> MosaicAlt Word64
forall a b. (a -> b) -> a -> b
$ Map Word64 Param
-> Word64 -> Ratio Integer -> Ratio Integer -> Map Word64 Param
alter Map Word64 Param
params Word64
w (Integer
4 Integer -> Integer -> Ratio Integer
forall a. Integral a => a -> a -> Ratio a
% Integer
5) Ratio Integer
1
        Just (ExpandWindowAlt Word64
w) -> MosaicAlt Word64 -> Maybe (MosaicAlt Word64)
forall a. a -> Maybe a
Just (MosaicAlt Word64 -> Maybe (MosaicAlt Word64))
-> MosaicAlt Word64 -> Maybe (MosaicAlt Word64)
forall a b. (a -> b) -> a -> b
$ Map Word64 Param -> MosaicAlt Word64
forall a. Map Word64 Param -> MosaicAlt a
MosaicAlt (Map Word64 Param -> MosaicAlt Word64)
-> Map Word64 Param -> MosaicAlt Word64
forall a b. (a -> b) -> a -> b
$ Map Word64 Param
-> Word64 -> Ratio Integer -> Ratio Integer -> Map Word64 Param
alter Map Word64 Param
params Word64
w (Integer
6 Integer -> Integer -> Ratio Integer
forall a. Integral a => a -> a -> Ratio a
% Integer
5) Ratio Integer
1
        Just (TallWindowAlt Word64
w) -> MosaicAlt Word64 -> Maybe (MosaicAlt Word64)
forall a. a -> Maybe a
Just (MosaicAlt Word64 -> Maybe (MosaicAlt Word64))
-> MosaicAlt Word64 -> Maybe (MosaicAlt Word64)
forall a b. (a -> b) -> a -> b
$ Map Word64 Param -> MosaicAlt Word64
forall a. Map Word64 Param -> MosaicAlt a
MosaicAlt (Map Word64 Param -> MosaicAlt Word64)
-> Map Word64 Param -> MosaicAlt Word64
forall a b. (a -> b) -> a -> b
$ Map Word64 Param
-> Word64 -> Ratio Integer -> Ratio Integer -> Map Word64 Param
alter Map Word64 Param
params Word64
w Ratio Integer
1 (Integer
3 Integer -> Integer -> Ratio Integer
forall a. Integral a => a -> a -> Ratio a
% Integer
4)
        Just (WideWindowAlt Word64
w) -> MosaicAlt Word64 -> Maybe (MosaicAlt Word64)
forall a. a -> Maybe a
Just (MosaicAlt Word64 -> Maybe (MosaicAlt Word64))
-> MosaicAlt Word64 -> Maybe (MosaicAlt Word64)
forall a b. (a -> b) -> a -> b
$ Map Word64 Param -> MosaicAlt Word64
forall a. Map Word64 Param -> MosaicAlt a
MosaicAlt (Map Word64 Param -> MosaicAlt Word64)
-> Map Word64 Param -> MosaicAlt Word64
forall a b. (a -> b) -> a -> b
$ Map Word64 Param
-> Word64 -> Ratio Integer -> Ratio Integer -> Map Word64 Param
alter Map Word64 Param
params Word64
w Ratio Integer
1 (Integer
5 Integer -> Integer -> Ratio Integer
forall a. Integral a => a -> a -> Ratio a
% Integer
4)
        Just HandleWindowAlt
ResetAlt -> MosaicAlt Word64 -> Maybe (MosaicAlt Word64)
forall a. a -> Maybe a
Just (MosaicAlt Word64 -> Maybe (MosaicAlt Word64))
-> MosaicAlt Word64 -> Maybe (MosaicAlt Word64)
forall a b. (a -> b) -> a -> b
$ Map Word64 Param -> MosaicAlt Word64
forall a. Map Word64 Param -> MosaicAlt a
MosaicAlt Map Word64 Param
forall k a. Map k a
M.empty
        Maybe HandleWindowAlt
_ -> Maybe (MosaicAlt Word64)
forall a. Maybe a
Nothing

-- Change requested params for a window.
alter :: Params -> Window -> Rational -> Rational -> Params
alter :: Map Word64 Param
-> Word64 -> Ratio Integer -> Ratio Integer -> Map Word64 Param
alter Map Word64 Param
params Word64
win Ratio Integer
arDelta Ratio Integer
asDelta = case Word64 -> Map Word64 Param -> Maybe Param
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Word64
win Map Word64 Param
params of
    Just (Param Ratio Integer
ar Ratio Integer
as) -> Word64 -> Param -> Map Word64 Param -> Map Word64 Param
forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert Word64
win (Ratio Integer -> Ratio Integer -> Param
Param (Ratio Integer
ar Ratio Integer -> Ratio Integer -> Ratio Integer
forall a. Num a => a -> a -> a
* Ratio Integer
arDelta) (Ratio Integer
as Ratio Integer -> Ratio Integer -> Ratio Integer
forall a. Num a => a -> a -> a
* Ratio Integer
asDelta)) Map Word64 Param
params
    Maybe Param
Nothing -> Word64 -> Param -> Map Word64 Param -> Map Word64 Param
forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert Word64
win (Ratio Integer -> Ratio Integer -> Param
Param Ratio Integer
arDelta (Ratio Integer
1.5 Ratio Integer -> Ratio Integer -> Ratio Integer
forall a. Num a => a -> a -> a
* Ratio Integer
asDelta)) Map Word64 Param
params

-- Layout algorithm entry point.
arrange :: Rectangle -> W.Stack Window -> Params -> [(Window, Rectangle)]
arrange :: Rectangle
-> Stack Word64 -> Map Word64 Param -> [(Word64, Rectangle)]
arrange Rectangle
rect Stack Word64
stack Map Word64 Param
params = [(Word64, Rectangle)]
r
    where
        (Double
_, [(Word64, Rectangle)]
r) = Int
-> Rectangle
-> Tree
-> Map Word64 Param
-> (Double, [(Word64, Rectangle)])
findSplits Int
3 Rectangle
rect Tree
tree Map Word64 Param
params
        tree :: Tree
tree = [Word64] -> Map Word64 Param -> Tree
makeTree ((Word64 -> Word64 -> Ordering) -> [Word64] -> [Word64]
forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy Word64 -> Word64 -> Ordering
areaCompare [Word64]
wins) Map Word64 Param
params
        wins :: [Word64]
wins = [Word64] -> [Word64]
forall a. [a] -> [a]
reverse (Stack Word64 -> [Word64]
forall a. Stack a -> [a]
W.up Stack Word64
stack) [Word64] -> [Word64] -> [Word64]
forall a. [a] -> [a] -> [a]
++ Stack Word64 -> Word64
forall a. Stack a -> a
W.focus Stack Word64
stack Word64 -> [Word64] -> [Word64]
forall a. a -> [a] -> [a]
: Stack Word64 -> [Word64]
forall a. Stack a -> [a]
W.down Stack Word64
stack
        areaCompare :: Word64 -> Word64 -> Ordering
areaCompare Word64
a Word64
b = Word64 -> Ratio Integer
or1 Word64
b Ratio Integer -> Ratio Integer -> Ordering
forall a. Ord a => a -> a -> Ordering
`compare` Word64 -> Ratio Integer
or1 Word64
a
        or1 :: Word64 -> Ratio Integer
or1 Word64
w = Ratio Integer
-> (Param -> Ratio Integer) -> Maybe Param -> Ratio Integer
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Ratio Integer
1 Param -> Ratio Integer
area (Maybe Param -> Ratio Integer) -> Maybe Param -> Ratio Integer
forall a b. (a -> b) -> a -> b
$ Word64 -> Map Word64 Param -> Maybe Param
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Word64
w Map Word64 Param
params

-- Recursively group windows into a binary tree. Aim to balance the tree
-- according to the total requested area in each branch.
data Tree = Node (Rational, Tree) (Rational, Tree) | Leaf Window | None
makeTree :: [Window] -> Params -> Tree
makeTree :: [Word64] -> Map Word64 Param -> Tree
makeTree [Word64]
wins Map Word64 Param
params = case [Word64]
wins of
    [] -> Tree
None
    [Word64
x] -> Word64 -> Tree
Leaf Word64
x
    [Word64]
_ -> (Ratio Integer, Tree) -> (Ratio Integer, Tree) -> Tree
Node (Ratio Integer
aArea, [Word64] -> Map Word64 Param -> Tree
makeTree [Word64]
aWins Map Word64 Param
params) (Ratio Integer
bArea, [Word64] -> Map Word64 Param -> Tree
makeTree [Word64]
bWins Map Word64 Param
params)
        where (([Word64]
aWins, Ratio Integer
aArea), ([Word64]
bWins, Ratio Integer
bArea)) = Map Word64 Param
-> [Word64]
-> (([Word64], Ratio Integer), ([Word64], Ratio Integer))
areaSplit Map Word64 Param
params [Word64]
wins

-- Split a list of windows in half by area.
areaSplit :: Params -> [Window] -> (([Window], Rational), ([Window], Rational))
areaSplit :: Map Word64 Param
-> [Word64]
-> (([Word64], Ratio Integer), ([Word64], Ratio Integer))
areaSplit Map Word64 Param
params = [Word64]
-> Ratio Integer
-> [Word64]
-> Ratio Integer
-> [Word64]
-> (([Word64], Ratio Integer), ([Word64], Ratio Integer))
gather [] Ratio Integer
0 [] Ratio Integer
0
    where
        gather :: [Word64]
-> Ratio Integer
-> [Word64]
-> Ratio Integer
-> [Word64]
-> (([Word64], Ratio Integer), ([Word64], Ratio Integer))
gather [Word64]
a Ratio Integer
aa [Word64]
b Ratio Integer
ba (Word64
r : [Word64]
rs) =
            if Ratio Integer
aa Ratio Integer -> Ratio Integer -> Bool
forall a. Ord a => a -> a -> Bool
<= Ratio Integer
ba
                then [Word64]
-> Ratio Integer
-> [Word64]
-> Ratio Integer
-> [Word64]
-> (([Word64], Ratio Integer), ([Word64], Ratio Integer))
gather (Word64
r Word64 -> [Word64] -> [Word64]
forall a. a -> [a] -> [a]
: [Word64]
a) (Ratio Integer
aa Ratio Integer -> Ratio Integer -> Ratio Integer
forall a. Num a => a -> a -> a
+ Word64 -> Ratio Integer
or1 Word64
r) [Word64]
b Ratio Integer
ba [Word64]
rs
                else [Word64]
-> Ratio Integer
-> [Word64]
-> Ratio Integer
-> [Word64]
-> (([Word64], Ratio Integer), ([Word64], Ratio Integer))
gather [Word64]
a Ratio Integer
aa (Word64
r Word64 -> [Word64] -> [Word64]
forall a. a -> [a] -> [a]
: [Word64]
b) (Ratio Integer
ba Ratio Integer -> Ratio Integer -> Ratio Integer
forall a. Num a => a -> a -> a
+ Word64 -> Ratio Integer
or1 Word64
r) [Word64]
rs
        gather [Word64]
a Ratio Integer
aa [Word64]
b Ratio Integer
ba [] = (([Word64] -> [Word64]
forall a. [a] -> [a]
reverse [Word64]
a, Ratio Integer
aa), ([Word64]
b, Ratio Integer
ba))
        or1 :: Word64 -> Ratio Integer
or1 Word64
w = Ratio Integer
-> (Param -> Ratio Integer) -> Maybe Param -> Ratio Integer
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Ratio Integer
1 Param -> Ratio Integer
area (Maybe Param -> Ratio Integer) -> Maybe Param -> Ratio Integer
forall a b. (a -> b) -> a -> b
$ Word64 -> Map Word64 Param -> Maybe Param
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Word64
w Map Word64 Param
params

-- Figure out which ways to split the space, by exhaustive search.
-- Complexity is quadratic in the number of windows.
findSplits :: Int -> Rectangle -> Tree -> Params -> (Double, [(Window, Rectangle)])
findSplits :: Int
-> Rectangle
-> Tree
-> Map Word64 Param
-> (Double, [(Word64, Rectangle)])
findSplits Int
_ Rectangle
_ Tree
None Map Word64 Param
_ = (Double
0, [])
findSplits Int
_ Rectangle
rect (Leaf Word64
w) Map Word64 Param
params = (Rectangle -> Word64 -> Map Word64 Param -> Double
aspectBadness Rectangle
rect Word64
w Map Word64 Param
params, [(Word64
w, Rectangle
rect)])
findSplits Int
depth Rectangle
rect (Node (Ratio Integer
aArea, Tree
aTree) (Ratio Integer
bArea, Tree
bTree)) Map Word64 Param
params =
        if Double
hBadness Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
< Double
vBadness then (Double
hBadness, [(Word64, Rectangle)]
hList) else (Double
vBadness, [(Word64, Rectangle)]
vList)
    where
        (Double
hBadness, [(Word64, Rectangle)]
hList) = (Ratio Integer -> Rectangle -> (Rectangle, Rectangle))
-> (Double, [(Word64, Rectangle)])
trySplit Ratio Integer -> Rectangle -> (Rectangle, Rectangle)
forall r. RealFrac r => r -> Rectangle -> (Rectangle, Rectangle)
splitHorizontallyBy
        (Double
vBadness, [(Word64, Rectangle)]
vList) = (Ratio Integer -> Rectangle -> (Rectangle, Rectangle))
-> (Double, [(Word64, Rectangle)])
trySplit Ratio Integer -> Rectangle -> (Rectangle, Rectangle)
forall r. RealFrac r => r -> Rectangle -> (Rectangle, Rectangle)
splitVerticallyBy
        trySplit :: (Ratio Integer -> Rectangle -> (Rectangle, Rectangle))
-> (Double, [(Word64, Rectangle)])
trySplit Ratio Integer -> Rectangle -> (Rectangle, Rectangle)
splitBy =
                (Double
aBadness Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
bBadness, [(Word64, Rectangle)]
aList [(Word64, Rectangle)]
-> [(Word64, Rectangle)] -> [(Word64, Rectangle)]
forall a. [a] -> [a] -> [a]
++ [(Word64, Rectangle)]
bList)
            where
                (Double
aBadness, [(Word64, Rectangle)]
aList) = Int
-> Rectangle
-> Tree
-> Map Word64 Param
-> (Double, [(Word64, Rectangle)])
findSplits (Int
depth Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) Rectangle
aRect Tree
aTree Map Word64 Param
params
                (Double
bBadness, [(Word64, Rectangle)]
bList) = Int
-> Rectangle
-> Tree
-> Map Word64 Param
-> (Double, [(Word64, Rectangle)])
findSplits (Int
depth Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) Rectangle
bRect Tree
bTree Map Word64 Param
params
                (Rectangle
aRect, Rectangle
bRect) = Ratio Integer -> Rectangle -> (Rectangle, Rectangle)
splitBy Ratio Integer
ratio Rectangle
rect
        ratio :: Ratio Integer
ratio = Ratio Integer
aArea Ratio Integer -> Ratio Integer -> Ratio Integer
forall a. Fractional a => a -> a -> a
/ (Ratio Integer
aArea Ratio Integer -> Ratio Integer -> Ratio Integer
forall a. Num a => a -> a -> a
+ Ratio Integer
bArea)

-- Decide how much we like this rectangle.
aspectBadness :: Rectangle -> Window -> Params -> Double
aspectBadness :: Rectangle -> Word64 -> Map Word64 Param -> Double
aspectBadness Rectangle
rect Word64
win Map Word64 Param
params =
        (if Double
a Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
< Double
1 then Double
tall else Double
wide) Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double -> Double
forall a. Floating a => a -> a
sqrt(Double
w Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
h)
    where
        tall :: Double
tall = if Double
w Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
< Double
700 then (Double
1 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
a) Double -> Double -> Double
forall a. Num a => a -> a -> a
* (Double
700 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
w) else Double
1 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
a
        wide :: Double
wide = if Double
w Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
< Double
700 then Double
a else Double
a Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
w Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
700
        a :: Double
a = (Double
w Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
h) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Ratio Integer -> Double
forall a. Fractional a => Ratio Integer -> a
fromRational (Ratio Integer
-> (Param -> Ratio Integer) -> Maybe Param -> Ratio Integer
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Ratio Integer
1.5 Param -> Ratio Integer
aspect (Maybe Param -> Ratio Integer) -> Maybe Param -> Ratio Integer
forall a b. (a -> b) -> a -> b
$ Word64 -> Map Word64 Param -> Maybe Param
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Word64
win Map Word64 Param
params)
        w :: Double
w = Dimension -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Dimension -> Double) -> Dimension -> Double
forall a b. (a -> b) -> a -> b
$ Rectangle -> Dimension
rect_width Rectangle
rect
        h :: Double
h = Dimension -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Dimension -> Double) -> Dimension -> Double
forall a b. (a -> b) -> a -> b
$ Rectangle -> Dimension
rect_height Rectangle
rect

-- vim: sw=4:et