{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses, TupleSections #-}

-----------------------------------------------------------------------------
-- |
-- Module      :  XMonad.Layout.Square
-- Description :  A layout that splits the screen into a square area and the rest of the screen.
-- Copyright   :  (c) David Roundy <droundy@darcs.net>
-- License     :  BSD3-style (see LICENSE)
--
-- Maintainer  :  none
-- Stability   :  unstable
-- Portability :  unportable
--
-- A layout that splits the screen into a square area and the rest of the
-- screen.
-- This is probably only ever useful in combination with
-- "XMonad.Layout.Combo".
-- It sticks one window in a square region, and makes the rest
-- of the windows live with what's left (in a full-screen sense).
--
-----------------------------------------------------------------------------

module XMonad.Layout.Square (
                             -- * Usage
                             -- $usage
                             Square(..) ) where

import XMonad
import XMonad.StackSet ( integrate )

-- $usage
-- You can use this module with the following in your @xmonad.hs@ file:
--
-- >   import XMonad.Layout.Square
--
-- An example layout using square together with "XMonad.Layout.Combo"
-- to make the very last area square:
--
-- > , combo (combo (mirror $ twoPane 0.03 0.85),1)] (twoPane 0.03 0.5) )
-- >                [(twoPane 0.03 0.2,1),(combo [(twoPane 0.03 0.8,1),(square,1)]
-- >         [(tabbed,3),(tabbed,30),(tabbed,1),(tabbed,1)]

-- For detailed instructions on editing your key bindings, see
-- <https://xmonad.org/TUTORIAL.html#customizing-xmonad the tutorial>.

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

instance LayoutClass Square a where
    pureLayout :: Square a -> Rectangle -> Stack a -> [(a, Rectangle)]
pureLayout Square a
Square Rectangle
r Stack a
s = [a] -> [(a, Rectangle)]
forall {a}. [a] -> [(a, Rectangle)]
arrange (Stack a -> [a]
forall a. Stack a -> [a]
integrate Stack a
s)
        where arrange :: [a] -> [(a, Rectangle)]
arrange ws :: [a]
ws@(a
_:[a]
_) = (a -> (a, Rectangle)) -> [a] -> [(a, Rectangle)]
forall a b. (a -> b) -> [a] -> [b]
map (, Rectangle
rest) ([a] -> [a]
forall a. HasCallStack => [a] -> [a]
init [a]
ws) [(a, Rectangle)] -> [(a, Rectangle)] -> [(a, Rectangle)]
forall a. [a] -> [a] -> [a]
++ [([a] -> a
forall a. HasCallStack => [a] -> a
last [a]
ws,Rectangle
sq)]
              arrange [] = [] -- actually, this is an impossible case
              (Rectangle
rest, Rectangle
sq) = Rectangle -> (Rectangle, Rectangle)
splitSquare Rectangle
r

splitSquare :: Rectangle -> (Rectangle, Rectangle)
splitSquare :: Rectangle -> (Rectangle, Rectangle)
splitSquare (Rectangle Int32
x Int32
y Word32
w Word32
h)
    | Word32
w Word32 -> Word32 -> Bool
forall a. Ord a => a -> a -> Bool
> Word32
h = (Int32 -> Int32 -> Word32 -> Word32 -> Rectangle
Rectangle Int32
x Int32
y (Word32
w Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
- Word32
h) Word32
h, Int32 -> Int32 -> Word32 -> Word32 -> Rectangle
Rectangle (Int32
xInt32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+Word32 -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word32
wWord32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
-Word32
h)) Int32
y Word32
h Word32
h)
    | Bool
otherwise = (Int32 -> Int32 -> Word32 -> Word32 -> Rectangle
Rectangle Int32
x Int32
y Word32
w (Word32
hWord32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
-Word32
w), Int32 -> Int32 -> Word32 -> Word32 -> Rectangle
Rectangle Int32
x (Int32
yInt32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+Word32 -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word32
hWord32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
-Word32
w)) Word32
w Word32
w)