forked from mamezou-tech/electron-example-browserview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
104 lines (90 loc) · 2.32 KB
/
main.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
const { app, BrowserView, BrowserWindow, Menu, ipcMain } = require('electron');
const path = require('path');
let mainWindow;
function createWindow () {
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
});
setupView('https://electronjs.org');
setupViewLocal('local.html');
mainWindow.loadFile('tabbar.html');
['resize'].forEach(e => {
mainWindow.on(e, () => {
mainWindow.getBrowserViews().forEach((view) => {
resizeView(view);
})
});
});
createMenu();
}
function setupView(url) {
const view = new BrowserView();
mainWindow.addBrowserView(view);
resizeView(view);
view.webContents.loadURL(url);
}
function setupViewLocal(file) {
const view = new BrowserView({
webPreferences: {
preload: path.join(__dirname, 'local_preload.js')
}
});
mainWindow.addBrowserView(view);
resizeView(view);
view.webContents.loadFile(file);
view.setBackgroundColor('white');
// view.webContents.openDevTools({ mode: 'detach' });
}
function resizeView(view) {
const bound = mainWindow.getBounds();
view.setBounds({ x: 0, y: 30, width: bound.width, height: bound.height - 30 });
}
app.whenReady().then(() => {
createWindow();
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
});
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit();
});
function createMenu() {
const template = [
{
label: 'View',
submenu: [
{
label: 'open dev tool',
click() {
mainWindow.webContents.openDevTools({ mode: 'detach' });
}
},
{ role: 'quit' }
]
}
];
if (!app.isPackaged) {
template.unshift({
label: 'Debug',
submenu: [
{ role: 'forceReload' }
]
});
}
const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
}
ipcMain.handle('tab1', e => {
mainWindow.setTopBrowserView(mainWindow.getBrowserViews()[0]);
});
ipcMain.handle('tab2', e => {
mainWindow.setTopBrowserView(mainWindow.getBrowserViews()[1]);
});
ipcMain.handle('switch-to-electronjs', (e, message) => {
console.log('from local.js', message);
mainWindow.setTopBrowserView(mainWindow.getBrowserViews()[0]);
});