-- |
-- Module      :  XMonad.Actions.OnScreen
-- Description :  Control workspaces on different screens (in xinerama mode).
-- Copyright   :  (c) 2009-2025 Nils Schweinsberg
-- License     :  BSD3-style (see LICENSE)
--
-- Maintainer  :  Nils Schweinsberg <mail@nils.cc>
-- Stability   :  unstable
-- Portability :  unportable
--
-- Control workspaces on different screens (in xinerama mode).
module XMonad.Actions.OnScreen
  ( -- * Usage
    -- $usage
    onScreen,
    onScreen',
    Focus (..),
    viewOnScreen,
    greedyViewOnScreen,
    onlyOnScreen,
    toggleOnScreen,
    toggleGreedyOnScreen,
  )
where

import XMonad
import XMonad.Prelude (empty, fromMaybe, guard)
import XMonad.StackSet hiding (new)

-- | Focus data definitions
data Focus
  = -- | always focus the new screen
    FocusNew
  | -- | always keep the focus on the current screen
    FocusCurrent
  | -- | always focus tag i on the new stack
    FocusTag WorkspaceId
  | -- | focus tag i only if workspace with tag i is visible on the old stack
    FocusTagVisible WorkspaceId

-- | Run any function that modifies the stack on a given screen. This function
-- will also need to know which Screen to focus after the function has been
-- run.
onScreen ::
  -- | function to run
  (WindowSet -> WindowSet) ->
  -- | what to do with the focus
  Focus ->
  -- | screen id
  ScreenId ->
  -- | current stack
  WindowSet ->
  WindowSet
onScreen :: (WindowSet -> WindowSet)
-> Focus -> ScreenId -> WindowSet -> WindowSet
onScreen WindowSet -> WindowSet
f Focus
foc ScreenId
sc WindowSet
st = forall a. a -> Maybe a -> a
fromMaybe WindowSet
st forall a b. (a -> b) -> a -> b
$ do
  WorkspaceId
ws <- forall s i l a sd. Eq s => s -> StackSet i l a s sd -> Maybe i
lookupWorkspace ScreenId
sc WindowSet
st

  let fStack :: WindowSet
fStack = WindowSet -> WindowSet
f forall a b. (a -> b) -> a -> b
$ forall s i l a sd.
(Eq s, Eq i) =>
i -> StackSet i l a s sd -> StackSet i l a s sd
view WorkspaceId
ws WindowSet
st

  forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Focus -> WindowSet -> WindowSet -> WindowSet
setFocus Focus
foc WindowSet
st WindowSet
fStack

-- set focus for new stack
setFocus ::
  Focus ->
  -- | old stack
  WindowSet ->
  -- | new stack
  WindowSet ->
  WindowSet
setFocus :: Focus -> WindowSet -> WindowSet -> WindowSet
setFocus Focus
FocusNew WindowSet
_ WindowSet
new = WindowSet
new
setFocus Focus
FocusCurrent WindowSet
old WindowSet
new =
  case forall s i l a sd. Eq s => s -> StackSet i l a s sd -> Maybe i
lookupWorkspace (forall i l a sid sd. Screen i l a sid sd -> sid
screen forall a b. (a -> b) -> a -> b
$ forall i l a sid sd. StackSet i l a sid sd -> Screen i l a sid sd
current WindowSet
old) WindowSet
new of
    Maybe WorkspaceId
Nothing -> WindowSet
new
    Just WorkspaceId
i -> forall s i l a sd.
(Eq s, Eq i) =>
i -> StackSet i l a s sd -> StackSet i l a s sd
view WorkspaceId
i WindowSet
new
setFocus (FocusTag WorkspaceId
i) WindowSet
_ WindowSet
new = forall s i l a sd.
(Eq s, Eq i) =>
i -> StackSet i l a s sd -> StackSet i l a s sd
view WorkspaceId
i WindowSet
new
setFocus (FocusTagVisible WorkspaceId
i) WindowSet
old WindowSet
new =
  if WorkspaceId
i forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` forall a b. (a -> b) -> [a] -> [b]
map (forall i l a. Workspace i l a -> i
tag forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall i l a sid sd. Screen i l a sid sd -> Workspace i l a
workspace) (forall i l a sid sd. StackSet i l a sid sd -> [Screen i l a sid sd]
visible WindowSet
old)
    then Focus -> WindowSet -> WindowSet -> WindowSet
setFocus (WorkspaceId -> Focus
FocusTag WorkspaceId
i) WindowSet
old WindowSet
new
    else Focus -> WindowSet -> WindowSet -> WindowSet
setFocus Focus
FocusCurrent WindowSet
old WindowSet
new

-- | A variation of @onScreen@ which will take any @X ()@ function and run it
-- on the given screen.
-- Warning: This function will change focus even if the function it's supposed
-- to run doesn't succeed.
onScreen' ::
  -- | X function to run
  X () ->
  -- | focus
  Focus ->
  -- | screen id
  ScreenId ->
  X ()
onScreen' :: X () -> Focus -> ScreenId -> X ()
onScreen' X ()
x Focus
foc ScreenId
sc = do
  WindowSet
st <- forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets XState -> WindowSet
windowset
  case forall s i l a sd. Eq s => s -> StackSet i l a s sd -> Maybe i
lookupWorkspace ScreenId
sc WindowSet
st of
    Maybe WorkspaceId
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return ()
    Just WorkspaceId
ws -> do
      (WindowSet -> WindowSet) -> X ()
windows forall a b. (a -> b) -> a -> b
$ forall s i l a sd.
(Eq s, Eq i) =>
i -> StackSet i l a s sd -> StackSet i l a s sd
view WorkspaceId
ws
      X ()
x
      (WindowSet -> WindowSet) -> X ()
windows forall a b. (a -> b) -> a -> b
$ Focus -> WindowSet -> WindowSet -> WindowSet
setFocus Focus
foc WindowSet
st

-- | Switch to workspace @i@ on screen @sc@. If @i@ is visible use @view@ to
-- switch focus to the workspace @i@.
viewOnScreen ::
  -- | screen id
  ScreenId ->
  -- | index of the workspace
  WorkspaceId ->
  -- | current stack
  WindowSet ->
  WindowSet
viewOnScreen :: ScreenId -> WorkspaceId -> WindowSet -> WindowSet
viewOnScreen ScreenId
sid WorkspaceId
i =
  (WindowSet -> WindowSet)
-> Focus -> ScreenId -> WindowSet -> WindowSet
onScreen (forall s i l a sd.
(Eq s, Eq i) =>
i -> StackSet i l a s sd -> StackSet i l a s sd
view WorkspaceId
i) (WorkspaceId -> Focus
FocusTag WorkspaceId
i) ScreenId
sid

-- | Switch to workspace @i@ on screen @sc@. If @i@ is visible use @greedyView@
-- to switch the current workspace with workspace @i@.
greedyViewOnScreen ::
  -- | screen id
  ScreenId ->
  -- | index of the workspace
  WorkspaceId ->
  -- | current stack
  WindowSet ->
  WindowSet
greedyViewOnScreen :: ScreenId -> WorkspaceId -> WindowSet -> WindowSet
greedyViewOnScreen ScreenId
sid WorkspaceId
i =
  (WindowSet -> WindowSet)
-> Focus -> ScreenId -> WindowSet -> WindowSet
onScreen (forall s i l a sd.
(Eq s, Eq i) =>
i -> StackSet i l a s sd -> StackSet i l a s sd
greedyView WorkspaceId
i) (WorkspaceId -> Focus
FocusTagVisible WorkspaceId
i) ScreenId
sid

-- | Switch to workspace @i@ on screen @sc@. If @i@ is visible do nothing.
onlyOnScreen ::
  -- | screen id
  ScreenId ->
  -- | index of the workspace
  WorkspaceId ->
  -- | current stack
  WindowSet ->
  WindowSet
onlyOnScreen :: ScreenId -> WorkspaceId -> WindowSet -> WindowSet
onlyOnScreen ScreenId
sid WorkspaceId
i =
  (WindowSet -> WindowSet)
-> Focus -> ScreenId -> WindowSet -> WindowSet
onScreen (forall s i l a sd.
(Eq s, Eq i) =>
i -> StackSet i l a s sd -> StackSet i l a s sd
view WorkspaceId
i) Focus
FocusCurrent ScreenId
sid

-- | @toggleOrView@ as in "XMonad.Actions.CycleWS" for @onScreen@ with view
toggleOnScreen ::
  -- | screen id
  ScreenId ->
  -- | index of the workspace
  WorkspaceId ->
  -- | current stack
  WindowSet ->
  WindowSet
toggleOnScreen :: ScreenId -> WorkspaceId -> WindowSet -> WindowSet
toggleOnScreen ScreenId
sid WorkspaceId
i =
  (WindowSet -> WindowSet)
-> Focus -> ScreenId -> WindowSet -> WindowSet
onScreen ((WorkspaceId -> WindowSet -> WindowSet)
-> WorkspaceId -> WindowSet -> WindowSet
toggleOrView' forall s i l a sd.
(Eq s, Eq i) =>
i -> StackSet i l a s sd -> StackSet i l a s sd
view WorkspaceId
i) Focus
FocusCurrent ScreenId
sid

-- | @toggleOrView@ from "XMonad.Actions.CycleWS" for @onScreen@ with greedyView
toggleGreedyOnScreen ::
  -- | screen id
  ScreenId ->
  -- | index of the workspace
  WorkspaceId ->
  -- | current stack
  WindowSet ->
  WindowSet
toggleGreedyOnScreen :: ScreenId -> WorkspaceId -> WindowSet -> WindowSet
toggleGreedyOnScreen ScreenId
sid WorkspaceId
i =
  (WindowSet -> WindowSet)
-> Focus -> ScreenId -> WindowSet -> WindowSet
onScreen ((WorkspaceId -> WindowSet -> WindowSet)
-> WorkspaceId -> WindowSet -> WindowSet
toggleOrView' forall s i l a sd.
(Eq s, Eq i) =>
i -> StackSet i l a s sd -> StackSet i l a s sd
greedyView WorkspaceId
i) Focus
FocusCurrent ScreenId
sid

-- a \"pure\" version of X.A.CycleWS.toggleOrDoSkip
toggleOrView' ::
  -- | function to run
  (WorkspaceId -> WindowSet -> WindowSet) ->
  -- | tag to look for
  WorkspaceId ->
  -- | current stackset
  WindowSet ->
  WindowSet
toggleOrView' :: (WorkspaceId -> WindowSet -> WindowSet)
-> WorkspaceId -> WindowSet -> WindowSet
toggleOrView' WorkspaceId -> WindowSet -> WindowSet
f WorkspaceId
i WindowSet
st = forall a. a -> Maybe a -> a
fromMaybe (WorkspaceId -> WindowSet -> WindowSet
f WorkspaceId
i WindowSet
st) forall a b. (a -> b) -> a -> b
$ do
  let st' :: [Workspace WorkspaceId (Layout Window) Window]
st' = forall i l a sid sd. StackSet i l a sid sd -> [Workspace i l a]
hidden WindowSet
st
  -- make sure we actually have to do something
  forall (f :: * -> *). Alternative f => Bool -> f ()
guard forall a b. (a -> b) -> a -> b
$ WorkspaceId
i forall a. Eq a => a -> a -> Bool
== (forall i l a. Workspace i l a -> i
tag forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall i l a sid sd. Screen i l a sid sd -> Workspace i l a
workspace forall a b. (a -> b) -> a -> b
$ forall i l a sid sd. StackSet i l a sid sd -> Screen i l a sid sd
current WindowSet
st)
  case [Workspace WorkspaceId (Layout Window) Window]
st' of
    [] -> forall (f :: * -> *) a. Alternative f => f a
empty
    (Workspace WorkspaceId (Layout Window) Window
h : [Workspace WorkspaceId (Layout Window) Window]
_) -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ WorkspaceId -> WindowSet -> WindowSet
f (forall i l a. Workspace i l a -> i
tag Workspace WorkspaceId (Layout Window) Window
h) WindowSet
st -- finally, toggle!

-- $usage
--
-- This module provides an easy way to control, what you see on other screens in
-- xinerama mode without having to focus them. Put this into your
-- @xmonad.hs@:
--
-- > import XMonad.Actions.OnScreen
--
-- Then add the appropriate keybindings, for example replace your current keys
-- to switch the workspaces with this at the bottom of your keybindings:
--
-- >     ++
-- >     [ ((m .|. modm, k), windows (f i))
-- >       | (i, k) <- zip (workspaces conf) ([xK_1 .. xK_9] ++ [xK_0])
-- >       , (f, m) <- [ (viewOnScreen 0, 0)
-- >                   , (viewOnScreen 1, controlMask)
-- >                   , (greedyView, controlMask .|. shiftMask) ]
-- >     ]
--
-- This will provide you with the following keybindings:
--
--      * modkey + 1-0:
--      Switch to workspace 1-0 on screen 0
--
--      * modkey + control + 1-0:
--      Switch to workspace 1-0 on screen 1
--
--      * modkey + control + shift + 1-0:
--      Default greedyView behaviour
--
--
-- A more basic version inside the default keybindings would be:
--
-- >        , ((modm .|. controlMask, xK_1), windows (viewOnScreen 0 "1"))
--
-- where 0 is the first screen and \"1\" the workspace with the tag \"1\".
--
-- For detailed instructions on editing your key bindings, see
-- <https://xmonad.org/TUTORIAL.html#customizing-xmonad the tutorial>.