----------------------------------------------------------------------------
-- |
-- Module      :  XMonad.Hooks.Minimize
-- Description :  Handle window manager hints to minimize and restore windows.
-- Copyright   :  (c) Justin Bogner 2010
-- License     :  BSD3-style (see LICENSE)
--
-- Maintainer  :  Justin Bogner <mail@justinbogner.com>
-- Stability   :  unstable
-- Portability :  not portable
--
-- Handles window manager hints to minimize and restore windows. Use
-- this with "XMonad.Layout.Minimize".
--
-----------------------------------------------------------------------------

module XMonad.Hooks.Minimize
    ( -- * Usage
      -- $usage
      minimizeEventHook
    ) where

import XMonad
import XMonad.Actions.Minimize
import XMonad.Prelude

-- $usage
-- You can use this module with the following in your @xmonad.hs@:
--
-- > import XMonad.Hooks.Minimize
-- > import XMonad.Layout.Minimize
-- >
-- > myHandleEventHook = minimizeEventHook
-- > myLayout = minimize (Tall 1 (3/100) (1/2)) ||| Full ||| etc..
-- > main = xmonad def { layoutHook = myLayout
-- >                   , handleEventHook = myHandleEventHook }

minimizeEventHook :: Event -> X All
minimizeEventHook :: Event -> X All
minimizeEventHook ClientMessageEvent{ev_window :: Event -> Atom
ev_window = Atom
w,
                                     ev_message_type :: Event -> Atom
ev_message_type = Atom
mt,
                                     ev_data :: Event -> [CInt]
ev_data = [CInt]
dt} = do
    Atom
a_aw <- String -> X Atom
getAtom String
"_NET_ACTIVE_WINDOW"
    Atom
a_cs <- String -> X Atom
getAtom String
"WM_CHANGE_STATE"

    forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Atom
mt forall a. Eq a => a -> a -> Bool
== Atom
a_aw) forall a b. (a -> b) -> a -> b
$ Atom -> X ()
maximizeWindow Atom
w
    forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Atom
mt forall a. Eq a => a -> a -> Bool
== Atom
a_cs) forall a b. (a -> b) -> a -> b
$ case forall a. [a] -> Maybe a
listToMaybe [CInt]
dt of
      Maybe CInt
Nothing  -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
      Just CInt
dth -> do
        let message :: Int
message = forall a b. (Integral a, Num b) => a -> b
fromIntegral CInt
dth
        forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
message forall a. Eq a => a -> a -> Bool
== Int
normalState) forall a b. (a -> b) -> a -> b
$ Atom -> X ()
maximizeWindow Atom
w
        forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
message forall a. Eq a => a -> a -> Bool
== Int
iconicState) forall a b. (a -> b) -> a -> b
$ Atom -> X ()
minimizeWindow Atom
w

    forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> All
All Bool
True)
minimizeEventHook Event
_ = forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> All
All Bool
True)