import { invoke } from '@tauri-apps/api/core' import { getCurrentWindow } from '@tauri-apps/api/window' import { getAppCommandMenuSections } from '../hooks/appCommandCatalog' import { createTranslator, translate, type AppLocale } from '../lib/i18n' import { Button } from './ui/button' import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuShortcut, DropdownMenuSub, DropdownMenuSubContent, DropdownMenuSubTrigger, DropdownMenuTrigger, } from './ui/dropdown-menu' type MenuItem = | { kind: 'separator' } | { kind: 'command' commandId: string label: string menuItemId: string shortcut?: string } | { kind: 'action'; action: () => void; label: string; shortcut?: string } type MenuSection = { items: ReadonlyArray label: string } function menuSections(locale: AppLocale): ReadonlyArray { const t = createTranslator(locale) return [ ...getAppCommandMenuSections(t), { label: t('menu.window'), items: [ { kind: 'action', label: t('window.minimize'), action: () => void getCurrentWindow().minimize().catch(() => {}) }, { kind: 'action', label: t('window.maximize'), action: () => void getCurrentWindow().toggleMaximize().catch(() => {}) }, { kind: 'separator' }, { kind: 'action', label: t('window.close'), action: () => void getCurrentWindow().close().catch(() => {}) }, ], }, ] } const MENU_SECTIONS: ReadonlyArray = menuSections('en') function getMenuSections(locale: AppLocale): ReadonlyArray { if (locale === 'en') return MENU_SECTIONS return menuSections(locale) } function triggerMenuCommand(menuItemId: string): void { void invoke('trigger_menu_command', { id: menuItemId }).catch(() => {}) } function menuSeparatorKey(section: MenuSection, item: MenuItem): string { const ordinal = section.items .slice(0, section.items.indexOf(item) + 1) .filter(candidate => candidate.kind === 'separator') .length return `${section.label}-separator-${ordinal}` } function MenuSectionItems({ section }: { section: MenuSection }) { return ( <> {section.items.map((item) => { if (item.kind === 'separator') { return } if (item.kind === 'command') { return ( triggerMenuCommand(item.menuItemId)} > {item.label} {item.shortcut && ( {item.shortcut} )} ) } return ( {item.label} {item.shortcut && {item.shortcut}} ) })} ) } function HamburgerIcon() { return ( ) } function AppMenuButton({ locale, sections }: { locale: AppLocale; sections: ReadonlyArray }) { return ( {sections.map((section) => ( {section.label} ))} ) } function HorizontalMenuBar({ sections }: { sections: ReadonlyArray }) { return (
{sections.map((section) => ( ))}
) } export function LinuxMenuButton({ locale = 'en' }: { locale?: AppLocale } = {}) { const sections = getMenuSections(locale) return ( <>
) }