{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RecordWildCards #-}
-----------------------------------------------------------------------------
-- |
-- Module      :  XMonad.Layout.CircleEx
-- Description :  An elliptical, overlapping layout—extended version.
-- Copyright   :  (c) Peter De Wachter, Ilya V. Portnov
-- License     :  BSD-style (see LICENSE)
--
-- Maintainer  :  Ilya V. Portnov <portnov84@rambler.ru>
-- Stability   :  unstable
-- Portability :  unportable
--
-- Circle is an elliptical, overlapping layout. Original code by Peter De Wachter,
-- extended by Ilya Porntov.
-----------------------------------------------------------------------------

module XMonad.Layout.CircleEx (
    -- * Usage
    -- $usage
    CircleEx (..), circle, circleEx,
    CircleExMsg (..)
  )
  where

import Data.Ratio

import XMonad
import XMonad.StackSet (Stack)
import XMonad.Prelude
import qualified XMonad.StackSet as W

-- $usage
--
-- The layout puts the first N windows (called master) into the center of
-- screen. All others (called secondary, or stack) are organized in a circle
-- (well, ellipse). When opening a new secondary window, its size will be
-- slightly smaller than that of its predecessor (this is configurable). If
-- the number of master windows is set to zero, all windows will be arranged
-- in a circle. If there is more than one master window, they will be stacked
-- in the center on top of each other. The size of each additional master
-- window will again be slightly smaller than that of the former.
--
-- Since a picture says more than a thousand words, you see one
-- <https://github.com/xmonad/xmonad-contrib/assets/50166980/90ef1273-5201-4380-8b94-9e62d3c98e1c here>.
--
-- You can use this module with the following in your @xmonad.hs@:
--
-- > import XMonad.Layout.CircleEx
--
-- Then edit your @layoutHook@ by adding the 'CircleEx' layout:
--
-- > myCircle = circleEx {cDelta = -3*pi/4}
-- > myLayout = myCircle ||| Full ||| etc..
-- > main = xmonad def { layoutHook = myLayout }
--
-- This layout understands standard messages:
--
-- * 'IncMasterN': increase or decrease the number of master windows.
-- * 'Shrink' and 'Expand': change the size of master windows.
--
-- More layout-specific messages are also supported, see 'CircleExMsg' below.
--
-- For more detailed instructions on editing the layoutHook see:
-- "XMonad.Doc.Extending#Editing_the_layout_hook"

-- | The layout data type. It is recommended to not use the 'CircleEx' data
-- constructor directly, and instead rely on record update syntax; for
-- example: @circleEx {cMasterRatio = 4%5}@. In this way you can avoid nasty
-- surprises if one day additional fields are added to @CircleEx@.
data CircleEx a = CircleEx
    { forall a. CircleEx a -> Int
cNMaster :: !Int          -- ^ Number of master windows. Default value is 1.
    , forall a. CircleEx a -> Rational
cMasterRatio :: !Rational -- ^ Size of master window in relation to screen size.
                                --   Default value is @4%5@.
    , forall a. CircleEx a -> Rational
cStackRatio :: !Rational  -- ^ Size of first secondary window in relation to screen size.
                                --   Default value is @3%5@.
    , forall a. CircleEx a -> Rational
cMultiplier :: !Rational  -- ^ Coefficient used to calculate the sizes of subsequent secondary
                                --   windows. The size of the next window is calculated as the
                                --   size of the previous one multiplied by this value.
                                --   This value is also used to scale master windows, in case
                                --   there is more than one.
                                --   Default value is @5%6@. Set this to 1 if you want all secondary
                                --   windows to have the same size.
    , forall a. CircleEx a -> Double
cDelta :: !Double         -- ^ Angle of rotation of the whole circle layout. Usual values
                                --   are from 0 to 2π, although it will work outside
                                --   this range as well. Default value of 0 means that the first
                                --   secondary window will be placed at the right side of screen.
    } deriving (CircleEx a -> CircleEx a -> Bool
forall a. CircleEx a -> CircleEx a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CircleEx a -> CircleEx a -> Bool
$c/= :: forall a. CircleEx a -> CircleEx a -> Bool
== :: CircleEx a -> CircleEx a -> Bool
$c== :: forall a. CircleEx a -> CircleEx a -> Bool
Eq, Int -> CircleEx a -> ShowS
forall a. Int -> CircleEx a -> ShowS
forall a. [CircleEx a] -> ShowS
forall a. CircleEx a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CircleEx a] -> ShowS
$cshowList :: forall a. [CircleEx a] -> ShowS
show :: CircleEx a -> String
$cshow :: forall a. CircleEx a -> String
showsPrec :: Int -> CircleEx a -> ShowS
$cshowsPrec :: forall a. Int -> CircleEx a -> ShowS
Show, ReadPrec [CircleEx a]
ReadPrec (CircleEx a)
ReadS [CircleEx a]
forall a. ReadPrec [CircleEx a]
forall a. ReadPrec (CircleEx a)
forall a. Int -> ReadS (CircleEx a)
forall a. ReadS [CircleEx a]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [CircleEx a]
$creadListPrec :: forall a. ReadPrec [CircleEx a]
readPrec :: ReadPrec (CircleEx a)
$creadPrec :: forall a. ReadPrec (CircleEx a)
readList :: ReadS [CircleEx a]
$creadList :: forall a. ReadS [CircleEx a]
readsPrec :: Int -> ReadS (CircleEx a)
$creadsPrec :: forall a. Int -> ReadS (CircleEx a)
Read)

