Skip to content

Commit 8932617

Browse files
committed
Replace deprecated UIEvent.which
1 parent 5127289 commit 8932617

7 files changed

Lines changed: 119 additions & 101 deletions

File tree

src/Aardvark.UI.Primitives/Controllers/ArcBallController.fs

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -244,39 +244,6 @@ module ArcBallController =
244244

245245
{ model with view = cam; dragStart = pos; orbitCenter = center }
246246

247-
[<Obsolete>]
248-
let onMouseDown (cb : MouseButtons -> V2i -> 'msg) =
249-
onEvent
250-
"onmousedown"
251-
["event.clientX"; "event.clientY"; "event.which"; "event.getModifierState('Control')" ]
252-
(fun args ->
253-
match args with
254-
| x :: y :: b :: isControl :: _ ->
255-
let x = int (float x)
256-
let y = int (float y)
257-
let b = MouseButtons.ofEventStr b
258-
let modKey = if b = MouseButtons.Left && isControl = "true" then MouseButtons.Right else b
259-
cb modKey (V2i(x,y))
260-
| _ ->
261-
failwith "asdasd"
262-
)
263-
264-
(*let onMouseUp (cb : MouseButtons -> V2i -> 'msg) =
265-
onEvent
266-
"onmouseup"
267-
["event.clientX"; "event.clientY"; "event.which"; "event.getModifierState('Control')" ]
268-
(fun args ->
269-
match args with
270-
| x :: y :: b :: isControl :: _ ->
271-
let x = int (float x)
272-
let y = int (float y)
273-
let b = Aardvark.UI.Helpers.button b
274-
let modKey = if b = MouseButtons.Left && isControl = "true" then MouseButtons.Right else b
275-
cb modKey (V2i(x,y))
276-
| _ ->
277-
failwith "asdasd"
278-
)*)
279-
280247
let attributes (state : AdaptiveCameraControllerState) (f : Message -> 'msg) =
281248
AttributeMap.ofListCond [
282249
always (onBlur (fun _ -> f Blur))

src/Aardvark.UI/Frontend/Attributes.fs

Lines changed: 69 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -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 =

src/Aardvark.UI/RenderControl/RenderControl.fs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,6 @@ module ``RenderControl DomNode`` =
2727
let needingButton = Set.ofList [SceneEventKind.Click; SceneEventKind.DoubleClick; SceneEventKind.Down; SceneEventKind.Up]
2828
fun k -> Set.contains k needingButton
2929

30-
let private button (code : int) =
31-
match code with
32-
| 1 -> MouseButtons.Left
33-
| 2 -> MouseButtons.Middle
34-
| 3 -> MouseButtons.Right
35-
| _ -> MouseButtons.None
36-
3730
type DomNode with
3831
static member RenderControl(attributes : AttributeMap<'msg>, processor : SceneEventProcessor<'msg>,
3932
getState : RenderClientInfo -> RenderState, scene : Scene) =
@@ -68,17 +61,17 @@ module ``RenderControl DomNode`` =
6861

6962
let rayEvent (includeButton : bool) (kind : SceneEventKind) =
7063
let args =
71-
if includeButton then ["event.offsetX"; "event.offsetY"; "event.altKey"; "event.shiftKey"; "event.ctrlKey"; "event.which"]
64+
if includeButton then ["event.offsetX"; "event.offsetY"; "event.altKey"; "event.shiftKey"; "event.ctrlKey"; "event.button"]
7265
else ["event.offsetX"; "event.offsetY"; "event.altKey"; "event.shiftKey"; "event.ctrlKey" ]
7366

7467
{
7568
clientSide = fun send id -> send id args + ";"
7669
serverSide = fun session id args ->
7770
match args with
78-
| x :: y :: alt :: shift :: ctrl :: which :: _ ->
71+
| x :: y :: alt :: shift :: ctrl :: buttons :: _ ->
7972
let x = round (float x) |> int
8073
let y = round (float y) |> int
81-
let button = int which |> button
74+
let button = MouseButtons.parseEventButton buttons
8275
let alt = Boolean.Parse alt
8376
let shift = Boolean.Parse shift
8477
let ctrl = Boolean.Parse ctrl

src/Aardvark.UI/Utilities/Common.fs

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,45 @@ type RayPartExtensions =
1515

1616
module MouseButtons =
1717

18-
let ofEvent (button: int) =
18+
/// See: https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/button
19+
let ofEventButton (button: int) =
1920
match button with
20-
| 1 -> MouseButtons.Left
21-
| 2 -> MouseButtons.Middle
22-
| 3 -> MouseButtons.Right
21+
| 0 -> MouseButtons.Left
22+
| 1 -> MouseButtons.Middle
23+
| 2 -> MouseButtons.Right
2324
| _ -> MouseButtons.None
2425

25-
let ofEventStr (button: string) =
26-
button |> float |> int |> ofEvent
26+
/// See: https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/button
27+
let toEventButton (button: MouseButtons) =
28+
match button with
29+
| MouseButtons.Left -> 0
30+
| MouseButtons.Middle -> 1
31+
| MouseButtons.Right -> 2
32+
| _ -> 0
33+
34+
/// See: https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/button
35+
let parseEventButton (button: string) =
36+
button |> float |> int |> ofEventButton
37+
38+
/// See: https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/buttons
39+
let ofEventButtons (buttons: int) =
40+
let mutable result = MouseButtons.None
41+
if buttons &&& 1 = 1 then &result |||= MouseButtons.Left
42+
if buttons &&& 2 = 2 then &result |||= MouseButtons.Right
43+
if buttons &&& 4 = 4 then &result |||= MouseButtons.Middle
44+
result
45+
46+
/// See: https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/buttons
47+
let parseEventButtons (buttons: string) =
48+
buttons |> float |> int |> ofEventButtons
49+
50+
/// See: https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/buttons
51+
let toEventButtons (buttons: MouseButtons) =
52+
let mutable result = 0
53+
if buttons.HasFlag MouseButtons.Left then &result |||= 1
54+
if buttons.HasFlag MouseButtons.Right then &result |||= 2
55+
if buttons.HasFlag MouseButtons.Middle then &result |||= 4
56+
result
2757

2858
[<AutoOpen>]
2959
module ``Path Utilities`` =

src/Examples (dotnetcore)/06 - DragDiv/resources/DragUtilities.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@
44
var x = event.clientX - bounds.left;
55
var y = event.clientY - bounds.top;
66
return { x: x, y: y };
7-
}
7+
}
8+
9+
function toFixedV2d(v) {
10+
return { X: v.x.toFixed(10), Y: v.y.toFixed(10) };
11+
}

src/Examples (dotnetcore)/15 - Svg/resources/SvgDragUtilities.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@ function getScreenCTM(doc) {
145145
return sCTM
146146
}
147147

148+
function toFixedV2d(v) {
149+
return { X: v.x.toFixed(10), Y: v.y.toFixed(10) };
150+
}
151+
148152
function mouseUp(documentElement, evt) {
149153
if (draggingElement) {
150154

src/Scratch/02 - DrawRects/App.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,12 @@ module ClientApp =
110110
let myMouseCbRelButton (evtName : string) (containerClass : string) (cb : MouseButtons -> V2d -> 'msg) =
111111
onEvent'
112112
evtName
113-
[sprintf "relativeCoords2(event,'%s')" containerClass; "event.which"]
113+
[sprintf "relativeCoords2(event,'%s')" containerClass; "event.button"]
114114
(fun args ->
115115
match args with
116116
| x :: b :: _ ->
117117
let v : Option<V2d> = Pickler.json.UnPickleOfString x
118-
let b : MouseButtons = b |> MouseButtons.ofEventStr
118+
let b : MouseButtons = b |> MouseButtons.parseEventButton
119119
match v with
120120
| Some v -> cb b v |> Seq.singleton
121121
| None -> Seq.empty

0 commit comments

Comments
 (0)