forked from ionic-team/capacitor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
169 lines (147 loc) · 5.78 KB
/
index.js
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
const fs = require('fs');
const path = require('path');
const url = require('url');
const { app, ipcMain, BrowserWindow } = require('electron');
function getURLFileContents(path) {
console.trace();
return new Promise((resolve, reject) => {
fs.readFile(path, (err, data) => {
if (err)
reject(err);
resolve(data.toString());
});
});
}
const configCapacitor = async function(mainWindow) {
let capConfigJson = JSON.parse(fs.readFileSync(`./capacitor.config.json`, 'utf-8'));
const appendUserAgent = capConfigJson.electron && capConfigJson.electron.appendUserAgent ? capConfigJson.electron.appendUserAgent : capConfigJson.appendUserAgent;
if (appendUserAgent) {
mainWindow.webContents.setUserAgent(mainWindow.webContents.getUserAgent() + " " + appendUserAgent);
}
const overrideUserAgent = capConfigJson.electron && capConfigJson.electron.overrideUserAgent ? capConfigJson.electron.overrideUserAgent : capConfigJson.overrideUserAgent;
if (overrideUserAgent) {
mainWindow.webContents.setUserAgent(overrideUserAgent);
}
}
class CapacitorSplashScreen {
/**
* @param {BrowserWindow} mainWindow
* @param {Object} splashOptions Options for customizing the splash screen.
* @param {string} splashOptions.imageFileName Name of file placed in splash_assets folder
* @param {number} splashOptions.windowWidth Width of the splash screen
* @param {number} splashOptions.windowHeight Height of the splash screen
* @param {string} splashOptions.textColor Loading text color
* @param {string} splashOptions.loadingText Loading text
* @param {number} splashOptions.textPercentageFromTop Relative distance of the loading text from top of the window
* @param {boolean} splashOptions.transparentWindow If the window should of transparent
* @param {boolean} splashOptions.autoHideLaunchSplash If auto hide the splash screen
* @param {string} splashOptions.customHtml Custom html string, if used all most of customization options will be ignored
*/
constructor(mainWindow, splashOptions) {
this.mainWindowRef = null;
this.splashWindow = null;
if (!splashOptions) {
splashOptions = {};
}
this.splashOptions = {
imageFileName: splashOptions.imageFileName || 'splash.png',
windowWidth: splashOptions.windowWidth || 400,
windowHeight: splashOptions.windowHeight || 400,
textColor: splashOptions.textColor || '#43A8FF',
loadingText: splashOptions.loadingText || 'Loading...',
textPercentageFromTop: splashOptions.textPercentageFromTop || 75,
transparentWindow: splashOptions.transparentWindow || false,
autoHideLaunchSplash: splashOptions.autoHideLaunchSplash || true,
customHtml: splashOptions.customHtml || false
};
this.mainWindowRef = mainWindow;
try {
let capConfigJson = JSON.parse(fs.readFileSync(`./capacitor.config.json`, 'utf-8'));
this.splashOptions = Object.assign(
this.splashOptions,
capConfigJson.plugins.SplashScreen || {}
);
} catch (e) {
console.error(e.message);
}
ipcMain.on('showCapacitorSplashScreen', (event, options) => {
this.show();
if (options) {
if (options.autoHide) {
let showTime = options.showDuration || 3000;
setTimeout(() => {
this.hide();
}, showTime);
}
}
});
ipcMain.on('hideCapacitorSplashScreen', (event, options) => {
this.hide();
});
}
init() {
let rootPath = app.getAppPath();
this.splashWindow = new BrowserWindow({
width: this.splashOptions.windowWidth,
height: this.splashOptions.windowHeight,
frame: false,
show: false,
transparent: this.splashOptions.transparentWindow,
webPreferences: {
// Required to load file:// splash screen
webSecurity: false
}
});
let imagePath = path.join(rootPath, 'splash_assets', this.splashOptions.imageFileName);
let imageUrl = '';
let useFallback = false;
try {
imageUrl = url.pathToFileURL(imagePath).href;
} catch (err) {
useFallback = true;
imageUrl = `./${this.splashOptions.imageFileName}`;
}
let splashHtml = this.splashOptions.customHtml || `
<html style="width: 100%; height: 100%; margin: 0; overflow: hidden;">
<body style="background-image: url('${imageUrl}'); background-position: center center; background-repeat: no-repeat; width: 100%; height: 100%; margin: 0; overflow: hidden;"> <div style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; color: ${this.splashOptions.textColor}; position: absolute; top: ${this.splashOptions.textPercentageFromTop}%; text-align: center; font-size: 10vw; width: 100vw;">
${this.splashOptions.loadingText}
</div>
</body>
</html>
`;
this.mainWindowRef.on('closed', () => {
if (this.splashWindow && !this.splashWindow.isDestroyed()) {
this.splashWindow.close();
}
});
if (useFallback) {
this.splashWindow.loadURL(`data:text/html;charset=UTF-8,${splashHtml}`, {baseURLForDataURL: `file://${rootPath}/splash_assets/`});
} else {
this.splashWindow.loadURL(`data:text/html;charset=UTF-8,${splashHtml}`);
}
this.splashWindow.webContents.on('dom-ready', async () => {
this.splashWindow.show();
setTimeout(async () => {
this.mainWindowRef.loadURL(`file://${rootPath}/app/index.html`);
}, 4500);
});
if (this.splashOptions.autoHideLaunchSplash) {
this.mainWindowRef.webContents.on('dom-ready', () => {
this.mainWindowRef.show();
this.splashWindow.hide();
});
}
}
show() {
this.splashWindow.show();
this.mainWindowRef.hide();
}
hide() {
this.mainWindowRef.show();
this.splashWindow.hide();
}
}
module.exports = {
configCapacitor,
CapacitorSplashScreen
};