Search code examples
xmonad

How to create and handle custom messages in Xmonad?


I'd like to implement custom messages to be handled by a custom layout.

data ModifySideContainer = IncrementLeftColumnContainer | IncrementRightColumnContainer deriving Typeable
instance Message ModifySideContainer

I'm not too sure how to handle the custom message within pureMessage(https://hackage.haskell.org/package/xmonad-0.13/docs/XMonad-Core.html#v:pureMessage)

This is my current pureMessage implementation (within the custom layout):

  pureMessage l@(MiddleColumn sr mcc deltaInc _) m = msum [
    fmap resize     (fromMessage m),
    fmap incmastern (fromMessage m)
    ]
    where
      resize Expand = l {splitRatio = (min 0.5 $ sr + deltaInc)}
      resize Shrink = l {splitRatio = (max 0 $ sr - deltaInc)}
      incmastern (IncMasterN x) = l { middleColumnCount = max 0 (mcc+x) }

I don't quite understand how this logic works (I've copied it from somewhere), what is msum doing here? I guess I'll know once I find the instance declaration of mplus for Maybe.


Solution

  • Figured it out. You just need to add additional 'layouts' within the list.

    Essentially all msum does is get the first Just value and return it for example: msum [Nothing, Nothing, Just 1, Just 2, Nothing] will return Just 1.

      pureMessage l@(MiddleColumn sRatio mcc deltaInc _ leftCount rightCount) m = msum [
        fmap resize     (fromMessage m),
        fmap incmastern (fromMessage m),
        fmap incSideContainer (fromMessage m)
        ]
        where
          incSideContainer IncrementLeftColumnContainer = l
            { leftContainerCount = leftCount + 1, rightContainerCount = rightCount - 1}
          incSideContainer IncrementRightColumnContainer = l
            { leftContainerCount = leftCount - 1, rightContainerCount = rightCount + 1}
          resize Expand = l {splitRatio = (min 0.5 $ sRatio + deltaInc)}
          resize Shrink = l {splitRatio = (max 0 $ sRatio - deltaInc)}
          incmastern (IncMasterN x) = l { middleColumnCount = max 0 (mcc+x) }