-- | Circle layout with default settings:
--
-- * Number of master windows is set to 1
-- * @cMasterRatio@ is set to @70/99@, which is nearly @1/sqrt(2)@
-- * @cStackRatio@ is set to @2/5@
-- * @cMultiplier@ is set to 1, which means all secondary windows
--   will have the same size
--
-- This can be used as a drop-in replacement for "XMonad.Layout.Circle".
circle :: CircleEx a
circle :: forall a. CircleEx a
circle = forall a.
Int -> Rational -> Rational -> Rational -> Double -> CircleEx a
CircleEx Int
1 (Integer
70forall a. Integral a => a -> a -> Ratio a
%Integer
99) (Integer
2forall a. Integral a => a -> a -> Ratio a
%Integer
5) Rational
1 Double
0

-- | Another variant of default settings for circle layout:
--
-- * Number of master windows is set to 1
-- * @cMasterRatio@ is set to @4/5@
-- * @cStackRatio@ is set to @3/5@
-- * @cMultiplier@ is set to @5/6@
--
circleEx :: CircleEx a
circleEx :: forall a. CircleEx a
circleEx = forall a.
Int -> Rational -> Rational -> Rational -> Double -> CircleEx a
CircleEx Int
1 (Integer
4forall a. Integral a => a -> a -> Ratio a
%Integer
5) (Integer
3forall a. Integral a => a -> a -> Ratio a
%Integer
5) (Integer
5forall a. Integral a => a -> a -> Ratio a
%Integer
6) Double
0

-- | Specific messages understood by CircleEx layout.
data CircleExMsg
  = Rotate !Double            -- ^ Rotate secondary windows by specific angle
  | IncStackRatio !Rational   -- ^ Increase (or decrease, with negative value) sizes of secondary windows
  | IncMultiplier !Rational   -- ^ Increase 'cMultiplier'.
  deriving (CircleExMsg -> CircleExMsg -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CircleExMsg -> CircleExMsg -> Bool
$c/= :: CircleExMsg -> CircleExMsg -> Bool
== :: CircleExMsg -> CircleExMsg -> Bool
$c== :: CircleExMsg -> CircleExMsg -> Bool
Eq, Int -> CircleExMsg -> ShowS
[CircleExMsg] -> ShowS
CircleExMsg -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CircleExMsg] -> ShowS
$cshowList :: [CircleExMsg] -> ShowS
show :: CircleExMsg -> String
$cshow :: CircleExMsg -> String
showsPrec :: Int -> CircleExMsg -> ShowS
$cshowsPrec :: Int -> CircleExMsg -> ShowS
Show, Typeable)

