blob: b9ed7d47ec632b7e445b29252a76669956168140 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
let px f =
Js.Float.toString f ^ "px"
type entry =
{ label: string
; action: unit -> unit
}
let show mouse_event actions =
let menu =
H.div
[| HA.id "g-ContextMenu"
; HA.style ("left: " ^ (px (Event.page_x mouse_event)) ^ "; top: " ^ (px (Event.page_y mouse_event)))
|]
(Js.Array.map
(fun entry ->
H.div
[| HA.class_ "g-ContextMenu__Entry"
; HE.on_click (fun _ -> entry.action ())
|]
[| H.text entry.label |])
actions)
in
let () = Element.append_child Document.body menu in
(* Remove on click or context menu *)
let _ =
Js.Global.setTimeout
(fun _ ->
let rec f = (fun _ ->
let () = Element.remove_child Document.body menu in
let () = Element.remove_event_listener Document.body "click" f in
Element.remove_event_listener Document.body "contextmenu" f)
in
let () = Element.add_event_listener Document.body "click" f in
Element.add_event_listener Document.body "contextmenu" f
)
0
in
()
|