-----------------------------------------------------------------------------
-- |
-- Module      :  XMonad.Layout.IfMax
-- Description :  Decide upon a layout depending on the number of windows.
-- Copyright   :  (c) 2013 Ilya Portnov
-- License     :  BSD3-style (see LICENSE)
--
-- Maintainer  :  Ilya Portnov <portnov84@rambler.ru>
-- Stability   :  unstable
-- Portability :  unportable
--
-- Provides IfMax layout, which will run one layout if there are maximum N
-- windows on workspace, and another layout, when number of windows is greater
-- than N.
--
-----------------------------------------------------------------------------

{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses, FlexibleContexts, PatternGuards #-}

module XMonad.Layout.IfMax
    ( -- * Usage
      -- $usage
      IfMax (..)
    , ifMax
    ) where

import Control.Arrow ((&&&))
import qualified Data.List as L
import qualified Data.Map  as M

import XMonad
import XMonad.Prelude
import qualified XMonad.StackSet as W

-- $usage
-- IfMax layout will run one layout if number of windows on workspace is as
-- maximum N, and else will run another layout.
--
-- You can use this module by adding folowing in your @xmonad.hs@:
--
-- > import XMonad.Layout.IfMax
--
-- Then add layouts to your layoutHook:
--
-- > myLayoutHook = IfMax 2 Full (Tall ...) ||| ...
--
-- In this example, if there are 1 or 2 windows, Full layout will be used;
-- otherwise, Tall layout will be used.
--

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

instance (LayoutClass l1 Window, LayoutClass l2 Window) => LayoutClass (IfMax l1 l2) Window where

  runLayout :: Workspace String (IfMax l1 l2 Window) Window
-> Rectangle
-> X ([(Window, Rectangle)], Maybe (IfMax l1 l2 Window))
runLayout (W.Workspace String
wname (IfMax Int
n l1 Window
l1 l2 Window
l2) Maybe (Stack Window)
s) Rectangle
rect = forall a. (WindowSet -> X a) -> X a
withWindowSet forall a b. (a -> b) -> a -> b
$ \WindowSet
ws -> forall {a}.
Eq a =>
[a] -> [a] -> X ([(Window, Rectangle)], Maybe (IfMax l1 l2 Window))
arrange (forall a. Maybe (Stack a) -> [a]
W.integrate' Maybe (Stack Window)
s) (forall k a. Map k a -> [k]
M.keys forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall i l a sid sd. StackSet i l a sid sd -> Map a RationalRect
W.floating forall a b. (a -> b) -> a -> b
$ WindowSet
ws)
    where
      arrange :: [a] -> [a] -> X ([(Window, Rectangle)], Maybe (IfMax l1 l2 Window))
arrange [a]
ws [a]
fw | forall (t :: * -> *) a. Foldable t => t a -> Int
length ([a]
ws forall a. Eq a => [a] -> [a] -> [a]
L.\\ [a]
fw) forall a. Ord a => a -> a -> Bool
<= Int
n = do
                                    ([(Window, Rectangle)]
wrs, Maybe (l1 Window)
ml1') <- forall (layout :: * -> *) a.
LayoutClass layout a =>
Workspace String (layout a) a
-> Rectangle -> X ([(a, Rectangle)], Maybe (layout a))
runLayout (forall i l a. i -> l -> Maybe (Stack a) -> Workspace i l a
W.Workspace String
wname l1 Window
l1 Maybe (Stack Window)
s) Rectangle
rect
                                    let l1' :: l1 Window
l1' = forall a. a -> Maybe a -> a
fromMaybe l1 Window
l1 Maybe (l1 Window)
ml1'
                                    l2 Window
l2' <- forall a. a -> Maybe a -> a
fromMaybe l2 Window
l2 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (layout :: * -> *) a.
LayoutClass layout a =>
layout a -> SomeMessage -> X (Maybe (layout a))
handleMessage l2 Window
l2 (forall a. Message a => a -> SomeMessage
SomeMessage LayoutMessages
Hide)
                                    forall (m :: * -> *) a. Monad m => a -> m a
return ([(Window, Rectangle)]
wrs, forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall (l1 :: * -> *) (l2 :: * -> *) w.
Int -> l1 w -> l2 w -> IfMax l1 l2 w
IfMax Int
n l1 Window
l1' l2 Window
l2')
                    | Bool
otherwise      = do
                                    ([(Window, Rectangle)]
wrs, Maybe (l2 Window)
ml2') <- forall (layout :: * -> *) a.
LayoutClass layout a =>
Workspace String (layout a) a
-> Rectangle -> X ([(a, Rectangle)], Maybe (layout a))
runLayout (forall i l a. i -> l -> Maybe (Stack a) -> Workspace i l a
W.Workspace String
wname l2 Window
l2 Maybe (Stack Window)
s) Rectangle
rect
                                    l1 Window
l1' <- forall a. a -> Maybe a -> a
fromMaybe l1 Window
l1 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (layout :: * -> *) a.
LayoutClass layout a =>
layout a -> SomeMessage -> X (Maybe (layout a))
handleMessage l1 Window
l1 (forall a. Message a => a -> SomeMessage
SomeMessage LayoutMessages
Hide)
                                    let l2' :: l2 Window
l2' = forall a. a -> Maybe a -> a
fromMaybe l2 Window
l2 Maybe (l2 Window)
ml2'
                                    forall (m :: * -> *) a. Monad m => a -> m a
return ([(Window, Rectangle)]
wrs, forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall (l1 :: * -> *) (l2 :: * -> *) w.
Int -> l1 w -> l2 w -> IfMax l1 l2 w
IfMax Int
n l1 Window
l1' l2 Window
l2')

  handleMessage :: IfMax l1 l2 Window -> SomeMessage -> X (Maybe (IfMax l1 l2 Window))
handleMessage (IfMax Int
n l1 Window
l1 l2 Window
l2) SomeMessage
m | Just LayoutMessages
ReleaseResources <- forall m. Message m => SomeMessage -> Maybe m
fromMessage SomeMessage
m = do
      Maybe (l1 Window)
l1' <- forall (layout :: * -> *) a.
LayoutClass layout a =>
layout a -> SomeMessage -> X (Maybe (layout a))
handleMessage l1 Window
l1 (forall a. Message a => a -> SomeMessage
SomeMessage LayoutMessages
ReleaseResources)
      Maybe (l2 Window)
l2' <- forall (layout :: * -> *) a.
LayoutClass layout a =>
layout a -> SomeMessage -> X (Maybe (layout a))
handleMessage l2 Window
l2 (forall a. Message a => a -> SomeMessage
SomeMessage LayoutMessages
ReleaseResources)
      if forall a. Maybe a -> Bool
isNothing Maybe (l1 Window)
l1' Bool -> Bool -> Bool
&& forall a. Maybe a -> Bool
isNothing Maybe (l2 Window)
l2'
         then forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
         else forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall (l1 :: * -> *) (l2 :: * -> *) w.
Int -> l1 w -> l2 w -> IfMax l1 l2 w
IfMax Int
n (forall a. a -> Maybe a -> a
fromMaybe l1 Window
l1 Maybe (l1 Window)
l1') (forall a. a -> Maybe a -> a
fromMaybe l2 Window
l2 Maybe (l2 Window)
l2')
  handleMessage (IfMax Int
n l1 Window
l1 l2 Window
l2) SomeMessage
m = do
      ([Window]
allWindows, [Window]
floatingWindows) <- forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets ((forall a. Maybe (Stack a) -> [a]
W.integrate' forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall i l a. Workspace i l a -> Maybe (Stack a)
W.stack forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall i l a sid sd. Screen i l a sid sd -> Workspace i l a
W.workspace forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall i l a sid sd. StackSet i l a sid sd -> Screen i l a sid sd
W.current forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& forall k a. Map k a -> [k]
M.keys forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall i l a sid sd. StackSet i l a sid sd -> Map a RationalRect
W.floating) forall b c a. (b -> c) -> (a -> b) -> a -> c
. XState -> WindowSet
windowset)
      if forall (t :: * -> *) a. Foldable t => t a -> Int
length ([Window]
allWindows forall a. Eq a => [a] -> [a] -> [a]
L.\\ [Window]
floatingWindows) forall a. Ord a => a -> a -> Bool
<= Int
n
        then do
          Maybe (l1 Window)
l1' <- forall (layout :: * -> *) a.
LayoutClass layout a =>
layout a -> SomeMessage -> X (Maybe (layout a))
handleMessage l1 Window
l1 SomeMessage
m
          forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a b c. (a -> b -> c) -> b -> a -> c
flip (forall (l1 :: * -> *) (l2 :: * -> *) w.
Int -> l1 w -> l2 w -> IfMax l1 l2 w
IfMax Int
n) l2 Window
l2 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (l1 Window)
l1'
        else do
          Maybe (l2 Window)
l2' <- forall (layout :: * -> *) a.
LayoutClass layout a =>
layout a -> SomeMessage -> X (Maybe (layout a))
handleMessage l2 Window
l2 SomeMessage
m
          forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall (l1 :: * -> *) (l2 :: * -> *) w.
Int -> l1 w -> l2 w -> IfMax l1 l2 w
IfMax Int
n l1 Window
l1 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (l2 Window)
l2'

  description :: IfMax l1 l2 Window -> String
description (IfMax Int
n l1 Window
l1 l2 Window
l2) = String
"If number of windows is <= " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
n forall a. [a] -> [a] -> [a]
++ String
", then " forall a. [a] -> [a] -> [a]
++
                                forall (layout :: * -> *) a.
LayoutClass layout a =>
layout a -> String
description l1 Window
l1 forall a. [a] -> [a] -> [a]
++ String
", else " forall a. [a] -> [a] -> [a]
++ forall (layout :: * -> *) a.
LayoutClass layout a =>
layout a -> String
description l2 Window
l2

-- | Layout itself
ifMax :: (LayoutClass l1 w, LayoutClass l2 w)
      => Int            -- ^ Maximum number of windows for the first layout
      -> l1 w           -- ^ First layout
      -> l2 w           -- ^ Second layout
      -> IfMax l1 l2 w
ifMax :: forall (l1 :: * -> *) w (l2 :: * -> *).
(LayoutClass l1 w, LayoutClass l2 w) =>
Int -> l1 w -> l2 w -> IfMax l1 l2 w
ifMax = forall (l1 :: * -> *) (l2 :: * -> *) w.
Int -> l1 w -> l2 w -> IfMax l1 l2 w
IfMax