Something went wrong. Try again.
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182import type { MouseEvent } from "react";import { Menu, PredefinedMenuItem } from "@tauri-apps/api/menu";import { LogicalPosition } from "@tauri-apps/api/dpi";import { getCurrentWindow } from "@tauri-apps/api/window";
let activeContextMenu: Menu | null = null;
export type NativeContextMenuItem = | { type?: "item"; id: string; text: string; enabled?: boolean; onSelect?: () => void | Promise<void>; } | { type: "separator"; } | { type: "predefined"; item: "Copy" | "Cut" | "Paste" | "SelectAll" | "Undo" | "Redo"; text?: string; };
export async function showNativeContextMenu( event: MouseEvent<HTMLElement>, items: NativeContextMenuItem[],) { if (items.length === 0) { return; }
event.preventDefault(); event.stopPropagation();
if (activeContextMenu) { await activeContextMenu.close().catch(() => undefined); activeContextMenu = null; }
let menu: Menu | null = null;
menu = await Menu.new({ items: await Promise.all( items.map(async (item) => { if (item.type === "separator") { return PredefinedMenuItem.new({ item: "Separator" }); }
if (item.type === "predefined") { return PredefinedMenuItem.new({ item: item.item, text: item.text, }); }
return { id: item.id, text: item.text, enabled: item.enabled, action: () => { if (menu) { void menu.close().catch(() => undefined); if (activeContextMenu?.rid === menu.rid) { activeContextMenu = null; } } void item.onSelect?.(); }, }; }), ), });
activeContextMenu = menu;
await menu.popup( new LogicalPosition(event.clientX, event.clientY), getCurrentWindow(), );}