instance Message CircleExMsg

instance LayoutClass CircleEx Window where
  doLayout :: CircleEx Window -> Rectangle -> Stack Window -> X ([(Window, Rectangle)], Maybe (CircleEx Window))
  doLayout :: CircleEx Window
-> Rectangle
-> Stack Window
-> X ([(Window, Rectangle)], Maybe (CircleEx Window))
doLayout CircleEx Window
layout Rectangle
rect Stack Window
stack = do
    [(Window, Rectangle)]
result <- [(Window, Rectangle)] -> X [(Window, Rectangle)]
raiseFocus forall a b. (a -> b) -> a -> b
$ forall a. CircleEx a -> Rectangle -> [a] -> [(a, Rectangle)]
circleLayout CircleEx Window
layout Rectangle
rect forall a b. (a -> b) -> a -> b
$ forall a. Stack a -> [a]
W.integrate Stack Window
stack
    forall (m :: * -> *) a. Monad m => a -> m a
return ([(Window, Rectangle)]
result, forall a. Maybe a
Nothing)

  pureMessage :: CircleEx Window -> SomeMessage -> Maybe (CircleEx Window)
  pureMessage :: CircleEx Window -> SomeMessage -> Maybe (CircleEx Window)
pureMessage CircleEx Window
layout SomeMessage
m =
      forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, MonadPlus m) =>
t (m a) -> m a
msum [forall a. IncMasterN -> CircleEx a
changeMasterN forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall m. Message m => SomeMessage -> Maybe m
fromMessage SomeMessage
m,
            forall a. Resize -> CircleEx a
resize forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall m. Message m => SomeMessage -> Maybe m
fromMessage SomeMessage
m,
            forall a. CircleExMsg -> CircleEx a
specific forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall m. Message m => SomeMessage -> Maybe m
fromMessage SomeMessage
m]
    where
      deltaSize :: Rational
deltaSize = Integer
11 forall a. Integral a => a -> a -> Ratio a
% Integer
10

      resize :: Resize -> CircleEx a
      resize :: forall a. Resize -> CircleEx a
resize Resize
Shrink = CircleEx Window
layout {cMasterRatio :: Rational
cMasterRatio = forall a. Ord a => a -> a -> a
max Rational
0.1 forall a b. (a -> b) -> a -> b
$ forall a. Ord a => a -> a -> a
min Rational
1.0 forall a b. (a -> b) -> a -> b
$ forall a. CircleEx a -> Rational
cMasterRatio CircleEx Window
layout forall a. Fractional a => a -> a -> a
/ Rational
deltaSize}
      resize Resize
Expand = CircleEx Window
layout {cMasterRatio :: Rational
cMasterRatio = forall a. Ord a => a -> a -> a
max Rational
0.1 forall a b. (a -> b) -> a -> b
$ forall a. Ord a => a -> a -> a
min Rational
1.0 forall a b. (a -> b) -> a -> b
$ forall a. CircleEx a -> Rational
cMasterRatio CircleEx Window
layout forall a. Num a => a -> a -> a
* Rational
deltaSize}

      changeMasterN :: IncMasterN -> CircleEx a
      changeMasterN :: forall a. IncMasterN -> CircleEx a
changeMasterN (IncMasterN Int
d) = CircleEx Window
layout {cNMaster :: Int
cNMaster = forall a. Ord a => a -> a -> a
max Int
0 (forall a. CircleEx a -> Int
cNMaster CircleEx Window
layout forall a. Num a => a -> a -> a
+ Int
d)}

      specific :: CircleExMsg -> CircleEx a
      specific :: forall a. CircleExMsg -> CircleEx a
