{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}

-- |
-- Module: XMonad.Layout.Columns
-- Description: A layout which tiles the windows in columns.
-- Copyright: Jean-Charles Quillet
-- License: BSD-style (see LICENSE)
--
-- Maintainer: none
-- Stability: unstable
-- Portability: unportable
--
-- A layout which tiles the windows in columns. The windows can be moved and
-- resized in every directions.
--
-- The first window appears in a single column in the center of the screen. Its
-- width is configurable (See 'coOneWindowWidth').
--
-- The second window appears in a second column. Starting with two columns, they
-- fill up the screen.
--
-- Subsequent windows appear on the bottom of the last columns.
module XMonad.Layout.Columns
  ( -- * Usage
    -- $usage
    ColumnsLayout (..),

    -- * Messages
    Focus (..),
    Move (..),
    Resize (..),

    -- * Tools
    focusDown,
    focusUp,
  )
where

import Control.Applicative ((<|>))
import Control.Arrow (Arrow (first), second)
import Control.Monad (guard)
import Control.Monad.State (modify)
import Control.Monad.Trans.Maybe (MaybeT (..))
import Data.Foldable (Foldable (..))
import Data.List (scanl')
import Data.Maybe (listToMaybe)
import Data.Ratio ((%))
import XMonad
  ( LayoutClass (..),
    Message,
    Rectangle (..),
    SomeMessage,
    Window,
    WindowSet,
    X,
    XState (..),
    fromMessage,
    gets,
    scaleRationalRect,
    sendMessage,
  )
import qualified XMonad.Operations as O
import XMonad.StackSet
  ( RationalRect (..),
    Screen (..),
    Stack (..),
    StackSet (..),
    integrate,
    peek,
  )
import qualified XMonad.StackSet as StackSet

-- $usage
-- Add 'Columns' to your @layoutHook@ with an initial empty state:
--
-- > myLayout = Full ||| Columns 1 []
--
-- Here is an example of keybindings:
--
-- > -- Focus up/down
-- > ((modm, xK_Tab), focusDown),
-- > ((modm .|. shiftMask, xK_Tab), focusUp),
-- > -- Move windows around
-- > ((modm .|. shiftMask, xK_l), sendMessage MoveRight),
-- > ((modm .|. shiftMask, xK_h), sendMessage MoveLeft),
-- > ((modm .|. shiftMask, xK_k), sendMessage MoveUp),
-- > ((modm .|. shiftMask, xK_j), sendMessage MoveDown),
-- > -- Resize them
-- > ((modm .|. controlMask, xK_l), sendMessage HorizontalExpand),
-- > ((modm .|. controlMask, xK_h), sendMessage HorizontalShrink),
-- > ((modm .|. controlMask, xK_k), sendMessage VerticalExpand),
-- > ((modm .|. controlMask, xK_j), sendMessage VerticalShrink),
--
-- This layout is known to work with:
--
-- * "XMonad.Layout.WindowNavigation" for changing focus with a direction using
-- 'XMonad.Layout.WindowNavigation.Go' messages.
-- * 'XMonad.Layout.SubLayouts.subTabbed' for docking windows together with
-- tabs. Note that sometimes when undocking windows, the layout is reset. This is
-- a minor annoyance caused by the difficulty to track windows in the sublayout.

-- | The windows can be moved in every directions.
--
-- Horizontally, a window alone in its column cannot be moved before the first
-- or after the last column. If not alone, moving the window outside those
-- limits will create a new column.
-- The windows can also be moved vertically in their column.
data Move = MoveLeft | MoveRight | MoveUp | MoveDown deriving (Int -> Move -> ShowS
[Move] -> ShowS
Move -> String
(Int -> Move -> ShowS)
-> (Move -> String) -> ([Move] -> ShowS) -> Show Move
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Move -> ShowS
showsPrec :: Int -> Move -> ShowS
$cshow :: Move -> String
show :: Move -> String
$cshowList :: [Move] -> ShowS
showList :: [Move] -> ShowS
Show, ReadPrec [Move]
ReadPrec Move
Int -> ReadS Move
ReadS [Move]
(Int -> ReadS Move)
-> ReadS [Move] -> ReadPrec Move -> ReadPrec [Move] -> Read Move
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS Move
readsPrec :: Int -> ReadS Move
$creadList :: ReadS [Move]
readList :: ReadS [Move]
$creadPrec :: ReadPrec Move
readPrec :: ReadPrec Move
$creadListPrec :: ReadPrec [Move]
readListPrec :: ReadPrec [Move]
Read)

instance Message Move

-- | The windows can be resized in every directions.
--
-- When resizing horizontally:
--
-- * if the window to be resized is not in the last column
--
--      * then the right side of the window will be moved
--      * the last column will compensate the size change
--
-- * if the window is in the last column
--
--      * then the left side of the window will be moved
--      * the column on the left of the current one will compensate the size change
--
-- The same applies when resizing vertically using the bottom side of the
-- window unless it is the last window in the column in which case we use the
-- top side.
data Resize
  = VerticalShrink
  | VerticalExpand
  | HorizontalShrink
  | HorizontalExpand
  deriving (Int -> Resize -> ShowS
[Resize] -> ShowS
Resize -> String
(Int -> Resize -> ShowS)
-> (Resize -> String) -> ([Resize] -> ShowS) -> Show Resize
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Resize -> ShowS
showsPrec :: Int -> Resize -> ShowS
$cshow :: Resize -> String
show :: Resize -> String
$cshowList :: [Resize] -> ShowS
showList :: [Resize] -> ShowS
Show, ReadPrec [Resize]
ReadPrec Resize
Int -> ReadS Resize
ReadS [Resize]
(Int -> ReadS Resize)
-> ReadS [Resize]
-> ReadPrec Resize
-> ReadPrec [Resize]
-> Read Resize
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS Resize
readsPrec :: Int -> ReadS Resize
$creadList :: ReadS [Resize]
readList :: ReadS [Resize]
$creadPrec :: ReadPrec Resize
readPrec :: ReadPrec Resize
$creadListPrec :: ReadPrec [Resize]
readListPrec :: ReadPrec [Resize]
Read)

instance Message Resize

-- | The layout handles focus change messages.
--
-- Built-in focus cannot be used here because @XMonad@ does not make it easy to
-- change the order of windows in the focus list. See also 'focusUp' and
-- 'focusDown' functions.
data Focus = FocusUp | FocusDown
  deriving (Int -> Focus -> ShowS
[Focus] -> ShowS
Focus -> String
(Int -> Focus -> ShowS)
-> (Focus -> String) -> ([Focus] -> ShowS) -> Show Focus
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Focus -> ShowS
showsPrec :: Int -> Focus -> ShowS
$cshow :: Focus -> String
show :: Focus -> String
$cshowList :: [Focus] -> ShowS
showList :: [Focus] -> ShowS
Show, ReadPrec [Focus]
ReadPrec Focus
Int -> ReadS Focus
ReadS [Focus]
(Int -> ReadS Focus)
-> ReadS [Focus]
-> ReadPrec Focus
-> ReadPrec [Focus]
-> Read Focus
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS Focus
readsPrec :: Int -> ReadS Focus
$creadList :: ReadS [Focus]
readList :: ReadS [Focus]
$creadPrec :: ReadPrec Focus
readPrec :: ReadPrec Focus
$creadListPrec :: ReadPrec [Focus]
readListPrec :: ReadPrec [Focus]
Read)

instance Message Focus

-- | A column is a list of windows with their relative vertical dimensions.
type Column = [(Rational, Window)]

-- | The layout is a list of 'Column' with their relative horizontal dimensions.
type Columns = [(Rational, Column)]

data ColumnsLayout a = Columns
  { -- | With of the first column when there is only one window. Usefull on wide
    -- screens.
    forall a. ColumnsLayout a -> Ratio Integer
coOneWindowWidth :: Rational,
    -- | The current state
    forall a. ColumnsLayout a -> Columns
coColumns :: Columns
  }
  deriving (Int -> ColumnsLayout a -> ShowS
[ColumnsLayout a] -> ShowS
ColumnsLayout a -> String
(Int -> ColumnsLayout a -> ShowS)
-> (ColumnsLayout a -> String)
-> ([ColumnsLayout a] -> ShowS)
-> Show (ColumnsLayout a)
forall a. Int -> ColumnsLayout a -> ShowS
forall a. [ColumnsLayout a] -> ShowS
forall a. ColumnsLayout a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Int -> ColumnsLayout a -> ShowS
showsPrec :: Int -> ColumnsLayout a -> ShowS
$cshow :: forall a. ColumnsLayout a -> String
show :: ColumnsLayout a -> String
$cshowList :: forall a. [ColumnsLayout a] -> ShowS
showList :: [ColumnsLayout a] -> ShowS
Show, ReadPrec [ColumnsLayout a]
ReadPrec (ColumnsLayout a)
Int -> ReadS (ColumnsLayout a)
ReadS [ColumnsLayout a]
(Int -> ReadS (ColumnsLayout a))
-> ReadS [ColumnsLayout a]
-> ReadPrec (ColumnsLayout a)
-> ReadPrec [ColumnsLayout a]
-> Read (ColumnsLayout a)
forall a. ReadPrec [ColumnsLayout a]
forall a. ReadPrec (ColumnsLayout a)
forall a. Int -> ReadS (ColumnsLayout a)
forall a. ReadS [ColumnsLayout a]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: forall a. Int -> ReadS (ColumnsLayout a)
readsPrec :: Int -> ReadS (ColumnsLayout a)
$creadList :: forall a. ReadS [ColumnsLayout a]
readList :: ReadS [ColumnsLayout a]
$creadPrec :: forall a. ReadPrec (ColumnsLayout a)
readPrec :: ReadPrec (ColumnsLayout a)
$creadListPrec :: forall a. ReadPrec [ColumnsLayout a]
readListPrec :: ReadPrec [ColumnsLayout a]
Read)

instance LayoutClass ColumnsLayout Window where
  description :: ColumnsLayout Window -> String
description ColumnsLayout Window
_ = String
layoutDescription

  doLayout :: ColumnsLayout Window
-> Rectangle
-> Stack Window
-> X ([(Window, Rectangle)], Maybe (ColumnsLayout Window))
doLayout (Columns Ratio Integer
oneWindowWidth Columns
columns) Rectangle
rectangle Stack Window
stack =
    ([(Window, Rectangle)], Maybe (ColumnsLayout Window))
-> X ([(Window, Rectangle)], Maybe (ColumnsLayout Window))
forall a. a -> X a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([(Window, Rectangle)]
rectangles, ColumnsLayout Window -> Maybe (ColumnsLayout Window)
forall a. a -> Maybe a
Just (Ratio Integer -> Columns -> ColumnsLayout Window
forall a. Ratio Integer -> Columns -> ColumnsLayout a
Columns Ratio Integer
oneWindowWidth Columns
columns'))
    where
      hackedColumns :: Columns
hackedColumns = Columns -> Stack Window -> Columns
hackForTabs Columns
columns Stack Window
stack
      columns' :: Columns
columns' = Columns -> Stack Window -> Columns
updateWindowList Columns
hackedColumns Stack Window
stack
      rectangles :: [(Window, Rectangle)]
rectangles = Rectangle -> Columns -> [(Window, Rectangle)]
forall a.
Rectangle
-> [(Ratio Integer, [(Ratio Integer, a)])] -> [(a, Rectangle)]
toRectangles Rectangle
rectangle' Columns
columns'
      -- If there is only one window, we set the destination rectangle according
      -- to the width in the layout setting.
      rectangle' :: Rectangle
rectangle'
        | ([Window] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([Window] -> Int)
-> (Stack Window -> [Window]) -> Stack Window -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Stack Window -> [Window]
forall a. Stack a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList (Stack Window -> Int) -> Stack Window -> Int
forall a b. (a -> b) -> a -> b
$ Stack Window
stack) Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1 =
            Rectangle -> RationalRect -> Rectangle
scaleRationalRect Rectangle
rectangle RationalRect
singleColumnRR
        | Bool
otherwise = Rectangle
rectangle
      singleColumnOffset :: Ratio Integer
singleColumnOffset = (Ratio Integer
1 Ratio Integer -> Ratio Integer -> Ratio Integer
forall a. Num a => a -> a -> a
- Ratio Integer
oneWindowWidth) Ratio Integer -> Ratio Integer -> Ratio Integer
forall a. Fractional a => a -> a -> a
/ Ratio Integer
2
      singleColumnRR :: RationalRect
singleColumnRR = Ratio Integer
-> Ratio Integer -> Ratio Integer -> Ratio Integer -> RationalRect
RationalRect Ratio Integer
singleColumnOffset Ratio Integer
0 Ratio Integer
oneWindowWidth Ratio Integer
1

  handleMessage :: ColumnsLayout Window
-> SomeMessage -> X (Maybe (ColumnsLayout Window))
handleMessage layout :: ColumnsLayout Window
layout@(Columns Ratio Integer
oneWindowWidth Columns
columns) SomeMessage
message = do
    mbStack <- MaybeT X (Stack Window) -> X (Maybe (Stack Window))
forall (m :: * -> *) a. MaybeT m a -> m (Maybe a)
runMaybeT (MaybeT X (Stack Window) -> X (Maybe (Stack Window)))
-> MaybeT X (Stack Window) -> X (Maybe (Stack Window))
forall a b. (a -> b) -> a -> b
$ Stack Window -> MaybeT X (Stack Window)
handleFocus' (Stack Window -> MaybeT X (Stack Window))
-> MaybeT X (Stack Window) -> MaybeT X (Stack Window)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< MaybeT X (Stack Window)
getStack
    changedFocus <- traverse updateStack' mbStack

    movedOrResized <-
      runMaybeT $
        Columns oneWindowWidth
          <$> (handleMoveOrResize' =<< peekFocus)

    pure $ movedOrResized <|> changedFocus
    where
      getStack :: MaybeT X (Stack Window)
getStack = X (Maybe (Stack Window)) -> MaybeT X (Stack Window)
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT (X (Maybe (Stack Window)) -> MaybeT X (Stack Window))
-> ((XState -> Maybe (Stack Window)) -> X (Maybe (Stack Window)))
-> (XState -> Maybe (Stack Window))
-> MaybeT X (Stack Window)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (XState -> Maybe (Stack Window)) -> X (Maybe (Stack Window))
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets ((XState -> Maybe (Stack Window)) -> MaybeT X (Stack Window))
-> (XState -> Maybe (Stack Window)) -> MaybeT X (Stack Window)
forall a b. (a -> b) -> a -> b
$ Workspace String (Layout Window) Window -> Maybe (Stack Window)
forall i l a. Workspace i l a -> Maybe (Stack a)
StackSet.stack (Workspace String (Layout Window) Window -> Maybe (Stack Window))
-> (XState -> Workspace String (Layout Window) Window)
-> XState
-> Maybe (Stack Window)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Screen String (Layout Window) Window ScreenId ScreenDetail
-> Workspace String (Layout Window) Window
forall i l a sid sd. Screen i l a sid sd -> Workspace i l a
workspace (Screen String (Layout Window) Window ScreenId ScreenDetail
 -> Workspace String (Layout Window) Window)
-> (XState
    -> Screen String (Layout Window) Window ScreenId ScreenDetail)
-> XState
-> Workspace String (Layout Window) Window
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StackSet String (Layout Window) Window ScreenId ScreenDetail
-> Screen String (Layout Window) Window ScreenId ScreenDetail
forall i l a sid sd. StackSet i l a sid sd -> Screen i l a sid sd
current (StackSet String (Layout Window) Window ScreenId ScreenDetail
 -> Screen String (Layout Window) Window ScreenId ScreenDetail)
-> (XState
    -> StackSet String (Layout Window) Window ScreenId ScreenDetail)
-> XState
-> Screen String (Layout Window) Window ScreenId ScreenDetail
forall b c a. (b -> c) -> (a -> b) -> a -> c
. XState
-> StackSet String (Layout Window) Window ScreenId ScreenDetail
windowset
      handleFocus' :: Stack Window -> MaybeT X (Stack Window)
handleFocus' = Maybe (Stack Window) -> MaybeT X (Stack Window)
forall {a}. Maybe a -> MaybeT X a
hoistMaybe (Maybe (Stack Window) -> MaybeT X (Stack Window))
-> (Stack Window -> Maybe (Stack Window))
-> Stack Window
-> MaybeT X (Stack Window)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Columns -> SomeMessage -> Stack Window -> Maybe (Stack Window)
handleFocus Columns
columns SomeMessage
message
      -- A 'Just' needs to be return for the new stack to be taken into account
      updateStack' :: Stack Window -> m (ColumnsLayout Window)
updateStack' Stack Window
s = (XState -> XState) -> m ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify (Stack Window -> XState -> XState
setStack Stack Window
s) m () -> m (ColumnsLayout Window) -> m (ColumnsLayout Window)
forall a b. m a -> m b -> m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ColumnsLayout Window -> m (ColumnsLayout Window)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ColumnsLayout Window
layout
      peekFocus :: MaybeT X Window
peekFocus = X (Maybe Window) -> MaybeT X Window
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT (X (Maybe Window) -> MaybeT X Window)
-> ((XState -> Maybe Window) -> X (Maybe Window))
-> (XState -> Maybe Window)
-> MaybeT X Window
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (XState -> Maybe Window) -> X (Maybe Window)
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets ((XState -> Maybe Window) -> MaybeT X Window)
-> (XState -> Maybe Window) -> MaybeT X Window
forall a b. (a -> b) -> a -> b
$ StackSet String (Layout Window) Window ScreenId ScreenDetail
-> Maybe Window
forall i l a s sd. StackSet i l a s sd -> Maybe a
peek (StackSet String (Layout Window) Window ScreenId ScreenDetail
 -> Maybe Window)
-> (XState
    -> StackSet String (Layout Window) Window ScreenId ScreenDetail)
-> XState
-> Maybe Window
forall b c a. (b -> c) -> (a -> b) -> a -> c
. XState
-> StackSet String (Layout Window) Window ScreenId ScreenDetail
windowset
      handleMoveOrResize' :: Window -> MaybeT X Columns
handleMoveOrResize' = Maybe Columns -> MaybeT X Columns
forall {a}. Maybe a -> MaybeT X a
hoistMaybe (Maybe Columns -> MaybeT X Columns)
-> (Window -> Maybe Columns) -> Window -> MaybeT X Columns
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Columns -> SomeMessage -> Window -> Maybe Columns
handleMoveOrResize Columns
columns SomeMessage
message
      hoistMaybe :: Maybe a -> MaybeT X a
hoistMaybe = X (Maybe a) -> MaybeT X a
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT (X (Maybe a) -> MaybeT X a)
-> (Maybe a -> X (Maybe a)) -> Maybe a -> MaybeT X a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe a -> X (Maybe a)
forall a. a -> X a
forall (f :: * -> *) a. Applicative f => a -> f a
pure

layoutDescription :: String
layoutDescription :: String
layoutDescription = String
"Columns"

-- | Change the keyboard focus to the previous window
focusUp :: X ()
focusUp :: X ()
focusUp =
  Focus
-> (StackSet String (Layout Window) Window ScreenId ScreenDetail
    -> StackSet String (Layout Window) Window ScreenId ScreenDetail)
-> String
-> X ()
forall a.
Message a =>
a
-> (StackSet String (Layout Window) Window ScreenId ScreenDetail
    -> StackSet String (Layout Window) Window ScreenId ScreenDetail)
-> String
-> X ()
sendMsgOrOnWindowsSet Focus
FocusUp StackSet String (Layout Window) Window ScreenId ScreenDetail
-> StackSet String (Layout Window) Window ScreenId ScreenDetail
forall i l a s sd. StackSet i l a s sd -> StackSet i l a s sd
StackSet.focusUp
    (String -> X ()) -> X String -> X ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< X String
getCurrentLayoutDescription

-- | Change the keyboard focus to the next window
focusDown :: X ()
focusDown :: X ()
focusDown =
  Focus
-> (StackSet String (Layout Window) Window ScreenId ScreenDetail
    -> StackSet String (Layout Window) Window ScreenId ScreenDetail)
-> String
-> X ()
forall a.
Message a =>
a
-> (StackSet String (Layout Window) Window ScreenId ScreenDetail
    -> StackSet String (Layout Window) Window ScreenId ScreenDetail)
-> String
-> X ()
sendMsgOrOnWindowsSet Focus
FocusDown StackSet String (Layout Window) Window ScreenId ScreenDetail
-> StackSet String (Layout Window) Window ScreenId ScreenDetail
forall i l a s sd. StackSet i l a s sd -> StackSet i l a s sd
StackSet.focusDown
    (String -> X ()) -> X String -> X ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< X String
getCurrentLayoutDescription

sendMsgOrOnWindowsSet :: (Message a) => a -> (WindowSet -> WindowSet) -> String -> X ()
sendMsgOrOnWindowsSet :: forall a.
Message a =>
a
-> (StackSet String (Layout Window) Window ScreenId ScreenDetail
    -> StackSet String (Layout Window) Window ScreenId ScreenDetail)
-> String
-> X ()
sendMsgOrOnWindowsSet a
message StackSet String (Layout Window) Window ScreenId ScreenDetail
-> StackSet String (Layout Window) Window ScreenId ScreenDetail
f String
description'
  | String
description' String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
layoutDescription = a -> X ()
forall a. Message a => a -> X ()
sendMessage a
message
  | Bool
otherwise = (StackSet String (Layout Window) Window ScreenId ScreenDetail
 -> StackSet String (Layout Window) Window ScreenId ScreenDetail)
-> X ()
O.windows StackSet String (Layout Window) Window ScreenId ScreenDetail
-> StackSet String (Layout Window) Window ScreenId ScreenDetail
f

getCurrentLayoutDescription :: X String
getCurrentLayoutDescription :: X String
getCurrentLayoutDescription =
  (XState -> String) -> X String
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets
    ( Layout Window -> String
forall (layout :: * -> *) a.
LayoutClass layout a =>
layout a -> String
description
        (Layout Window -> String)
-> (XState -> Layout Window) -> XState -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Workspace String (Layout Window) Window -> Layout Window
forall i l a. Workspace i l a -> l
StackSet.layout
        (Workspace String (Layout Window) Window -> Layout Window)
-> (XState -> Workspace String (Layout Window) Window)
-> XState
-> Layout Window
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Screen String (Layout Window) Window ScreenId ScreenDetail
-> Workspace String (Layout Window) Window
forall i l a sid sd. Screen i l a sid sd -> Workspace i l a
workspace
        (Screen String (Layout Window) Window ScreenId ScreenDetail
 -> Workspace String (Layout Window) Window)
-> (XState
    -> Screen String (Layout Window) Window ScreenId ScreenDetail)
-> XState
-> Workspace String (Layout Window) Window
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StackSet String (Layout Window) Window ScreenId ScreenDetail
-> Screen String (Layout Window) Window ScreenId ScreenDetail
forall i l a sid sd. StackSet i l a sid sd -> Screen i l a sid sd
current
        (StackSet String (Layout Window) Window ScreenId ScreenDetail
 -> Screen String (Layout Window) Window ScreenId ScreenDetail)
-> (XState
    -> StackSet String (Layout Window) Window ScreenId ScreenDetail)
-> XState
-> Screen String (Layout Window) Window ScreenId ScreenDetail
forall b c a. (b -> c) -> (a -> b) -> a -> c
. XState
-> StackSet String (Layout Window) Window ScreenId ScreenDetail
windowset
    )

setStack :: Stack Window -> XState -> XState
setStack :: Stack Window -> XState -> XState
setStack Stack Window
stack XState
state =
  XState
state
    { windowset =
        (windowset state)
          { current =
              (current $ windowset state)
                { workspace =
                    (workspace . current $ windowset state)
                      { StackSet.stack = Just stack
                      }
                }
          }
    }

handleFocus :: Columns -> SomeMessage -> Stack Window -> Maybe (Stack Window)
handleFocus :: Columns -> SomeMessage -> Stack Window -> Maybe (Stack Window)
handleFocus Columns
columns SomeMessage
message Stack Window
stack
  | Just Focus
FocusDown <- SomeMessage -> Maybe Focus
forall m. Message m => SomeMessage -> Maybe m
fromMessage SomeMessage
message = Stack Window -> Window -> Stack Window
setFocus' Stack Window
stack (Window -> Stack Window) -> Maybe Window -> Maybe (Stack Window)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Window
mbNext
  | Just Focus
FocusUp <- SomeMessage -> Maybe Focus
forall m. Message m => SomeMessage -> Maybe m
fromMessage SomeMessage
message = Stack Window -> Window -> Stack Window
setFocus' Stack Window
stack (Window -> Stack Window) -> Maybe Window -> Maybe (Stack Window)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Window
mbPrevious
  | Bool
otherwise = Maybe (Stack Window)
forall a. Maybe a
Nothing
  where
    focused :: Window
focused = Stack Window -> Window
forall a. Stack a -> a
focus Stack Window
stack
    windows :: [Window]
windows = Columns -> [Window]
columnsToWindows Columns
columns
    exists :: Bool
exists = Window
focused Window -> [Window] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Window]
windows
    mbNext :: Maybe Window
mbNext = Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard Bool
exists Maybe () -> Maybe Window -> Maybe Window
forall a b. Maybe a -> Maybe b -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Window -> [Window] -> Maybe Window
forall {t}. Eq t => t -> [t] -> Maybe t
next Window
focused [Window]
windows
    mbPrevious :: Maybe Window
mbPrevious = Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard Bool
exists Maybe () -> Maybe Window -> Maybe Window
forall a b. Maybe a -> Maybe b -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Window -> [Window] -> Maybe Window
forall {t}. Eq t => t -> [t] -> Maybe t
previous Window
focused [Window]
windows
    setFocus' :: Stack Window -> Window -> Stack Window
setFocus' = (Window -> Stack Window -> Stack Window)
-> Stack Window -> Window -> Stack Window
forall a b c. (a -> b -> c) -> b -> a -> c
flip Window -> Stack Window -> Stack Window
forall {a}. Eq a => a -> Stack a -> Stack a
setFocus
    previous :: t -> [t] -> Maybe t
previous t
a = t -> [t] -> Maybe t
forall {t}. Eq t => t -> [t] -> Maybe t
next t
a ([t] -> Maybe t) -> ([t] -> [t]) -> [t] -> Maybe t
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [t] -> [t]
forall a. [a] -> [a]
reverse
    setFocus :: a -> Stack a -> Stack a
setFocus a
w = (Stack a -> Bool) -> (Stack a -> Stack a) -> Stack a -> Stack a
forall a. (a -> Bool) -> (a -> a) -> a -> a
until (a -> a -> Bool
forall a. Eq a => a -> a -> Bool
(==) a
w (a -> Bool) -> (Stack a -> a) -> Stack a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Stack a -> a
forall a. Stack a -> a
focus) Stack a -> Stack a
forall a. Stack a -> Stack a
StackSet.focusDown'
    next :: t -> [t] -> Maybe t
next t
_ [] = Maybe t
forall a. Maybe a
Nothing
    next t
a (t
x : [t]
xs)
      | t
a t -> t -> Bool
forall a. Eq a => a -> a -> Bool
== t
x = [t] -> Maybe t
forall a. [a] -> Maybe a
listToMaybe [t]
xs
      | Bool
otherwise = t -> [t] -> Maybe t
next t
a ([t]
xs [t] -> [t] -> [t]
forall a. Semigroup a => a -> a -> a
<> [t
x])

oldNewWindows :: Columns -> Stack Window -> ([Window], [Window])
oldNewWindows :: Columns -> Stack Window -> ([Window], [Window])
oldNewWindows Columns
columns Stack Window
stack = ([Window]
old, [Window]
new)
  where
    old :: [Window]
old = (Window -> Bool) -> [Window] -> [Window]
forall a. (a -> Bool) -> [a] -> [a]
filter (Window -> [Window] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [Window]
stackList) [Window]
windows
    new :: [Window]
new = (Window -> Bool) -> [Window] -> [Window]
forall a. (a -> Bool) -> [a] -> [a]
filter (Window -> [Window] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [Window]
windows) [Window]
stackList
    stackList :: [Window]
stackList = Stack Window -> [Window]
forall a. Stack a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList Stack Window
stack
    windows :: [Window]
windows = Columns -> [Window]
columnsToWindows Columns
columns

-- | Add the new windows to the layout and remove the old ones.
updateWindowList :: Columns -> Stack Window -> Columns
updateWindowList :: Columns -> Stack Window -> Columns
updateWindowList Columns
columns Stack Window
stack = [Window] -> Columns -> Columns
addWindows [Window]
newWindows ([Window] -> Columns -> Columns
removeWindows [Window]
oldWindows Columns
columns)
  where
    ([Window]
oldWindows, [Window]
newWindows) = Columns -> Stack Window -> ([Window], [Window])
oldNewWindows Columns
columns Stack Window
stack

-- | If one window disappeared and another appeared, we assume that the sublayout
-- tabs just changed focused.
hackForTabs :: Columns -> Stack Window -> Columns
hackForTabs :: Columns -> Stack Window -> Columns
hackForTabs Columns
columns Stack Window
stack = (Window -> Window) -> Columns -> Columns
mapWindow Window -> Window
replace Columns
columns
  where
    replace :: Window -> Window
replace Window
window
      | (Window
w1 : [Window]
_, [Window
w2]) <- Columns -> Stack Window -> ([Window], [Window])
oldNewWindows Columns
columns Stack Window
stack =
          if Window
window Window -> Window -> Bool
forall a. Eq a => a -> a -> Bool
== Window
w1
            then Window
w2
            else Window
window
      | Bool
otherwise = Window
window

toRectangles :: Rectangle -> [(Rational, [(Rational, a)])] -> [(a, Rectangle)]
toRectangles :: forall a.
Rectangle
-> [(Ratio Integer, [(Ratio Integer, a)])] -> [(a, Rectangle)]
toRectangles Rectangle
rectangle [(Ratio Integer, [(Ratio Integer, a)])]
columns =
  (RationalRect -> Rectangle) -> (a, RationalRect) -> (a, Rectangle)
forall b c d. (b -> c) -> (d, b) -> (d, c)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second (Rectangle -> RationalRect -> Rectangle
scaleRationalRect Rectangle
rectangle) ((a, RationalRect) -> (a, Rectangle))
-> [(a, RationalRect)] -> [(a, Rectangle)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [(a, RationalRect)]
windowsAndRectangles
  where
    offsetsAndRatios :: [(Ratio Integer, Ratio Integer,
  [(Ratio Integer, Ratio Integer, a)])]
offsetsAndRatios = [(Ratio Integer, [(Ratio Integer, Ratio Integer, a)])]
-> [(Ratio Integer, Ratio Integer,
     [(Ratio Integer, Ratio Integer, a)])]
forall a.
[(Ratio Integer, a)] -> [(Ratio Integer, Ratio Integer, a)]
toOffsetRatio (([(Ratio Integer, a)] -> [(Ratio Integer, Ratio Integer, a)])
-> (Ratio Integer, [(Ratio Integer, a)])
-> (Ratio Integer, [(Ratio Integer, Ratio Integer, a)])
forall b c d. (b -> c) -> (d, b) -> (d, c)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second [(Ratio Integer, a)] -> [(Ratio Integer, Ratio Integer, a)]
forall a.
[(Ratio Integer, a)] -> [(Ratio Integer, Ratio Integer, a)]
toOffsetRatio ((Ratio Integer, [(Ratio Integer, a)])
 -> (Ratio Integer, [(Ratio Integer, Ratio Integer, a)]))
-> [(Ratio Integer, [(Ratio Integer, a)])]
-> [(Ratio Integer, [(Ratio Integer, Ratio Integer, a)])]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [(Ratio Integer, [(Ratio Integer, a)])]
columns)
    windowsAndRectangles :: [(a, RationalRect)]
windowsAndRectangles = ((Ratio Integer, Ratio Integer,
  [(Ratio Integer, Ratio Integer, a)])
 -> [(a, RationalRect)])
-> [(Ratio Integer, Ratio Integer,
     [(Ratio Integer, Ratio Integer, a)])]
-> [(a, RationalRect)]
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (Ratio Integer, Ratio Integer, [(Ratio Integer, Ratio Integer, a)])
-> [(a, RationalRect)]
forall {f :: * -> *} {a}.
Functor f =>
(Ratio Integer, Ratio Integer, f (Ratio Integer, Ratio Integer, a))
-> f (a, RationalRect)
toWindowAndRectangle [(Ratio Integer, Ratio Integer,
  [(Ratio Integer, Ratio Integer, a)])]
offsetsAndRatios
    toWindowAndRectangle :: (Ratio Integer, Ratio Integer, f (Ratio Integer, Ratio Integer, a))
-> f (a, RationalRect)
toWindowAndRectangle (Ratio Integer
x, Ratio Integer
w, f (Ratio Integer, Ratio Integer, a)
cs) = (\(Ratio Integer
y, Ratio Integer
h, a
ws) -> (a
ws, Ratio Integer
-> Ratio Integer -> Ratio Integer -> Ratio Integer -> RationalRect
RationalRect Ratio Integer
x Ratio Integer
y Ratio Integer
w Ratio Integer
h)) ((Ratio Integer, Ratio Integer, a) -> (a, RationalRect))
-> f (Ratio Integer, Ratio Integer, a) -> f (a, RationalRect)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f (Ratio Integer, Ratio Integer, a)
cs

onFocused :: (a -> a) -> Stack a -> Stack a
onFocused :: forall a. (a -> a) -> Stack a -> Stack a
onFocused a -> a
f (Stack a
a [a]
before [a]
after) = a -> [a] -> [a] -> Stack a
forall a. a -> [a] -> [a] -> Stack a
Stack (a -> a
f a
a) [a]
before [a]
after

onFocusedM :: (Monad m) => (a -> m a) -> Stack a -> m (Stack a)
onFocusedM :: forall (m :: * -> *) a.
Monad m =>
(a -> m a) -> Stack a -> m (Stack a)
onFocusedM a -> m a
f (Stack a
a [a]
before [a]
after) = a -> [a] -> [a] -> Stack a
forall a. a -> [a] -> [a] -> Stack a
Stack (a -> [a] -> [a] -> Stack a) -> m a -> m ([a] -> [a] -> Stack a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> m a
f a
a m ([a] -> [a] -> Stack a) -> m [a] -> m ([a] -> Stack a)
forall a b. m (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> [a] -> m [a]
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure [a]
before m ([a] -> Stack a) -> m [a] -> m (Stack a)
forall a b. m (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> [a] -> m [a]
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure [a]
after

onFocusedOrPrevious :: (a -> a) -> Stack a -> Stack a
onFocusedOrPrevious :: forall a. (a -> a) -> Stack a -> Stack a
onFocusedOrPrevious a -> a
f (Stack a
a (a
a' : [a]
others) []) = a -> [a] -> [a] -> Stack a
forall a. a -> [a] -> [a] -> Stack a
Stack a
a (a -> a
f a
a' a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
others) []
onFocusedOrPrevious a -> a
f Stack a
stack = (a -> a) -> Stack a -> Stack a
forall a. (a -> a) -> Stack a -> Stack a
onFocused a -> a
f Stack a
stack

handleMoveOrResize :: Columns -> SomeMessage -> Window -> Maybe Columns
handleMoveOrResize :: Columns -> SomeMessage -> Window -> Maybe Columns
handleMoveOrResize Columns
columns SomeMessage
message Window
window
  | Just Move
msg <- SomeMessage -> Maybe Move
forall m. Message m => SomeMessage -> Maybe m
fromMessage SomeMessage
message = Move -> Window -> Columns -> Maybe Columns
move Move
msg Window
window Columns
columns
  | Just Resize
HorizontalShrink <- SomeMessage -> Maybe Resize
forall m. Message m => SomeMessage -> Maybe m
fromMessage SomeMessage
message =
      ((Ratio Integer, [(Ratio Integer, Window)])
 -> (Ratio Integer, [(Ratio Integer, Window)]))
-> Stack (Ratio Integer, [(Ratio Integer, Window)]) -> Columns
forall {a}.
((Ratio Integer, a) -> (Ratio Integer, a))
-> Stack (Ratio Integer, a) -> [(Ratio Integer, a)]
onFocusedOrPrevious' (Ratio Integer, [(Ratio Integer, Window)])
-> (Ratio Integer, [(Ratio Integer, Window)])
forall {d}. (Ratio Integer, d) -> (Ratio Integer, d)
shrink (Stack (Ratio Integer, [(Ratio Integer, Window)]) -> Columns)
-> Maybe (Stack (Ratio Integer, [(Ratio Integer, Window)]))
-> Maybe Columns
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Window
-> Columns
-> Maybe (Stack (Ratio Integer, [(Ratio Integer, Window)]))
findInColumns Window
window Columns
columns
  | Just Resize
HorizontalExpand <- SomeMessage -> Maybe Resize
forall m. Message m => SomeMessage -> Maybe m
fromMessage SomeMessage
message =
      ((Ratio Integer, [(Ratio Integer, Window)])
 -> (Ratio Integer, [(Ratio Integer, Window)]))
-> Stack (Ratio Integer, [(Ratio Integer, Window)]) -> Columns
forall {a}.
((Ratio Integer, a) -> (Ratio Integer, a))
-> Stack (Ratio Integer, a) -> [(Ratio Integer, a)]
onFocusedOrPrevious' (Ratio Integer, [(Ratio Integer, Window)])
-> (Ratio Integer, [(Ratio Integer, Window)])
forall {d}. (Ratio Integer, d) -> (Ratio Integer, d)
expand (Stack (Ratio Integer, [(Ratio Integer, Window)]) -> Columns)
-> Maybe (Stack (Ratio Integer, [(Ratio Integer, Window)]))
-> Maybe Columns
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Window
-> Columns
-> Maybe (Stack (Ratio Integer, [(Ratio Integer, Window)]))
findInColumns Window
window Columns
columns
  | Just Resize
VerticalExpand <- SomeMessage -> Maybe Resize
forall m. Message m => SomeMessage -> Maybe m
fromMessage SomeMessage
message =
      ([(Ratio Integer, Window)] -> Maybe [(Ratio Integer, Window)])
-> Stack (Ratio Integer, [(Ratio Integer, Window)])
-> Maybe Columns
forall {f :: * -> *} {a} {d}.
Monad f =>
(a -> f a) -> Stack (d, a) -> f [(d, a)]
onFocusedM'
        ((Stack (Ratio Integer, Window) -> [(Ratio Integer, Window)])
-> Maybe (Stack (Ratio Integer, Window))
-> Maybe [(Ratio Integer, Window)]
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (((Ratio Integer, Window) -> (Ratio Integer, Window))
-> Stack (Ratio Integer, Window) -> [(Ratio Integer, Window)]
forall {a}.
((Ratio Integer, a) -> (Ratio Integer, a))
-> Stack (Ratio Integer, a) -> [(Ratio Integer, a)]
onFocusedOrPrevious' (Ratio Integer, Window) -> (Ratio Integer, Window)
forall {d}. (Ratio Integer, d) -> (Ratio Integer, d)
shrink) (Maybe (Stack (Ratio Integer, Window))
 -> Maybe [(Ratio Integer, Window)])
-> ([(Ratio Integer, Window)]
    -> Maybe (Stack (Ratio Integer, Window)))
-> [(Ratio Integer, Window)]
-> Maybe [(Ratio Integer, Window)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Window
-> [(Ratio Integer, Window)]
-> Maybe (Stack (Ratio Integer, Window))
findInColumn Window
window)
        (Stack (Ratio Integer, [(Ratio Integer, Window)]) -> Maybe Columns)
-> Maybe (Stack (Ratio Integer, [(Ratio Integer, Window)]))
-> Maybe Columns
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Window
-> Columns
-> Maybe (Stack (Ratio Integer, [(Ratio Integer, Window)]))
findInColumns Window
window Columns
columns
  | Just Resize
VerticalShrink <- SomeMessage -> Maybe Resize
forall m. Message m => SomeMessage -> Maybe m
fromMessage SomeMessage
message =
      ([(Ratio Integer, Window)] -> Maybe [(Ratio Integer, Window)])
-> Stack (Ratio Integer, [(Ratio Integer, Window)])
-> Maybe Columns
forall {f :: * -> *} {a} {d}.
Monad f =>
(a -> f a) -> Stack (d, a) -> f [(d, a)]
onFocusedM'
        ((Stack (Ratio Integer, Window) -> [(Ratio Integer, Window)])
-> Maybe (Stack (Ratio Integer, Window))
-> Maybe [(Ratio Integer, Window)]
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (((Ratio Integer, Window) -> (Ratio Integer, Window))
-> Stack (Ratio Integer, Window) -> [(Ratio Integer, Window)]
forall {a}.
((Ratio Integer, a) -> (Ratio Integer, a))
-> Stack (Ratio Integer, a) -> [(Ratio Integer, a)]
onFocusedOrPrevious' (Ratio Integer, Window) -> (Ratio Integer, Window)
forall {d}. (Ratio Integer, d) -> (Ratio Integer, d)
expand) (Maybe (Stack (Ratio Integer, Window))
 -> Maybe [(Ratio Integer, Window)])
-> ([(Ratio Integer, Window)]
    -> Maybe (Stack (Ratio Integer, Window)))
-> [(Ratio Integer, Window)]
-> Maybe [(Ratio Integer, Window)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Window
-> [(Ratio Integer, Window)]
-> Maybe (Stack (Ratio Integer, Window))
findInColumn Window
window)
        (Stack (Ratio Integer, [(Ratio Integer, Window)]) -> Maybe Columns)
-> Maybe (Stack (Ratio Integer, [(Ratio Integer, Window)]))
-> Maybe Columns
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Window
-> Columns
-> Maybe (Stack (Ratio Integer, [(Ratio Integer, Window)]))
findInColumns Window
window Columns
columns
  | Bool
otherwise = Maybe Columns
forall a. Maybe a
Nothing
  where
    expand :: (Ratio Integer, d) -> (Ratio Integer, d)
expand = (Ratio Integer -> Ratio Integer)
-> (Ratio Integer, d) -> (Ratio Integer, d)
forall b c d. (b -> c) -> (b, d) -> (c, d)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (b, d) (c, d)
first ((Ratio Integer -> Ratio Integer)
 -> (Ratio Integer, d) -> (Ratio Integer, d))
-> (Ratio Integer -> Ratio Integer)
-> (Ratio Integer, d)
-> (Ratio Integer, d)
forall a b. (a -> b) -> a -> b
$ (Ratio Integer -> Ratio Integer -> Ratio Integer)
-> Ratio Integer -> Ratio Integer -> Ratio Integer
forall a b c. (a -> b -> c) -> b -> a -> c
flip Ratio Integer -> Ratio Integer -> Ratio Integer
forall a. Num a => a -> a -> a
(+) (Ratio Integer
3 Ratio Integer -> Ratio Integer -> Ratio Integer
forall a. Fractional a => a -> a -> a
/ Ratio Integer
100)
    shrink :: (Ratio Integer, d) -> (Ratio Integer, d)
shrink = (Ratio Integer -> Ratio Integer)
-> (Ratio Integer, d) -> (Ratio Integer, d)
forall b c d. (b -> c) -> (b, d) -> (c, d)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (b, d) (c, d)
first ((Ratio Integer -> Ratio Integer)
 -> (Ratio Integer, d) -> (Ratio Integer, d))
-> (Ratio Integer -> Ratio Integer)
-> (Ratio Integer, d)
-> (Ratio Integer, d)
forall a b. (a -> b) -> a -> b
$ (Ratio Integer -> Ratio Integer -> Ratio Integer)
-> Ratio Integer -> Ratio Integer -> Ratio Integer
forall a b c. (a -> b -> c) -> b -> a -> c
flip (-) (Ratio Integer
3 Ratio Integer -> Ratio Integer -> Ratio Integer
forall a. Fractional a => a -> a -> a
/ Ratio Integer
100)
    onFocusedM' :: (a -> f a) -> Stack (d, a) -> f [(d, a)]
onFocusedM' a -> f a
f = (Stack (d, a) -> [(d, a)]) -> f (Stack (d, a)) -> f [(d, a)]
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Stack (d, a) -> [(d, a)]
forall a. Stack a -> [a]
integrate (f (Stack (d, a)) -> f [(d, a)])
-> (Stack (d, a) -> f (Stack (d, a))) -> Stack (d, a) -> f [(d, a)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((d, a) -> f (d, a)) -> Stack (d, a) -> f (Stack (d, a))
forall (m :: * -> *) a.
Monad m =>
(a -> m a) -> Stack a -> m (Stack a)
onFocusedM ((d, f a) -> f (d, a)
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
forall (m :: * -> *) a. Monad m => (d, m a) -> m (d, a)
sequence ((d, f a) -> f (d, a))
-> ((d, a) -> (d, f a)) -> (d, a) -> f (d, a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> f a) -> (d, a) -> (d, f a)
forall b c d. (b -> c) -> (d, b) -> (d, c)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second a -> f a
f)
    onFocusedOrPrevious' :: ((Ratio Integer, a) -> (Ratio Integer, a))
-> Stack (Ratio Integer, a) -> [(Ratio Integer, a)]
onFocusedOrPrevious' (Ratio Integer, a) -> (Ratio Integer, a)
f = [(Ratio Integer, a)] -> [(Ratio Integer, a)]
forall a. [(Ratio Integer, a)] -> [(Ratio Integer, a)]
sanitize ([(Ratio Integer, a)] -> [(Ratio Integer, a)])
-> (Stack (Ratio Integer, a) -> [(Ratio Integer, a)])
-> Stack (Ratio Integer, a)
-> [(Ratio Integer, a)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Stack (Ratio Integer, a) -> [(Ratio Integer, a)]
forall a. Stack a -> [a]
integrate (Stack (Ratio Integer, a) -> [(Ratio Integer, a)])
-> (Stack (Ratio Integer, a) -> Stack (Ratio Integer, a))
-> Stack (Ratio Integer, a)
-> [(Ratio Integer, a)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Ratio Integer, a) -> (Ratio Integer, a))
-> Stack (Ratio Integer, a) -> Stack (Ratio Integer, a)
forall a. (a -> a) -> Stack a -> Stack a
onFocusedOrPrevious (Ratio Integer, a) -> (Ratio Integer, a)
f

move :: Move -> Window -> Columns -> Maybe Columns
move :: Move -> Window -> Columns -> Maybe Columns
move Move
direction Window
window Columns
columns =
  case (Move
direction, Window
-> Columns
-> Maybe (Stack (Ratio Integer, [(Ratio Integer, Window)]))
findInColumns Window
window Columns
columns) of
    (Move
MoveRight, Just (Stack (Ratio Integer
_, [(Ratio Integer
_, Window
_)]) Columns
_ [])) -> Maybe Columns
forall a. Maybe a
Nothing
    (Move
MoveLeft, Just (Stack (Ratio Integer
_, [(Ratio Integer
_, Window
_)]) [] Columns
_)) -> Maybe Columns
forall a. Maybe a
Nothing
    (Move
MoveRight, Just (Stack column :: (Ratio Integer, [(Ratio Integer, Window)])
column@(Ratio Integer
_, [(Ratio Integer
_, Window
_)]) Columns
before ((Ratio Integer, [(Ratio Integer, Window)])
next : Columns
others))) ->
      let ((Ratio Integer, [(Ratio Integer, Window)])
column', (Ratio Integer, [(Ratio Integer, Window)])
next') = Window
-> (Ratio Integer, [(Ratio Integer, Window)])
-> (Ratio Integer, [(Ratio Integer, Window)])
-> ((Ratio Integer, [(Ratio Integer, Window)]),
    (Ratio Integer, [(Ratio Integer, Window)]))
swapWindowBetween Window
window (Ratio Integer, [(Ratio Integer, Window)])
column (Ratio Integer, [(Ratio Integer, Window)])
next
       in Columns -> Maybe Columns
forall a. a -> Maybe a
Just (Columns -> Maybe Columns)
-> (Stack (Ratio Integer, [(Ratio Integer, Window)]) -> Columns)
-> Stack (Ratio Integer, [(Ratio Integer, Window)])
-> Maybe Columns
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Stack (Ratio Integer, [(Ratio Integer, Window)]) -> Columns
forall a. Stack a -> [a]
integrate (Stack (Ratio Integer, [(Ratio Integer, Window)]) -> Maybe Columns)
-> Stack (Ratio Integer, [(Ratio Integer, Window)])
-> Maybe Columns
forall a b. (a -> b) -> a -> b
$ (Ratio Integer, [(Ratio Integer, Window)])
-> Columns
-> Columns
-> Stack (Ratio Integer, [(Ratio Integer, Window)])
forall a. a -> [a] -> [a] -> Stack a
Stack (Ratio Integer, [(Ratio Integer, Window)])
column' Columns
before ((Ratio Integer, [(Ratio Integer, Window)])
next' (Ratio Integer, [(Ratio Integer, Window)]) -> Columns -> Columns
forall a. a -> [a] -> [a]
: Columns
others)
    (Move
MoveLeft, Just (Stack column :: (Ratio Integer, [(Ratio Integer, Window)])
column@(Ratio Integer
_, [(Ratio Integer
_, Window
_)]) ((Ratio Integer, [(Ratio Integer, Window)])
previous : Columns
others) Columns
after)) ->
      let ((Ratio Integer, [(Ratio Integer, Window)])
column', (Ratio Integer, [(Ratio Integer, Window)])
previous') = Window
-> (Ratio Integer, [(Ratio Integer, Window)])
-> (Ratio Integer, [(Ratio Integer, Window)])
-> ((Ratio Integer, [(Ratio Integer, Window)]),
    (Ratio Integer, [(Ratio Integer, Window)]))
swapWindowBetween Window
window (Ratio Integer, [(Ratio Integer, Window)])
column (Ratio Integer, [(Ratio Integer, Window)])
previous
       in Columns -> Maybe Columns
forall a. a -> Maybe a
Just (Columns -> Maybe Columns)
-> (Stack (Ratio Integer, [(Ratio Integer, Window)]) -> Columns)
-> Stack (Ratio Integer, [(Ratio Integer, Window)])
-> Maybe Columns
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Stack (Ratio Integer, [(Ratio Integer, Window)]) -> Columns
forall a. Stack a -> [a]
integrate (Stack (Ratio Integer, [(Ratio Integer, Window)]) -> Maybe Columns)
-> Stack (Ratio Integer, [(Ratio Integer, Window)])
-> Maybe Columns
forall a b. (a -> b) -> a -> b
$ (Ratio Integer, [(Ratio Integer, Window)])
-> Columns
-> Columns
-> Stack (Ratio Integer, [(Ratio Integer, Window)])
forall a. a -> [a] -> [a] -> Stack a
Stack (Ratio Integer, [(Ratio Integer, Window)])
column' ((Ratio Integer, [(Ratio Integer, Window)])
previous' (Ratio Integer, [(Ratio Integer, Window)]) -> Columns -> Columns
forall a. a -> [a] -> [a]
: Columns
others) Columns
after
    (Move
MoveRight, Just Stack (Ratio Integer, [(Ratio Integer, Window)])
stack) ->
      let (Columns
newColumns', Stack (Ratio Integer, [(Ratio Integer, Window)])
column Columns
before Columns
after) = [[(Ratio Integer, Window)]]
-> Stack (Ratio Integer, [(Ratio Integer, Window)])
-> (Columns, Stack (Ratio Integer, [(Ratio Integer, Window)]))
forall (f :: * -> *) a.
(Functor f, Foldable f) =>
[a]
-> f (Ratio Integer, a)
-> ([(Ratio Integer, a)], f (Ratio Integer, a))
rationalize [[(Ratio Integer, Window)]]
newColumns Stack (Ratio Integer, [(Ratio Integer, Window)])
stack
          windows :: (Ratio Integer, [(Ratio Integer, Window)])
windows = Window
-> (Ratio Integer, [(Ratio Integer, Window)])
-> (Ratio Integer, [(Ratio Integer, Window)])
removeWindow Window
window (Ratio Integer, [(Ratio Integer, Window)])
column
       in Columns -> Maybe Columns
forall a. a -> Maybe a
Just (Columns -> Maybe Columns)
-> (Stack (Ratio Integer, [(Ratio Integer, Window)]) -> Columns)
-> Stack (Ratio Integer, [(Ratio Integer, Window)])
-> Maybe Columns
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Stack (Ratio Integer, [(Ratio Integer, Window)]) -> Columns
forall a. Stack a -> [a]
integrate (Stack (Ratio Integer, [(Ratio Integer, Window)]) -> Maybe Columns)
-> Stack (Ratio Integer, [(Ratio Integer, Window)])
-> Maybe Columns
forall a b. (a -> b) -> a -> b
$ (Ratio Integer, [(Ratio Integer, Window)])
-> Columns
-> Columns
-> Stack (Ratio Integer, [(Ratio Integer, Window)])
forall a. a -> [a] -> [a] -> Stack a
Stack (Ratio Integer, [(Ratio Integer, Window)])
windows Columns
before (Columns
newColumns' Columns -> Columns -> Columns
forall a. Semigroup a => a -> a -> a
<> Columns
after)
    (Move
MoveLeft, Just Stack (Ratio Integer, [(Ratio Integer, Window)])
stack) ->
      let (Columns
newColumns', Stack (Ratio Integer, [(Ratio Integer, Window)])
column Columns
before Columns
after) = [[(Ratio Integer, Window)]]
-> Stack (Ratio Integer, [(Ratio Integer, Window)])
-> (Columns, Stack (Ratio Integer, [(Ratio Integer, Window)]))
forall (f :: * -> *) a.
(Functor f, Foldable f) =>
[a]
-> f (Ratio Integer, a)
-> ([(Ratio Integer, a)], f (Ratio Integer, a))
rationalize [[(Ratio Integer, Window)]]
newColumns Stack (Ratio Integer, [(Ratio Integer, Window)])
stack
          windows :: (Ratio Integer, [(Ratio Integer, Window)])
windows = Window
-> (Ratio Integer, [(Ratio Integer, Window)])
-> (Ratio Integer, [(Ratio Integer, Window)])
removeWindow Window
window (Ratio Integer, [(Ratio Integer, Window)])
column
       in Columns -> Maybe Columns
forall a. a -> Maybe a
Just (Columns -> Maybe Columns)
-> (Stack (Ratio Integer, [(Ratio Integer, Window)]) -> Columns)
-> Stack (Ratio Integer, [(Ratio Integer, Window)])
-> Maybe Columns
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Stack (Ratio Integer, [(Ratio Integer, Window)]) -> Columns
forall a. Stack a -> [a]
integrate (Stack (Ratio Integer, [(Ratio Integer, Window)]) -> Maybe Columns)
-> Stack (Ratio Integer, [(Ratio Integer, Window)])
-> Maybe Columns
forall a b. (a -> b) -> a -> b
$ (Ratio Integer, [(Ratio Integer, Window)])
-> Columns
-> Columns
-> Stack (Ratio Integer, [(Ratio Integer, Window)])
forall a. a -> [a] -> [a] -> Stack a
Stack (Ratio Integer, [(Ratio Integer, Window)])
windows (Columns
newColumns' Columns -> Columns -> Columns
forall a. Semigroup a => a -> a -> a
<> Columns
before) Columns
after
    (Move
MoveUp, Just Stack (Ratio Integer, [(Ratio Integer, Window)])
stack) -> Stack (Ratio Integer, [(Ratio Integer, Window)]) -> Columns
forall a. Stack a -> [a]
integrate (Stack (Ratio Integer, [(Ratio Integer, Window)]) -> Columns)
-> Maybe (Stack (Ratio Integer, [(Ratio Integer, Window)]))
-> Maybe Columns
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((Ratio Integer, [(Ratio Integer, Window)])
 -> Maybe (Ratio Integer, [(Ratio Integer, Window)]))
-> Stack (Ratio Integer, [(Ratio Integer, Window)])
-> Maybe (Stack (Ratio Integer, [(Ratio Integer, Window)]))
forall (m :: * -> *) a.
Monad m =>
(a -> m a) -> Stack a -> m (Stack a)
onFocusedM (Window
-> (Ratio Integer, [(Ratio Integer, Window)])
-> Maybe (Ratio Integer, [(Ratio Integer, Window)])
swapWindowUp Window
window) Stack (Ratio Integer, [(Ratio Integer, Window)])
stack
    (Move
MoveDown, Just Stack (Ratio Integer, [(Ratio Integer, Window)])
stack) -> Stack (Ratio Integer, [(Ratio Integer, Window)]) -> Columns
forall a. Stack a -> [a]
integrate (Stack (Ratio Integer, [(Ratio Integer, Window)]) -> Columns)
-> Maybe (Stack (Ratio Integer, [(Ratio Integer, Window)]))
-> Maybe Columns
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((Ratio Integer, [(Ratio Integer, Window)])
 -> Maybe (Ratio Integer, [(Ratio Integer, Window)]))
-> Stack (Ratio Integer, [(Ratio Integer, Window)])
-> Maybe (Stack (Ratio Integer, [(Ratio Integer, Window)]))
forall (m :: * -> *) a.
Monad m =>
(a -> m a) -> Stack a -> m (Stack a)
onFocusedM (Window
-> (Ratio Integer, [(Ratio Integer, Window)])
-> Maybe (Ratio Integer, [(Ratio Integer, Window)])
swapWindowDown Window
window) Stack (Ratio Integer, [(Ratio Integer, Window)])
stack
    (Move, Maybe (Stack (Ratio Integer, [(Ratio Integer, Window)])))
_ -> Maybe Columns
forall a. Maybe a
Nothing
  where
    newColumns :: [[(Ratio Integer, Window)]]
newColumns = [[(Ratio Integer
1, Window
window)]]

mapWindow :: (Window -> Window) -> Columns -> Columns
mapWindow :: (Window -> Window) -> Columns -> Columns
mapWindow = ((Ratio Integer, [(Ratio Integer, Window)])
 -> (Ratio Integer, [(Ratio Integer, Window)]))
-> Columns -> Columns
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (((Ratio Integer, [(Ratio Integer, Window)])
  -> (Ratio Integer, [(Ratio Integer, Window)]))
 -> Columns -> Columns)
-> ((Window -> Window)
    -> (Ratio Integer, [(Ratio Integer, Window)])
    -> (Ratio Integer, [(Ratio Integer, Window)]))
-> (Window -> Window)
-> Columns
-> Columns
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([(Ratio Integer, Window)] -> [(Ratio Integer, Window)])
-> (Ratio Integer, [(Ratio Integer, Window)])
-> (Ratio Integer, [(Ratio Integer, Window)])
forall a b. (a -> b) -> (Ratio Integer, a) -> (Ratio Integer, b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (([(Ratio Integer, Window)] -> [(Ratio Integer, Window)])
 -> (Ratio Integer, [(Ratio Integer, Window)])
 -> (Ratio Integer, [(Ratio Integer, Window)]))
-> ((Window -> Window)
    -> [(Ratio Integer, Window)] -> [(Ratio Integer, Window)])
-> (Window -> Window)
-> (Ratio Integer, [(Ratio Integer, Window)])
-> (Ratio Integer, [(Ratio Integer, Window)])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Ratio Integer, Window) -> (Ratio Integer, Window))
-> [(Ratio Integer, Window)] -> [(Ratio Integer, Window)]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (((Ratio Integer, Window) -> (Ratio Integer, Window))
 -> [(Ratio Integer, Window)] -> [(Ratio Integer, Window)])
-> ((Window -> Window)
    -> (Ratio Integer, Window) -> (Ratio Integer, Window))
-> (Window -> Window)
-> [(Ratio Integer, Window)]
-> [(Ratio Integer, Window)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Window -> Window)
-> (Ratio Integer, Window) -> (Ratio Integer, Window)
forall a b. (a -> b) -> (Ratio Integer, a) -> (Ratio Integer, b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap

columnsToWindows :: Columns -> [Window]
columnsToWindows :: Columns -> [Window]
columnsToWindows = ((Ratio Integer, Window) -> [Window])
-> [(Ratio Integer, Window)] -> [Window]
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap ((Window -> [Window] -> [Window]
forall a. a -> [a] -> [a]
: []) (Window -> [Window])
-> ((Ratio Integer, Window) -> Window)
-> (Ratio Integer, Window)
-> [Window]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Ratio Integer, Window) -> Window
forall a b. (a, b) -> b
snd) ([(Ratio Integer, Window)] -> [Window])
-> (Columns -> [(Ratio Integer, Window)]) -> Columns -> [Window]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Ratio Integer, [(Ratio Integer, Window)])
 -> [(Ratio Integer, Window)])
-> Columns -> [(Ratio Integer, Window)]
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (Ratio Integer, [(Ratio Integer, Window)])
-> [(Ratio Integer, Window)]
forall a b. (a, b) -> b
snd

swapWindowBetween ::
  Window ->
  (Rational, Column) ->
  (Rational, Column) ->
  ((Rational, Column), (Rational, Column))
swapWindowBetween :: Window
-> (Ratio Integer, [(Ratio Integer, Window)])
-> (Ratio Integer, [(Ratio Integer, Window)])
-> ((Ratio Integer, [(Ratio Integer, Window)]),
    (Ratio Integer, [(Ratio Integer, Window)]))
swapWindowBetween Window
window (Ratio Integer, [(Ratio Integer, Window)])
from (Ratio Integer, [(Ratio Integer, Window)])
to = ((Ratio Integer, [(Ratio Integer, Window)])
removed, (Ratio Integer, [(Ratio Integer, Window)])
added)
  where
    removed :: (Ratio Integer, [(Ratio Integer, Window)])
removed = Window
-> (Ratio Integer, [(Ratio Integer, Window)])
-> (Ratio Integer, [(Ratio Integer, Window)])
removeWindow Window
window (Ratio Integer, [(Ratio Integer, Window)])
from
    added :: (Ratio Integer, [(Ratio Integer, Window)])
added = [Window]
-> (Ratio Integer, [(Ratio Integer, Window)])
-> (Ratio Integer, [(Ratio Integer, Window)])
appendWindows [Window
window] (Ratio Integer, [(Ratio Integer, Window)])
to

swapWindowUp :: Window -> (Rational, Column) -> Maybe (Rational, Column)
swapWindowUp :: Window
-> (Ratio Integer, [(Ratio Integer, Window)])
-> Maybe (Ratio Integer, [(Ratio Integer, Window)])
swapWindowUp Window
window (Ratio Integer
width, [(Ratio Integer, Window)]
column)
  | Just (Stack (Ratio Integer
height, Window
_) ((Ratio Integer, Window)
previous : [(Ratio Integer, Window)]
before') [(Ratio Integer, Window)]
after) <- Window
-> [(Ratio Integer, Window)]
-> Maybe (Stack (Ratio Integer, Window))
findInColumn Window
window [(Ratio Integer, Window)]
column =
      (Ratio Integer, [(Ratio Integer, Window)])
-> Maybe (Ratio Integer, [(Ratio Integer, Window)])
forall a. a -> Maybe a
Just (Ratio Integer
width, Stack (Ratio Integer, Window) -> [(Ratio Integer, Window)]
forall a. Stack a -> [a]
integrate (Stack (Ratio Integer, Window) -> [(Ratio Integer, Window)])
-> Stack (Ratio Integer, Window) -> [(Ratio Integer, Window)]
forall a b. (a -> b) -> a -> b
$ (Ratio Integer, Window)
-> [(Ratio Integer, Window)]
-> [(Ratio Integer, Window)]
-> Stack (Ratio Integer, Window)
forall a. a -> [a] -> [a] -> Stack a
Stack (Ratio Integer, Window)
previous ((Ratio Integer
height, Window
window) (Ratio Integer, Window)
-> [(Ratio Integer, Window)] -> [(Ratio Integer, Window)]
forall a. a -> [a] -> [a]
: [(Ratio Integer, Window)]
before') [(Ratio Integer, Window)]
after)
  | Bool
otherwise = Maybe (Ratio Integer, [(Ratio Integer, Window)])
forall a. Maybe a
Nothing

swapWindowDown :: Window -> (Rational, Column) -> Maybe (Rational, Column)
swapWindowDown :: Window
-> (Ratio Integer, [(Ratio Integer, Window)])
-> Maybe (Ratio Integer, [(Ratio Integer, Window)])
swapWindowDown Window
window (Ratio Integer
width, [(Ratio Integer, Window)]
column)
  | Just (Stack (Ratio Integer
height, Window
_) [(Ratio Integer, Window)]
before ((Ratio Integer, Window)
next : [(Ratio Integer, Window)]
others)) <- Window
-> [(Ratio Integer, Window)]
-> Maybe (Stack (Ratio Integer, Window))
findInColumn Window
window [(Ratio Integer, Window)]
column =
      (Ratio Integer, [(Ratio Integer, Window)])
-> Maybe (Ratio Integer, [(Ratio Integer, Window)])
forall a. a -> Maybe a
Just (Ratio Integer
width, Stack (Ratio Integer, Window) -> [(Ratio Integer, Window)]
forall a. Stack a -> [a]
integrate (Stack (Ratio Integer, Window) -> [(Ratio Integer, Window)])
-> Stack (Ratio Integer, Window) -> [(Ratio Integer, Window)]
forall a b. (a -> b) -> a -> b
$ (Ratio Integer, Window)
-> [(Ratio Integer, Window)]
-> [(Ratio Integer, Window)]
-> Stack (Ratio Integer, Window)
forall a. a -> [a] -> [a] -> Stack a
Stack (Ratio Integer, Window)
next [(Ratio Integer, Window)]
before ((Ratio Integer
height, Window
window) (Ratio Integer, Window)
-> [(Ratio Integer, Window)] -> [(Ratio Integer, Window)]
forall a. a -> [a] -> [a]
: [(Ratio Integer, Window)]
others))
  | Bool
otherwise = Maybe (Ratio Integer, [(Ratio Integer, Window)])
forall a. Maybe a
Nothing

-- | Adjust the ratio of a list or a stack of elts so that when adding new
--  elements:
-- - the new elements are distributed according to the total number of elements
-- - the existing elements keep their proportion in the remaining space
rationalize ::
  (Functor f, Foldable f) =>
  [a] ->
  f (Rational, a) ->
  ([(Rational, a)], f (Rational, a))
rationalize :: forall (f :: * -> *) a.
(Functor f, Foldable f) =>
[a]
-> f (Ratio Integer, a)
-> ([(Ratio Integer, a)], f (Ratio Integer, a))
rationalize [a]
new f (Ratio Integer, a)
existing = ([(Ratio Integer, a)]
new', f (Ratio Integer, a)
existing')
  where
    nbNew :: Integer
nbNew = Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Integer) -> Int -> Integer
forall a b. (a -> b) -> a -> b
$ [a] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
new
    nbInColumn :: Integer
nbInColumn = Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Integer) -> Int -> Integer
forall a b. (a -> b) -> a -> b
$ f (Ratio Integer, a) -> Int
forall a. f a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length f (Ratio Integer, a)
existing
    newRatio :: Ratio Integer
newRatio = Integer
nbNew Integer -> Integer -> Ratio Integer
forall a. Integral a => a -> a -> Ratio a
% (Integer
nbNew Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
nbInColumn)
    existingRatio :: Ratio Integer
existingRatio = Ratio Integer
1 Ratio Integer -> Ratio Integer -> Ratio Integer
forall a. Num a => a -> a -> a
- Ratio Integer
newRatio
    new' :: [(Ratio Integer, a)]
new' = Ratio Integer -> [a] -> [(Ratio Integer, a)]
forall a. Ratio Integer -> [a] -> [(Ratio Integer, a)]
fitElements Ratio Integer
newRatio [a]
new
    existing' :: f (Ratio Integer, a)
existing' = (Ratio Integer -> Ratio Integer)
-> (Ratio Integer, a) -> (Ratio Integer, a)
forall b c d. (b -> c) -> (b, d) -> (c, d)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (b, d) (c, d)
first (Ratio Integer -> Ratio Integer -> Ratio Integer
forall a. Num a => a -> a -> a
* Ratio Integer
existingRatio) ((Ratio Integer, a) -> (Ratio Integer, a))
-> f (Ratio Integer, a) -> f (Ratio Integer, a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f (Ratio Integer, a)
existing

append :: [a] -> [(Rational, a)] -> [(Rational, a)]
append :: forall a. [a] -> [(Ratio Integer, a)] -> [(Ratio Integer, a)]
append [a]
new [(Ratio Integer, a)]
existing = ([(Ratio Integer, a)]
 -> [(Ratio Integer, a)] -> [(Ratio Integer, a)])
-> ([(Ratio Integer, a)], [(Ratio Integer, a)])
-> [(Ratio Integer, a)]
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (([(Ratio Integer, a)]
 -> [(Ratio Integer, a)] -> [(Ratio Integer, a)])
-> [(Ratio Integer, a)]
-> [(Ratio Integer, a)]
-> [(Ratio Integer, a)]
forall a b c. (a -> b -> c) -> b -> a -> c
flip [(Ratio Integer, a)]
-> [(Ratio Integer, a)] -> [(Ratio Integer, a)]
forall a. Monoid a => a -> a -> a
mappend) ([a]
-> [(Ratio Integer, a)]
-> ([(Ratio Integer, a)], [(Ratio Integer, a)])
forall (f :: * -> *) a.
(Functor f, Foldable f) =>
[a]
-> f (Ratio Integer, a)
-> ([(Ratio Integer, a)], f (Ratio Integer, a))
rationalize [a]
new [(Ratio Integer, a)]
existing)

appendWindows ::
  [Window] ->
  (Rational, [(Rational, Window)]) ->
  (Rational, [(Rational, Window)])
appendWindows :: [Window]
-> (Ratio Integer, [(Ratio Integer, Window)])
-> (Ratio Integer, [(Ratio Integer, Window)])
appendWindows [Window]
windows = ([(Ratio Integer, Window)] -> [(Ratio Integer, Window)])
-> (Ratio Integer, [(Ratio Integer, Window)])
-> (Ratio Integer, [(Ratio Integer, Window)])
forall b c d. (b -> c) -> (d, b) -> (d, c)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second ([Window] -> [(Ratio Integer, Window)] -> [(Ratio Integer, Window)]
forall a. [a] -> [(Ratio Integer, a)] -> [(Ratio Integer, a)]
append [Window]
windows)

fitElements :: Rational -> [a] -> [(Rational, a)]
fitElements :: forall a. Ratio Integer -> [a] -> [(Ratio Integer, a)]
fitElements Ratio Integer
dimension [a]
elts = (Ratio Integer
dimension',) (a -> (Ratio Integer, a)) -> [a] -> [(Ratio Integer, a)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [a]
elts
  where
    dimension' :: Ratio Integer
dimension' = Ratio Integer
dimension Ratio Integer -> Ratio Integer -> Ratio Integer
forall a. Fractional a => a -> a -> a
/ Int -> Ratio Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral ([a] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
elts)

singleColumn :: Rational -> Rational -> [Window] -> Columns
singleColumn :: Ratio Integer -> Ratio Integer -> [Window] -> Columns
singleColumn Ratio Integer
width Ratio Integer
height [Window]
windows = [(Ratio Integer
width, Ratio Integer -> [Window] -> [(Ratio Integer, Window)]
forall a. Ratio Integer -> [a] -> [(Ratio Integer, a)]
fitElements Ratio Integer
height [Window]
windows)]

findElement' :: (a -> Bool) -> [(Rational, a)] -> Maybe (Stack (Rational, a))
findElement' :: forall a.
(a -> Bool)
-> [(Ratio Integer, a)] -> Maybe (Stack (Ratio Integer, a))
findElement' a -> Bool
predicate [(Ratio Integer, a)]
list
  | ([(Ratio Integer, a)]
before, (Ratio Integer, a)
c : [(Ratio Integer, a)]
after) <- ((Ratio Integer, a) -> Bool)
-> [(Ratio Integer, a)]
-> ([(Ratio Integer, a)], [(Ratio Integer, a)])
forall a. (a -> Bool) -> [a] -> ([a], [a])
break (a -> Bool
predicate (a -> Bool)
-> ((Ratio Integer, a) -> a) -> (Ratio Integer, a) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Ratio Integer, a) -> a
forall a b. (a, b) -> b
snd) [(Ratio Integer, a)]
list =
      Stack (Ratio Integer, a) -> Maybe (Stack (Ratio Integer, a))
forall a. a -> Maybe a
Just (Stack (Ratio Integer, a) -> Maybe (Stack (Ratio Integer, a)))
-> Stack (Ratio Integer, a) -> Maybe (Stack (Ratio Integer, a))
forall a b. (a -> b) -> a -> b
$ (Ratio Integer, a)
-> [(Ratio Integer, a)]
-> [(Ratio Integer, a)]
-> Stack (Ratio Integer, a)
forall a. a -> [a] -> [a] -> Stack a
Stack (Ratio Integer, a)
c ([(Ratio Integer, a)] -> [(Ratio Integer, a)]
forall a. [a] -> [a]
reverse [(Ratio Integer, a)]
before) [(Ratio Integer, a)]
after
  | Bool
otherwise = Maybe (Stack (Ratio Integer, a))
forall a. Maybe a
Nothing

findInColumns :: Window -> Columns -> Maybe (Stack (Rational, Column))
findInColumns :: Window
-> Columns
-> Maybe (Stack (Ratio Integer, [(Ratio Integer, Window)]))
findInColumns Window
window = ([(Ratio Integer, Window)] -> Bool)
-> Columns
-> Maybe (Stack (Ratio Integer, [(Ratio Integer, Window)]))
forall a.
(a -> Bool)
-> [(Ratio Integer, a)] -> Maybe (Stack (Ratio Integer, a))
findElement' (((Ratio Integer, Window) -> Bool)
-> [(Ratio Integer, Window)] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any ((Window -> Window -> Bool
forall a. Eq a => a -> a -> Bool
== Window
window) (Window -> Bool)
-> ((Ratio Integer, Window) -> Window)
-> (Ratio Integer, Window)
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Ratio Integer, Window) -> Window
forall a b. (a, b) -> b
snd))

findInColumn :: Window -> Column -> Maybe (Stack (Rational, Window))
findInColumn :: Window
-> [(Ratio Integer, Window)]
-> Maybe (Stack (Ratio Integer, Window))
findInColumn Window
window = (Window -> Bool)
-> [(Ratio Integer, Window)]
-> Maybe (Stack (Ratio Integer, Window))
forall a.
(a -> Bool)
-> [(Ratio Integer, a)] -> Maybe (Stack (Ratio Integer, a))
findElement' (Window -> Window -> Bool
forall a. Eq a => a -> a -> Bool
== Window
window)

removeWindows :: [Window] -> Columns -> Columns
removeWindows :: [Window] -> Columns -> Columns
removeWindows [Window]
windows = Columns -> Columns
forall {a}. [(Ratio Integer, [a])] -> [(Ratio Integer, [a])]
removeEmptyColumns (Columns -> Columns) -> (Columns -> Columns) -> Columns -> Columns
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Ratio Integer, [(Ratio Integer, Window)])
 -> (Ratio Integer, [(Ratio Integer, Window)]))
-> Columns -> Columns
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (([(Ratio Integer, Window)] -> [(Ratio Integer, Window)])
-> (Ratio Integer, [(Ratio Integer, Window)])
-> (Ratio Integer, [(Ratio Integer, Window)])
forall b c d. (b -> c) -> (d, b) -> (d, c)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second [(Ratio Integer, Window)] -> [(Ratio Integer, Window)]
removeWindows')
  where
    inWindows :: (a, Window) -> Bool
inWindows (a
_, Window
window) = Window
window Window -> [Window] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [Window]
windows
    removeWindows' :: [(Ratio Integer, Window)] -> [(Ratio Integer, Window)]
removeWindows' = [(Ratio Integer, Window)] -> [(Ratio Integer, Window)]
forall a. [(Ratio Integer, a)] -> [(Ratio Integer, a)]
normalize ([(Ratio Integer, Window)] -> [(Ratio Integer, Window)])
-> ([(Ratio Integer, Window)] -> [(Ratio Integer, Window)])
-> [(Ratio Integer, Window)]
-> [(Ratio Integer, Window)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Ratio Integer, Window) -> Bool)
-> [(Ratio Integer, Window)] -> [(Ratio Integer, Window)]
forall a. (a -> Bool) -> [a] -> [a]
filter (Ratio Integer, Window) -> Bool
forall {a}. (a, Window) -> Bool
inWindows
    removeEmptyColumns :: [(Ratio Integer, [a])] -> [(Ratio Integer, [a])]
removeEmptyColumns = [(Ratio Integer, [a])] -> [(Ratio Integer, [a])]
forall a. [(Ratio Integer, a)] -> [(Ratio Integer, a)]
normalize ([(Ratio Integer, [a])] -> [(Ratio Integer, [a])])
-> ([(Ratio Integer, [a])] -> [(Ratio Integer, [a])])
-> [(Ratio Integer, [a])]
-> [(Ratio Integer, [a])]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Ratio Integer, [a]) -> Bool)
-> [(Ratio Integer, [a])] -> [(Ratio Integer, [a])]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool)
-> ((Ratio Integer, [a]) -> Bool) -> (Ratio Integer, [a]) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null ([a] -> Bool)
-> ((Ratio Integer, [a]) -> [a]) -> (Ratio Integer, [a]) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Ratio Integer, [a]) -> [a]
forall a b. (a, b) -> b
snd)

