/
build-default-menu.ts
637 lines (589 loc) · 17.5 KB
/
build-default-menu.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
import { Menu, ipcMain, shell, app } from 'electron'
import { ensureItemIds } from './ensure-item-ids'
import { MenuEvent } from './menu-event'
import { truncateWithEllipsis } from '../../lib/truncate-with-ellipsis'
import { getLogDirectoryPath } from '../../lib/logging/get-log-path'
import { ensureDir } from 'fs-extra'
import { log } from '../log'
import { openDirectorySafe } from '../shell'
import { enableRebaseDialog, enableStashing } from '../../lib/feature-flag'
import { MenuLabelsEvent } from '../../models/menu-labels'
import { DefaultEditorLabel } from '../../ui/lib/context-menu'
const defaultShellLabel = __DARWIN__
? 'Open in Terminal'
: 'Open in Command Prompt'
const createPullRequestLabel = __DARWIN__
? 'Create Pull Request'
: 'Create &pull request'
const showPullRequestLabel = __DARWIN__
? 'Show Pull Request'
: 'Show &pull request'
const defaultBranchNameValue = __DARWIN__ ? 'Default Branch' : 'default branch'
const confirmRepositoryRemovalLabel = __DARWIN__ ? 'Remove…' : '&Remove…'
const repositoryRemovalLabel = __DARWIN__ ? 'Remove' : '&Remove'
enum ZoomDirection {
Reset,
In,
Out,
}
export function buildDefaultMenu({
selectedExternalEditor,
selectedShell,
askForConfirmationOnForcePush,
askForConfirmationOnRepositoryRemoval,
hasCurrentPullRequest = false,
defaultBranchName = defaultBranchNameValue,
isForcePushForCurrentRepository = false,
isStashedChangesVisible = false,
}: MenuLabelsEvent): Electron.Menu {
defaultBranchName = truncateWithEllipsis(defaultBranchName, 25)
const removeRepoLabel = askForConfirmationOnRepositoryRemoval
? confirmRepositoryRemovalLabel
: repositoryRemovalLabel
const pullRequestLabel = hasCurrentPullRequest
? showPullRequestLabel
: createPullRequestLabel
const shellLabel =
selectedShell === null ? defaultShellLabel : `Open in ${selectedShell}`
const editorLabel =
selectedExternalEditor === null
? DefaultEditorLabel
: `Open in ${selectedExternalEditor}`
const template = new Array<Electron.MenuItemConstructorOptions>()
const separator: Electron.MenuItemConstructorOptions = { type: 'separator' }
if (__DARWIN__) {
template.push({
label: 'GitHub Desktop',
submenu: [
{
label: 'About GitHub Desktop',
click: emit('show-about'),
id: 'about',
},
separator,
{
label: 'Preferences…',
id: 'preferences',
accelerator: 'CmdOrCtrl+,',
click: emit('show-preferences'),
},
separator,
{
label: 'Install Command Line Tool…',
id: 'install-cli',
click: emit('install-cli'),
},
separator,
{
role: 'services',
submenu: [],
},
separator,
{ role: 'hide' },
{ role: 'hideothers' },
{ role: 'unhide' },
separator,
{ role: 'quit' },
],
})
}
const fileMenu: Electron.MenuItemConstructorOptions = {
label: __DARWIN__ ? 'File' : '&File',
submenu: [
{
label: __DARWIN__ ? 'New Repository…' : 'New &repository…',
id: 'new-repository',
click: emit('create-repository'),
accelerator: 'CmdOrCtrl+N',
},
separator,
{
label: __DARWIN__ ? 'Add Local Repository…' : 'Add &local repository…',
id: 'add-local-repository',
accelerator: 'CmdOrCtrl+O',
click: emit('add-local-repository'),
},
{
label: __DARWIN__ ? 'Clone Repository…' : 'Clo&ne repository…',
id: 'clone-repository',
accelerator: 'CmdOrCtrl+Shift+O',
click: emit('clone-repository'),
},
],
}
if (!__DARWIN__) {
const fileItems = fileMenu.submenu as Electron.MenuItemConstructorOptions[]
fileItems.push(
separator,
{
label: '&Options…',
id: 'preferences',
accelerator: 'CmdOrCtrl+,',
click: emit('show-preferences'),
},
separator,
{
role: 'quit',
label: 'E&xit',
accelerator: 'Alt+F4',
}
)
}
template.push(fileMenu)
template.push({
label: __DARWIN__ ? 'Edit' : '&Edit',
submenu: [
{ role: 'undo', label: __DARWIN__ ? 'Undo' : '&Undo' },
{ role: 'redo', label: __DARWIN__ ? 'Redo' : '&Redo' },
separator,
{ role: 'cut', label: __DARWIN__ ? 'Cut' : 'Cu&t' },
{ role: 'copy', label: __DARWIN__ ? 'Copy' : '&Copy' },
{ role: 'paste', label: __DARWIN__ ? 'Paste' : '&Paste' },
{
label: __DARWIN__ ? 'Select All' : 'Select &all',
accelerator: 'CmdOrCtrl+A',
click: emit('select-all'),
},
separator,
{
id: 'find',
label: __DARWIN__ ? 'Find' : '&Find',
accelerator: 'CmdOrCtrl+F',
click: emit('find-text'),
},
],
})
template.push({
label: __DARWIN__ ? 'View' : '&View',
submenu: [
{
label: __DARWIN__ ? 'Show Changes' : '&Changes',
id: 'show-changes',
accelerator: 'CmdOrCtrl+1',
click: emit('show-changes'),
},
{
label: __DARWIN__ ? 'Show History' : '&History',
id: 'show-history',
accelerator: 'CmdOrCtrl+2',
click: emit('show-history'),
},
{
label: __DARWIN__ ? 'Show Repository List' : 'Repository &list',
id: 'show-repository-list',
accelerator: 'CmdOrCtrl+T',
click: emit('choose-repository'),
},
{
label: __DARWIN__ ? 'Show Branches List' : '&Branches list',
id: 'show-branches-list',
accelerator: 'CmdOrCtrl+B',
click: emit('show-branches'),
},
separator,
{
label: __DARWIN__ ? 'Go to Summary' : 'Go to &Summary',
id: 'go-to-commit-message',
accelerator: 'CmdOrCtrl+G',
click: emit('go-to-commit-message'),
},
{
label: getStashedChangesLabel(isStashedChangesVisible),
id: 'toggle-stashed-changes',
accelerator: 'Ctrl+H',
click: isStashedChangesVisible
? emit('hide-stashed-changes')
: emit('show-stashed-changes'),
visible: enableStashing(),
},
{
label: __DARWIN__ ? 'Toggle Full Screen' : 'Toggle &full screen',
role: 'togglefullscreen',
},
separator,
{
label: __DARWIN__ ? 'Reset Zoom' : 'Reset zoom',
accelerator: 'CmdOrCtrl+0',
click: zoom(ZoomDirection.Reset),
},
{
label: __DARWIN__ ? 'Zoom In' : 'Zoom in',
accelerator: 'CmdOrCtrl+=',
click: zoom(ZoomDirection.In),
},
{
label: __DARWIN__ ? 'Zoom Out' : 'Zoom out',
accelerator: 'CmdOrCtrl+-',
click: zoom(ZoomDirection.Out),
},
separator,
{
label: '&Reload',
id: 'reload-window',
// Ctrl+Alt is interpreted as AltGr on international keyboards and this
// can clash with other shortcuts. We should always use Ctrl+Shift for
// chorded shortcuts, but this menu item is not a user-facing feature
// so we are going to keep this one around.
accelerator: 'CmdOrCtrl+Alt+R',
click(item: any, focusedWindow: Electron.BrowserWindow) {
if (focusedWindow) {
focusedWindow.reload()
}
},
visible: __RELEASE_CHANNEL__ === 'development',
},
{
id: 'show-devtools',
label: __DARWIN__
? 'Toggle Developer Tools'
: '&Toggle developer tools',
accelerator: (() => {
return __DARWIN__ ? 'Alt+Command+I' : 'Ctrl+Shift+I'
})(),
click(item: any, focusedWindow: Electron.BrowserWindow) {
if (focusedWindow) {
focusedWindow.webContents.toggleDevTools()
}
},
},
],
})
const pushLabel = getPushLabel(
isForcePushForCurrentRepository,
askForConfirmationOnForcePush
)
const pushEventType = isForcePushForCurrentRepository ? 'force-push' : 'push'
template.push({
label: __DARWIN__ ? 'Repository' : '&Repository',
id: 'repository',
submenu: [
{
id: 'push',
label: pushLabel,
accelerator: 'CmdOrCtrl+P',
click: emit(pushEventType),
},
{
id: 'pull',
label: __DARWIN__ ? 'Pull' : 'Pu&ll',
accelerator: 'CmdOrCtrl+Shift+P',
click: emit('pull'),
},
{
label: removeRepoLabel,
id: 'remove-repository',
accelerator: 'CmdOrCtrl+Backspace',
click: emit('remove-repository'),
},
separator,
{
id: 'view-repository-on-github',
label: __DARWIN__ ? 'View on GitHub' : '&View on GitHub',
accelerator: 'CmdOrCtrl+Shift+G',
click: emit('view-repository-on-github'),
},
{
label: shellLabel,
id: 'open-in-shell',
accelerator: 'Ctrl+`',
click: emit('open-in-shell'),
},
{
label: __DARWIN__
? 'Show in Finder'
: __WIN32__
? 'Show in E&xplorer'
: 'Show in your File Manager',
id: 'open-working-directory',
accelerator: 'CmdOrCtrl+Shift+F',
click: emit('open-working-directory'),
},
{
label: editorLabel,
id: 'open-external-editor',
accelerator: 'CmdOrCtrl+Shift+A',
click: emit('open-external-editor'),
},
separator,
{
label: __DARWIN__ ? 'Repository Settings…' : 'Repository &settings…',
id: 'show-repository-settings',
click: emit('show-repository-settings'),
},
],
})
template.push({
label: __DARWIN__ ? 'Branch' : '&Branch',
id: 'branch',
submenu: [
{
label: __DARWIN__ ? 'New Branch…' : 'New &branch…',
id: 'create-branch',
accelerator: 'CmdOrCtrl+Shift+N',
click: emit('create-branch'),
},
{
label: __DARWIN__ ? 'Rename…' : '&Rename…',
id: 'rename-branch',
accelerator: 'CmdOrCtrl+Shift+R',
click: emit('rename-branch'),
},
{
label: __DARWIN__ ? 'Delete…' : '&Delete…',
id: 'delete-branch',
accelerator: 'CmdOrCtrl+Shift+D',
click: emit('delete-branch'),
},
separator,
{
label: __DARWIN__ ? 'Discard All Changes…' : 'Discard all changes…',
id: 'discard-all-changes',
accelerator: 'CmdOrCtrl+Shift+Backspace',
click: emit('discard-all-changes'),
},
separator,
{
label: __DARWIN__
? `Update from ${defaultBranchName}`
: `&Update from ${defaultBranchName}`,
id: 'update-branch',
accelerator: 'CmdOrCtrl+Shift+U',
click: emit('update-branch'),
},
{
label: __DARWIN__ ? 'Compare to Branch' : '&Compare to branch',
id: 'compare-to-branch',
accelerator: 'CmdOrCtrl+Shift+B',
click: emit('compare-to-branch'),
},
{
label: __DARWIN__
? 'Merge into Current Branch…'
: '&Merge into current branch…',
id: 'merge-branch',
accelerator: 'CmdOrCtrl+Shift+M',
click: emit('merge-branch'),
},
{
label: __DARWIN__
? 'Rebase Current Branch…'
: 'R&ebase current branch…',
id: 'rebase-branch',
accelerator: 'CmdOrCtrl+Shift+E',
click: emit('rebase-branch'),
visible: enableRebaseDialog(),
},
separator,
{
label: __DARWIN__ ? 'Compare on GitHub' : 'Compare on &GitHub',
id: 'compare-on-github',
accelerator: 'CmdOrCtrl+Shift+C',
click: emit('compare-on-github'),
},
{
label: pullRequestLabel,
id: 'create-pull-request',
accelerator: 'CmdOrCtrl+R',
click: emit('open-pull-request'),
},
],
})
if (__DARWIN__) {
template.push({
role: 'window',
submenu: [
{ role: 'minimize' },
{ role: 'zoom' },
{ role: 'close' },
separator,
{ role: 'front' },
],
})
}
const submitIssueItem: Electron.MenuItemConstructorOptions = {
label: __DARWIN__ ? 'Report Issue…' : 'Report issue…',
click() {
shell.openExternal('https://github.com/desktop/desktop/issues/new/choose')
},
}
const contactSupportItem: Electron.MenuItemConstructorOptions = {
label: __DARWIN__ ? 'Contact GitHub Support…' : '&Contact GitHub support…',
click() {
shell.openExternal(
`https://github.com/contact?from_desktop_app=1&app_version=${app.getVersion()}`
)
},
}
const showUserGuides: Electron.MenuItemConstructorOptions = {
label: 'Show User Guides',
click() {
shell.openExternal('https://help.github.com/desktop/guides/')
},
}
const showKeyboardShortcuts: Electron.MenuItemConstructorOptions = {
label: __DARWIN__ ? 'Show Keyboard Shortcuts' : 'Show keyboard shortcuts',
click() {
shell.openExternal(
'https://help.github.com/en/desktop/getting-started-with-github-desktop/keyboard-shortcuts-in-github-desktop'
)
},
}
const showLogsLabel = __DARWIN__
? 'Show Logs in Finder'
: __WIN32__
? 'S&how logs in Explorer'
: 'S&how logs in your File Manager'
const showLogsItem: Electron.MenuItemConstructorOptions = {
label: showLogsLabel,
click() {
const logPath = getLogDirectoryPath()
ensureDir(logPath)
.then(() => {
openDirectorySafe(logPath)
})
.catch(err => {
log('error', err.message)
})
},
}
const helpItems = [
submitIssueItem,
contactSupportItem,
showUserGuides,
showKeyboardShortcuts,
showLogsItem,
]
if (__DEV__) {
helpItems.push(
separator,
{
label: 'Crash main process…',
click() {
throw new Error('Boomtown!')
},
},
{
label: 'Crash renderer process…',
click: emit('boomtown'),
},
{
label: 'Show popup',
submenu: [
{
label: 'Release notes',
click: emit('show-release-notes-popup'),
},
],
},
{
label: 'Prune branches',
click: emit('test-prune-branches'),
}
)
}
if (__DARWIN__) {
template.push({
role: 'help',
submenu: helpItems,
})
} else {
template.push({
label: '&Help',
submenu: [
...helpItems,
separator,
{
label: '&About GitHub Desktop',
click: emit('show-about'),
id: 'about',
},
],
})
}
ensureItemIds(template)
return Menu.buildFromTemplate(template)
}
function getPushLabel(
isForcePushForCurrentRepository: boolean,
askForConfirmationOnForcePush: boolean
): string {
if (!isForcePushForCurrentRepository) {
return __DARWIN__ ? 'Push' : 'P&ush'
}
if (askForConfirmationOnForcePush) {
return __DARWIN__ ? 'Force Push…' : 'Force P&ush…'
}
return __DARWIN__ ? 'Force Push' : 'Force P&ush'
}
function getStashedChangesLabel(isStashedChangesVisible: boolean): string {
if (isStashedChangesVisible) {
return __DARWIN__ ? 'Hide Stashed Changes' : 'H&ide stashed changes'
}
return __DARWIN__ ? 'Show Stashed Changes' : 'Sho&w stashed changes'
}
type ClickHandler = (
menuItem: Electron.MenuItem,
browserWindow: Electron.BrowserWindow,
event: Electron.Event
) => void
/**
* Utility function returning a Click event handler which, when invoked, emits
* the provided menu event over IPC.
*/
function emit(name: MenuEvent): ClickHandler {
return (menuItem, window) => {
if (window) {
window.webContents.send('menu-event', { name })
} else {
ipcMain.emit('menu-event', { name })
}
}
}
/** The zoom steps that we support, these factors must sorted */
const ZoomInFactors = [1, 1.1, 1.25, 1.5, 1.75, 2]
const ZoomOutFactors = ZoomInFactors.slice().reverse()
/**
* Returns the element in the array that's closest to the value parameter. Note
* that this function will throw if passed an empty array.
*/
function findClosestValue(arr: Array<number>, value: number) {
return arr.reduce((previous, current) => {
return Math.abs(current - value) < Math.abs(previous - value)
? current
: previous
})
}
/**
* Figure out the next zoom level for the given direction and alert the renderer
* about a change in zoom factor if necessary.
*/
function zoom(direction: ZoomDirection): ClickHandler {
return (menuItem, window) => {
if (!window) {
return
}
const { webContents } = window
if (direction === ZoomDirection.Reset) {
webContents.setZoomFactor(1)
webContents.send('zoom-factor-changed', 1)
} else {
const rawZoom = webContents.getZoomFactor()
const zoomFactors =
direction === ZoomDirection.In ? ZoomInFactors : ZoomOutFactors
// So the values that we get from getZoomFactor are floating point
// precision numbers from chromium that don't always round nicely so
// we'll have to do a little trick to figure out which of our supported
// zoom factors the value is referring to.
const currentZoom = findClosestValue(zoomFactors, rawZoom)
const nextZoomLevel = zoomFactors.find(f =>
direction === ZoomDirection.In ? f > currentZoom : f < currentZoom
)
// If we couldn't find a zoom level (likely due to manual manipulation
// of the zoom factor in devtools) we'll just snap to the closest valid
// factor we've got.
const newZoom = nextZoomLevel === undefined ? currentZoom : nextZoomLevel
webContents.setZoomFactor(newZoom)
webContents.send('zoom-factor-changed', newZoom)
}
}
}