X.U.NamedScratchpad: Extract common parts of ns{HideOnFocusLoss,SingleScratchpadPerWorkspace}

This commit is contained in:
Tony Zorman
2023-10-24 20:58:43 +02:00
parent 105cbe0362
commit e1dc2a3750

View File

@@ -62,7 +62,7 @@ import XMonad.Actions.TagWindows (addTag, delTag)
import XMonad.Hooks.ManageHelpers (doRectFloat) import XMonad.Hooks.ManageHelpers (doRectFloat)
import XMonad.Hooks.RefocusLast (withRecentsIn) import XMonad.Hooks.RefocusLast (withRecentsIn)
import XMonad.Hooks.StatusBar.PP (PP, ppSort) import XMonad.Hooks.StatusBar.PP (PP, ppSort)
import XMonad.Prelude (appEndo, filterM, findM, foldl', for_, unless, void, when, (<=<)) import XMonad.Prelude (appEndo, filterM, findM, foldl', for_, liftA2, unless, void, when, (<=<))
import qualified Data.List.NonEmpty as NE import qualified Data.List.NonEmpty as NE
import qualified Data.Map.Strict as Map import qualified Data.Map.Strict as Map
@@ -285,23 +285,15 @@ allNamedScratchpadAction = someNamedScratchpadAction mapM_ runApplication
-- > -- enable hiding for all of @myScratchpads@ -- > -- enable hiding for all of @myScratchpads@
-- > } -- > }
nsHideOnFocusLoss :: NamedScratchpads -> X () nsHideOnFocusLoss :: NamedScratchpads -> X ()
nsHideOnFocusLoss scratches = withWindowSet $ \winSet -> do nsHideOnFocusLoss scratches =
let cur = W.currentTag winSet nsHideOnCondition $ \ lastFocus _curFoc _ws hideScratch ->
withRecentsIn cur () $ \lastFocus curFocus -> do whenX (isNSP lastFocus scratches) $
let isWorthy = hideScratch lastFocus
-- Check for the window being on the current workspace; if there
-- is no history (i.e., curFocus ≡ lastFocus), don't do anything
-- because the potential scratchpad is definitely focused.
lastFocus `elem` W.index winSet && lastFocus /= curFocus
-- Don't do anything on the NSP workspace, lest the world explodes.
&& cur /= scratchpadWorkspaceTag
when isWorthy $
whenX (isNSP lastFocus scratches) $
shiftToNSP (W.workspaces winSet) ($ lastFocus)
-- | A @logHook@ to have only one active scratchpad on a workspace. This can be -- | A @logHook@ to have only one active scratchpad on a workspace. This can
-- useful when working with multiple floating scratchpads which would otherwise be stacked. Note that this also requires you -- be useful when working with multiple floating scratchpads which would
-- to use the 'XMonad.Hooks.RefocusLast.refocusLastLogHook'. -- otherwise be stacked. Note that this also requires you to use the
-- 'XMonad.Hooks.RefocusLast.refocusLastLogHook'.
-- --
-- ==== __Example__ -- ==== __Example__
-- --
@@ -314,24 +306,37 @@ nsHideOnFocusLoss scratches = withWindowSet $ \winSet -> do
-- > -- enable hiding for all of @myScratchpads@ -- > -- enable hiding for all of @myScratchpads@
-- > } -- > }
nsSingleScratchpadPerWorkspace :: NamedScratchpads -> X () nsSingleScratchpadPerWorkspace :: NamedScratchpads -> X ()
nsSingleScratchpadPerWorkspace scratches = withWindowSet $ \winSet -> do nsSingleScratchpadPerWorkspace scratches =
let cur = W.currentTag winSet nsHideOnCondition $ \ _lastFocus curFocus winSet hideScratch -> do
let allWindowsOnCurrentWorkspace = W.index winSet allScratchesButCurrent <-
withRecentsIn cur () $ \lastFocus curFocus -> do filterM (liftA2 (<||>) (pure . (/= curFocus)) (`isNSP` scratches))
allScratchesOnCurrentWS <- filterM (`isNSP` scratches) allWindowsOnCurrentWorkspace (W.index winSet)
let allScratchesButCurrent = filter (/= curFocus) allScratchesOnCurrentWS whenX (isNSP curFocus scratches) $
hideScratch = \s -> shiftToNSP (W.workspaces winSet) ($ s) for_ allScratchesButCurrent hideScratch
let isWorthy = -- | Hide scratchpads according to some condition. See 'nsHideOnFocusLoss' and
-- Check for the window being on the current workspace; if there -- 'nsSingleScratchpadPerWorkspace' for usage examples.
-- is no history (i.e., curFocus ≡ lastFocus), don't do anything nsHideOnCondition
-- because the potential scratchpad is definitely focused. :: ( Window -- Last focus.
lastFocus `elem` W.index winSet && lastFocus /= curFocus -> Window -- Current focus.
-- Don't do anything on the NSP workspace, lest the world explodes. -> WindowSet -- Current windowset.
&& cur /= scratchpadWorkspaceTag -> (Window -> X ()) -- A function to hide the named scratchpad.
-> X ())
-> X ()
nsHideOnCondition cond = withWindowSet $ \winSet -> do
let cur = W.currentTag winSet
withRecentsIn cur () $ \lastFocus curFocus -> do
let hideScratch :: Window -> X ()
hideScratch win = shiftToNSP (W.workspaces winSet) ($ win)
isWorthy =
-- Check for the window being on the current workspace; if there
-- is no history (i.e., curFocus ≡ lastFocus), don't do anything
-- because the potential scratchpad is definitely focused.
lastFocus `elem` W.index winSet && lastFocus /= curFocus
-- Don't do anything on the NSP workspace, lest the world explodes.
&& cur /= scratchpadWorkspaceTag
when isWorthy $ when isWorthy $
whenX (isNSP curFocus scratches) $ cond lastFocus curFocus winSet hideScratch
for_ allScratchesButCurrent hideScratch
-- | Execute some action on a named scratchpad. -- | Execute some action on a named scratchpad.
-- --