module XMonad.Actions.CycleWorkspaceByScreen (
cycleWorkspaceOnScreen
, cycleWorkspaceOnCurrentScreen
, handleKeyEvent
, repeatableAction
) where
import Data.IORef
import XMonad
import XMonad.Prelude
import XMonad.Hooks.WorkspaceHistory
import XMonad.Actions.Repeatable (repeatable)
import qualified XMonad.StackSet as W
{-# DEPRECATED repeatableAction "Use XMonad.Actions.Repeatable.repeatable" #-}
repeatableAction :: [KeySym] -> KeySym -> (EventType -> KeySym -> X ()) -> X ()
repeatableAction :: [KeySym] -> KeySym -> (EventType -> KeySym -> X ()) -> X ()
repeatableAction = [KeySym] -> KeySym -> (EventType -> KeySym -> X ()) -> X ()
repeatable
handleKeyEvent :: EventType
-> KeySym
-> X ()
-> EventType
-> KeySym
-> Maybe (X ())
handleKeyEvent :: EventType -> KeySym -> X () -> EventType -> KeySym -> Maybe (X ())
handleKeyEvent EventType
eventType KeySym
key X ()
action = EventType -> KeySym -> Maybe (X ())
helper
where
helper :: EventType -> KeySym -> Maybe (X ())
helper EventType
et KeySym
k
| EventType
et forall a. Eq a => a -> a -> Bool
== EventType
eventType Bool -> Bool -> Bool
&& KeySym
k forall a. Eq a => a -> a -> Bool
== KeySym
key = forall a. a -> Maybe a
Just X ()
action
| Bool
otherwise = forall a. Maybe a
Nothing
runFirst :: [EventType -> KeySym -> Maybe (X ())] -> EventType -> KeySym -> X ()
runFirst :: [EventType -> KeySym -> Maybe (X ())]
-> EventType -> KeySym -> X ()
runFirst [EventType -> KeySym -> Maybe (X ())]
matchers EventType
eventType KeySym
key =
forall a. a -> Maybe a -> a
fromMaybe (forall (m :: * -> *) a. Monad m => a -> m a
return ()) forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. Monad m => m (m a) -> m a
join forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find forall a. Maybe a -> Bool
isJust forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (\EventType -> KeySym -> Maybe (X ())
fn -> EventType -> KeySym -> Maybe (X ())
fn EventType
eventType KeySym
key) [EventType -> KeySym -> Maybe (X ())]
matchers
cycleWorkspaceOnScreen
:: ScreenId
-> [KeySym]
-> KeySym
-> KeySym
-> X ()
cycleWorkspaceOnScreen :: ScreenId -> [KeySym] -> KeySym -> KeySym -> X ()
cycleWorkspaceOnScreen ScreenId
screenId [KeySym]
mods KeySym
nextKey KeySym
prevKey = X () -> X ()
workspaceHistoryTransaction forall a b. (a -> b) -> a -> b
$ do
[(ScreenId, [WorkspaceId])]
startingHistory <- X [(ScreenId, [WorkspaceId])]
workspaceHistoryByScreen
IORef Int
currentWSIndex <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
io forall a b. (a -> b) -> a -> b
$ forall a. a -> IO (IORef a)
newIORef Int
1
let cycleWorkspaces :: [WorkspaceId]
cycleWorkspaces = forall a. a -> Maybe a -> a
fromMaybe [] forall a b. (a -> b) -> a -> b
$ forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup ScreenId
screenId [(ScreenId, [WorkspaceId])]
startingHistory
getAndIncrementWS :: Int -> IO WorkspaceId
getAndIncrementWS Int
increment = do
Int
current <- forall a. IORef a -> IO a
readIORef IORef Int
currentWSIndex
forall a. IORef a -> (a -> a) -> IO ()
modifyIORef
IORef Int
currentWSIndex
((forall a. Integral a => a -> a -> a
`mod` forall (t :: * -> *) a. Foldable t => t a -> Int
length [WorkspaceId]
cycleWorkspaces) forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. Num a => a -> a -> a
+ Int
increment))
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ [WorkspaceId]
cycleWorkspaces forall a. [a] -> Int -> a
!! Int
current
focusIncrement :: Int -> X ()
focusIncrement Int
i = forall (m :: * -> *) a. MonadIO m => IO a -> m a
io (Int -> IO WorkspaceId
getAndIncrementWS Int
i) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ((WindowSet -> WindowSet) -> X ()
windows forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s i l a sd.
(Eq s, Eq i) =>
i -> StackSet i l a s sd -> StackSet i l a s sd
W.greedyView)
[KeySym] -> KeySym -> (EventType -> KeySym -> X ()) -> X ()
repeatable [KeySym]
mods KeySym
nextKey forall a b. (a -> b) -> a -> b
$
[EventType -> KeySym -> Maybe (X ())]
-> EventType -> KeySym -> X ()
runFirst
[ EventType -> KeySym -> X () -> EventType -> KeySym -> Maybe (X ())
handleKeyEvent EventType
keyPress KeySym
nextKey forall a b. (a -> b) -> a -> b
$ Int -> X ()
focusIncrement Int
1
, EventType -> KeySym -> X () -> EventType -> KeySym -> Maybe (X ())
handleKeyEvent EventType
keyPress KeySym
prevKey forall a b. (a -> b) -> a -> b
$ Int -> X ()
focusIncrement (-Int
1)
]
forall (m :: * -> *) a. Monad m => a -> m a
return ()
cycleWorkspaceOnCurrentScreen
:: [KeySym] -> KeySym -> KeySym -> X ()
cycleWorkspaceOnCurrentScreen :: [KeySym] -> KeySym -> KeySym -> X ()
cycleWorkspaceOnCurrentScreen [KeySym]
mods KeySym
n KeySym
p =
forall a. (WindowSet -> X a) -> X a
withWindowSet forall a b. (a -> b) -> a -> b
$ \WindowSet
ws ->
ScreenId -> [KeySym] -> KeySym -> KeySym -> X ()
cycleWorkspaceOnScreen (forall i l a sid sd. Screen i l a sid sd -> sid
W.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
W.current WindowSet
ws) [KeySym]
mods KeySym
n KeySym
p