Reactive Banana:备用按钮事件

时间:2015-08-31 20:43:44

标签: haskell wxwidgets frp reactive-banana

我第一次尝试使用Reactive Banana(WX)在五个按钮的顶部显示“按下按钮1”,“按下按钮2”等文本:

{-# LANGUAGE ScopedTypeVariables #-}

import Graphics.UI.WX hiding (Event)
import Reactive.Banana
import Reactive.Banana.WX

data Buttons = One | Two | Three | Four | Five deriving (Show)

main :: IO ()
main = start $ do
  f      <- frame [text := “Five Buttons“]
  bone   <- button f [text := “One”]
  btwo   <- button f [text := “Two”]
  bthree <- button f [text := “Three”]
  bfour  <- button f [text := “Four”]
  bfive  <- button f [text := “Five”]
  out    <- staticText f []

  set f [layout := margin 10 $
          column 5 [row 5 [widget bone, widget btwo, widget bthree, widget bfour, widget bfive],
                    grid 5 5 [[label “Output:” , widget out]
                              ]]]

  let networkDescription :: forall t. Frameworks t => Moment t ()
      networkDescription = do
        eone   <- event0 bone   command
        etwo   <- event0 btwo   command
        ethree <- event0 bthree command
        efour  <- event0 bfour  command
        efive  <- event0 bfive  command

        let
          somethinghere::Behaviour t Buttons
          somethinghere = ....
        sink out [text :== "Pressed button " ++ show <$> somethinghere]

  network <- compile networkDescription    
  actuate network

代码只是主要的骨架。我目前无法做的是填写somethinghere方法。如上所述,如果按下按钮“One”,那么somethinghere应该返回参考Buttons ADT数据One,依此类推。 我尝试使用accumBunionWith,但我认为我的机制并不正确。有什么帮助吗?

1 个答案:

答案 0 :(得分:3)

我相信你想要的东西:

let eChangeSelection :: Event t Buttons
    eChangeSelection = unions
        [ One <$ eone
        , Two <$ etwo
        , Three <$ ethree
        , Four <$ efour
        , Five <$ efive
        ]

    -- Your `somethinghere`:
    bSelection :: Behavior t Buttons
    bSelection = stepper One eChangeSelection

bSelection表示当前选定的按钮,eChangeSelection是对其进行更新的流。 unions合并五个事件流,(<$)使用适当的值标记每个单独的流。请注意,我任意选择One作为初始选择值;您可能更喜欢做一些不同的事情(例如使用Behavior t (Maybe Buttons)以便可以不选择按钮)。