-
Notifications
You must be signed in to change notification settings - Fork 914
Lite: native menus #13317
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lite: native menus #13317
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| import type { NativeMenuPopupItem, NativeMenuPosition } from "#electron/ipc.ts"; | ||
| import { MouseEvent } from "react"; | ||
|
OliverJAsh marked this conversation as resolved.
|
||
|
|
||
| type NativeMenuAction = () => void | Promise<void>; | ||
|
|
||
| type NativeMenuItemData = { | ||
| label: string; | ||
| enabled?: boolean; | ||
| onSelect?: NativeMenuAction; | ||
| submenu?: Array<NativeMenuItem>; | ||
| }; | ||
|
|
||
| export type NativeMenuItem = { _tag: "Separator" } | ({ _tag: "Item" } & NativeMenuItemData); | ||
|
|
||
| const serializeNativeMenuItems = ( | ||
| items: Array<NativeMenuItem>, | ||
| handlers: Map<string, NativeMenuAction | undefined>, | ||
| nextActionId: { value: number }, | ||
| ): Array<NativeMenuPopupItem> => | ||
| items.map((item): NativeMenuPopupItem => { | ||
| if (item._tag === "Separator") return { _tag: "Separator" }; | ||
|
|
||
| if (item.submenu) | ||
| return { | ||
| _tag: "Item", | ||
| label: item.label, | ||
| enabled: item.enabled, | ||
| submenu: serializeNativeMenuItems(item.submenu, handlers, nextActionId), | ||
| }; | ||
|
|
||
| const itemId = `native-menu:${nextActionId.value++}`; | ||
| handlers.set(itemId, item.onSelect); | ||
|
|
||
| return { | ||
| _tag: "Item", | ||
| label: item.label, | ||
| enabled: item.enabled, | ||
| itemId, | ||
| }; | ||
| }); | ||
|
|
||
| const showNativeMenu = async ( | ||
| items: Array<NativeMenuItem>, | ||
| position: NativeMenuPosition, | ||
| ): Promise<void> => { | ||
| if (items.length === 0) return; | ||
|
|
||
| const handlers = new Map<string, NativeMenuAction | undefined>(); | ||
| const serializedItems = serializeNativeMenuItems(items, handlers, { value: 0 }); | ||
|
|
||
|
Comment on lines
+48
to
+50
|
||
| const selectedItemId = await window.lite.showNativeMenu({ items: serializedItems, position }); | ||
| if (selectedItemId === null) return; | ||
| await handlers.get(selectedItemId)?.(); | ||
| }; | ||
|
|
||
| const getBottomLeft = (element: HTMLElement): NativeMenuPosition => { | ||
| const rect = element.getBoundingClientRect(); | ||
| return { | ||
| x: Math.round(rect.left), | ||
| y: Math.round(rect.bottom), | ||
| }; | ||
| }; | ||
|
|
||
| export const showNativeContextMenu = async ( | ||
| event: MouseEvent<HTMLButtonElement>, | ||
| items: Array<NativeMenuItem>, | ||
| ): Promise<void> => { | ||
| event.preventDefault(); | ||
|
|
||
| const position = | ||
| event.clientX === 0 && event.clientY === 0 | ||
| ? getBottomLeft(event.currentTarget) | ||
| : { | ||
| x: Math.round(event.clientX), | ||
| y: Math.round(event.clientY), | ||
| }; | ||
|
|
||
| await showNativeMenu(items, position); | ||
| }; | ||
|
|
||
| export const showNativeMenuFromTrigger = async ( | ||
| trigger: HTMLElement, | ||
| items: Array<NativeMenuItem>, | ||
| ): Promise<void> => showNativeMenu(items, getBottomLeft(trigger)); | ||
Uh oh!
There was an error while loading. Please reload this page.