![在当前工作区中查找一个窗口并对其应用一些操作](https://linux22.com/image/1363712/%E5%9C%A8%E5%BD%93%E5%89%8D%E5%B7%A5%E4%BD%9C%E5%8C%BA%E4%B8%AD%E6%9F%A5%E6%89%BE%E4%B8%80%E4%B8%AA%E7%AA%97%E5%8F%A3%E5%B9%B6%E5%AF%B9%E5%85%B6%E5%BA%94%E7%94%A8%E4%B8%80%E4%BA%9B%E6%93%8D%E4%BD%9C.png)
我认为这对 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