removeWindow :: Window -> (Rational, Column) -> (Rational, Column)
removeWindow :: Window
-> (Ratio Integer, [(Ratio Integer, Window)])
-> (Ratio Integer, [(Ratio Integer, Window)])
removeWindow Window
window = ([(Ratio Integer, Window)] -> [(Ratio Integer, Window)])
-> (Ratio Integer, [(Ratio Integer, Window)])
-> (Ratio Integer, [(Ratio Integer, Window)])
forall b c d. (b -> c) -> (d, b) -> (d, c)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second ([(Ratio Integer, Window)] -> [(Ratio Integer, Window)]
forall a. [(Ratio Integer, a)] -> [(Ratio Integer, a)]
normalize ([(Ratio Integer, Window)] -> [(Ratio Integer, Window)])
-> ([(Ratio Integer, Window)] -> [(Ratio Integer, Window)])
-> [(Ratio Integer, Window)]
-> [(Ratio Integer, Window)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Ratio Integer, Window) -> Bool)
-> [(Ratio Integer, Window)] -> [(Ratio Integer, Window)]
forall a. (a -> Bool) -> [a] -> [a]
filter ((Window -> Window -> Bool
forall a. Eq a => a -> a -> Bool
/= Window
window) (Window -> Bool)
-> ((Ratio Integer, Window) -> Window)
-> (Ratio Integer, Window)
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Ratio Integer, Window) -> Window
forall a b. (a, b) -> b
snd))

