Skip to content

Commit

Permalink
X.H.Rescreen: Allow overriding rescreen itself
Browse files Browse the repository at this point in the history
The primary motivation is to fix `rescreen` messing up the
workspaces/screens order when making small changes to the layout of
multiple screens — such as resizing virtual monitors via `xrandr
--setmonitor`.
  • Loading branch information
liskin committed Oct 17, 2024
1 parent 1c5261d commit 5680205
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions XMonad/Hooks/Rescreen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module XMonad.Hooks.Rescreen (
-- $usage
addAfterRescreenHook,
addRandrChangeHook,
setRescreenWorkspacesHook,
RescreenConfig(..),
rescreenHook,
) where
Expand Down Expand Up @@ -59,16 +60,18 @@ import qualified XMonad.Util.ExtensibleConf as XC
data RescreenConfig = RescreenConfig
{ afterRescreenHook :: X () -- ^ hook to invoke after 'rescreen'
, randrChangeHook :: X () -- ^ hook for other randr changes, e.g. (dis)connects
, rescreenWorkspacesHook :: Last (X ()) -- ^ hook to invoke instead of 'rescreen'
}

instance Default RescreenConfig where
def = RescreenConfig
{ afterRescreenHook = mempty
, randrChangeHook = mempty
, rescreenWorkspacesHook = mempty
}

instance Semigroup RescreenConfig where
RescreenConfig arh rch <> RescreenConfig arh' rch' = RescreenConfig (arh <> arh') (rch <> rch')
RescreenConfig arh rch rwh <> RescreenConfig arh' rch' rwh' = RescreenConfig (arh <> arh') (rch <> rch') (rwh <> rwh')

instance Monoid RescreenConfig where
mempty = def
Expand All @@ -89,8 +92,14 @@ instance Monoid RescreenConfig where
-- 'randrChangeHook' may be used to automatically trigger xrandr (or perhaps
-- autorandr) when outputs are (dis)connected.
--
-- 'rescreenWorkspacesHook' allows tweaking the 'rescreen' implementation,
-- to change the order workspaces are assigned to physical screens for
-- example.
--
-- Note that 'rescreenHook' is safe to use several times, 'rescreen' is still
-- done just once and hooks are invoked in sequence, also just once.
-- done just once and hooks are invoked in sequence (except
-- 'rescreenWorkspacesHook', which has a replace rather than sequence
-- semantics), also just once.
rescreenHook :: RescreenConfig -> XConfig l -> XConfig l
rescreenHook = XC.once $ \c -> c
{ startupHook = startupHook c <> rescreenStartupHook
Expand All @@ -104,6 +113,10 @@ addAfterRescreenHook h = rescreenHook def{ afterRescreenHook = userCodeDef () h
addRandrChangeHook :: X () -> XConfig l -> XConfig l
addRandrChangeHook h = rescreenHook def{ randrChangeHook = userCodeDef () h }

-- | Shortcut for 'rescreenHook'.
setRescreenWorkspacesHook :: X () -> XConfig l -> XConfig l
setRescreenWorkspacesHook h = rescreenHook def{ rescreenWorkspacesHook = pure (catchX h rescreen) }

-- | Startup hook to listen for @RRScreenChangeNotify@ events.
rescreenStartupHook :: X ()
rescreenStartupHook = do
Expand Down Expand Up @@ -132,7 +145,7 @@ handleEvent e = XC.with $ \RescreenConfig{..} -> do
-- configuration change, so rescreen and fire rescreenHook. Otherwise,
-- this is just a connect/disconnect, fire randrChangeHook.
if ev_event_type e == configureNotify || moreConfigureEvents
then rescreen >> afterRescreenHook
then fromMaybe rescreen (getLast rescreenWorkspacesHook) >> afterRescreenHook
else randrChangeHook

-- | Remove all X events of a given window and type from the event queue,
Expand Down

0 comments on commit 5680205

Please sign in to comment.