HeinrichApfelmus / frp-guides

A collection of tutorials, guidelines, examples, patterns and half-baked ideas on functional reactive programming (FRP).
65 stars 4 forks source link

Question: advice on holding a widget state #3

Closed dimsuz closed 7 years ago

dimsuz commented 7 years ago

Hi!

I am not sure if this is a proper place, but I wanted to ask you a question knowing you as an frp-expert :) I'm rather new to FRP and haskell, but already familiar with basic concepts (am trying to write more and more functional code).

What I got interested in is to try to build a small UI-toolkit like app, which will render some widgets from ground up. I'm using a nanovg-hs library to do painting in OpenGL.

All FRP libraries I see, rely on some existing UI toolkit (for example threpenny-gui, reflex-dom use browser for this (html elements), reactive-banana gets hooked up to either wx or gtk or sdl)... so things like pushed state, click animations, focused rectangle etc are handled not by these libraries, but by lower level (and imperative-language) library.

In my case I want to do everything in Haskell and this is where I got confused. Mainly I am not sure where to put state related to animation? For example, consider user pressing a button. I want to draw it pressed, then, after user released it, I want to play some fade animation of button's background for example. And if all I have is a function which draws a button and accepts current time (or dt between frames), it would also need to know a time at which click has happened and store the current fade value etc etc. Where should this be stored? Should I create some typeclass where I would store this state and draw-function will read/modify it?

Perhaps this is a rather generic question, but I'm trying to at least figure out a direction in which to do my reasearch/googling :) I'd be glad to dive deep in some existing libraries for an answers or examples, but none of them does this kind of stuff by itself (if I'm not mistaken).

If you would give me some advice, or if you know some existing work I could use to learn from, it would be much appreciated. Also I guess you should immediately close this issue, because I've used it as a means to simply ask a question, it's not a real issue :)

HeinrichApfelmus commented 7 years ago

I'm not sure how to answer, your question is very broad. But if you want to go the "everything FRP" route, then I think that the paper "Declarative event-oriented programming" mentioned in the README might be a good starting point.

dimsuz commented 7 years ago

I understand that my question was very vague, but still dared to ask :) Thank you, I will read it!

HeinrichApfelmus commented 7 years ago

No worries. Hope I could help! :smile:

dimsuz commented 7 years ago

Can't resist, I'm so excited! I thought I would never understand how to program using FRP, but I actually started reading that Conal Eliott's paper you suggested above and trying to replicate same stuff for widget rendering, not with Fran, but using Reactive Banana and its glfw bindings I found on github. Also created a couple of my own AddHandlers on top of that.

It almost feels like magic, I wire up some Behaviors, some fmap-ping, hook events to sample them and it works like I intended. Haskell-world just doesn't stop to amaze me with these things: when I constantly feel like I'm only starting to learn something and at the same time I'm able to actually achieve things which seemed rather non-trivial at start, it's so rewarding! But my code is still awful and that's ok for now :))

Thanks for your help and for Reactive Banana, its api is really concise and nice :)

HeinrichApfelmus commented 7 years ago

Glad I could help. :banana: