-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviewport.js
71 lines (60 loc) · 2.12 KB
/
viewport.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
const electron = require('electron');
var { width, height } = electron.screen.getPrimaryDisplay().workAreaSize;
var { BrowserWindow } = electron;
var viewport = {};
module.exports = function(targetWidth, targetHeight, emulate = false, opts = {}) {
//if emulate is an object, then options was passed in its place
if(typeof emulate === "object") {
opts = emulate;
emulate = false;
console.log("options passed");
}
viewport.opts = opts;
viewport.emulate = emulate;
viewport.targetWidth = targetWidth;
viewport.targetHeight = targetHeight;
//if h and w are set, then we use their values to determine viewport size
if(targetHeight && targetWidth) {
//only allow emulation when height or width are larger than the display workarea
if(emulate && targetHeight <= height && targetWidth <= width) {
emulate = false;
}
// if one aspect of the specified resolution is larger than the display,
// we scale the screen based on the larger component of its size
if(targetWidth > width || targetHeight > height) {
if(targetWidth > width && targetHeight > height) {
if(targetWidth > targetHeight)
height = Math.ceil(targetHeight / targetWidth * width);
else
width = Math.ceil(targetWidth / targetHeight * height);
} else if(targetWidth > width) {
height = Math.ceil(targetHeight / targetWidth * width);
} else {
width = Math.ceil(targetWidth / targetHeight * height);
}
} else {
width = targetWidth;
height = targetHeight;
}
}
viewport.width = opts.width = width;
viewport.height = opts.height = height;
return viewport;
}
viewport.getWindow = function() {
viewport.window = new BrowserWindow(viewport.opts);
if(viewport.emulate) {
viewport.window.on('show', function() {
viewport.window.webContents.enableDeviceEmulation({
screenPosition: 'mobile',
screenSize: { width: viewport.targetWidth, height: viewport.targetHeight },
deviceScaleFactor: 0,
viewPosition: { x: 0, y: 0 },
viewSize: { width: viewport.targetWidth, height: viewport.targetHeight },
fitToView: true,
offset: { x: 0, y: 0 }
});
});
}
return viewport.window;
}