forked from sindresorhus/electron-util
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
162 lines (128 loc) · 3.92 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
'use strict';
const path = require('path');
const url = require('url');
const electron = require('electron');
const isDev = require('electron-is-dev');
const node = require('./node');
const api = new Proxy(electron, {
get: (target, prop) => target[prop] || target.remote[prop]
});
exports.api = api;
const is = {
macos: process.platform === 'darwin',
linux: process.platform === 'linux',
windows: process.platform === 'win32',
main: process.type === 'browser',
renderer: process.type === 'renderer',
usingAsar: node.isUsingAsar,
development: isDev,
macAppStore: process.mas === true,
windowsStore: process.windowsStore === true
};
exports.is = is;
exports.appReady = new Promise(resolve => {
if (api.app.isReady()) {
resolve();
} else {
api.app.on('ready', resolve);
}
});
exports.electronVersion = node.electronVersion;
exports.chromeVersion = process.versions.chrome.replace(/\.\d+$/, '');
exports.platform = obj => {
let {platform} = process;
if (platform === 'darwin') {
platform = 'macos';
} else if (platform === 'win32') {
platform = 'windows';
}
const fn = platform in obj ? obj[platform] : obj.default;
return typeof fn === 'function' ? fn() : fn;
};
const activeWindow = () => is.main ?
electron.BrowserWindow.getFocusedWindow() :
electron.remote.getCurrentWindow();
exports.activeWindow = activeWindow;
exports.loadFile = (win, filePath) => win.loadURL(url.format({
protocol: 'file',
slashes: true,
pathname: path.resolve(electron.app.getAppPath(), filePath)
}));
exports.runJS = (code, win = activeWindow()) => win.webContents.executeJavaScript(code);
exports.fixPathForAsarUnpack = node.fixPathForAsarUnpack;
exports.enforceMacOSAppLocation = () => {
if (is.development || !is.macos) {
return;
}
if (api.app.isInApplicationsFolder()) {
return;
}
const clickedButtonIndex = api.dialog.showMessageBox({
type: 'error',
message: 'Move to Applications folder?',
detail: `${api.app.getName()} must live in the Applications folder to be able to run correctly.`,
buttons: ['Move to Applications folder', `Quit ${api.app.getName()}`],
defaultId: 0,
cancelId: 1
});
if (clickedButtonIndex === 1) {
api.app.quit();
return;
}
api.app.moveToApplicationsFolder();
};
exports.menuBarHeight = () => is.macos ? api.screen.getPrimaryDisplay().workArea.y : 0;
exports.getWindowBoundsCentered = options => {
options = Object.assign({
window: activeWindow()
}, options);
const [width, height] = options.window.getSize();
const windowSize = options.size || {width, height};
const screenSize = api.screen.getDisplayNearestPoint(api.screen.getCursorScreenPoint()).workArea;
const x = Math.floor(screenSize.x + ((screenSize.width / 2) - (windowSize.width / 2)));
const y = Math.floor(((screenSize.height + screenSize.y) / 2) - (windowSize.height / 2));
return {
x,
y,
width: windowSize.width,
height: windowSize.height
};
};
exports.setWindowBounds = (bounds, options) => {
options = Object.assign({
window: activeWindow(),
animated: false
}, options);
bounds = Object.assign(options.window.getBounds(), bounds);
options.window.setBounds(bounds, options.animated);
};
exports.centerWindow = options => {
options = Object.assign({
window: activeWindow(),
animated: false
}, options);
const bounds = exports.getWindowBoundsCentered(options);
exports.setWindowBounds(bounds, options);
};
exports.disableZoom = (win = activeWindow()) => {
const {webContents} = win;
const run = () => {
webContents.setZoomFactor(1);
webContents.setVisualZoomLevelLimits(1, 1);
webContents.setLayoutZoomLevelLimits(0, 0);
};
webContents.on('did-finish-load', run);
run();
};
exports.appLaunchTimestamp = Date.now();
if (is.main) {
exports.isFirstAppLaunch = () => {
const fs = require('fs');
const checkFile = path.join(api.app.getPath('userData'), '.electron-util--has-app-launched');
if (fs.existsSync(checkFile)) {
return false;
}
fs.writeFileSync(checkFile, '');
return true;
};
}