addWindows :: [Window] -> Columns -> Columns
addWindows :: [Window] -> Columns -> Columns
addWindows [] Columns
columns = Columns
columns
-- When there is only one column, create a new one on the right
addWindows [Window]
windows [(Ratio Integer
_, [(Ratio Integer, Window)]
windows')] = (Integer
1 Integer -> Integer -> Ratio Integer
forall a. Integral a => a -> a -> Ratio a
% Integer
2, [(Ratio Integer, Window)]
windows') (Ratio Integer, [(Ratio Integer, Window)]) -> Columns -> Columns
forall a. a -> [a] -> [a]
: Ratio Integer -> Ratio Integer -> [Window] -> Columns
singleColumn (Integer
1 Integer -> Integer -> Ratio Integer
forall a. Integral a => a -> a -> Ratio a
% Integer
2) Ratio Integer
1 [Window]
windows
-- When there is more, append the windows to the last column
addWindows [Window]
windows Columns
columns
  | Just (Columns
columns', (Ratio Integer, [(Ratio Integer, Window)])
column) <- Columns
-> Maybe (Columns, (Ratio Integer, [(Ratio Integer, Window)]))
forall a. [a] -> Maybe ([a], a)
unsnoc Columns
columns =
      Columns -> Columns
sanitizeColumns (Columns -> Columns) -> Columns -> Columns
forall a b. (a -> b) -> a -> b
$ Columns
columns' Columns -> Columns -> Columns
forall a. Semigroup a => a -> a -> a
<> [[Window]
-> (Ratio Integer, [(Ratio Integer, Window)])
-> (Ratio Integer, [(Ratio Integer, Window)])
appendWindows [Window]
windows (Ratio Integer, [(Ratio Integer, Window)])
column]
  | Bool
otherwise = Ratio Integer -> Ratio Integer -> [Window] -> Columns
singleColumn Ratio Integer
1 Ratio Integer
1 [Window]
windows

-- | Make sure the sum of all dimensions is 1
normalize :: [(Rational, a)] -> [(Rational, a)]
normalize :: forall a. [(Ratio Integer, a)] -> [(Ratio Integer, a)]
normalize [(Ratio Integer, a)]
elts = ((Ratio Integer, a) -> (Ratio Integer, a))
-> [(Ratio Integer, a)] -> [(Ratio Integer, a)]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Ratio Integer -> Ratio Integer)
-> (Ratio Integer, a) -> (Ratio Integer, a)
forall b c d. (b -> c) -> (b, d) -> (c, d)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (b, d) (c, d)
first (Ratio Integer -> Ratio Integer -> Ratio Integer
forall a. Fractional a => a -> a -> a
/ Ratio Integer
total)) [(Ratio Integer, a)]
elts
  where
    total :: Ratio Integer
