{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses #-}
{-# LANGUAGE ParallelListComp, PatternGuards #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE LambdaCase #-}
module XMonad.Layout.LayoutHints
(
layoutHints
, layoutHintsWithPlacement
, layoutHintsToCenter
, LayoutHints
, LayoutHintsToCenter
, hintsEventHook
, placeRectangle
) where
import XMonad(LayoutClass(runLayout), mkAdjust, Window,
Dimension, Position, Rectangle(Rectangle), D,
X, refresh, Event(..), propertyNotify, wM_NORMAL_HINTS,
(<&&>), io, applySizeHints, whenX, isClient, withDisplay,
getWMNormalHints, WindowAttributes(..))
import XMonad.Prelude
import qualified XMonad.StackSet as W
import XMonad.Layout.Decoration(isInStack)
import XMonad.Layout.LayoutModifier(ModifiedLayout(..),
LayoutModifier(modifyLayout, redoLayout, modifierDescription))
import XMonad.Util.Types(Direction2D(..))
import Control.Arrow(Arrow((***), first, second))
import Data.Set (Set)
import qualified Data.Set as Set
layoutHints :: (LayoutClass l a) => l a -> ModifiedLayout LayoutHints l a
layoutHints :: forall (l :: * -> *) a.
LayoutClass l a =>
l a -> ModifiedLayout LayoutHints l a
layoutHints = LayoutHints a -> l a -> ModifiedLayout LayoutHints l a
forall (m :: * -> *) (l :: * -> *) a.
m a -> l a -> ModifiedLayout m l a
ModifiedLayout ((Double, Double) -> LayoutHints a
forall a. (Double, Double) -> LayoutHints a
LayoutHints (Double
0, Double
0))
layoutHintsWithPlacement :: (LayoutClass l a) => (Double, Double)
-> l a -> ModifiedLayout LayoutHints l a
layoutHintsWithPlacement :: forall (l :: * -> *) a.
LayoutClass l a =>
(Double, Double) -> l a -> ModifiedLayout LayoutHints l a
layoutHintsWithPlacement (Double, Double)
rs = LayoutHints a -> l a -> ModifiedLayout LayoutHints l a
forall (m :: * -> *) (l :: * -> *) a.
m a -> l a -> ModifiedLayout m l a
ModifiedLayout ((Double, Double) -> LayoutHints a
forall a. (Double, Double) -> LayoutHints a
LayoutHints (Double, Double)
rs)
layoutHintsToCenter :: (LayoutClass l a) => l a -> ModifiedLayout LayoutHintsToCenter l a
layoutHintsToCenter :: forall (l :: * -> *) a.
LayoutClass l a =>
l a -> ModifiedLayout LayoutHintsToCenter l a
layoutHintsToCenter = LayoutHintsToCenter a
-> l a -> ModifiedLayout LayoutHintsToCenter l a
forall (m :: * -> *) (l :: * -> *) a.
m a -> l a -> ModifiedLayout m l a
ModifiedLayout LayoutHintsToCenter a
forall a. LayoutHintsToCenter a
LayoutHintsToCenter
newtype LayoutHints a = LayoutHints (Double, Double)
deriving (ReadPrec [LayoutHints a]
ReadPrec (LayoutHints a)
Int -> ReadS (LayoutHints a)
ReadS [LayoutHints a]
(Int -> ReadS (LayoutHints a))
-> ReadS [LayoutHints a]
-> ReadPrec (LayoutHints a)
-> ReadPrec [LayoutHints a]
-> Read (LayoutHints a)
forall a. ReadPrec [LayoutHints a]
forall a. ReadPrec (LayoutHints a)
forall a. Int -> ReadS (LayoutHints a)
forall a. ReadS [LayoutHints a]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: forall a. Int -> ReadS (LayoutHints a)
readsPrec :: Int -> ReadS (LayoutHints a)
$creadList :: forall a. ReadS [LayoutHints a]
readList :: ReadS [LayoutHints a]
$creadPrec :: forall a. ReadPrec (LayoutHints a)
readPrec :: ReadPrec (LayoutHints a)
$creadListPrec :: forall a. ReadPrec [LayoutHints a]
readListPrec :: ReadPrec [LayoutHints a]
Read, Int -> LayoutHints a -> ShowS
[LayoutHints a] -> ShowS
LayoutHints a -> String
(Int -> LayoutHints a -> ShowS)
-> (LayoutHints a -> String)
-> ([LayoutHints a] -> ShowS)
-> Show (LayoutHints a)
forall a. Int -> LayoutHints a -> ShowS
forall a. [LayoutHints a] -> ShowS
forall a. LayoutHints a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Int -> LayoutHints a -> ShowS
showsPrec :: Int -> LayoutHints a -> ShowS
$cshow :: forall a. LayoutHints a -> String
show :: LayoutHints a -> String
$cshowList :: forall a. [LayoutHints a] -> ShowS
showList :: [LayoutHints a] -> ShowS
Show)
instance LayoutModifier LayoutHints Window where
modifierDescription :: LayoutHints Word64 -> String
modifierDescription LayoutHints Word64
_ = String
"Hinted"
redoLayout :: LayoutHints Word64
-> Rectangle
-> Maybe (Stack Word64)
-> [(Word64, Rectangle)]
-> X ([(Word64, Rectangle)], Maybe (LayoutHints Word64))
redoLayout LayoutHints Word64
_ Rectangle
_ Maybe (Stack Word64)
Nothing [(Word64, Rectangle)]
xs = ([(Word64, Rectangle)], Maybe (LayoutHints Word64))
-> X ([(Word64, Rectangle)], Maybe (LayoutHints Word64))
forall a. a -> X a
forall (m :: * -> *) a. Monad m => a -> m a
return ([(Word64, Rectangle)]
xs, Maybe (LayoutHints Word64)
forall a. Maybe a
Nothing)
redoLayout (LayoutHints (Double, Double)
al) Rectangle
_ (Just Stack Word64
s) [(Word64, Rectangle)]
xs
= do xs' <- ((Word64, Rectangle) -> X (Word64, Rectangle))
-> [(Word64, Rectangle)] -> X [(Word64, Rectangle)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (\x :: (Word64, Rectangle)
x@(Word64
_, Rectangle
r) -> (Rectangle -> Rectangle)
-> (Word64, Rectangle) -> (Word64, 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 ((Double, Double) -> Rectangle -> Rectangle -> Rectangle
forall r.
RealFrac r =>
(r, r) -> Rectangle -> Rectangle -> Rectangle
placeRectangle (Double, Double)
al Rectangle
r) ((Word64, Rectangle) -> (Word64, Rectangle))
-> X (Word64, Rectangle) -> X (Word64, Rectangle)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Word64, Rectangle) -> X (Word64, Rectangle)
applyHint (Word64, Rectangle)
x) [(Word64, Rectangle)]
xs
return (xs', Nothing)
where
applyHint :: (Word64, Rectangle) -> X (Word64, Rectangle)
applyHint (Word64
w,r :: Rectangle
r@(Rectangle Int32
a Int32
b Dimension
c Dimension
d)) = do
adj <- Word64 -> X ((Dimension, Dimension) -> (Dimension, Dimension))
mkAdjust Word64
w
let (c',d') = adj (c,d)
return (w, if isInStack s w then Rectangle a b c' d' else r)
placeRectangle :: RealFrac r => (r, r) -> Rectangle -> Rectangle -> Rectangle
placeRectangle :: forall r.
RealFrac r =>
(r, r) -> Rectangle -> Rectangle -> Rectangle
placeRectangle (r
rx, r
ry) (Rectangle Int32
x0 Int32
y0 Dimension
w Dimension
h) (Rectangle Int32
_ Int32
_ Dimension
dx Dimension
dy)
= Int32 -> Int32 -> Dimension -> Dimension -> Rectangle
Rectangle (Int32 -> Dimension -> Dimension -> r -> Int32
forall r.
RealFrac r =>
Int32 -> Dimension -> Dimension -> r -> Int32
align Int32
x0 Dimension
dx Dimension
w r
rx) (Int32 -> Dimension -> Dimension -> r -> Int32
forall r.
RealFrac r =>
Int32 -> Dimension -> Dimension -> r -> Int32
align Int32
y0 Dimension
dy Dimension
h r
ry) Dimension
dx Dimension
dy
where align :: RealFrac r => Position -> Dimension -> Dimension -> r -> Position
align :: forall r.
RealFrac r =>
Int32 -> Dimension -> Dimension -> r -> Int32
align Int32
z0 Dimension
dz Dimension
d r
r = Int32
z0 Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ r -> Int32
forall b. Integral b => r -> b
forall a b. (RealFrac a, Integral b) => a -> b
truncate (Dimension -> r
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Dimension
d Dimension -> Dimension -> Dimension
forall a. Num a => a -> a -> a
- Dimension
dz) r -> r -> r
forall a. Num a => a -> a -> a
* r
r)
fitting :: [Rectangle] -> Int
fitting :: [Rectangle] -> Int
fitting [Rectangle]
rects = [Int] -> Int
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum ([Int] -> Int) -> [Int] -> Int
forall a b. (a -> b) -> a -> b
$ do
r <- [Rectangle]
rects
return $ length $ filter (touching r) rects
applyOrder :: Rectangle -> [((Window, Rectangle),t)] -> [[((Window, Rectangle),t)]]
applyOrder :: forall t.
Rectangle
-> [((Word64, Rectangle), t)] -> [[((Word64, Rectangle), t)]]
applyOrder Rectangle
root [((Word64, Rectangle), t)]
wrs = do
f <- [[Int32] -> Int32
forall a. Ord a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum, [Int32] -> Int32
forall a. Ord a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
minimum, [Int32] -> Int32
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum, [Int32] -> Int32
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum ([Int32] -> Int32) -> ([Int32] -> [Int32]) -> [Int32] -> Int32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int32 -> Int32) -> [Int32] -> [Int32]
forall a b. (a -> b) -> [a] -> [b]
map Int32 -> Int32
sq]
return $ sortBy (compare `on` (f . distance)) wrs
where distFC :: (Int32, Int32) -> Int32
distFC = (Int32 -> Int32 -> Int32) -> (Int32, Int32) -> Int32
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
(+) (Int32 -> Int32 -> Int32)
-> (Int32 -> Int32) -> Int32 -> Int32 -> Int32
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` Int32 -> Int32
sq) ((Int32, Int32) -> Int32)
-> ((Int32, Int32) -> (Int32, Int32)) -> (Int32, Int32) -> Int32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int32 -> Int32 -> Int32)
-> (Int32, Int32) -> (Int32, Int32) -> (Int32, Int32)
forall {t} {t} {b}. (t -> t -> b) -> (t, t) -> (t, t) -> (b, b)
pairWise (-) (Rectangle -> (Int32, Int32)
center Rectangle
root)
distance :: ((a, Rectangle), b) -> [Int32]
distance = ((Int32, Int32) -> Int32) -> [(Int32, Int32)] -> [Int32]
forall a b. (a -> b) -> [a] -> [b]
map (Int32, Int32) -> Int32
distFC ([(Int32, Int32)] -> [Int32])
-> (((a, Rectangle), b) -> [(Int32, Int32)])
-> ((a, Rectangle), b)
-> [Int32]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rectangle -> [(Int32, Int32)]
corners (Rectangle -> [(Int32, Int32)])
-> (((a, Rectangle), b) -> Rectangle)
-> ((a, Rectangle), b)
-> [(Int32, Int32)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a, Rectangle) -> Rectangle
forall a b. (a, b) -> b
snd ((a, Rectangle) -> Rectangle)
-> (((a, Rectangle), b) -> (a, Rectangle))
-> ((a, Rectangle), b)
-> Rectangle
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((a, Rectangle), b) -> (a, Rectangle)
forall a b. (a, b) -> a
fst
pairWise :: (t -> t -> b) -> (t, t) -> (t, t) -> (b, b)
pairWise t -> t -> b
f (t
a,t
b) (t
c,t
d) = (t -> t -> b
f t
a t
c, t -> t -> b
f t
b t
d)
sq :: Int32 -> Int32
sq = (Int32 -> Int32 -> Int32) -> Int32 -> Int32
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
(*)
data LayoutHintsToCenter a = LayoutHintsToCenter deriving (ReadPrec [LayoutHintsToCenter a]
ReadPrec (LayoutHintsToCenter a)
Int -> ReadS (LayoutHintsToCenter a)
ReadS [LayoutHintsToCenter a]
(Int -> ReadS (LayoutHintsToCenter a))
-> ReadS [LayoutHintsToCenter a]
-> ReadPrec (LayoutHintsToCenter a)
-> ReadPrec [LayoutHintsToCenter a]
-> Read (LayoutHintsToCenter a)
forall a. ReadPrec [LayoutHintsToCenter a]
forall a. ReadPrec (LayoutHintsToCenter a)
forall a. Int -> ReadS (LayoutHintsToCenter a)
forall a. ReadS [LayoutHintsToCenter a]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: forall a. Int -> ReadS (LayoutHintsToCenter a)
readsPrec :: Int -> ReadS (LayoutHintsToCenter a)
$creadList :: forall a. ReadS [LayoutHintsToCenter a]
readList :: ReadS [LayoutHintsToCenter a]
$creadPrec :: forall a. ReadPrec (LayoutHintsToCenter a)
readPrec :: ReadPrec (LayoutHintsToCenter a)
$creadListPrec :: forall a. ReadPrec [LayoutHintsToCenter a]
readListPrec :: ReadPrec [LayoutHintsToCenter a]
Read, Int -> LayoutHintsToCenter a -> ShowS
[LayoutHintsToCenter a] -> ShowS
LayoutHintsToCenter a -> String
(Int -> LayoutHintsToCenter a -> ShowS)
-> (LayoutHintsToCenter a -> String)
-> ([LayoutHintsToCenter a] -> ShowS)
-> Show (LayoutHintsToCenter a)
forall a. Int -> LayoutHintsToCenter a -> ShowS
forall a. [LayoutHintsToCenter a] -> ShowS
forall a. LayoutHintsToCenter a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Int -> LayoutHintsToCenter a -> ShowS
showsPrec :: Int -> LayoutHintsToCenter a -> ShowS
$cshow :: forall a. LayoutHintsToCenter a -> String
show :: LayoutHintsToCenter a -> String
$cshowList :: forall a. [LayoutHintsToCenter a] -> ShowS
showList :: [LayoutHintsToCenter a] -> ShowS
Show)
instance LayoutModifier LayoutHintsToCenter Window where
modifyLayout :: forall (l :: * -> *).
LayoutClass l Word64 =>
LayoutHintsToCenter Word64
-> Workspace String (l Word64) Word64
-> Rectangle
-> X ([(Word64, Rectangle)], Maybe (l Word64))
modifyLayout LayoutHintsToCenter Word64
_ ws :: Workspace String (l Word64) Word64
ws@(W.Workspace String
_ l Word64
_ Maybe (Stack Word64)
Nothing) Rectangle
r = Workspace String (l Word64) Word64
-> Rectangle -> X ([(Word64, Rectangle)], Maybe (l Word64))
forall (layout :: * -> *) a.
LayoutClass layout a =>
Workspace String (layout a) a
-> Rectangle -> X ([(a, Rectangle)], Maybe (layout a))
runLayout Workspace String (l Word64) Word64
ws Rectangle
r
modifyLayout LayoutHintsToCenter Word64
_ ws :: Workspace String (l Word64) Word64
ws@(W.Workspace String
_ l Word64
_ (Just Stack Word64
st)) Rectangle
r = do
(arrs,ol) <- Workspace String (l Word64) Word64
-> Rectangle -> X ([(Word64, Rectangle)], Maybe (l Word64))
forall (layout :: * -> *) a.
LayoutClass layout a =>
Workspace String (layout a) a
-> Rectangle -> X ([(a, Rectangle)], Maybe (layout a))
runLayout Workspace String (l Word64) Word64
ws Rectangle
r
(, ol) . changeOrder (W.focus st : filter (/= W.focus st) (map fst arrs))
. maximumBy (compare `on` (fitting . map snd))
. map (applyHints st r) . applyOrder r
<$> mapM (\(Word64, Rectangle)
x -> ((Word64, Rectangle)
x,) (((Dimension, Dimension) -> (Dimension, Dimension))
-> ((Word64, Rectangle),
(Dimension, Dimension) -> (Dimension, Dimension)))
-> X ((Dimension, Dimension) -> (Dimension, Dimension))
-> X ((Word64, Rectangle),
(Dimension, Dimension) -> (Dimension, Dimension))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Word64 -> X ((Dimension, Dimension) -> (Dimension, Dimension))
mkAdjust ((Word64, Rectangle) -> Word64
forall a b. (a, b) -> a
fst (Word64, Rectangle)
x)) arrs
changeOrder :: [Window] -> [(Window, Rectangle)] -> [(Window, Rectangle)]
changeOrder :: [Word64] -> [(Word64, Rectangle)] -> [(Word64, Rectangle)]
changeOrder [Word64]
w [(Word64, Rectangle)]
wr = [Word64] -> [Rectangle] -> [(Word64, Rectangle)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Word64]
w' ([Rectangle] -> [(Word64, Rectangle)])
-> [Rectangle] -> [(Word64, Rectangle)]
forall a b. (a -> b) -> a -> b
$ (Word64 -> Rectangle) -> [Word64] -> [Rectangle]
forall a b. (a -> b) -> [a] -> [b]
map (Maybe Rectangle -> Rectangle
forall a. HasCallStack => Maybe a -> a
fromJust (Maybe Rectangle -> Rectangle)
-> (Word64 -> Maybe Rectangle) -> Word64 -> Rectangle
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Word64 -> [(Word64, Rectangle)] -> Maybe Rectangle)
-> [(Word64, Rectangle)] -> Word64 -> Maybe Rectangle
forall a b c. (a -> b -> c) -> b -> a -> c
flip Word64 -> [(Word64, Rectangle)] -> Maybe Rectangle
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup [(Word64, Rectangle)]
wr) [Word64]
w'
where w' :: [Word64]
w' = (Word64 -> Bool) -> [Word64] -> [Word64]
forall a. (a -> Bool) -> [a] -> [a]
filter (Word64 -> [Word64] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` ((Word64, Rectangle) -> Word64)
-> [(Word64, Rectangle)] -> [Word64]
forall a b. (a -> b) -> [a] -> [b]
map (Word64, Rectangle) -> Word64
forall a b. (a, b) -> a
fst [(Word64, Rectangle)]
wr) [Word64]
w
applyHints :: W.Stack Window -> Rectangle -> [((Window, Rectangle),D -> D)] -> [(Window, Rectangle)]
applyHints :: Stack Word64
-> Rectangle
-> [((Word64, Rectangle),
(Dimension, Dimension) -> (Dimension, Dimension))]
-> [(Word64, Rectangle)]
applyHints Stack Word64
_ Rectangle
_ [] = []
applyHints Stack Word64
s Rectangle
root (((Word64
w,lrect :: Rectangle
lrect@(Rectangle Int32
a Int32
b Dimension
c Dimension
d)),(Dimension, Dimension) -> (Dimension, Dimension)
adj):[((Word64, Rectangle),
(Dimension, Dimension) -> (Dimension, Dimension))]
xs) =
let (Dimension
c',Dimension
d') = (Dimension, Dimension) -> (Dimension, Dimension)
adj (Dimension
c,Dimension
d)
redr :: Rectangle
redr = (Double, Double) -> Rectangle -> Rectangle -> Rectangle
forall r.
RealFrac r =>
(r, r) -> Rectangle -> Rectangle -> Rectangle
placeRectangle (Rectangle -> Rectangle -> (Double, Double)
forall r. RealFrac r => Rectangle -> Rectangle -> (r, r)
centerPlacement Rectangle
root Rectangle
lrect :: (Double,Double)) Rectangle
lrect
(Rectangle -> Rectangle) -> Rectangle -> Rectangle
forall a b. (a -> b) -> a -> b
$ if Stack Word64 -> Word64 -> Bool
forall a. Eq a => Stack a -> a -> Bool
isInStack Stack Word64
s Word64
w then Int32 -> Int32 -> Dimension -> Dimension -> Rectangle
Rectangle Int32
a Int32
b Dimension
c' Dimension
d' else Rectangle
lrect
ds :: (Int32, Int32)
ds = (Dimension -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Dimension
c Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Dimension -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Dimension
c',Dimension -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Dimension
d Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Dimension -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Dimension
d')
growOther' :: Rectangle -> Rectangle
growOther' = (Int32, Int32)
-> Rectangle -> Set Direction2D -> Rectangle -> Rectangle
growOther (Int32, Int32)
ds Rectangle
lrect (Rectangle -> Rectangle -> Set Direction2D
freeDirs Rectangle
root Rectangle
lrect)
mapSnd :: (t -> c) -> [((d, t), d)] -> [((d, c), d)]
mapSnd t -> c
f = (((d, t), d) -> ((d, c), d)) -> [((d, t), d)] -> [((d, c), d)]
forall a b. (a -> b) -> [a] -> [b]
map (((d, t) -> (d, c)) -> ((d, t), d) -> ((d, c), 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 (((d, t) -> (d, c)) -> ((d, t), d) -> ((d, c), d))
-> ((d, t) -> (d, c)) -> ((d, t), d) -> ((d, c), d)
forall a b. (a -> b) -> a -> b
$ (t -> c) -> (d, t) -> (d, c)
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 t -> c
f)
next :: [(Word64, Rectangle)]
next = Stack Word64
-> Rectangle
-> [((Word64, Rectangle),
(Dimension, Dimension) -> (Dimension, Dimension))]
-> [(Word64, Rectangle)]
applyHints Stack Word64
s Rectangle
root ([((Word64, Rectangle),
(Dimension, Dimension) -> (Dimension, Dimension))]
-> [(Word64, Rectangle)])
-> [((Word64, Rectangle),
(Dimension, Dimension) -> (Dimension, Dimension))]
-> [(Word64, Rectangle)]
forall a b. (a -> b) -> a -> b
$ (Rectangle -> Rectangle)
-> [((Word64, Rectangle),
(Dimension, Dimension) -> (Dimension, Dimension))]
-> [((Word64, Rectangle),
(Dimension, Dimension) -> (Dimension, Dimension))]
forall {t} {c} {d} {d}. (t -> c) -> [((d, t), d)] -> [((d, c), d)]
mapSnd Rectangle -> Rectangle
growOther' [((Word64, Rectangle),
(Dimension, Dimension) -> (Dimension, Dimension))]
xs
in (Word64
w,Rectangle
redr)(Word64, Rectangle)
-> [(Word64, Rectangle)] -> [(Word64, Rectangle)]
forall a. a -> [a] -> [a]
:[(Word64, Rectangle)]
next
growOther :: (Position, Position) -> Rectangle -> Set Direction2D -> Rectangle -> Rectangle
growOther :: (Int32, Int32)
-> Rectangle -> Set Direction2D -> Rectangle -> Rectangle
growOther (Int32, Int32)
ds Rectangle
lrect Set Direction2D
fds Rectangle
r
| [Direction2D]
dirs <- Direction2D -> Direction2D
flipDir (Direction2D -> Direction2D) -> [Direction2D] -> [Direction2D]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Set Direction2D -> [Direction2D]
forall a. Set a -> [a]
Set.toList (Set Direction2D -> Set Direction2D -> Set Direction2D
forall a. Ord a => Set a -> Set a -> Set a
Set.intersection Set Direction2D
adj Set Direction2D
fds)
, Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ ((Direction2D, Direction2D) -> Bool)
-> [(Direction2D, Direction2D)] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any ((Direction2D -> Direction2D -> Bool)
-> (Direction2D, Direction2D) -> Bool
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Direction2D -> Direction2D -> Bool
opposite) ([(Direction2D, Direction2D)] -> Bool)
-> [(Direction2D, Direction2D)] -> Bool
forall a b. (a -> b) -> a -> b
$ [Direction2D] -> [(Direction2D, Direction2D)]
forall {b}. [b] -> [(b, b)]
cross [Direction2D]
dirs =
(Direction2D -> Rectangle -> Rectangle)
-> Rectangle -> [Direction2D] -> Rectangle
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (Direction2D -> (Int32, Int32) -> Rectangle -> Rectangle
`grow` (Int32, Int32)
ds) Rectangle
r [Direction2D]
dirs
| Bool
otherwise = Rectangle
r
where
adj :: Set Direction2D
adj = Rectangle -> Rectangle -> Set Direction2D
adjacent Rectangle
lrect Rectangle
r
cross :: [b] -> [(b, b)]
cross [b]
xs = [ (b
a,b
b) | b
a <- [b]
xs, b
b <- [b]
xs ]
flipDir :: Direction2D -> Direction2D
flipDir :: Direction2D -> Direction2D
flipDir Direction2D
d = case Direction2D
d of { Direction2D
L -> Direction2D
R; Direction2D
U -> Direction2D
D; Direction2D
R -> Direction2D
L; Direction2D
D -> Direction2D
U }
opposite :: Direction2D -> Direction2D -> Bool
opposite :: Direction2D -> Direction2D -> Bool
opposite Direction2D
x Direction2D
y = Direction2D -> Direction2D
flipDir Direction2D
x Direction2D -> Direction2D -> Bool
forall a. Eq a => a -> a -> Bool
== Direction2D
y
grow :: Direction2D -> (Position,Position) -> Rectangle -> Rectangle
grow :: Direction2D -> (Int32, Int32) -> Rectangle -> Rectangle
grow Direction2D
L (Int32
px,Int32
_ ) (Rectangle Int32
x Int32
y Dimension
w Dimension
h) = Int32 -> Int32 -> Dimension -> Dimension -> Rectangle
Rectangle (Int32
xInt32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
-Int32
px) Int32
y (Dimension
wDimension -> Dimension -> Dimension
forall a. Num a => a -> a -> a
+Int32 -> Dimension
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int32
px) Dimension
h
grow Direction2D
U (Int32
_ ,Int32
py) (Rectangle Int32
x Int32
y Dimension
w Dimension
h) = Int32 -> Int32 -> Dimension -> Dimension -> Rectangle
Rectangle Int32
x (Int32
yInt32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
-Int32
py) Dimension
w (Dimension
hDimension -> Dimension -> Dimension
forall a. Num a => a -> a -> a
+Int32 -> Dimension
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int32
py)
grow Direction2D
R (Int32
px,Int32
_ ) (Rectangle Int32
x Int32
y Dimension
w Dimension
h) = Int32 -> Int32 -> Dimension -> Dimension -> Rectangle
Rectangle Int32
x Int32
y (Dimension
wDimension -> Dimension -> Dimension
forall a. Num a => a -> a -> a
+Int32 -> Dimension
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int32
px) Dimension
h
grow Direction2D
D (Int32
_ ,Int32
py) (Rectangle Int32
x Int32
y Dimension
w Dimension
h) = Int32 -> Int32 -> Dimension -> Dimension -> Rectangle
Rectangle Int32
x Int32
y Dimension
w (Dimension
hDimension -> Dimension -> Dimension
forall a. Num a => a -> a -> a
+Int32 -> Dimension
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int32
py)
comparingEdges :: ([Position] -> [Position] -> Bool) -> Rectangle -> Rectangle -> Set Direction2D
comparingEdges :: ([Int32] -> [Int32] -> Bool)
-> Rectangle -> Rectangle -> Set Direction2D
comparingEdges [Int32] -> [Int32] -> Bool
surrounds Rectangle
r1 Rectangle
r2 = [Direction2D] -> Set Direction2D
forall a. Ord a => [a] -> Set a
Set.fromList ([Direction2D] -> Set Direction2D)
-> [Direction2D] -> Set Direction2D
forall a b. (a -> b) -> a -> b
$ ((Direction2D, Bool) -> Direction2D)
-> [(Direction2D, Bool)] -> [Direction2D]
forall a b. (a -> b) -> [a] -> [b]
map (Direction2D, Bool) -> Direction2D
forall a b. (a, b) -> a
fst ([(Direction2D, Bool)] -> [Direction2D])
-> [(Direction2D, Bool)] -> [Direction2D]
forall a b. (a -> b) -> a -> b
$ ((Direction2D, Bool) -> Bool)
-> [(Direction2D, Bool)] -> [(Direction2D, Bool)]
forall a. (a -> Bool) -> [a] -> [a]
filter (Direction2D, Bool) -> Bool
forall a b. (a, b) -> b
snd [ (Direction2D
dir,) (Bool -> (Direction2D, Bool)) -> Bool -> (Direction2D, Bool)
forall a b. (a -> b) -> a -> b
$
([Bool] -> Bool) -> [[Bool]] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any [Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
and [[Direction2D
dir Direction2D -> [Direction2D] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Direction2D
R,Direction2D
L], [Int32] -> Bool
allEq [Int32
a,Int32
c,Int32
w,Int32
y], [Int32
b,Int32
d] [Int32] -> [Int32] -> Bool
`surrounds` [Int32
x,Int32
z]]
,[Direction2D
dir Direction2D -> [Direction2D] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Direction2D
U,Direction2D
D], [Int32] -> Bool
allEq [Int32
b,Int32
d,Int32
x,Int32
z], [Int32
a,Int32
c] [Int32] -> [Int32] -> Bool
`surrounds` [Int32
w,Int32
y]]]
| ((Int32
a,Int32
b),(Int32
c,Int32
d)) <- [(Int32, Int32)] -> [((Int32, Int32), (Int32, Int32))]
forall {b}. [b] -> [(b, b)]
edge ([(Int32, Int32)] -> [((Int32, Int32), (Int32, Int32))])
-> [(Int32, Int32)] -> [((Int32, Int32), (Int32, Int32))]
forall a b. (a -> b) -> a -> b
$ Rectangle -> [(Int32, Int32)]
corners Rectangle
r1
| ((Int32
w,Int32
x),(Int32
y,Int32
z)) <- [(Int32, Int32)] -> [((Int32, Int32), (Int32, Int32))]
forall {b}. [b] -> [(b, b)]
edge ([(Int32, Int32)] -> [((Int32, Int32), (Int32, Int32))])
-> [(Int32, Int32)] -> [((Int32, Int32), (Int32, Int32))]
forall a b. (a -> b) -> a -> b
$ Int -> [(Int32, Int32)] -> [(Int32, Int32)]
forall {a}. Int -> [a] -> [a]
delay Int
2 ([(Int32, Int32)] -> [(Int32, Int32)])
-> [(Int32, Int32)] -> [(Int32, Int32)]
forall a b. (a -> b) -> a -> b
$ Rectangle -> [(Int32, Int32)]
corners Rectangle
r2
| Direction2D
dir <- [Direction2D
U,Direction2D
R,Direction2D
D,Direction2D
L]]
where edge :: [b] -> [(b, b)]
edge (b
x:[b]
xs) = [b] -> [b] -> [(b, b)]
forall a b. [a] -> [b] -> [(a, b)]
zip (b
xb -> [b] -> [b]
forall a. a -> [a] -> [a]
:[b]
xs) ([b]
xs [b] -> [b] -> [b]
forall a. [a] -> [a] -> [a]
++ [b
x])
edge [] = []
delay :: Int -> [a] -> [a]
delay Int
n [a]
xs = Int -> [a] -> [a]
forall {a}. Int -> [a] -> [a]
drop Int
n [a]
xs [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ Int -> [a] -> [a]
forall {a}. Int -> [a] -> [a]
take Int
n [a]
xs
allEq :: [Int32] -> Bool
allEq = ((Int32, Int32) -> Bool) -> [(Int32, Int32)] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all ((Int32 -> Int32 -> Bool) -> (Int32, Int32) -> Bool
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Int32 -> Int32 -> Bool
forall a. Eq a => a -> a -> Bool
(==)) ([(Int32, Int32)] -> Bool)
-> ([Int32] -> [(Int32, Int32)]) -> [Int32] -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Int32] -> [(Int32, Int32)]
forall {b}. [b] -> [(b, b)]
edge
adjacent :: Rectangle -> Rectangle -> Set Direction2D
adjacent :: Rectangle -> Rectangle -> Set Direction2D
adjacent = ([Int32] -> [Int32] -> Bool)
-> Rectangle -> Rectangle -> Set Direction2D
comparingEdges ((Int32 -> Bool) -> [Int32] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all ((Int32 -> Bool) -> [Int32] -> Bool)
-> ([Int32] -> Int32 -> Bool) -> [Int32] -> [Int32] -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Int32] -> Int32 -> Bool
forall a. Ord a => [a] -> a -> Bool
onClosedInterval)
touching :: Rectangle -> Rectangle -> Bool
touching :: Rectangle -> Rectangle -> Bool
touching Rectangle
a Rectangle
b = Bool -> Bool
not (Bool -> Bool)
-> (Set Direction2D -> Bool) -> Set Direction2D -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Set Direction2D -> Bool
forall a. Set a -> Bool
Set.null (Set Direction2D -> Bool) -> Set Direction2D -> Bool
forall a b. (a -> b) -> a -> b
$ ([Int32] -> [Int32] -> Bool)
-> Rectangle -> Rectangle -> Set Direction2D
comparingEdges [Int32] -> [Int32] -> Bool
forall {a}. Ord a => [a] -> [a] -> Bool
c Rectangle
a Rectangle
b
where c :: [a] -> [a] -> Bool
c [a]
x [a]
y = (a -> Bool) -> [a] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any ([a] -> a -> Bool
forall a. Ord a => [a] -> a -> Bool
onClosedInterval [a]
x) [a]
y Bool -> Bool -> Bool
|| (a -> Bool) -> [a] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any ([a] -> a -> Bool
forall a. Ord a => [a] -> a -> Bool
onClosedInterval [a]
y) [a]
x
onClosedInterval :: Ord a => [a] -> a -> Bool
onClosedInterval :: forall a. Ord a => [a] -> a -> Bool
onClosedInterval [a]
bds a
x = [a] -> a
forall a. Ord a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
minimum [a]
bds a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
x Bool -> Bool -> Bool
&& [a] -> a
forall a. Ord a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum [a]
bds a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>= a
x
corners :: Rectangle -> [(Position, Position)]
corners :: Rectangle -> [(Int32, Int32)]
corners (Rectangle Int32
x Int32
y Dimension
w Dimension
h) = [(Int32
x,Int32
y)
,(Int32
xInt32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+Dimension -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Dimension
w, Int32
y)
,(Int32
xInt32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+Dimension -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Dimension
w, Int32
yInt32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+Dimension -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Dimension
h)
,(Int32
x, Int32
yInt32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+Dimension -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Dimension
h)]
center :: Rectangle -> (Position, Position)
center :: Rectangle -> (Int32, Int32)
center (Rectangle Int32
x Int32
y Dimension
w Dimension
h) = (Int32 -> Dimension -> Int32
forall {a} {a}. (Integral a, Integral a) => a -> a -> a
avg Int32
x Dimension
w, Int32 -> Dimension -> Int32
forall {a} {a}. (Integral a, Integral a) => a -> a -> a
avg Int32
y Dimension
h)
where avg :: a -> a -> a
avg a
a a
b = a
a a -> a -> a
forall a. Num a => a -> a -> a
+ a -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
b a -> a -> a
forall a. Integral a => a -> a -> a
`div` a
2
centerPlacement :: RealFrac r => Rectangle -> Rectangle -> (r, r)
centerPlacement :: forall r. RealFrac r => Rectangle -> Rectangle -> (r, r)
centerPlacement = (Int32 -> r) -> Rectangle -> Rectangle -> (r, r)
forall r. (Int32 -> r) -> Rectangle -> Rectangle -> (r, r)
centerPlacement' Int32 -> r
forall {a} {a}. (Eq a, Fractional a, Num a) => a -> a
clamp
where clamp :: a -> a
clamp a
n = case a -> a
forall a. Num a => a -> a
signum a
n of
a
0 -> a
0.5
a
1 -> a
1
a
_ -> a
0
freeDirs :: Rectangle -> Rectangle -> Set Direction2D
freeDirs :: Rectangle -> Rectangle -> Set Direction2D
freeDirs Rectangle
root = [Direction2D] -> Set Direction2D
forall a. Ord a => [a] -> Set a
Set.fromList ([Direction2D] -> Set Direction2D)
-> (Rectangle -> [Direction2D]) -> Rectangle -> Set Direction2D
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Direction2D] -> [Direction2D] -> [Direction2D])
-> ([Direction2D], [Direction2D]) -> [Direction2D]
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry [Direction2D] -> [Direction2D] -> [Direction2D]
forall a. [a] -> [a] -> [a]
(++) (([Direction2D], [Direction2D]) -> [Direction2D])
-> (Rectangle -> ([Direction2D], [Direction2D]))
-> Rectangle
-> [Direction2D]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int32 -> [Direction2D]
forall {a}. (Eq a, Num a) => a -> [Direction2D]
lr (Int32 -> [Direction2D])
-> (Int32 -> [Direction2D])
-> (Int32, Int32)
-> ([Direction2D], [Direction2D])
forall b c b' c'. (b -> c) -> (b' -> c') -> (b, b') -> (c, c')
forall (a :: * -> * -> *) b c b' c'.
Arrow a =>
a b c -> a b' c' -> a (b, b') (c, c')
*** Int32 -> [Direction2D]
forall {a}. (Eq a, Num a) => a -> [Direction2D]
ud)
((Int32, Int32) -> ([Direction2D], [Direction2D]))
-> (Rectangle -> (Int32, Int32))
-> Rectangle
-> ([Direction2D], [Direction2D])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int32 -> Int32) -> Rectangle -> Rectangle -> (Int32, Int32)
forall r. (Int32 -> r) -> Rectangle -> Rectangle -> (r, r)
centerPlacement' Int32 -> Int32
forall a. Num a => a -> a
signum Rectangle
root
where
lr :: a -> [Direction2D]
lr a
1 = [Direction2D
L]
lr (-1) = [Direction2D
R]
lr a
_ = [Direction2D
L,Direction2D
R]
ud :: a -> [Direction2D]
ud a
1 = [Direction2D
U]
ud (-1) = [Direction2D
D]
ud a
_ = [Direction2D
U,Direction2D
D]
centerPlacement' :: (Position -> r) -> Rectangle -> Rectangle -> (r, r)
centerPlacement' :: forall r. (Int32 -> r) -> Rectangle -> Rectangle -> (r, r)
centerPlacement' Int32 -> r
cf Rectangle
root Rectangle
assigned
= (Int32 -> r
cf (Int32 -> r) -> Int32 -> r
forall a b. (a -> b) -> a -> b
$ Int32
cx Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
cwx, Int32 -> r
cf (Int32 -> r) -> Int32 -> r
forall a b. (a -> b) -> a -> b
$ Int32
cy Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
cwy)
where (Int32
cx,Int32
cy) = Rectangle -> (Int32, Int32)
center Rectangle
root
(Int32
cwx,Int32
cwy) = Rectangle -> (Int32, Int32)
center Rectangle
assigned
hintsEventHook :: Event -> X All
hintsEventHook :: Event -> X All
hintsEventHook PropertyEvent{ ev_event_type :: Event -> Dimension
ev_event_type = Dimension
t, ev_atom :: Event -> Word64
ev_atom = Word64
a, ev_window :: Event -> Word64
ev_window = Word64
w }
| Dimension
t Dimension -> Dimension -> Bool
forall a. Eq a => a -> a -> Bool
== Dimension
propertyNotify Bool -> Bool -> Bool
&& Word64
a Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64
wM_NORMAL_HINTS = do
X Bool -> X () -> X ()
whenX (Word64 -> X Bool
isClient Word64
w X Bool -> X Bool -> X Bool
forall (m :: * -> *). Monad m => m Bool -> m Bool -> m Bool
<&&> Word64 -> X Bool
hintsMismatch Word64
w) X ()
refresh
All -> X All
forall a. a -> X a
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> All
All Bool
True)
hintsEventHook Event
_ = All -> X All
forall a. a -> X a
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> All
All Bool
True)
hintsMismatch :: Window -> X Bool
hintsMismatch :: Word64 -> X Bool
hintsMismatch Word64
w = Word64 -> X (Maybe WindowAttributes)
safeGetWindowAttributes Word64
w X (Maybe WindowAttributes)
-> (Maybe WindowAttributes -> X Bool) -> X Bool
forall a b. X a -> (a -> X b) -> X b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Maybe WindowAttributes
Nothing -> Bool -> X Bool
forall a. a -> X a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
False
Just WindowAttributes
wa -> do
sh <- (Display -> X SizeHints) -> X SizeHints
forall a. (Display -> X a) -> X a
withDisplay ((Display -> X SizeHints) -> X SizeHints)
-> (Display -> X SizeHints) -> X SizeHints
forall a b. (a -> b) -> a -> b
$ \Display
d -> IO SizeHints -> X SizeHints
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io (Display -> Word64 -> IO SizeHints
getWMNormalHints Display
d Word64
w)
let dim = (CInt -> Dimension
forall a b. (Integral a, Num b) => a -> b
fromIntegral (CInt -> Dimension) -> CInt -> Dimension
forall a b. (a -> b) -> a -> b
$ WindowAttributes -> CInt
wa_width WindowAttributes
wa, CInt -> Dimension
forall a b. (Integral a, Num b) => a -> b
fromIntegral (CInt -> Dimension) -> CInt -> Dimension
forall a b. (a -> b) -> a -> b
$ WindowAttributes -> CInt
wa_height WindowAttributes
wa)
return $ dim /= applySizeHints 0 sh dim