1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586(* The native side builds the menu incrementally through opaque handles (an
NSMenu, a GtkMenuShell or an HMENU) rather than receiving the tree below,
so the tree walk lives here -- see menu_stubs.cpp.
Activation goes through a single closure registered once with the stub: it
receives the integer command id of the chosen item, and looks the real
callback up in [callbacks]. One GC root for the whole menu, instead of one
per item. *)externalraw_set_dispatch:(int->unit)->unit="ocaml_webview_menu_set_dispatch"externalraw_create_bar:unit->nativeint="ocaml_webview_menu_create_bar"externalraw_add_submenu:nativeint->string->nativeint="ocaml_webview_menu_add_submenu"externalraw_add_item:nativeint->string->string->int->int->unit="ocaml_webview_menu_add_item"externalraw_add_separator:nativeint->unit="ocaml_webview_menu_add_separator"(* window, browser controller (only used on Windows), bar *)externalraw_install:nativeint->nativeint->nativeint->unit="ocaml_webview_menu_install"typemodifier=Cmd|Ctrl|Alt|Shifttypeitem=|Itemof{label:string;key:charoption;modifiers:modifierlist;on_click:unit->unit;}|Separator|Submenuof{label:string;items:itemlist}letitem?key?(modifiers=[])labelon_click=Item{label;key;modifiers;on_click}letseparator=Separatorletsubmenulabelitems=Submenu{label;items}(* Command ids are never reused, so a stale id left over in a menu that is
being replaced can never reach a new callback. *)letcallbacks:(int,unit->unit)Hashtbl.t=Hashtbl.create16letnext_command=ref0letdispatch_registered=reffalse(* Keep in sync with the WV_MOD_* enum in menu_stubs.cpp. *)letmask_of_modifiersmodifiers=List.fold_left(funaccm->acclormatchmwithCmd->1|Ctrl->2|Alt->4|Shift->8)0modifiersletrecadd_itemsparentitems=List.iter(add_item_toparent)itemsandadd_item_toparent=function|Separator->raw_add_separatorparent|Submenu{label;items}->add_items(raw_add_submenuparentlabel)items|Item{label;key;modifiers;on_click}->incrnext_command;letcommand=!next_commandinHashtbl.replacecallbackscommandon_click;letkey=matchkeywithNone->""|Somec->String.make1cinraw_add_itemparentlabelkey(mask_of_modifiersmodifiers)commandletdispatchcommand=matchHashtbl.find_optcallbackscommandwithSomef->f()|None->()letsetwmenus=ifnot!dispatch_registeredthenbeginraw_set_dispatchdispatch;dispatch_registered:=trueend;(* The previous bar is about to be replaced, so its callbacks go with it. *)Hashtbl.resetcallbacks;letbar=raw_create_bar()inList.iter(fun(label,items)->add_items(raw_add_submenubarlabel)items)menus;raw_install(Webview.get_windoww)(Webview.get_native_handlewWebview.Browser_controller)bar