-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmain-window.ts
172 lines (150 loc) · 4.95 KB
/
main-window.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
import { BrowserWindow, type BrowserWindowConstructorOptions } from 'electron';
import installExtension, { REACT_DEVELOPER_TOOLS } from 'electron-devtools-installer';
import {
DEFAULT_WIDTH,
MAIN_MIN_HEIGHT,
MAIN_MIN_WIDTH,
WINDOWS_TITLEBAR_HEIGHT,
} from 'src/constants';
import { sendIpcEventToRendererWithWindow } from 'src/ipc-utils';
import { isEmptyDir } from 'src/lib/fs-utils';
import { portFinder } from 'src/lib/port-finder';
import { keepSqliteIntegrationUpdated } from 'src/lib/sqlite-versions';
import { removeMenu } from 'src/menu';
import { UserData } from 'src/storage/storage-types';
import { loadUserData, saveUserData } from 'src/storage/user-data';
import { moveDatabasesInSitu } from 'vendor/wp-now/src';
// This allows TypeScript to pick up the magic constants that's auto-generated by Forge's Webpack
// plugin that tells the Electron app where to look for the Webpack-bundled app code (depending on
// whether you're running in development or production).
declare const MAIN_WINDOW_WEBPACK_ENTRY: string;
declare const MAIN_WINDOW_PRELOAD_WEBPACK_ENTRY: string;
let mainWindow: BrowserWindow | null;
function setupDevTools( mainWindow: BrowserWindow | null, devToolsOpen?: boolean ) {
if ( devToolsOpen || ( process.env.NODE_ENV === 'development' && devToolsOpen === undefined ) ) {
mainWindow?.webContents.openDevTools();
installExtension( REACT_DEVELOPER_TOOLS );
}
}
function initializePortFinder( sites: SiteDetails[] ) {
sites.forEach( ( site ) => {
if ( site.port ) {
portFinder.addUnavailablePort( site.port );
}
} );
}
async function removeSitesWithEmptyDirectories( userData: UserData ) {
const sitesWithNonEmptyDirectories: SiteDetails[] = [];
const storedSites = userData.sites || [];
for ( const site of storedSites ) {
if ( ! site.path ) {
continue;
}
const directoryIsEmpty = await isEmptyDir( site.path );
if ( ! directoryIsEmpty ) {
sitesWithNonEmptyDirectories.push( site );
}
}
saveUserData( { ...userData, sites: sitesWithNonEmptyDirectories } );
}
export function createMainWindow(): BrowserWindow {
if ( mainWindow && ! mainWindow.isDestroyed() ) {
return mainWindow;
}
mainWindow = new BrowserWindow( {
height: MAIN_MIN_HEIGHT,
width: DEFAULT_WIDTH,
backgroundColor: 'rgba(30, 30, 30, 1)',
minHeight: MAIN_MIN_HEIGHT,
minWidth: MAIN_MIN_WIDTH,
webPreferences: {
preload: MAIN_WINDOW_PRELOAD_WEBPACK_ENTRY,
},
...getOSWindowOptions(),
} );
mainWindow.loadURL( MAIN_WINDOW_WEBPACK_ENTRY );
// Open the DevTools if the user had it open last time they used the app.
// During development the dev tools default to open.
loadUserData().then( ( userData ) => {
const { devToolsOpen, sites } = userData;
setupDevTools( mainWindow, devToolsOpen );
initializePortFinder( sites );
removeSitesWithEmptyDirectories( userData );
for ( const site of sites ) {
moveDatabasesInSitu( site.path ).then( () => {
keepSqliteIntegrationUpdated( site.path );
} );
}
} );
mainWindow.webContents.on( 'devtools-opened', async () => {
const data = await loadUserData();
data.devToolsOpen = true;
await saveUserData( data );
} );
mainWindow.webContents.on( 'devtools-closed', async () => {
const data = await loadUserData();
data.devToolsOpen = false;
await saveUserData( data );
} );
mainWindow.on( 'closed', () => {
removeMenu();
mainWindow = null;
} );
mainWindow.on( 'enter-full-screen', () => {
sendIpcEventToRendererWithWindow( mainWindow, 'window-fullscreen-change', true );
} );
mainWindow.on( 'leave-full-screen', () => {
sendIpcEventToRendererWithWindow( mainWindow, 'window-fullscreen-change', false );
} );
return mainWindow;
}
function getOSWindowOptions(): Partial< BrowserWindowConstructorOptions > {
switch ( process.platform ) {
case 'darwin':
return {
frame: false,
titleBarStyle: 'hidden',
trafficLightPosition: { x: 20, y: 20 },
};
case 'win32':
return {
titleBarStyle: 'hidden',
titleBarOverlay: {
color: 'rgba(30, 30, 30, 1)',
symbolColor: 'white',
height: WINDOWS_TITLEBAR_HEIGHT,
},
minHeight: MAIN_MIN_HEIGHT + WINDOWS_TITLEBAR_HEIGHT,
};
default:
return {};
}
}
export function getMainWindow() {
return new Promise< BrowserWindow >( ( resolve ) => {
if ( mainWindow && ! mainWindow.isDestroyed() && ! mainWindow.webContents.isDestroyed() ) {
resolve( mainWindow );
return;
}
const windows = BrowserWindow.getAllWindows();
if ( windows.length > 0 ) {
mainWindow = BrowserWindow.getFocusedWindow() || windows[ 0 ];
if ( ! mainWindow.webContents.isDestroyed() ) {
resolve( mainWindow );
}
return;
}
const newWindow = createMainWindow();
mainWindow = newWindow;
newWindow.webContents.on( 'did-finish-load', () => {
resolve( newWindow );
} );
} );
}
/**
* Reset the main window reference. Exported for testing as resetting modules
* with Jest while preserving manual Electron mocks proved quite difficult.
*/
export function __resetMainWindow() {
mainWindow = null;
}