@@ -14,100 +14,108 @@ const isLinux = process.platform == 'linux';
14
14
let mainMenu ;
15
15
let cachedMenus ;
16
16
17
+ // Binds click events to all menu and submenu items recursively.
18
+ function bindMenuClicks ( pgadminMenus , pgAdminMainScreen ) {
19
+ return pgadminMenus . map ( ( menuItem ) => ( {
20
+ ...menuItem ,
21
+ submenu : menuItem . submenu ?. map ( ( subMenuItem ) => {
22
+ const smName = `${ menuItem . name } _${ subMenuItem . name } ` ;
23
+ return {
24
+ ...subMenuItem ,
25
+ click : ( ) => {
26
+ pgAdminMainScreen . webContents . send ( 'menu-click' , smName ) ;
27
+ } ,
28
+ submenu : subMenuItem . submenu ?. map ( ( deeperSubMenuItem ) => ( {
29
+ ...deeperSubMenuItem ,
30
+ click : ( ) => {
31
+ pgAdminMainScreen . webContents . send ( 'menu-click' , `${ smName } _${ deeperSubMenuItem . name } ` ) ;
32
+ } ,
33
+ } ) ) ,
34
+ } ;
35
+ } ) ,
36
+ } ) ) ;
37
+ }
38
+
39
+ // Handles auto-update related menu items for macOS.
40
+ // Adds or disables update menu items based on config state.
41
+ function handleAutoUpdateMenu ( menuFile , configStore , callbacks ) {
42
+ if ( ! configStore . get ( 'auto_update_enabled' ) ) return ;
43
+ if ( configStore . get ( 'update_downloaded' ) ) {
44
+ // Add "Restart to Update" if update is downloaded
45
+ menuFile . submenu . unshift ( {
46
+ name : 'mnu_restart_to_update' ,
47
+ id : 'mnu_restart_to_update' ,
48
+ label : 'Restart to Update...' ,
49
+ enabled : true ,
50
+ priority : 998 ,
51
+ click : callbacks [ 'restart_to_update' ] ,
52
+ } ) ;
53
+ } else {
54
+ // Add "Check for Updates" if update is not downloaded
55
+ menuFile . submenu . unshift ( {
56
+ name : 'mnu_check_updates' ,
57
+ id : 'mnu_check_updates' ,
58
+ label : 'Check for Updates...' ,
59
+ enabled : true ,
60
+ priority : 998 ,
61
+ click : callbacks [ 'check_for_updates' ] ,
62
+ } ) ;
63
+ }
64
+ // Disable "Check for Updates" if update is downloading
65
+ if ( configStore . get ( 'update_downloading' ) ) {
66
+ menuFile . submenu . forEach ( ( item ) => {
67
+ if ( item . id == 'mnu_check_updates' ) item . enabled = false ;
68
+ } ) ;
69
+ }
70
+ }
71
+
72
+ // Remove About pgAdmin 4 from help menu and add it to the top of menuFile submenu.
73
+ function moveAboutMenuToTop ( pgadminMenus , menuFile ) {
74
+ const helpMenu = pgadminMenus . find ( ( menu ) => menu . name == 'help' ) ;
75
+ if ( ! helpMenu ) return ;
76
+ const aboutItem = helpMenu . submenu . find ( ( item ) => item . name === 'mnu_about' ) ;
77
+ if ( ! aboutItem ) return ;
78
+ helpMenu . submenu = helpMenu . submenu . filter ( ( item ) => item . name !== 'mnu_about' ) ;
79
+ menuFile . submenu . unshift ( aboutItem ) ;
80
+ menuFile . submenu . splice ( 2 , 0 , { type : 'separator' } ) ;
81
+ }
82
+
83
+ // Builds the application menu template and binds menu click events.
84
+ // Handles platform-specific menu structure and dynamic menu items.
17
85
function buildMenu ( pgadminMenus , pgAdminMainScreen , configStore , callbacks ) {
18
86
const template = [ ] ;
19
87
20
- // bind all menus click event.
21
- pgadminMenus = pgadminMenus . map ( ( menuItem ) => {
22
- return {
23
- ...menuItem ,
24
- submenu : menuItem . submenu ?. map ( ( subMenuItem ) => {
25
- const smName = `${ menuItem . name } _${ subMenuItem . name } ` ;
26
- return {
27
- ...subMenuItem ,
28
- click : ( ) => {
29
- pgAdminMainScreen . webContents . send ( 'menu-click' , smName ) ;
30
- } ,
31
- submenu : subMenuItem . submenu ?. map ( ( deeperSubMenuItem ) => {
32
- return {
33
- ...deeperSubMenuItem ,
34
- click : ( ) => {
35
- pgAdminMainScreen . webContents . send ( 'menu-click' , `${ smName } _${ deeperSubMenuItem . name } ` ) ;
36
- } ,
37
- } ;
38
- } ) ,
39
- } ;
40
- } ) ,
41
- } ;
42
- } ) ;
88
+ pgadminMenus = bindMenuClicks ( pgadminMenus , pgAdminMainScreen ) ;
43
89
44
90
let menuFile = pgadminMenus . shift ( ) ;
45
91
92
+ // macOS-specific menu modifications
46
93
if ( isMac ) {
47
- if ( configStore . get ( 'update_downloaded' ) ) {
48
- //Add Restart to update menu item in the app menu if update is downloaded.
49
- menuFile . submenu . unshift ( {
50
- name :'mnu_restart_to_update' ,
51
- id : 'mnu_restart_to_update' ,
52
- label : 'Restart to Update...' ,
53
- enabled : true ,
54
- priority : 998 ,
55
- click : callbacks [ 'restart_to_update' ] ,
56
- } ) ;
57
- } else {
58
- // Add Check for Updates menu item in the app menu.
59
- menuFile . submenu . unshift ( {
60
- name :'mnu_check_updates' ,
61
- id : 'mnu_check_updates' ,
62
- label : 'Check for Updates...' ,
63
- enabled : true ,
64
- priority : 998 ,
65
- click : callbacks [ 'check_for_updates' ] ,
66
- } ) ;
67
- }
68
-
69
- // Disable the Check for updates menu item if update is downloading.
70
- if ( configStore . get ( 'update_downloading' ) ) {
71
- menuFile . submenu . forEach ( ( item ) => {
72
- if ( item . id == 'mnu_check_updates' ) item . enabled = false ;
73
- } ) ;
74
- }
75
-
76
- // Remove About pgAdmin 4 from help menu and add it to the top of menuFile submenu.
77
- const helpMenu = pgadminMenus . find ( ( menu ) => menu . name == 'help' ) ;
78
- if ( helpMenu ) {
79
- const aboutItem = helpMenu . submenu . find ( ( item ) => item . name === 'mnu_about' ) ;
80
- if ( aboutItem ) {
81
- helpMenu . submenu = helpMenu . submenu . filter ( ( item ) => item . name !== 'mnu_about' ) ;
82
- menuFile . submenu . unshift ( aboutItem ) ;
83
- menuFile . submenu . splice ( 2 , 0 , { type : 'separator' } ) ;
84
- }
85
- }
94
+ handleAutoUpdateMenu ( menuFile , configStore , callbacks ) ;
95
+ moveAboutMenuToTop ( pgadminMenus , menuFile ) ;
86
96
}
87
-
97
+
88
98
template . push ( {
89
99
...menuFile ,
90
100
submenu : [
91
101
...menuFile . submenu ,
92
102
{ type : 'separator' } ,
93
- {
94
- label : 'View Logs...' , click : callbacks [ 'view_logs' ] ,
95
- } ,
96
- {
97
- label : 'Configure runtime...' , click : callbacks [ 'configure' ] ,
98
- } ,
103
+ { label : 'View Logs...' , click : callbacks [ 'view_logs' ] } ,
104
+ { label : 'Configure runtime...' , click : callbacks [ 'configure' ] } ,
99
105
{ type : 'separator' } ,
100
- ...( isMac ? [
101
- { role : 'hide' } ,
102
- { role : 'hideOthers' } ,
103
- { role : 'unhide' } ,
104
- { type : 'separator' } ,
105
- ] : [ ] ) ,
106
+ ...( isMac
107
+ ? [
108
+ { role : 'hide' } ,
109
+ { role : 'hideOthers' } ,
110
+ { role : 'unhide' } ,
111
+ { type : 'separator' } ,
112
+ ]
113
+ : [ ] ) ,
106
114
{ role : 'quit' } ,
107
115
] ,
108
116
} ) ;
109
117
110
- if ( isMac ) {
118
+ if ( isMac ) {
111
119
template [ 0 ] . label = app . name ;
112
120
}
113
121
@@ -119,19 +127,25 @@ function buildMenu(pgadminMenus, pgAdminMainScreen, configStore, callbacks) {
119
127
{
120
128
label : 'View' ,
121
129
submenu : [
122
- { label : 'Reload' , click : callbacks [ 'reloadApp' ] } ,
123
- { label : 'Toggle Developer Tools' , click : ( ) => BrowserWindow . getFocusedWindow ( ) . webContents . openDevTools ( { mode : 'bottom' } ) } ,
130
+ { label : 'Reload' , click : callbacks [ 'reloadApp' ] } ,
131
+ {
132
+ label : 'Toggle Developer Tools' ,
133
+ click : ( ) =>
134
+ BrowserWindow . getFocusedWindow ( ) . webContents . openDevTools ( {
135
+ mode : 'bottom' ,
136
+ } ) ,
137
+ } ,
124
138
{ type : 'separator' } ,
125
139
{ role : 'resetZoom' } ,
126
140
{ role : 'zoomIn' } ,
127
141
{ role : 'zoomOut' } ,
128
142
{ type : 'separator' } ,
129
143
] . concat ( isLinux ? [ ] : [ { role : 'togglefullscreen' } ] ) ,
130
144
} ,
131
- { role : 'windowMenu' } ,
145
+ { role : 'windowMenu' }
132
146
) ;
133
147
134
- template . push ( pgadminMenus [ pgadminMenus . length - 1 ] ) ;
148
+ template . push ( pgadminMenus [ pgadminMenus . length - 1 ] ) ;
135
149
136
150
return Menu . buildFromTemplate ( template ) ;
137
151
}
0 commit comments