{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses #-}

-----------------------------------------------------------------------------
-- |
-- Module      :  XMonad.Layout.TwoPane
-- Description :  A layout that splits the screen horizontally and shows two windows.
-- Copyright   :  (c) Spencer Janssen <spencerjanssen@gmail.com>
-- License     :  BSD3-style (see LICENSE)
--
-- Maintainer  :  Spencer Janssen <spencerjanssen@gmail.com>
-- Stability   :  unstable
-- Portability :  unportable
--
-- A layout that splits the screen horizontally and shows two windows.  The
-- left window is always the master window, and the right is either the
-- currently focused window or the second window in layout order.
--
-----------------------------------------------------------------------------

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

import XMonad hiding (focus)
import XMonad.StackSet ( focus, up, down)

-- $usage
-- You can use this module with the following in your @xmonad.hs@:
--
-- > import XMonad.Layout.TwoPane
--
-- Then edit your @layoutHook@ by adding the TwoPane layout:
--
-- > myLayout = TwoPane (3/100) (1/2)  ||| 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".

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

instance LayoutClass TwoPane a where
    doLayout :: TwoPane a
-> Rectangle -> Stack a -> X ([(a, Rectangle)], Maybe (TwoPane a))
doLayout (TwoPane Rational
_ Rational
split) Rectangle
r Stack a
s = forall (m :: * -> *) a. Monad m => a -> m a
return (forall {a}. Rectangle -> Stack a -> [(a, Rectangle)]
arrange Rectangle
r Stack a
s,forall a. Maybe a
Nothing)
        where
          arrange :: Rectangle -> Stack a -> [(a, Rectangle)]
arrange Rectangle
rect Stack a
st = case forall a. [a] -> [a]
reverse (forall a. Stack a -> [a]
up Stack a
st) of
                              (a
master:[a]
_) -> [(a
master,Rectangle
left),(forall a. Stack a -> a
focus Stack a
st,Rectangle
right)]
                              [] -> case forall a. Stack a -> [a]
down Stack a
st of
                                      (a
next:[a]
_) -> [(forall a. Stack a -> a
focus Stack a
st,Rectangle
left),(a
next,Rectangle
right)]
                                      [] -> [(forall a. Stack a -> a
focus Stack a
st, Rectangle
rect)]
              where (Rectangle
left, Rectangle
right) = forall r. RealFrac r => r -> Rectangle -> (Rectangle, Rectangle)
splitHorizontallyBy Rational
split Rectangle
rect

    handleMessage :: TwoPane a -> SomeMessage -> X (Maybe (TwoPane a))
handleMessage (TwoPane Rational
delta Rational
split) SomeMessage
x =
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case forall m. Message m => SomeMessage -> Maybe m
fromMessage SomeMessage
x of
                   Just Resize
Shrink -> forall a. a -> Maybe a
Just (forall a. Rational -> Rational -> TwoPane a
TwoPane Rational
delta (forall a. Ord a => a -> a -> a
max Rational
0 (Rational
split forall a. Num a => a -> a -> a
- Rational
delta)))
                   Just Resize
Expand -> forall a. a -> Maybe a
Just (forall a. Rational -> Rational -> TwoPane a
TwoPane Rational
delta (forall a. Ord a => a -> a -> a
min Rational
1 (Rational
split forall a. Num a => a -> a -> a
+ Rational
delta)))
                   Maybe Resize
_           -> forall a. Maybe a
Nothing

    description :: TwoPane a -> String
description TwoPane a
_ = String
"TwoPane"