{-# LANGUAGE FlexibleInstances #-}
module XMonad.Hooks.FadeWindows (
fadeWindowsLogHook
,FadeHook
,Opacity
,idFadeHook
,opaque
,solid
,transparent
,invisible
,transparency
,translucence
,fadeBy
,opacity
,fadeTo
,fadeWindowsEventHook
,doS
,isFloating
,isUnfocused
) where
import XMonad.Core
import XMonad.Prelude
import XMonad.ManageHook (liftX)
import qualified XMonad.StackSet as W
import XMonad.Hooks.FadeInactive (setOpacity
,isUnfocused
)
import Control.Monad.Reader (ask
,asks)
import Control.Monad.State (gets)
import qualified Data.Map as M
import Graphics.X11.Xlib.Extras (Event(..))
data Opacity = Opacity Rational | OEmpty
instance Monoid Opacity where
mempty :: Opacity
mempty = Opacity
OEmpty
Opacity
r mappend :: Opacity -> Opacity -> Opacity
`mappend` Opacity
OEmpty = Opacity
r
Opacity
_ `mappend` Opacity
r = Opacity
r
instance Semigroup Opacity where
<> :: Opacity -> Opacity -> Opacity
(<>) = Opacity -> Opacity -> Opacity
forall a. Monoid a => a -> a -> a
mappend
type FadeHook = Query Opacity
opaque :: FadeHook
opaque :: FadeHook
opaque = Opacity -> FadeHook
forall m. Monoid m => m -> Query m
doS (Rational -> Opacity
Opacity Rational
1)
transparent :: FadeHook
transparent :: FadeHook
transparent = Opacity -> FadeHook
forall m. Monoid m => m -> Query m
doS (Rational -> Opacity
Opacity Rational
0)
transparency :: Rational
-> FadeHook
transparency :: Rational -> FadeHook
transparency = Opacity -> FadeHook
forall m. Monoid m => m -> Query m
doS (Opacity -> FadeHook)
-> (Rational -> Opacity) -> Rational -> FadeHook
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rational -> Opacity
Opacity (Rational -> Opacity)
-> (Rational -> Rational) -> Rational -> Opacity
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Rational
1Rational -> Rational -> Rational
forall a. Num a => a -> a -> a
-) (Rational -> Rational)
-> (Rational -> Rational) -> Rational -> Rational
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rational -> Rational
clampRatio
opacity :: Rational
-> FadeHook
opacity :: Rational -> FadeHook
opacity = Opacity -> FadeHook
forall m. Monoid m => m -> Query m
doS (Opacity -> FadeHook)
-> (Rational -> Opacity) -> Rational -> FadeHook
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rational -> Opacity
Opacity (Rational -> Opacity)
-> (Rational -> Rational) -> Rational -> Opacity
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rational -> Rational
clampRatio
fadeTo, translucence, fadeBy :: Rational -> FadeHook
fadeTo :: Rational -> FadeHook
fadeTo = Rational -> FadeHook
transparency
translucence :: Rational -> FadeHook
translucence = Rational -> FadeHook
transparency
fadeBy :: Rational -> FadeHook
fadeBy = Rational -> FadeHook
opacity
invisible, solid :: FadeHook
invisible :: FadeHook
invisible = FadeHook
transparent
solid :: FadeHook
solid = FadeHook
opaque
doS :: Monoid m => m -> Query m
doS :: m -> Query m
doS = m -> Query m
forall (m :: * -> *) a. Monad m => a -> m a
return
idFadeHook :: FadeHook
idFadeHook :: FadeHook
idFadeHook = FadeHook
opaque
isFloating :: Query Bool
isFloating :: Query Bool
isFloating = Query Window
forall r (m :: * -> *). MonadReader r m => m r
ask Query Window -> (Window -> Query Bool) -> Query Bool
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Window
w -> X Bool -> Query Bool
forall a. X a -> Query a
liftX (X Bool -> Query Bool)
-> ((XState -> Bool) -> X Bool) -> (XState -> Bool) -> Query Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (XState -> Bool) -> X Bool
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets ((XState -> Bool) -> Query Bool) -> (XState -> Bool) -> Query Bool
forall a b. (a -> b) -> a -> b
$ Window -> Map Window RationalRect -> Bool
forall k a. Ord k => k -> Map k a -> Bool
M.member Window
w (Map Window RationalRect -> Bool)
-> (XState -> Map Window RationalRect) -> XState -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StackSet WorkspaceId (Layout Window) Window ScreenId ScreenDetail
-> Map Window RationalRect
forall i l a sid sd. StackSet i l a sid sd -> Map a RationalRect
W.floating (StackSet WorkspaceId (Layout Window) Window ScreenId ScreenDetail
-> Map Window RationalRect)
-> (XState
-> StackSet
WorkspaceId (Layout Window) Window ScreenId ScreenDetail)
-> XState
-> Map Window RationalRect
forall b c a. (b -> c) -> (a -> b) -> a -> c
. XState
-> StackSet
WorkspaceId (Layout Window) Window ScreenId ScreenDetail
windowset
fadeWindowsLogHook :: FadeHook -> X ()
fadeWindowsLogHook :: FadeHook -> X ()
fadeWindowsLogHook FadeHook
h = (StackSet WorkspaceId (Layout Window) Window ScreenId ScreenDetail
-> X ())
-> X ()
forall a.
(StackSet WorkspaceId (Layout Window) Window ScreenId ScreenDetail
-> X a)
-> X a
withWindowSet ((StackSet WorkspaceId (Layout Window) Window ScreenId ScreenDetail
-> X ())
-> X ())
-> (StackSet
WorkspaceId (Layout Window) Window ScreenId ScreenDetail
-> X ())
-> X ()
forall a b. (a -> b) -> a -> b
$ \StackSet WorkspaceId (Layout Window) Window ScreenId ScreenDetail
s -> do
let visibleWins :: [Window]
visibleWins = (Maybe (Stack Window) -> [Window]
forall a. Maybe (Stack a) -> [a]
W.integrate' (Maybe (Stack Window) -> [Window])
-> (StackSet
WorkspaceId (Layout Window) Window ScreenId ScreenDetail
-> Maybe (Stack Window))
-> StackSet
WorkspaceId (Layout Window) Window ScreenId ScreenDetail
-> [Window]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Workspace WorkspaceId (Layout Window) Window
-> Maybe (Stack Window)
forall i l a. Workspace i l a -> Maybe (Stack a)
W.stack (Workspace WorkspaceId (Layout Window) Window
-> Maybe (Stack Window))
-> (StackSet
WorkspaceId (Layout Window) Window ScreenId ScreenDetail
-> Workspace WorkspaceId (Layout Window) Window)
-> StackSet
WorkspaceId (Layout Window) Window ScreenId ScreenDetail
-> Maybe (Stack Window)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Screen WorkspaceId (Layout Window) Window ScreenId ScreenDetail
-> Workspace WorkspaceId (Layout Window) Window
forall i l a sid sd. Screen i l a sid sd -> Workspace i l a
W.workspace (Screen WorkspaceId (Layout Window) Window ScreenId ScreenDetail
-> Workspace WorkspaceId (Layout Window) Window)
-> (StackSet
WorkspaceId (Layout Window) Window ScreenId ScreenDetail
-> Screen WorkspaceId (Layout Window) Window ScreenId ScreenDetail)
-> StackSet
WorkspaceId (Layout Window) Window ScreenId ScreenDetail
-> Workspace WorkspaceId (Layout Window) Window
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StackSet WorkspaceId (Layout Window) Window ScreenId ScreenDetail
-> Screen WorkspaceId (Layout Window) Window ScreenId ScreenDetail
forall i l a sid sd. StackSet i l a sid sd -> Screen i l a sid sd
W.current (StackSet WorkspaceId (Layout Window) Window ScreenId ScreenDetail
-> [Window])
-> StackSet
WorkspaceId (Layout Window) Window ScreenId ScreenDetail
-> [Window]
forall a b. (a -> b) -> a -> b
$ StackSet WorkspaceId (Layout Window) Window ScreenId ScreenDetail
s) [Window] -> [Window] -> [Window]
forall a. [a] -> [a] -> [a]
++
(Screen WorkspaceId (Layout Window) Window ScreenId ScreenDetail
-> [Window])
-> [Screen
WorkspaceId (Layout Window) Window ScreenId ScreenDetail]
-> [Window]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Maybe (Stack Window) -> [Window]
forall a. Maybe (Stack a) -> [a]
W.integrate' (Maybe (Stack Window) -> [Window])
-> (Screen WorkspaceId (Layout Window) Window ScreenId ScreenDetail
-> Maybe (Stack Window))
-> Screen WorkspaceId (Layout Window) Window ScreenId ScreenDetail
-> [Window]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Workspace WorkspaceId (Layout Window) Window
-> Maybe (Stack Window)
forall i l a. Workspace i l a -> Maybe (Stack a)
W.stack (Workspace WorkspaceId (Layout Window) Window
-> Maybe (Stack Window))
-> (Screen WorkspaceId (Layout Window) Window ScreenId ScreenDetail
-> Workspace WorkspaceId (Layout Window) Window)
-> Screen WorkspaceId (Layout Window) Window ScreenId ScreenDetail
-> Maybe (Stack Window)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Screen WorkspaceId (Layout Window) Window ScreenId ScreenDetail
-> Workspace WorkspaceId (Layout Window) Window
forall i l a sid sd. Screen i l a sid sd -> Workspace i l a
W.workspace) (StackSet WorkspaceId (Layout Window) Window ScreenId ScreenDetail
-> [Screen
WorkspaceId (Layout Window) Window ScreenId ScreenDetail]
forall i l a sid sd. StackSet i l a sid sd -> [Screen i l a sid sd]
W.visible StackSet WorkspaceId (Layout Window) Window ScreenId ScreenDetail
s)
[Window] -> (Window -> X ()) -> X ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Window]
visibleWins ((Window -> X ()) -> X ()) -> (Window -> X ()) -> X ()
forall a b. (a -> b) -> a -> b
$ \Window
w -> do
Opacity
o <- Opacity -> X Opacity -> X Opacity
forall a. a -> X a -> X a
userCodeDef (Rational -> Opacity
Opacity Rational
1) (FadeHook -> Window -> X Opacity
forall a. Query a -> Window -> X a
runQuery FadeHook
h Window
w)
Window -> Rational -> X ()
setOpacity Window
w (Rational -> X ()) -> Rational -> X ()
forall a b. (a -> b) -> a -> b
$ case Opacity
o of
Opacity
OEmpty -> Rational
1
Opacity Rational
r -> Rational
r
fadeWindowsEventHook :: Event -> X All
fadeWindowsEventHook :: Event -> X All
fadeWindowsEventHook MapNotifyEvent{} =
(XConf -> XConfig Layout) -> X (XConfig Layout)
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks XConf -> XConfig Layout
config X (XConfig Layout) -> (XConfig Layout -> X ()) -> X ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= XConfig Layout -> X ()
forall (l :: * -> *). XConfig l -> X ()
logHook X () -> X All -> X All
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> All -> X All
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> All
All Bool
True)
fadeWindowsEventHook Event
_ = All -> X All
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> All
All Bool
True)
clampRatio :: Rational -> Rational
clampRatio :: Rational -> Rational
clampRatio Rational
r | Rational
r Rational -> Rational -> Bool
forall a. Ord a => a -> a -> Bool
>= Rational
0 Bool -> Bool -> Bool
&& Rational
r Rational -> Rational -> Bool
forall a. Ord a => a -> a -> Bool
<= Rational
1 = Rational
r
| Rational
r Rational -> Rational -> Bool
forall a. Ord a => a -> a -> Bool
< Rational
0 = Rational
0
| Bool
otherwise = Rational
1