total = [Ratio Integer] -> Ratio Integer
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum ((Ratio Integer, a) -> Ratio Integer
forall a b. (a, b) -> a
fst ((Ratio Integer, a) -> Ratio Integer)
-> [(Ratio Integer, a)] -> [Ratio Integer]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [(Ratio Integer, a)]
elts)

-- | Update the last dimension so that the sum of all dimensions is 1
sanitize :: [(Rational, a)] -> [(Rational, a)]
sanitize :: forall a. [(Ratio Integer, a)] -> [(Ratio Integer, a)]
sanitize [(Ratio Integer, a)]
list
  | Just ([(Ratio Integer, a)]
elts, (Ratio Integer
_, a
a)) <- [(Ratio Integer, a)]
-> Maybe ([(Ratio Integer, a)], (Ratio Integer, a))
forall a. [a] -> Maybe ([a], a)
unsnoc [(Ratio Integer, a)]
list = [(Ratio Integer, a)]
elts [(Ratio Integer, a)]
-> [(Ratio Integer, a)] -> [(Ratio Integer, a)]
forall a. Semigroup a => a -> a -> a
<> [(Ratio Integer
1 Ratio Integer -> Ratio Integer -> Ratio Integer
forall a. Num a => a -> a -> a
- [Ratio Integer] -> Ratio Integer
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum ((Ratio Integer, a) -> Ratio Integer
forall a b. (a, b) -> a
fst ((Ratio Integer, a) -> Ratio Integer)
-> [(Ratio Integer, a)] -> [Ratio Integer]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [(Ratio Integer, a)]
elts), a
a)]
  | Bool
