----------------------------------------------------------------------------
-- |
-- 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\/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 -> Window
ev_window = Window
w,
                                     ev_message_type :: Event -> Window
ev_message_type = Window
mt,
                                     ev_data :: Event -> [CInt]
ev_data = [CInt]
dt} = do
    Window
a_aw <- String -> X Window
getAtom String
"_NET_ACTIVE_WINDOW"
    Window
a_cs <- String -> X Window
getAtom String
"WM_CHANGE_STATE"

    Bool -> X () -> X ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Window
mt Window -> Window -> Bool
forall a. Eq a => a -> a -> Bool
== Window
a_aw) (X () -> X ()) -> X () -> X ()
forall a b. (a -> b) -> a -> b
$ Window -> X ()
maximizeWindow Window
w
    Bool -> X () -> X ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Window
mt Window -> Window -> Bool
forall a. Eq a => a -> a -> Bool
== Window
a_cs) (X () -> X ()) -> X () -> X ()
forall a b. (a -> b) -> a -> b
$ do
      let message :: Int
message = CInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (CInt -> Int) -> ([CInt] -> CInt) -> [CInt] -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [CInt] -> CInt
forall a. [a] -> a
head ([CInt] -> Int) -> [CInt] -> Int
forall a b. (a -> b) -> a -> b
$ [CInt]
dt
      Bool -> X () -> X ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
message Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
normalState) (X () -> X ()) -> X () -> X ()
forall a b. (a -> b) -> a -> b
$ Window -> X ()
maximizeWindow Window
w
      Bool -> X () -> X ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
message Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
iconicState) (X () -> X ()) -> X () -> X ()
forall a b. (a -> b) -> a -> b
$ Window -> X ()
minimizeWindow Window
w

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