-----------------------------------------------------------------------------
-- |
-- Module       : XMonad.Actions.WithAll
-- Description  : Perform a given action on all or certain groups of windows.
-- License      : BSD3-style (see LICENSE)
-- Stability    : unstable
-- Portability  : unportable
--
-- Provides functions for performing a given action on all or certain
-- groups of windows on the current workspace.
-----------------------------------------------------------------------------

module XMonad.Actions.WithAll (
    -- * Usage
    -- $usage
    sinkAll, withAll,
    withAll', killAll,
    killOthers) where

import XMonad.Prelude hiding (foldr)

import XMonad
import XMonad.StackSet

-- $usage
--
-- You can use this module with the following in your @xmonad.hs@:
--
-- > import XMonad.Actions.WithAll
--
-- then add a keybinding; for example:
--
--     , ((modm .|. shiftMask, xK_t), sinkAll)
--
-- For detailed instructions on editing your key bindings, see
-- <https://xmonad.org/TUTORIAL.html#customizing-xmonad the tutorial>.

-- | Un-float all floating windows on the current workspace.
sinkAll :: X ()
sinkAll :: X ()
sinkAll = (Window -> WindowSet -> WindowSet) -> X ()
withAll' Window -> WindowSet -> WindowSet
forall a i l s sd.
Ord a =>
a -> StackSet i l a s sd -> StackSet i l a s sd
sink

-- | Apply a function to all windows on the current workspace.
withAll' :: (Window -> WindowSet -> WindowSet) -> X ()
withAll' :: (Window -> WindowSet -> WindowSet) -> X ()
withAll' Window -> WindowSet -> WindowSet
f = (WindowSet -> WindowSet) -> X ()
windows ((WindowSet -> WindowSet) -> X ())
-> (WindowSet -> WindowSet) -> X ()
forall a b. (a -> b) -> a -> b
$ \WindowSet
ws -> let all' :: [Window]
all' = Maybe (Stack Window) -> [Window]
forall a. Maybe (Stack a) -> [a]
integrate' (Maybe (Stack Window) -> [Window])
-> (WindowSet -> Maybe (Stack Window)) -> WindowSet -> [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)
stack (Workspace WorkspaceId (Layout Window) Window
 -> Maybe (Stack Window))
-> (WindowSet -> Workspace WorkspaceId (Layout Window) Window)
-> WindowSet
-> 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
workspace (Screen WorkspaceId (Layout Window) Window ScreenId ScreenDetail
 -> Workspace WorkspaceId (Layout Window) Window)
-> (WindowSet
    -> Screen WorkspaceId (Layout Window) Window ScreenId ScreenDetail)
-> WindowSet
-> Workspace WorkspaceId (Layout Window) Window
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WindowSet
-> 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
current (WindowSet -> [Window]) -> WindowSet -> [Window]
forall a b. (a -> b) -> a -> b
$ WindowSet
ws
                              in (Window -> WindowSet -> WindowSet)
-> WindowSet -> [Window] -> WindowSet
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Window -> WindowSet -> WindowSet
f WindowSet
ws [Window]
all'

-- | Execute an 'X' action for each window on the current workspace.
withAll :: (Window -> X ()) -> X()
withAll :: (Window -> X ()) -> X ()
withAll Window -> X ()
f = (WindowSet -> X ()) -> X ()
forall a. (WindowSet -> X a) -> X a
withWindowSet ((WindowSet -> X ()) -> X ()) -> (WindowSet -> X ()) -> X ()
forall a b. (a -> b) -> a -> b
$ \WindowSet
ws -> let all' :: [Window]
all' = Maybe (Stack Window) -> [Window]
forall a. Maybe (Stack a) -> [a]
integrate' (Maybe (Stack Window) -> [Window])
-> (WindowSet -> Maybe (Stack Window)) -> WindowSet -> [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)
stack (Workspace WorkspaceId (Layout Window) Window
 -> Maybe (Stack Window))
-> (WindowSet -> Workspace WorkspaceId (Layout Window) Window)
-> WindowSet
-> 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
workspace (Screen WorkspaceId (Layout Window) Window ScreenId ScreenDetail
 -> Workspace WorkspaceId (Layout Window) Window)
-> (WindowSet
    -> Screen WorkspaceId (Layout Window) Window ScreenId ScreenDetail)
-> WindowSet
-> Workspace WorkspaceId (Layout Window) Window
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WindowSet
-> 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
current (WindowSet -> [Window]) -> WindowSet -> [Window]
forall a b. (a -> b) -> a -> b
$ WindowSet
ws
                                   in [Window] -> (Window -> X ()) -> X ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Window]
all' Window -> X ()
f

-- | Kill all the windows on the current workspace.
killAll :: X()
killAll :: X ()
killAll = (Window -> X ()) -> X ()
withAll Window -> X ()
killWindow

-- | Kill all the unfocused windows on the current workspace.
killOthers :: X ()
killOthers :: X ()
killOthers = (Window -> X ()) -> X ()
withUnfocused Window -> X ()
killWindow