Hi folks,
I have recently been experimenting with combining Reactive X, WPF, and F# and have found the combination to be very palatable. I chose drag and drop as the test case because it is both non trivial and generally deeply stateful. The resulting Rx turns out to be one fifth the code of my original C#, much easier to read and has fewer errors.
Drag and Drop code:
leftDown
|> map (fun e1 -> (e1, mouseMove |> takeUntil leftUp |> takeUntil mouseLeave))
|> filter (fun (e1, e2s) -> e1.Source.Equals(canvas) |> not)
|> map (fun (e1, e2s) ->
let control = e1.Source :?> UIElement
let leftTop = (Canvas.GetLeft(control), Canvas.GetTop(control))
let pt1 = e1.GetPosition(canvas)
e2s |> map (fun e2 ->
let pt2 = e2.GetPosition(canvas)
(control, leftTop, pt2 - pt1)))
|> mergeAll
|> subscribe (fun (control, (left,top), delta) -> Canvas.SetLeft(control, left + delta.X); Canvas.SetTop(control, top + delta.Y))
As always, I am interested in feedback.
Cheers,
Matt
Full Code:
