File size: 3,332 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/* eslint-disable import/order */
const electron = require( 'electron' );
const BrowserWindow = electron.BrowserWindow;
const app = electron.app;
const { getPath } = require( '../../lib/assets' );

// HACK(sendhilp, 9/6/2016): The reason for this strange importing is there seems to be a
// bug post Electron 1.1.1 in which attempting to access electron.screen
// outside of app.on('ready') results in an error about require being unable
// to find '../screen.js'. I'm not 100% certain of the cause but I think it's
// something to do with Electron's common/reset-search-paths.js. For now this
// is a temporary workaround that will enable us to utilize the latest version of
// electron.
let screen;
app.on( 'ready', () => {
	if ( process.platform === 'win32' ) {
		// required to display application notification settings in Windows
		app.setAppUserModelId( 'com.automattic.wordpress' );
	}
	screen = electron.screen;
} );

const Config = require( '../../lib/config' );

/**
 * Module variables
 */
const windows = {
	about: {
		file: getPath( 'about.html' ),
		config: 'aboutWindow',
		handle: null,
		preload: getPath( 'preload_about.js' ),
	},
	preferences: {
		file: getPath( 'preferences.html' ),
		config: 'preferencesWindow',
		handle: null,
		preload: getPath( 'preload_preferences.js' ),
	},
	secret: {
		file: getPath( 'secret.html' ),
		config: 'secretWindow',
		handle: null,
		preload: null,
	},
	wapuu: {
		file: getPath( 'wapuu.html' ),
		config: 'secretWindow',
		full: true,
		handle: null,
		preload: null,
	},
};

function setDimensions( config ) {
	const full = screen.getPrimaryDisplay();

	if ( config.width === 'full' ) {
		config.width = full.bounds.width;
	}

	if ( config.height === 'full' ) {
		config.height = full.bounds.height;
	}

	return config;
}

async function openWindow( windowName ) {
	if ( windows[ windowName ] ) {
		const settings = windows[ windowName ];

		if ( settings.handle === null ) {
			let config = Config[ settings.config ];
			config = setDimensions( config );

			const webPreferences = Object.assign( {}, config.webPreferences, {
				preload: windows[ windowName ].preload,
				contextIsolation: true,
				enableRemoteModule: false,
				nodeIntegration: false,
			} );
			config.webPreferences = webPreferences;

			windows[ windowName ].handle = new BrowserWindow( config );
			windows[ windowName ].handle.setMenuBarVisibility( false );
			await windows[ windowName ].handle.webContents.session.setProxy( {
				proxyRules: 'direct://',
			} );
			windows[ windowName ].handle.loadURL( `file://${ settings.file }` );

			windows[ windowName ].handle.on( 'closed', function () {
				windows[ windowName ].handle = null;
			} );

			// TODO: add a check to disable navigation events only for drag & drop
			// https://github.com/Automattic/wp-desktop/pull/464#discussion_r198071749
			if ( config.wpDragAndDropDisabled ) {
				windows[ windowName ].handle.webContents.on( 'will-navigate', function ( event ) {
					event.preventDefault();
					return false;
				} );
			}
		} else {
			settings.handle.show();
		}
	}
}

module.exports = {
	openPreferences: function () {
		openWindow( 'preferences' );
	},
	openAbout: function () {
		openWindow( 'about' );
	},
	openSecret: function () {
		openWindow( 'secret' );
	},
	openWapuu: function () {
		openWindow( 'wapuu' );
	},
};