-
Notifications
You must be signed in to change notification settings - Fork 719
/
Copy pathdialogs.ts
86 lines (74 loc) · 2.34 KB
/
dialogs.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
import * as path from 'node:path';
import { BrowserWindow, dialog } from 'electron';
import { ipcMainManager } from './ipc';
import { isValidElectronPath } from './utils/local-version';
import { SelectedLocalVersion } from '../interfaces';
import { IpcEvents } from '../ipc-events';
/**
* Build a default name for a local Electron version
* from its dirname.
* @returns human-readable local build name
*/
function makeLocalName(folderPath: string): string {
// take a dirname like '/home/username/electron/gn/main/src/out/testing'
// and return something like 'gn/main - testing'
const tokens = folderPath.split(path.sep);
const buildType = tokens.pop(); // e.g. 'testing' or 'release'
const leader = tokens
// remove 'src/out/' -- they are in every local build, so make poor names
.slice(0, -2)
.join(path.sep)
// extract about enough for the end result to be about 20 chars
.slice(-20 + buildType!.length)
// remove any fragment in case the prev slice cut in the middle of a name
.split(path.sep)
.slice(1)
.join(path.sep);
return `${leader} - ${buildType}`;
}
/**
* Listens to IPC events related to dialogs and message boxes
*/
export function setupDialogs() {
ipcMainManager.on(IpcEvents.SHOW_WARNING_DIALOG, (event, args) => {
showWarningDialog(BrowserWindow.fromWebContents(event.sender)!, args);
});
ipcMainManager.handle(
IpcEvents.LOAD_LOCAL_VERSION_FOLDER,
async (event): Promise<SelectedLocalVersion | undefined> => {
const folderPath = await showOpenDialog(
BrowserWindow.fromWebContents(event.sender)!,
);
if (folderPath) {
const isValidElectron = isValidElectronPath(folderPath);
const localName = isValidElectron
? makeLocalName(folderPath)
: undefined;
return { folderPath, isValidElectron, localName };
}
return undefined;
},
);
}
/**
* Shows a warning dialog
*/
function showWarningDialog(
window: BrowserWindow,
args: Electron.MessageBoxOptions,
) {
dialog.showMessageBox(window, {
type: 'warning',
...args,
});
}
async function showOpenDialog(window: BrowserWindow) {
const { filePaths } = await dialog.showOpenDialog(window, {
title: 'Open Folder',
properties: ['openDirectory'],
});
if (!filePaths || filePaths.length < 1) {
return;
}
return filePaths[0];
}