otherwise = []

-- | Same on the whole layout
sanitizeColumns :: Columns -> Columns
sanitizeColumns :: Columns -> Columns
sanitizeColumns = Columns -> Columns
forall a. [(Ratio Integer, a)] -> [(Ratio Integer, a)]
sanitize (Columns -> Columns) -> (Columns -> Columns) -> Columns -> Columns
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Ratio Integer, [(Ratio Integer, Window)])
 -> (Ratio Integer, [(Ratio Integer, Window)]))
-> Columns -> Columns
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (([(Ratio Integer, Window)] -> [(Ratio Integer, Window)])
-> (Ratio Integer, [(Ratio Integer, Window)])
-> (Ratio Integer, [(Ratio Integer, Window)])
forall b c d. (b -> c) -> (d, b) -> (d, c)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second [(Ratio Integer, Window)] -> [(Ratio Integer, Window)]
forall a. [(Ratio Integer, a)] -> [(Ratio Integer, a)]
sanitize)

toOffsetRatio :: [(Rational, a)] -> [(Rational, Rational, a)]
toOffsetRatio :: forall a.
[(Ratio Integer, a)] -> [(Ratio Integer, Ratio Integer, a)]
toOffsetRatio [(Ratio Integer, a)]
ra = ((Ratio Integer, a)
 -> Ratio Integer -> (Ratio Integer, Ratio Integer, a))
