From eab9a3a58e40ee44e7d904287f6ea7ff6fefc2c5 Mon Sep 17 00:00:00 2001 From: Tomas Janousek Date: Thu, 3 Jun 2021 01:01:12 +0100 Subject: [PATCH] X.U.ExtensibleConf: Flip arguments of once(M) This appears to be more natural. The function will most often be fixed by the module using `XC.once` and the configuration will often be supplied by users of those modules, so it's better to partially apply the function first. --- XMonad/Hooks/Rescreen.hs | 8 +++----- XMonad/Util/ExtensibleConf.hs | 12 ++++++------ tests/ExtensibleConf.hs | 4 ++-- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/XMonad/Hooks/Rescreen.hs b/XMonad/Hooks/Rescreen.hs index 4f726a96..8338b84e 100644 --- a/XMonad/Hooks/Rescreen.hs +++ b/XMonad/Hooks/Rescreen.hs @@ -90,7 +90,9 @@ instance Monoid RescreenConfig where -- Note that 'rescreenHook' is safe to use several times, 'rescreen' is still -- done just once and hooks are invoked in sequence, also just once. rescreenHook :: RescreenConfig -> XConfig a -> XConfig a -rescreenHook = flip XC.once rescreenHook' +rescreenHook = XC.once $ \c -> c + { startupHook = startupHook c <> rescreenStartupHook + , handleEventHook = handleEventHook c <> rescreenEventHook } -- | Shortcut for 'rescreenHook'. addAfterRescreenHook :: X () -> XConfig a -> XConfig a @@ -100,10 +102,6 @@ addAfterRescreenHook h = rescreenHook def{ afterRescreenHook = h } addRandrChangeHook :: X () -> XConfig a -> XConfig a addRandrChangeHook h = rescreenHook def{ randrChangeHook = h } -rescreenHook' :: XConfig a -> XConfig a -rescreenHook' c = c{ startupHook = startupHook c <> rescreenStartupHook - , handleEventHook = handleEventHook c <> rescreenEventHook } - -- | Startup hook to listen for @RRScreenChangeNotify@ events. rescreenStartupHook :: X () rescreenStartupHook = do diff --git a/XMonad/Util/ExtensibleConf.hs b/XMonad/Util/ExtensibleConf.hs index fac7d8ef..a4f214d1 100644 --- a/XMonad/Util/ExtensibleConf.hs +++ b/XMonad/Util/ExtensibleConf.hs @@ -113,16 +113,16 @@ add x = alter (<> Just x) -- This can be used to implement a composable interface for modules that must -- only hook into xmonad core once. once :: forall a l. (Semigroup a, Typeable a) - => a -- ^ configuration to add - -> (XConfig l -> XConfig l) -- ^ 'XConfig' modification done only once + => (XConfig l -> XConfig l) -- ^ 'XConfig' modification done only once + -> a -- ^ configuration to add -> XConfig l -> XConfig l -once x f c = add x $ maybe f (const id) (lookup @a c) c +once f x c = add x $ maybe f (const id) (lookup @a c) c -- | Config-time: Applicative (monadic) variant of 'once', useful if the -- 'XConfig' modification needs to do some 'IO' (e.g. create an -- 'Data.IORef.IORef'). onceM :: forall a l m. (Applicative m, Semigroup a, Typeable a) - => a -- ^ configuration to add - -> (XConfig l -> m (XConfig l)) -- ^ 'XConfig' modification done only once + => (XConfig l -> m (XConfig l)) -- ^ 'XConfig' modification done only once + -> a -- ^ configuration to add -> XConfig l -> m (XConfig l) -onceM x f c = add x <$> maybe f (const pure) (lookup @a c) c +onceM f x c = add x <$> maybe f (const pure) (lookup @a c) c diff --git a/tests/ExtensibleConf.hs b/tests/ExtensibleConf.hs index 61404b4c..bfb55560 100644 --- a/tests/ExtensibleConf.hs +++ b/tests/ExtensibleConf.hs @@ -22,9 +22,9 @@ spec = do XC.lookup (XC.add "a" (XC.add [1 :: Int] def)) `shouldBe` (Nothing :: Maybe ()) specify "once" $ - borderWidth (XC.once "a" incBorderWidth def) `shouldBe` succ (borderWidth def) + borderWidth (XC.once incBorderWidth "a" def) `shouldBe` succ (borderWidth def) specify "once . once" $ - borderWidth (XC.once "b" incBorderWidth (XC.once "a" incBorderWidth def)) + borderWidth (XC.once incBorderWidth "b" (XC.once incBorderWidth "a" def)) `shouldBe` succ (borderWidth def) incBorderWidth :: XConfig l -> XConfig l