{-# LANGUAGE MultiParamTypeClasses, TypeSynonymInstances, FlexibleContexts, PatternGuards #-}
----------------------------------------------------------------------------
-- |
-- Module      :  XMonad.Layout.Minimize
-- Description :  Minimize windows, temporarily removing them from the layout.
-- Copyright   :  (c) Jan Vornberger 2009, Alejandro Serrano 2010
-- License     :  BSD3-style (see LICENSE)
--
-- Maintainer  :  jan.vornberger@informatik.uni-oldenburg.de
-- Stability   :  unstable
-- Portability :  not portable
--
-- Makes it possible to minimize windows, temporarily removing them
-- from the layout until they are restored.
--
-----------------------------------------------------------------------------

module XMonad.Layout.Minimize (
        -- * Usage
        -- $usage
        Minimize,
        minimize,
    ) where

import XMonad
import qualified XMonad.StackSet as W
import XMonad.Util.Minimize (Minimized(..))
import XMonad.Layout.LayoutModifier
import XMonad.Layout.BoringWindows as BW
import qualified XMonad.Util.ExtensibleState as XS

-- $usage
-- You can use this module with the following in your @~\/.xmonad\/xmonad.hs@:
--
-- > import XMonad.Layout.Minimize
--
-- Then edit your @layoutHook@ by adding the Minimize layout modifier:
--
-- > myLayout = minimize (Tall 1 (3/100) (1/2)) ||| Full ||| etc..
-- > main = xmonad def { layoutHook = myLayout }
--
-- For more detailed instructions on editing the layoutHook see:
--
-- "XMonad.Doc.Extending#Editing_the_layout_hook"
--
-- The module is designed to work together with "XMonad.Layout.BoringWindows" so
-- that minimized windows will be skipped over when switching the focused window with
-- the keyboard. Include 'BW.boringWindows' in your layout hook and see the
-- documentation of "XMonad.Layout.BoringWindows" on how to modify your keybindings.
--
-- See "XMonad.Actions.Minimize" for possible actions for minimizing/restoring windows
--
-- Also see "XMonad.Hooks.Minimize" if you want to be able to minimize
-- and restore windows from your taskbar.

data Minimize a = Minimize deriving ( ReadPrec [Minimize a]
ReadPrec (Minimize a)
Int -> ReadS (Minimize a)
ReadS [Minimize a]
(Int -> ReadS (Minimize a))
-> ReadS [Minimize a]
-> ReadPrec (Minimize a)
-> ReadPrec [Minimize a]
-> Read (Minimize a)
forall a. ReadPrec [Minimize a]
forall a. ReadPrec (Minimize a)
forall a. Int -> ReadS (Minimize a)
forall a. ReadS [Minimize a]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [Minimize a]
$creadListPrec :: forall a. ReadPrec [Minimize a]
readPrec :: ReadPrec (Minimize a)
$creadPrec :: forall a. ReadPrec (Minimize a)
readList :: ReadS [Minimize a]
$creadList :: forall a. ReadS [Minimize a]
readsPrec :: Int -> ReadS (Minimize a)
$creadsPrec :: forall a. Int -> ReadS (Minimize a)
Read, Int -> Minimize a -> ShowS
[Minimize a] -> ShowS
Minimize a -> String
(Int -> Minimize a -> ShowS)
-> (Minimize a -> String)
-> ([Minimize a] -> ShowS)
-> Show (Minimize a)
forall a. Int -> Minimize a -> ShowS
forall a. [Minimize a] -> ShowS
forall a. Minimize a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Minimize a] -> ShowS
$cshowList :: forall a. [Minimize a] -> ShowS
show :: Minimize a -> String
$cshow :: forall a. Minimize a -> String
showsPrec :: Int -> Minimize a -> ShowS
$cshowsPrec :: forall a. Int -> Minimize a -> ShowS
Show )
minimize :: l Window -> ModifiedLayout Minimize l Window
minimize :: forall (l :: * -> *). l Window -> ModifiedLayout Minimize l Window
minimize = Minimize Window -> l Window -> ModifiedLayout Minimize l Window
forall (m :: * -> *) (l :: * -> *) a.
m a -> l a -> ModifiedLayout m l a
ModifiedLayout Minimize Window
forall a. Minimize a
Minimize


instance LayoutModifier Minimize Window where
    modifierDescription :: Minimize Window -> String
modifierDescription Minimize Window
_ = String
"Minimize"

    modifyLayout :: forall (l :: * -> *).
