diff options
Diffstat (limited to 'src/Lib/ContextMenu.ml')
-rw-r--r-- | src/Lib/ContextMenu.ml | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/Lib/ContextMenu.ml b/src/Lib/ContextMenu.ml new file mode 100644 index 0000000..b9ed7d4 --- /dev/null +++ b/src/Lib/ContextMenu.ml @@ -0,0 +1,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 + () |