我认为这对 Haskell 熟练的人来说应该很容易,但我就是搞不懂,因为我不擅长。基本上,我想搜索当前工作区中的窗口列表,找到一个符合某些条件的窗口并对其应用某些操作;如果没有找到,则创建此窗口。我有这样一个框架代码
import qualified XMonad.StackSet as W
import XMonad.Actions.WindowGo
findWindow condition actionIfFound actionIfNotFound =
ifWindow (condition <&&> member W.current) windowFound actionIfNotFound
where windowFound = ask >>= w -> do actionIfFound w
答案1
这是旧帖子,但对于仍然想要答案的人来说:
基本上,使用XMonad.StackSet.index
,您可以像这样获取当前工作区中的窗口列表:
do
windows <- gets (W.index . windowset)
-- something...
因此findWindow
可以这样实现:
import qualified XMonad.StackSet as W
import Data.List (find)
findWindow :: (Window -> Bool) -> (Window -> X()) -> X() -> X()
findWindow condition actionIfFound actionIfNotFound = do
windows <- gets (W.index . windowset)
let found = find condition windows -- found has type Maybe Window
case found of
Nothing -> actionIfNotFound
Just w -> actionIfFound w
PS:最后三行可以缩短为maybe actionIfNotFound actionIfFound found