{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE PatternGuards #-}
module XMonad.Layout.FixedAspectRatio
(
fixedAspectRatio
, FixedAspectRatio
, ManageAspectRatio(..)
, doFixAspect
) where
import Control.Arrow
import qualified Data.Map as M
import Data.Ratio
import XMonad
import XMonad.Actions.MessageFeedback
import XMonad.Layout.Decoration
import XMonad.Layout.LayoutHints
fixedAspectRatio
:: (Double, Double) -> l a -> ModifiedLayout FixedAspectRatio l a
fixedAspectRatio :: forall (l :: * -> *) a.
(Double, Double) -> l a -> ModifiedLayout FixedAspectRatio l a
fixedAspectRatio = FixedAspectRatio a -> l a -> ModifiedLayout FixedAspectRatio l a
forall (m :: * -> *) (l :: * -> *) a.
m a -> l a -> ModifiedLayout m l a
ModifiedLayout (FixedAspectRatio a -> l a -> ModifiedLayout FixedAspectRatio l a)
-> ((Double, Double) -> FixedAspectRatio a)
-> (Double, Double)
-> l a
-> ModifiedLayout FixedAspectRatio l a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map Word64 (Ratio Integer)
-> (Double, Double) -> FixedAspectRatio a
forall a.
Map Word64 (Ratio Integer)
-> (Double, Double) -> FixedAspectRatio a
FixedAspectRatio Map Word64 (Ratio Integer)
forall a. Monoid a => a
mempty
data FixedAspectRatio a = FixedAspectRatio (M.Map Window Rational)
(Double, Double)
deriving (ReadPrec [FixedAspectRatio a]
ReadPrec (FixedAspectRatio a)
Int -> ReadS (FixedAspectRatio a)
ReadS [FixedAspectRatio a]
(Int -> ReadS (FixedAspectRatio a))
-> ReadS [FixedAspectRatio a]
-> ReadPrec (FixedAspectRatio a)
-> ReadPrec [FixedAspectRatio a]
-> Read (FixedAspectRatio a)
forall a. ReadPrec [FixedAspectRatio a]
forall a. ReadPrec (FixedAspectRatio a)
forall a. Int -> ReadS (FixedAspectRatio a)
forall a. ReadS [FixedAspectRatio a]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: forall a. Int -> ReadS (FixedAspectRatio a)
readsPrec :: Int -> ReadS (FixedAspectRatio a)
$creadList :: forall a. ReadS [FixedAspectRatio a]
readList :: ReadS [FixedAspectRatio a]
$creadPrec :: forall a. ReadPrec (FixedAspectRatio a)
readPrec :: ReadPrec (FixedAspectRatio a)
$creadListPrec :: forall a. ReadPrec [FixedAspectRatio a]
readListPrec :: ReadPrec [FixedAspectRatio a]
Read, Int -> FixedAspectRatio a -> ShowS
[FixedAspectRatio a] -> ShowS
FixedAspectRatio a -> String
(Int -> FixedAspectRatio a -> ShowS)
-> (FixedAspectRatio a -> String)
-> ([FixedAspectRatio a] -> ShowS)
-> Show (FixedAspectRatio a)
forall a. Int -> FixedAspectRatio a -> ShowS
forall a. [FixedAspectRatio a] -> ShowS
forall a. FixedAspectRatio a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Int -> FixedAspectRatio a -> ShowS
showsPrec :: Int -> FixedAspectRatio a -> ShowS
$cshow :: forall a. FixedAspectRatio a -> String
show :: FixedAspectRatio a -> String
$cshowList :: forall a. [FixedAspectRatio a] -> ShowS
showList :: [FixedAspectRatio a] -> ShowS
Show)
instance LayoutModifier FixedAspectRatio Window where
pureModifier :: FixedAspectRatio Word64
-> Rectangle
-> Maybe (Stack Word64)
-> [(Word64, Rectangle)]
-> ([(Word64, Rectangle)], Maybe (FixedAspectRatio Word64))
pureModifier (FixedAspectRatio Map Word64 (Ratio Integer)
ratios (Double, Double)
placement) Rectangle
_ (Just Stack Word64
s) [(Word64, Rectangle)]
xs =
([(Word64, Rectangle)]
xs', Maybe (FixedAspectRatio Word64)
forall a. Maybe a
Nothing)
where
xs' :: [(Word64, Rectangle)]
xs' =
((Word64, Rectangle) -> (Word64, Rectangle))
-> [(Word64, Rectangle)] -> [(Word64, Rectangle)]
forall a b. (a -> b) -> [a] -> [b]
map (\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)
placement Rectangle
r) ((Word64, Rectangle) -> (Word64, Rectangle))
-> (Word64, Rectangle) -> (Word64, Rectangle)
forall a b. (a -> b) -> a -> b
$ (Word64, Rectangle) -> (Word64, Rectangle)
applyHint (Word64, Rectangle)
x) [(Word64, Rectangle)]
xs
applyHint :: (Word64, Rectangle) -> (Word64, Rectangle)
applyHint (Word64
win, r :: Rectangle
r@(Rectangle Position
x Position
y Dimension
w Dimension
h)) =
let ar :: Maybe (Ratio Integer)
ar = Word64 -> Map Word64 (Ratio Integer) -> Maybe (Ratio Integer)
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Word64
win Map Word64 (Ratio Integer)
ratios
(Dimension
w', Dimension
h') = (Dimension, Dimension)
-> (Ratio Integer -> (Dimension, Dimension))
-> Maybe (Ratio Integer)
-> (Dimension, Dimension)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Dimension
w, Dimension
h) ((Dimension, Dimension) -> Ratio Integer -> (Dimension, Dimension)
adj (Dimension
w, Dimension
h)) Maybe (Ratio Integer)
ar
in (Word64
win, if Stack Word64 -> Word64 -> Bool
forall a. Eq a => Stack a -> a -> Bool
isInStack Stack Word64
s Word64
win then Position -> Position -> Dimension -> Dimension -> Rectangle
Rectangle Position
x Position
y Dimension
w' Dimension
h' else Rectangle
r)
pureModifier FixedAspectRatio Word64
_ Rectangle
_ Maybe (Stack Word64)
_ [(Word64, Rectangle)]
xs = ([(Word64, Rectangle)]
xs, Maybe (FixedAspectRatio Word64)
forall a. Maybe a
Nothing)
handleMess :: FixedAspectRatio Word64
-> SomeMessage -> X (Maybe (FixedAspectRatio Word64))
handleMess (FixedAspectRatio Map Word64 (Ratio Integer)
ratios (Double, Double)
placement) SomeMessage
mess
| Just DestroyWindowEvent { ev_window :: Event -> Word64
ev_window = Word64
w } <- SomeMessage -> Maybe Event
forall m. Message m => SomeMessage -> Maybe m
fromMessage SomeMessage
mess
= Maybe (FixedAspectRatio Word64)
-> X (Maybe (FixedAspectRatio Word64))
forall a. a -> X a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (FixedAspectRatio Word64)
-> X (Maybe (FixedAspectRatio Word64)))
-> (FixedAspectRatio Word64 -> Maybe (FixedAspectRatio Word64))
-> FixedAspectRatio Word64
-> X (Maybe (FixedAspectRatio Word64))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FixedAspectRatio Word64 -> Maybe (FixedAspectRatio Word64)
forall a. a -> Maybe a
Just (FixedAspectRatio Word64 -> X (Maybe (FixedAspectRatio Word64)))
-> FixedAspectRatio Word64 -> X (Maybe (FixedAspectRatio Word64))
forall a b. (a -> b) -> a -> b
$ Map Word64 (Ratio Integer)
-> (Double, Double) -> FixedAspectRatio Word64
forall a.
Map Word64 (Ratio Integer)
-> (Double, Double) -> FixedAspectRatio a
FixedAspectRatio (Word64 -> Map Word64 (Ratio Integer)
deleted Word64
w) (Double, Double)
placement
| Bool
otherwise
= case SomeMessage -> Maybe ManageAspectRatio
forall m. Message m => SomeMessage -> Maybe m
fromMessage SomeMessage
mess of
Just (FixRatio Ratio Integer
r Word64
w) ->
Maybe (FixedAspectRatio Word64)
-> X (Maybe (FixedAspectRatio Word64))
forall a. a -> X a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (FixedAspectRatio Word64)
-> X (Maybe (FixedAspectRatio Word64)))
-> (FixedAspectRatio Word64 -> Maybe (FixedAspectRatio Word64))
-> FixedAspectRatio Word64
-> X (Maybe (FixedAspectRatio Word64))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FixedAspectRatio Word64 -> Maybe (FixedAspectRatio Word64)
forall a. a -> Maybe a
Just (FixedAspectRatio Word64 -> X (Maybe (FixedAspectRatio Word64)))
-> FixedAspectRatio Word64 -> X (Maybe (FixedAspectRatio Word64))
forall a b. (a -> b) -> a -> b
$ Map Word64 (Ratio Integer)
-> (Double, Double) -> FixedAspectRatio Word64
forall a.
Map Word64 (Ratio Integer)
-> (Double, Double) -> FixedAspectRatio a
FixedAspectRatio (Word64 -> Ratio Integer -> Map Word64 (Ratio Integer)
inserted Word64
w Ratio Integer
r) (Double, Double)
placement
Just (ResetRatio Word64
w) ->
Maybe (FixedAspectRatio Word64)
-> X (Maybe (FixedAspectRatio Word64))
forall a. a -> X a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (FixedAspectRatio Word64)
-> X (Maybe (FixedAspectRatio Word64)))
-> (FixedAspectRatio Word64 -> Maybe (FixedAspectRatio Word64))
-> FixedAspectRatio Word64
-> X (Maybe (FixedAspectRatio Word64))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FixedAspectRatio Word64 -> Maybe (FixedAspectRatio Word64)
forall a. a -> Maybe a
Just (FixedAspectRatio Word64 -> X (Maybe (FixedAspectRatio Word64)))
-> FixedAspectRatio Word64 -> X (Maybe (FixedAspectRatio Word64))
forall a b. (a -> b) -> a -> b
$ Map Word64 (Ratio Integer)
-> (Double, Double) -> FixedAspectRatio Word64
forall a.
Map Word64 (Ratio Integer)
-> (Double, Double) -> FixedAspectRatio a
FixedAspectRatio (Word64 -> Map Word64 (Ratio Integer)
deleted Word64
w) (Double, Double)
placement
Just (ToggleRatio Ratio Integer
r Word64
w) ->
Maybe (FixedAspectRatio Word64)
-> X (Maybe (FixedAspectRatio Word64))
forall a. a -> X a
forall (m :: * -> *) a. Monad m => a -> m a
return
(Maybe (FixedAspectRatio Word64)
-> X (Maybe (FixedAspectRatio Word64)))
-> (Maybe (Ratio Integer) -> Maybe (FixedAspectRatio Word64))
-> Maybe (Ratio Integer)
-> X (Maybe (FixedAspectRatio Word64))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FixedAspectRatio Word64 -> Maybe (FixedAspectRatio Word64)
forall a. a -> Maybe a
Just
(FixedAspectRatio Word64 -> Maybe (FixedAspectRatio Word64))
-> (Maybe (Ratio Integer) -> FixedAspectRatio Word64)
-> Maybe (Ratio Integer)
-> Maybe (FixedAspectRatio Word64)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Map Word64 (Ratio Integer)
-> (Double, Double) -> FixedAspectRatio Word64)
-> (Double, Double)
-> Map Word64 (Ratio Integer)
-> FixedAspectRatio Word64
forall a b c. (a -> b -> c) -> b -> a -> c
flip Map Word64 (Ratio Integer)
-> (Double, Double) -> FixedAspectRatio Word64
forall a.
Map Word64 (Ratio Integer)
-> (Double, Double) -> FixedAspectRatio a
FixedAspectRatio (Double, Double)
placement
(Map Word64 (Ratio Integer) -> FixedAspectRatio Word64)
-> (Maybe (Ratio Integer) -> Map Word64 (Ratio Integer))
-> Maybe (Ratio Integer)
-> FixedAspectRatio Word64
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map Word64 (Ratio Integer)
-> (Ratio Integer -> Map Word64 (Ratio Integer))
-> Maybe (Ratio Integer)
-> Map Word64 (Ratio Integer)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Word64 -> Ratio Integer -> Map Word64 (Ratio Integer)
inserted Word64
w Ratio Integer
r) (Map Word64 (Ratio Integer)
-> Ratio Integer -> Map Word64 (Ratio Integer)
forall a b. a -> b -> a
const (Map Word64 (Ratio Integer)
-> Ratio Integer -> Map Word64 (Ratio Integer))
-> Map Word64 (Ratio Integer)
-> Ratio Integer
-> Map Word64 (Ratio Integer)
forall a b. (a -> b) -> a -> b
$ Word64 -> Map Word64 (Ratio Integer)
deleted Word64
w)
(Maybe (Ratio Integer) -> X (Maybe (FixedAspectRatio Word64)))
-> Maybe (Ratio Integer) -> X (Maybe (FixedAspectRatio Word64))
forall a b. (a -> b) -> a -> b
$ Word64 -> Map Word64 (Ratio Integer) -> Maybe (Ratio Integer)
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Word64
w Map Word64 (Ratio Integer)
ratios
Maybe ManageAspectRatio
_ -> Maybe (FixedAspectRatio Word64)
-> X (Maybe (FixedAspectRatio Word64))
forall a. a -> X a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (FixedAspectRatio Word64)
forall a. Maybe a
Nothing
where
inserted :: Word64 -> Ratio Integer -> Map Word64 (Ratio Integer)
inserted Word64
w Ratio Integer
r = Word64
-> Ratio Integer
-> Map Word64 (Ratio Integer)
-> Map Word64 (Ratio Integer)
forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert Word64
w Ratio Integer
r Map Word64 (Ratio Integer)
ratios
deleted :: Word64 -> Map Word64 (Ratio Integer)
deleted Word64
w = Word64 -> Map Word64 (Ratio Integer) -> Map Word64 (Ratio Integer)
forall k a. Ord k => k -> Map k a -> Map k a
M.delete Word64
w Map Word64 (Ratio Integer)
ratios
doFixAspect
:: Rational
-> ManageHook
doFixAspect :: Ratio Integer -> Query (Endo WindowSet)
doFixAspect Ratio Integer
r = Query Word64
forall r (m :: * -> *). MonadReader r m => m r
ask
Query Word64
-> (Word64 -> Query (Endo WindowSet)) -> Query (Endo WindowSet)
forall a b. Query a -> (a -> Query b) -> Query b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Word64
w -> X () -> Query ()
forall a. X a -> Query a
liftX (ManageAspectRatio -> X ()
forall a. Message a => a -> X ()
sendMessageWithNoRefreshToCurrent (Ratio Integer -> Word64 -> ManageAspectRatio
FixRatio Ratio Integer
r Word64
w)) Query () -> Query (Endo WindowSet) -> Query (Endo WindowSet)
forall a b. Query a -> Query b -> Query b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Query (Endo WindowSet)
forall a. Monoid a => a
mempty
adj :: (Dimension, Dimension) -> Rational -> (Dimension, Dimension)
adj :: (Dimension, Dimension) -> Ratio Integer -> (Dimension, Dimension)
adj (Dimension
w, Dimension
h) Ratio Integer
ar | Ratio Integer
ar' Ratio Integer -> Ratio Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Ratio Integer
ar = (Ratio Integer -> Dimension
forall b. Integral b => Ratio Integer -> b
forall a b. (RealFrac a, Integral b) => a -> b
ceiling (Ratio Integer -> Dimension) -> Ratio Integer -> Dimension
forall a b. (a -> b) -> a -> b
$ Dimension -> Ratio Integer
forall a b. (Integral a, Num b) => a -> b
fi Dimension
h Ratio Integer -> Ratio Integer -> Ratio Integer
forall a. Num a => a -> a -> a
* Ratio Integer
ar, Dimension
h)
| Bool
otherwise = (Dimension
w, Ratio Integer -> Dimension
forall b. Integral b => Ratio Integer -> b
forall a b. (RealFrac a, Integral b) => a -> b
ceiling (Ratio Integer -> Dimension) -> Ratio Integer -> Dimension
forall a b. (a -> b) -> a -> b
$ Dimension -> Ratio Integer
forall a b. (Integral a, Num b) => a -> b
fi Dimension
w Ratio Integer -> Ratio Integer -> Ratio Integer
forall a. Fractional a => a -> a -> a
/ Ratio Integer
ar)
where ar' :: Ratio Integer
ar' = Dimension -> Integer
forall a b. (Integral a, Num b) => a -> b
fi Dimension
w Integer -> Integer -> Ratio Integer
forall a. Integral a => a -> a -> Ratio a
% Dimension -> Integer
forall a b. (Integral a, Num b) => a -> b
fi Dimension
h
data ManageAspectRatio =
FixRatio Rational Window
| ResetRatio Window
| ToggleRatio Rational Window
instance Message ManageAspectRatio