-> [(Ratio Integer, a)]
-> [Ratio Integer]
-> [(Ratio Integer, Ratio Integer, a)]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (Ratio Integer, a)
-> Ratio Integer -> (Ratio Integer, Ratio Integer, a)
forall {b} {c} {a}. (b, c) -> a -> (a, b, c)
toTruple [(Ratio Integer, a)]
ra [Ratio Integer]
positions
  where
    toTruple :: (b, c) -> a -> (a, b, c)
toTruple (b
dimension, c
a) a
position = (a
position, b
dimension, c
a)
    positions :: [Ratio Integer]
positions = (Ratio Integer -> (Ratio Integer, a) -> Ratio Integer)
-> Ratio Integer -> [(Ratio Integer, a)] -> [Ratio Integer]
forall b a. (b -> a -> b) -> b -> [a] -> [b]
scanl' (\Ratio Integer
position (Ratio Integer
dimension, a
_) -> Ratio Integer
position Ratio Integer -> Ratio Integer -> Ratio Integer
forall a. Num a => a -> a -> a
+ Ratio Integer
dimension) Ratio Integer
0 [(Ratio Integer, a)]
ra

unsnoc :: [a] -> Maybe ([a], a)
unsnoc :: forall a. [a] -> Maybe ([a], a)
unsnoc [] = Maybe ([a], a)
forall a. Maybe a
Nothing
unsnoc (a
x : [a]
xs)
  | Just ([a]
is, a
l) <- [a] -> Maybe ([a], a)
forall a. [a] -> Maybe ([a], a)
unsnoc [a]
xs = ([a], a) -> Maybe ([a], a)
forall a. a -> Maybe a
Just (a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
is, a
l)
  | Bool
otherwise = ([a], a) -> Maybe ([a], a)
forall a. a -> Maybe a
Just ([], a
x)