| 175 | } |
| 176 | |
| 177 | private async showContextMenu(event: Electron.IpcMainInvokeEvent, menuItems: any): Promise<void> { |
| 178 | if (!this.mainWindow) return |
| 179 | |
| 180 | const senderWebContents = event.sender |
| 181 | |
| 182 | const buildTemplate = (items: any[], senderWebContents: Electron.WebContents): any[] => { |
| 183 | return items.map((item: any) => { |
| 184 | const menuItem: any = { |
| 185 | label: item.label |
| 186 | } |
| 187 | |
| 188 | if (item.submenu) { |
| 189 | menuItem.submenu = buildTemplate(item.submenu, senderWebContents) |
| 190 | } else { |
| 191 | menuItem.click = () => { |
| 192 | // 将命令发送到触发菜单的窗口,而不是总是发送到主窗口 |
| 193 | senderWebContents.send('context-menu-command', item.id) |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | // 支持 checkbox 类型的菜单项 |
| 198 | if (item.type === 'checkbox') { |
| 199 | menuItem.type = 'checkbox' |
| 200 | menuItem.checked = item.checked || false |
| 201 | } |
| 202 | |
| 203 | return menuItem |
| 204 | }) |
| 205 | } |
| 206 | |
| 207 | const template = buildTemplate(menuItems, senderWebContents) |
| 208 | |
| 209 | const menu = Menu.buildFromTemplate(template) |
| 210 | menu.popup({ window: this.mainWindow }) |
| 211 | } |
| 212 | |
| 213 | public async selectAvatar(): Promise<any> { |
| 214 | try { |