LayoutClass l Window =>
Minimize Window
-> Workspace String (l Window) Window
-> Rectangle
-> X ([(Window, Rectangle)], Maybe (l Window))
modifyLayout Minimize Window
Minimize Workspace String (l Window) Window
wksp Rectangle
rect = do
        [Window]
minimized <- (Minimized -> [Window]) -> X [Window]
forall a (m :: * -> *) b.
(ExtensionClass a, XLike m) =>
(a -> b) -> m b
XS.gets Minimized -> [Window]
minimizedStack
        let stack :: Maybe (Stack Window)
stack = Workspace String (l Window) Window -> Maybe (Stack Window)
forall i l a. Workspace i l a -> Maybe (Stack a)
W.stack Workspace String (l Window) Window
wksp
            filtStack :: Maybe (Stack Window)
filtStack = Maybe (Stack Window)
stack Maybe (Stack Window)
-> (Stack Window -> Maybe (Stack Window)) -> Maybe (Stack Window)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Window -> Bool) -> Stack Window -> Maybe (Stack Window)
forall a. (a -> Bool) -> Stack a -> Maybe (Stack a)
W.filter (Window -> [Window] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [Window]
minimized)
        Workspace String (l Window) Window
-> Rectangle -> X ([(Window, Rectangle)], Maybe (l Window))
forall (layout :: * -> *) a.
LayoutClass layout a =>
Workspace String (layout a) a
-> Rectangle -> X ([(a, Rectangle)], Maybe (layout a))
runLayout (Workspace String (l Window) Window
wksp {stack :: Maybe (Stack Window)
W.stack = Maybe (Stack Window)
filtStack}) Rectangle
rect

    handleMess :: Minimize Window -> SomeMessage -> X (Maybe (Minimize Window))
handleMess Minimize Window
Minimize SomeMessage
m
        | Just UpdateBoring
BW.UpdateBoring <- SomeMessage -> Maybe UpdateBoring
forall m. Message m => SomeMessage -> Maybe m
fromMessage SomeMessage
m = do
            [Window]
minimized <- (Minimized -> [Window]) -> X [Window]
forall a (m :: * -> *) b.
(ExtensionClass a, XLike m) =>
(a -> b) -> m b
XS.gets Minimized -> [Window]
minimizedStack
            Workspace String (Layout Window) Window
ws <- (XState -> Workspace String (Layout Window) Window)
-> X (Workspace String (Layout Window) Window)
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets (Screen String (Layout Window) Window ScreenId ScreenDetail
-> Workspace String (Layout Window) Window
forall i l a sid sd. Screen i l a sid sd -> Workspace i l a
W.workspace (Screen String (Layout Window) Window ScreenId ScreenDetail
 -> Workspace String (Layout Window) Window)
-> (XState
    -> Screen String (Layout Window) Window ScreenId ScreenDetail)
-> XState
-> Workspace String (Layout Window) Window
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StackSet String (Layout Window) Window ScreenId ScreenDetail
-> Screen String (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 String (Layout Window) Window ScreenId ScreenDetail
 -> Screen String (Layout Window) Window ScreenId ScreenDetail)
-> (XState
    -> StackSet String (Layout Window) Window ScreenId ScreenDetail)
-> XState
-> Screen String (Layout Window) Window ScreenId ScreenDetail
forall b c a. (b -> c) -> (a -> b) -> a -> c
. XState
-> StackSet String (Layout Window) Window ScreenId ScreenDetail
windowset)
            (BoringMessage -> Workspace String (Layout Window) Window -> X ())
-> Workspace String (Layout Window) Window -> BoringMessage -> X ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip BoringMessage -> Workspace String (Layout Window) Window -> X ()
forall a.
Message a =>
a -> Workspace String (Layout Window) Window -> X ()
sendMessageWithNoRefresh Workspace String (Layout Window) Window
ws (BoringMessage -> X ()) -> BoringMessage -> X ()
forall a b. (a -> b) -> a -> b
$ String -> [Window] -> BoringMessage
BW.Replace String
"Minimize" [Window]
minimized
            Maybe (Minimize Window) -> X (Maybe (Minimize Window))
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (Minimize Window)
forall a. Maybe a
Nothing
        | Bool
otherwise = Maybe (Minimize Window) -> X (Maybe (Minimize Window))
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (Minimize Window)
forall a. Maybe a
Nothing