specific (Rotate Double
delta) = CircleEx Window
layout {cDelta :: Double
cDelta = Double
delta forall a. Num a => a -> a -> a
+ forall a. CircleEx a -> Double
cDelta CircleEx Window
layout}
      specific (IncStackRatio Rational
delta) = CircleEx Window
layout {cStackRatio :: Rational
cStackRatio = forall a. Ord a => a -> a -> a
max Rational
0.1 forall a b. (a -> b) -> a -> b
$ forall a. Ord a => a -> a -> a
min Rational
2.0 forall a b. (a -> b) -> a -> b
$ Rational
delta forall a. Num a => a -> a -> a
+ forall a. CircleEx a -> Rational
cStackRatio CircleEx Window
layout}
      specific (IncMultiplier Rational
delta) = CircleEx Window
layout {cMultiplier :: Rational
cMultiplier = forall a. Ord a => a -> a -> a
max Rational
0.1 forall a b. (a -> b) -> a -> b
$ forall a. Ord a => a -> a -> a
min Rational
2.0 forall a b. (a -> b) -> a -> b
$ Rational
delta forall a. Num a => a -> a -> a
+ forall a. CircleEx a -> Rational
cMultiplier CircleEx Window
layout}

circleLayout :: CircleEx a -> Rectangle -> [a] -> [(a, Rectangle)]
circleLayout :: forall a. CircleEx a -> Rectangle -> [a] -> [(a, Rectangle)]
circleLayout CircleEx a
_ Rectangle
_ [] = []
circleLayout (CircleEx {Double
Int
Rational
cDelta :: Double
cMultiplier :: Rational
cStackRatio :: Rational
cMasterRatio :: Rational
cNMaster :: Int
cDelta :: forall a. CircleEx a -> Double
cMultiplier :: forall a. CircleEx a -> Rational
cStackRatio :: forall a. CircleEx a -> Rational
cMasterRatio :: forall a. CircleEx a -> Rational
cNMaster :: forall a. CircleEx a -> Int
..}) Rectangle
rectangle [a]
wins =
    forall a. [a] -> [(a, Rectangle)]
master (forall a. Int -> [a] -> [a]
take Int
cNMaster [a]
wins) forall a. [a] -> [a] -> [a]
++ forall a. [a] -> [(a, Rectangle)]
rest (forall a. Int -> [a] -> [a]
drop Int
cNMaster [a]
wins)
  where
    master :: [a] -> [(a, Rectangle)]
    master :: forall a. [a] -> [(a, Rectangle)]
master [a]
ws = forall a b. [a] -> [b] -> [(a, b)]
zip [a]
ws forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (Rational -> Rational -> Rectangle -> Int -> Rectangle
placeCenter Rational
cMasterRatio Rational
cMultiplier Rectangle
rectangle)
                           [Int
cNMasterforall a. Num a => a -> a -> a
-Int
1, Int
cNMasterforall a. Num a => a -> a -> a
-Int
2 .. Int
0]
    rest :: [a] -> [(a, Rectangle)]
    rest :: forall a. [a] -> [(a, Rectangle)]
rest [a]
ws = forall a b. [a] -> [b] -> [(a, b)]
zip [a]
ws forall a b. (a -> b) -> a -> b
$ forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (Rational -> Rational -> Rectangle -> Double -> Int -> Rectangle
placeSatellite Rational
cStackRatio Rational
cMultiplier Rectangle
rectangle)
                        (forall a b. (a -> b) -> [a] -> [b]
map (forall a. Num a => a -> a -> a
+ Double
cDelta) [Double
0, forall a. Floating a => a
piforall a. Num a => a -> a -> a
*Double
2 forall a. Fractional a => a -> a -> a
/ forall a b. (Integral a, Num b) => a -> b
fromIntegral (forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
ws) ..])
                        [Int
0 ..]


raiseFocus :: [(Window, Rectangle)] -> X [(Window, Rectangle)]
raiseFocus :: [(Window, Rectangle)] -> X [(Window, Rectangle)]
raiseFocus [(Window, Rectangle)]
wrs = do
  Maybe Window
focused <- forall a. (WindowSet -> X a) -> X a
withWindowSet (forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall i l a s sd. StackSet i l a s sd -> Maybe a
W.peek)
  forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find ((forall a. Eq a => a -> a -> Bool
== Maybe Window
focused) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> a
fst) [(Window, Rectangle)]
wrs of
             Just (Window, Rectangle)
