@@ -160,6 +160,35 @@ const TITLEBAR_COLOR = "#01000000"; // #00000000 does not work correctly on Linu
160160const TITLEBAR_LIGHT_SYMBOL_COLOR = "#1f2937" ;
161161const TITLEBAR_DARK_SYMBOL_COLOR = "#f8fafc" ;
162162
163+ function normalizeContextMenuItems ( source : readonly ContextMenuItem [ ] ) : ContextMenuItem [ ] {
164+ const normalizedItems : ContextMenuItem [ ] = [ ] ;
165+
166+ for ( const sourceItem of source ) {
167+ if ( typeof sourceItem . id !== "string" || typeof sourceItem . label !== "string" ) {
168+ continue ;
169+ }
170+
171+ const normalizedItem : ContextMenuItem = {
172+ id : sourceItem . id ,
173+ label : sourceItem . label ,
174+ destructive : sourceItem . destructive === true ,
175+ disabled : sourceItem . disabled === true ,
176+ } ;
177+
178+ if ( sourceItem . children ) {
179+ const normalizedChildren = normalizeContextMenuItems ( sourceItem . children ) ;
180+ if ( normalizedChildren . length === 0 ) {
181+ continue ;
182+ }
183+ normalizedItem . children = normalizedChildren ;
184+ }
185+
186+ normalizedItems . push ( normalizedItem ) ;
187+ }
188+
189+ return normalizedItems ;
190+ }
191+
163192type WindowTitleBarOptions = Pick <
164193 BrowserWindowConstructorOptions ,
165194 "titleBarOverlay" | "titleBarStyle" | "trafficLightPosition"
@@ -1715,14 +1744,7 @@ function registerIpcHandlers(): void {
17151744 ipcMain . handle (
17161745 CONTEXT_MENU_CHANNEL ,
17171746 async ( _event , items : ContextMenuItem [ ] , position ?: { x : number ; y : number } ) => {
1718- const normalizedItems = items
1719- . filter ( ( item ) => typeof item . id === "string" && typeof item . label === "string" )
1720- . map ( ( item ) => ( {
1721- id : item . id ,
1722- label : item . label ,
1723- destructive : item . destructive === true ,
1724- disabled : item . disabled === true ,
1725- } ) ) ;
1747+ const normalizedItems = normalizeContextMenuItems ( items ) ;
17261748 if ( normalizedItems . length === 0 ) {
17271749 return null ;
17281750 }
@@ -1743,28 +1765,37 @@ function registerIpcHandlers(): void {
17431765 if ( ! window ) return null ;
17441766
17451767 return new Promise < string | null > ( ( resolve ) => {
1746- const template : MenuItemConstructorOptions [ ] = [ ] ;
1747- let hasInsertedDestructiveSeparator = false ;
1748- for ( const item of normalizedItems ) {
1749- if ( item . destructive && ! hasInsertedDestructiveSeparator && template . length > 0 ) {
1750- template . push ( { type : "separator" } ) ;
1751- hasInsertedDestructiveSeparator = true ;
1752- }
1753- const itemOption : MenuItemConstructorOptions = {
1754- label : item . label ,
1755- enabled : ! item . disabled ,
1756- click : ( ) => resolve ( item . id ) ,
1757- } ;
1758- if ( item . destructive ) {
1759- const destructiveIcon = getDestructiveMenuIcon ( ) ;
1760- if ( destructiveIcon ) {
1761- itemOption . icon = destructiveIcon ;
1768+ const buildTemplate = (
1769+ entries : readonly ContextMenuItem [ ] ,
1770+ ) : MenuItemConstructorOptions [ ] => {
1771+ const template : MenuItemConstructorOptions [ ] = [ ] ;
1772+ let hasInsertedDestructiveSeparator = false ;
1773+ for ( const item of entries ) {
1774+ if ( item . destructive && ! hasInsertedDestructiveSeparator && template . length > 0 ) {
1775+ template . push ( { type : "separator" } ) ;
1776+ hasInsertedDestructiveSeparator = true ;
1777+ }
1778+ const itemOption : MenuItemConstructorOptions = {
1779+ label : item . label ,
1780+ enabled : ! item . disabled ,
1781+ } ;
1782+ if ( item . children && item . children . length > 0 ) {
1783+ itemOption . submenu = buildTemplate ( item . children ) ;
1784+ } else {
1785+ itemOption . click = ( ) => resolve ( item . id ) ;
17621786 }
1787+ if ( item . destructive && ( ! item . children || item . children . length === 0 ) ) {
1788+ const destructiveIcon = getDestructiveMenuIcon ( ) ;
1789+ if ( destructiveIcon ) {
1790+ itemOption . icon = destructiveIcon ;
1791+ }
1792+ }
1793+ template . push ( itemOption ) ;
17631794 }
1764- template . push ( itemOption ) ;
1765- }
1795+ return template ;
1796+ } ;
17661797
1767- const menu = Menu . buildFromTemplate ( template ) ;
1798+ const menu = Menu . buildFromTemplate ( buildTemplate ( normalizedItems ) ) ;
17681799 menu . popup ( {
17691800 window,
17701801 ...popupPosition ,
0 commit comments