* feat: implement new context menu functionality * refactor: simplify native menu * refactor: simplify web context menu * refactor: replace native menu with context menu hooks * refactor: update naming * chore: clean code * fix: types * chore: add polyfill Signed-off-by: Innei <tukon479@gmail.com> * comment Signed-off-by: Innei <tukon479@gmail.com> --------- Signed-off-by: Innei <tukon479@gmail.com> Co-authored-by: Innei <tukon479@gmail.com>
55 lines
1.7 KiB
TypeScript
55 lines
1.7 KiB
TypeScript
import type { MenuItemConstructorOptions } from "electron"
|
|
|
|
import { tipcClient } from "./client"
|
|
|
|
export type ElectronMenuItem = Omit<MenuItemConstructorOptions, "click" | "submenu"> & {
|
|
click?: () => void
|
|
submenu?: ElectronMenuItem[]
|
|
}
|
|
|
|
export const showElectronContextMenu = async (items: Array<ElectronMenuItem>) => {
|
|
if (!window.electron) throw new Error("electron is not available")
|
|
const dispose = window.electron.ipcRenderer.on(
|
|
"menu-click",
|
|
(_, { path }: { path: number[] }) => {
|
|
const targetMenu = getMenuItemByPath(items, path)
|
|
if (targetMenu && typeof targetMenu.click === "function") {
|
|
targetMenu.click()
|
|
} else {
|
|
console.warn(`Menu item not found or click handler missing for path: ${path}`)
|
|
}
|
|
},
|
|
)
|
|
const itemsWithoutClick = removeClick(items)
|
|
await tipcClient?.showContextMenu({ items: itemsWithoutClick })
|
|
dispose()
|
|
}
|
|
|
|
const removeClick = (item: ElectronMenuItem[]): ElectronMenuItem[] =>
|
|
item.map(({ click, ...rest }) => {
|
|
if (rest.submenu)
|
|
return {
|
|
...rest,
|
|
submenu: removeClick(rest.submenu),
|
|
}
|
|
return rest
|
|
})
|
|
|
|
// Function to retrieve the menu item based on the provided path
|
|
const getMenuItemByPath = (items: ElectronMenuItem[], path: number[]): ElectronMenuItem | null => {
|
|
let currentItems = items
|
|
let currentItem: ElectronMenuItem | null = null
|
|
|
|
for (const index of path) {
|
|
if (!currentItems || index >= currentItems.length) return null
|
|
currentItem = currentItems[index]
|
|
|
|
if (currentItem.submenu && Array.isArray(currentItem.submenu)) {
|
|
currentItems = currentItem.submenu
|
|
} else {
|
|
currentItems = []
|
|
}
|
|
}
|
|
return currentItem
|
|
}
|