Created
January 24, 2017 17:32
-
-
Save mchav/dae6b4be69c76f8c42ef11f07d36dcca to your computer and use it in GitHub Desktop.
Simple Counter in froid using callbacks.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module io.github.mchav.simplecounter.CounterActivity where | |
import froid.app.Activity | |
import froid.os.Bundle | |
import froid.view.View | |
import froid.widget.Button | |
import froid.widget.TextView | |
native module type Activity where {} | |
onCreate :: MutableIO Activity -> Maybe (MutableIO Bundle) -> IO () | |
onCreate this bundle = do | |
-- assume we have defined some XML resources as mentioned below | |
this.setContentView activityCounter | |
txtOutput <- asTextView this.findViewById textViewOutput | |
btnIncrement <- asButton this.findViewById buttonIncrement | |
btnDecrement <- asButton this.findViewById buttonDecrement | |
-- click listener takes in a view and returns an IO action | |
btnDecrement.setOnClick (\v -> modifyCounter txtOutput (-1)) | |
btnIncrement.setOnClick (\v -> modifyCounter txtOutput 1) | |
modifyCounter :: MutableIO TextView -> Int -> IO () | |
modifyCounter txt n = do | |
curr <- liftM (_.toInt) txt.getText | |
txt.setText (curr + n) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There are several ways to incorporate reactive-banana into a GUI framework.
Here is a leightweight way, where the GUI framework doesn't really know anything about reactive-banana. A small glue library, here named "reactive.banana.froid", simplifies using the GUI widgets from the reactive-banana side. The result is very similar to the Counter.hs example from reactive-banana-wx. Here is my attempt at pseudo-code:
It is also possible to make the integration more tight. This is what my threepenny-gui library is trying to incorporate. For an example in this style, see the CRUD.hs example. The difference to the previous example is essentially that the GUI library now knows about reactive-banana, and can use Events and Behaviors directly in widget code. In Froid, this would probably look like this:
As you can see, the widgets
btnIncrement
andbtnDecrement
can now return aclick
event directly, and thetxtOutput
widget can accept a string behavior directly.The more tightly you want to integrate FRP, the more you probably have to rethink your approach to GUI frameworks in general.