x  -> (Window, Rectangle)
x forall a. a -> [a] -> [a]
: forall a. Eq a => a -> [a] -> [a]
delete (Window, Rectangle)
x [(Window, Rectangle)]
wrs
             Maybe (Window, Rectangle)
Nothing -> [(Window, Rectangle)]
wrs

placeCenter :: Rational -> Rational -> Rectangle -> Int -> Rectangle
placeCenter :: Rational -> Rational -> Rectangle -> Int -> Rectangle
placeCenter Rational
ratio Rational
multiplier (Rectangle Position
x Position
y Dimension
width Dimension
height) Int
n = Position -> Position -> Dimension -> Dimension -> Rectangle
Rectangle Position
x' Position
y' Dimension
width' Dimension
height'
  where
    m :: Rational
m = Rational
ratio forall a. Num a => a -> a -> a
* Rational
multiplier forall a b. (Num a, Integral b) => a -> b -> a
^ Int
n
    width' :: Dimension
width' = forall a b. (RealFrac a, Integral b) => a -> b
round (Rational
m forall a. Num a => a -> a -> a
* forall a b. (Integral a, Num b) => a -> b
fromIntegral Dimension
width)
    height' :: Dimension
height' = forall a b. (RealFrac a, Integral b) => a -> b
round (Rational
m forall a. Num a => a -> a -> a
* forall a b. (Integral a, Num b) => a -> b
fromIntegral Dimension
height)
    x' :: Position
x' = Position
x forall a. Num a => a -> a -> a
+ forall a b. (Integral a, Num b) => a -> b
fromIntegral (Dimension
width forall a. Num a => a -> a -> a
- Dimension
width') forall a. Integral a => a -> a -> a
`div` Position
2
    y' :: Position
y' = Position
y forall a. Num a => a -> a -> a
+ forall a b. (Integral a, Num b) => a -> b
fromIntegral (Dimension
height forall a. Num a => a -> a -> a
- Dimension
height') forall a. Integral a => a -> a -> a
`div` Position
2

placeSatellite :: Rational -> Rational -> Rectangle -> Double -> Int -> Rectangle
placeSatellite :: Rational -> Rational -> Rectangle -> Double -> Int -> Rectangle
placeSatellite Rational
ratio Rational
multiplier (Rectangle Position
x Position
y Dimension
width Dimension
height) Double
alpha Int
n =
    Position -> Position -> Dimension -> Dimension -> Rectangle
Rectangle Position
x' Position
y' Dimension
width' Dimension
height'
  where
    m :: Rational
m = Rational
ratio forall a. Num a => a -> a -> a
* Rational
multiplier forall a b. (Num a, Integral b) => a -> b -> a
^ Int
n
    x' :: Position
x' = Position
x forall a. Num a => a -> a -> a
+ forall a b. (RealFrac a, Integral b) => a -> b
round (Double
rx forall a. Num a => a -> a -> a
+ Double
rx forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
cos Double
alpha)
    y' :: Position
y' = Position
y forall a. Num a => a -> a -> a
+ forall a b. (RealFrac a, Integral b) => a -> b
round (Double
ry forall a. Num a => a -> a -> a
+ Double
ry forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
sin Double
alpha)
    rx :: Double
rx = forall a b. (Integral a, Num b) => a -> b
fromIntegral (Dimension
width forall a. Num a => a -> a -> a
- Dimension
width') forall a. Fractional a => a -> a -> a
/ Double
2
    ry :: Double
ry = forall a b. (Integral a, Num b) => a -> b
fromIntegral (Dimension
height forall a. Num a => a -> a -> a
- Dimension
height') forall a. Fractional a => a -> a -> a
/ Double
2
    width' :: Dimension
width' = forall a b. (RealFrac a, Integral b) => a -> b
round (forall a b. (Integral a, Num b) => a -> b
fromIntegral Dimension
width forall a. Num a => a -> a -> a
* Rational
m)
    height' :: Dimension
height' = forall a b. (RealFrac a, Integral b) => a -> b
round (forall a b. (Integral a, Num b) => a -> b
fromIntegral Dimension
height forall a. Num a => a -> a -> a
* Rational
m)