@@ -170,13 +170,13 @@ module Events =
170170 let onMouseDown ( cb : MouseButtons -> V2i -> 'msg ) =
171171 onEvent
172172 " onmousedown"
173- [ " event.clientX" ; " event.clientY" ; " event.which " ]
173+ [ " event.clientX" ; " event.clientY" ; " event.button " ]
174174 ( fun args ->
175175 match args with
176176 | x :: y :: b :: _ ->
177177 let x = int ( float x)
178178 let y = int ( float y)
179- let b = MouseButtons.ofEventStr b
179+ let b = MouseButtons.parseEventButton b
180180 cb b ( V2i( x, y))
181181 | _ ->
182182 failwith " asdasd"
@@ -185,13 +185,13 @@ module Events =
185185 let onMouseUp ( cb : MouseButtons -> V2i -> 'msg ) =
186186 onEvent
187187 " onmouseup"
188- [ " event.clientX" ; " event.clientY" ; " event.which " ]
188+ [ " event.clientX" ; " event.clientY" ; " event.button " ]
189189 ( fun args ->
190190 match args with
191191 | x :: y :: b :: _ ->
192192 let x = int ( float x)
193193 let y = int ( float y)
194- let b = MouseButtons.ofEventStr b
194+ let b = MouseButtons.parseEventButton b
195195 cb b ( V2i( x, y))
196196 | _ ->
197197 failwith " asdasd"
@@ -303,45 +303,55 @@ module Events =
303303 k, m |> AVal.map ( function true -> Some v | false -> None)
304304
305305
306- let internal onMouseRel ( kind : string ) ( needButton : bool ) ( f : MouseButtons -> V2d -> 'msg ) =
306+ let private onMouseRel ( kind : string ) ( needButton : bool ) ( cb : MouseButtons -> V2d -> 'msg ) =
307+ let eventButton , parseEventButton =
308+ if needButton then
309+ " button" , MouseButtons.parseEventButton
310+ else
311+ " buttons" , MouseButtons.parseEventButtons
312+
307313 kind, AttributeValue.Event {
308314 clientSide = fun send src ->
309315 String.concat " ;" [
310316 " var rect = getBoundingClientRect(event.target)"
311317 " var x = (event.clientX - rect.left) / rect.width"
312318 " var y = (event.clientY - rect.top) / rect.height"
313- send src [ " event.which" ; " { X: x.toFixed(10), Y: y.toFixed(10) }" ]
314-
319+ send src [ $" event.{eventButton}" ; " { X: x.toFixed(10), Y: y.toFixed(10) }" ]
315320 ]
316321 serverSide = fun client src args ->
317322 match args with
318- | which :: pos :: _ ->
319- let v : V2d = Pickler.json.UnPickleOfString pos
320- let button = if needButton then MouseButtons.ofEventStr which else MouseButtons.Left
321- Seq.singleton ( f button v)
322- | _ ->
323- Seq.empty
323+ | buttons :: pos :: _ ->
324+ let v : V2d = Pickler.json.UnPickleOfString pos
325+ let button = parseEventButton buttons
326+ Seq.singleton ( cb button v)
327+ | _ ->
328+ Seq.empty
324329 }
325330
326- let internal onMouseAbs ( kind : string ) ( needButton : bool ) ( f : MouseButtons -> V2d -> V2d -> 'msg ) =
331+ let private onMouseAbs ( kind : string ) ( needButton : bool ) ( cb : MouseButtons -> V2d -> V2d -> 'msg ) =
332+ let eventButton , parseEventButton =
333+ if needButton then
334+ " button" , MouseButtons.parseEventButton
335+ else
336+ " buttons" , MouseButtons.parseEventButtons
337+
327338 kind, AttributeValue.Event {
328339 clientSide = fun send src ->
329340 String.concat " ;" [
330341 " var rect = getBoundingClientRect(event.target)"
331342 " var x = (event.clientX - rect.left)"
332343 " var y = (event.clientY - rect.top)"
333- send src [ " event.which" ; " { X: x.toFixed(10), Y: y.toFixed(10) }" ; " { X: rect.width.toFixed(10), Y: rect.height.toFixed(10) }" ]
334-
344+ send src [ $" event.{eventButton}" ; " { X: x.toFixed(10), Y: y.toFixed(10) }" ; " { X: rect.width.toFixed(10), Y: rect.height.toFixed(10) }" ]
335345 ]
336346 serverSide = fun client src args ->
337347 match args with
338- | which :: pos :: size :: _ ->
339- let pos : V2d = Pickler.json.UnPickleOfString pos
340- let size : V2d = Pickler.json.UnPickleOfString size
341- let button = if needButton then MouseButtons.ofEventStr which else MouseButtons.Left
342- Seq.singleton ( f button pos size)
343- | _ ->
344- Seq.empty
348+ | buttons :: pos :: size :: _ ->
349+ let pos : V2d = Pickler.json.UnPickleOfString pos
350+ let size : V2d = Pickler.json.UnPickleOfString size
351+ let button = parseEventButton buttons
352+ Seq.singleton ( cb button pos size)
353+ | _ ->
354+ Seq.empty
345355 }
346356
347357 let onMouseDownAbs ( f : MouseButtons -> V2d -> V2d -> 'msg ) =
@@ -355,12 +365,10 @@ module Events =
355365
356366 let onMouseClickAbs ( f : MouseButtons -> V2d -> V2d -> 'msg ) =
357367 onMouseAbs " onclick" true f
358-
368+
359369 let onMouseDoubleClickAbs ( f : MouseButtons -> V2d -> V2d -> 'msg ) =
360370 onMouseAbs " ondblclick" true f
361371
362-
363-
364372 let onMouseDownRel ( f : MouseButtons -> V2d -> 'msg ) =
365373 onMouseRel " onmousedown" true f
366374
@@ -408,32 +416,44 @@ module Events =
408416 | " pen" -> Pen
409417 | _ -> failwith $" PointerType '{str}' not supported"
410418
411- let onPointerEventModifiers ( name : string ) ( needButton : bool ) ( preventDefault : Option < int >) ( useCapture : Option < bool >)
419+ let onPointerEventModifiers ( name : string ) ( needButton : bool ) ( preventDefault : Option < MouseButtons >) ( useCapture : Option < bool >)
412420 ( cb : PointerType -> KeyModifiers -> MouseButtons -> V2i -> 'msg ) =
421+ let eventButton , parseEventButton , toEventButton =
422+ if needButton then
423+ " button" , MouseButtons.parseEventButton, MouseButtons.toEventButton
424+ else
425+ " buttons" , MouseButtons.parseEventButtons, MouseButtons.toEventButtons
426+
413427 name, AttributeValue.Event {
414- clientSide = fun send src ->
415- String.concat " ;" [
416- yield " var rect = getBoundingClientRect(this)"
417- yield " var x = (event.clientX - rect.left)"
418- yield " var y = (event.clientY - rect.top)"
419- match preventDefault with | None -> () | Some i -> yield ( sprintf " if(event.which==%d ){event.preventDefault();};" i)
420- match useCapture with | None -> () | Some b -> if b then yield " this.setPointerCapture(event.pointerId)" else yield " this.releasePointerCapture(event.pointerId)"
421- yield send src [ " event.pointerType" ; " event.which" ; " x|0" ; " y|0" ; " event.shiftKey" ; " event.altKey" ; " event.ctrlKey" ]
422-
423- ]
424- serverSide = fun client src args ->
425- match args with
426- | pointertypestr :: which :: x :: y :: shift :: alt :: ctrl :: _ ->
427- let v : V2i = V2i( int x, int y)
428- let button = if needButton then MouseButtons.ofEventStr which else MouseButtons.None
429- let modifiers = KeyModifiers.ofString shift alt ctrl
430- let pointertype = PointerType.ofString pointertypestr
431- Seq.singleton ( cb pointertype modifiers button v)
432- | _ ->
433- Seq.empty
434- }
428+ clientSide = fun send src ->
429+ String.concat " ;" [
430+ " var rect = getBoundingClientRect(this)"
431+ " var x = (event.clientX - rect.left)"
432+ " var y = (event.clientY - rect.top)"
433+
434+ match preventDefault with
435+ | Some b -> $" if(event.{eventButton}=={toEventButton b}){{event.preventDefault();}};"
436+ | _ -> ()
437+
438+ match useCapture with
439+ | Some b -> if b then " this.setPointerCapture(event.pointerId)" else " this.releasePointerCapture(event.pointerId)"
440+ | _ -> ()
441+
442+ send src [ " event.pointerType" ; $" event.{eventButton}" ; " x|0" ; " y|0" ; " event.shiftKey" ; " event.altKey" ; " event.ctrlKey" ]
443+ ]
444+ serverSide = fun client src args ->
445+ match args with
446+ | pointertypestr :: buttons :: x :: y :: shift :: alt :: ctrl :: _ ->
447+ let v : V2i = V2i( int x, int y)
448+ let button = parseEventButton buttons
449+ let modifiers = KeyModifiers.ofString shift alt ctrl
450+ let pointertype = PointerType.ofString pointertypestr
451+ Seq.singleton ( cb pointertype modifiers button v)
452+ | _ ->
453+ Seq.empty
454+ }
435455
436- let onPointerEvent name ( needButton : bool ) ( preventDefault : Option < int >) ( useCapture : Option < bool >) ( f : PointerType -> MouseButtons -> V2i -> 'msg ) =
456+ let onPointerEvent name ( needButton : bool ) ( preventDefault : Option < MouseButtons >) ( useCapture : Option < bool >) ( f : PointerType -> MouseButtons -> V2i -> 'msg ) =
437457 onPointerEventModifiers name needButton preventDefault useCapture ( fun t _ b p -> f t b p)
438458
439459 let onCapturedPointerDown preventDefault cb =
0 commit comments