To implement a subscription to the PointerMoved event in an Elmish-style application using FuncUI and Avalonia, follow the pattern below. This allows you to observe mouse movements on a Canvas, dispatch messages, and update the model accordingly — all while adhering to the Elmish architecture.
✅ Goal
You want to:
- Subscribe to
PointerMoved events on a Canvas.
- Dispatch a
PointerMoved message with the current mouse position.
- Update the model to reflect the new point (e.g., add to a list of ellipses).
- Render those points visually as a feedback mechanism.
🧱 Model and Messages
You already have a basic structure. Let's refine it slightly:
type Model = {
helloText: string
listOfEllipses: Point list
}
let init () = { helloText = "letsgo 4"; listOfEllipses = [] }, Cmd.none
type Msg =
| PointerPressed of Point
| PointerMoved of Point
| CanvasInitialized of Canvas
🔁 Update Function
Handle the CanvasInitialized message by subscribing to the PointerMoved event using a Cmd.
let update (msg: Msg) (model: Model) : Model * Cmd<Msg> =
match msg with
| PointerPressed point ->
{ model with listOfEllipses = point :: model.listOfEllipses }, Cmd.none
| PointerMoved point ->
{ model with listOfEllipses = point :: model.listOfEllipses }, Cmd.none
| CanvasInitialized canvas ->
model, subscribeToPointerMoved canvas
📡 Command: Subscribe to PointerMoved
Use Cmd.ofSub to create a subscription that listens to PointerMoved and dispatches a message with the position.
open Avalonia.Input
open Avalonia.Controls
open System.Reactive.Linq
open Elmish
let subscribeToPointerMoved (canvas: Canvas) : Cmd<Msg> =
let sub dispatch =
canvas.GetObservable(InputElement.PointerMovedEvent)
|> Observable.map (fun args ->
let e = args :?> PointerEventArgs
e.GetPosition(canvas)
)
|> Observable.subscribe (fun point ->
dispatch (PointerMoved point)
)
:> System.IDisposable
Cmd.ofSub sub
🎨 View: Initialize Canvas and Render Ellipses
Use onLoaded to capture the actual Canvas instance and dispatch CanvasInitialized.
let view (model: Model) (dispatch: Msg -> unit) =
Canvas.create [
Canvas.background Brushes.LightGray
Canvas.onLoaded (fun canvas -> dispatch (CanvasInitialized canvas))
// Render ellipses
Canvas.children [
for point in model.listOfEllipses do
Ellipse.create [
Ellipse.width 10
Ellipse.height 10
Ellipse.fill Brushes.Red
Ellipse.margin (Thickness(point.X - 5., point.Y - 5., 0., 0.))
]
]
]
📌 Summary
- You use
Canvas.onLoaded to get a reference to the actual Canvas and dispatch CanvasInitialized.
- The
CanvasInitialized message triggers a subscription via Cmd.ofSub.
- That subscription listens to
PointerMoved, gets the position, and dispatches PointerMoved point.
- The
update function adds the point to the list of ellipses.
- The
view function renders each point as a red ellipse on the canvas.
🧪 Result
As you move your mouse over the canvas, red ellipses will be added under the cursor — providing immediate visual feedback that the event subscription is working as expected.
This pattern cleanly separates concerns:
- View initializes the canvas and renders the model.
- Update handles messages and updates the model.
- Command manages external subscriptions (like mouse events) in a safe, disposable way.