Add "open new window" button (#1153)

This commit is contained in:
PikachuEXE 2021-04-16 02:28:35 +08:00 committed by GitHub
parent b5d58255a6
commit ebc829cef0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 110 additions and 73 deletions

View File

@ -31,6 +31,7 @@ function runApp() {
const isDev = process.env.NODE_ENV === 'development' const isDev = process.env.NODE_ENV === 'development'
const isDebug = process.argv.includes('--debug') const isDebug = process.argv.includes('--debug')
let mainWindow let mainWindow
let openedWindows = []
let startupUrl let startupUrl
// CORS somehow gets re-enabled in Electron v9.0.4 // CORS somehow gets re-enabled in Electron v9.0.4
@ -219,11 +220,11 @@ function runApp() {
} }
} }
function createWindow (useProxy = false, proxyUrl = '') { function createWindow (useProxy = false, proxyUrl = '', replaceMainWindow = true) {
/** /**
* Initial window options * Initial window options
*/ */
mainWindow = new BrowserWindow({ const newWindow = new BrowserWindow({
backgroundColor: '#fff', backgroundColor: '#fff',
icon: isDev icon: isDev
? path.join(__dirname, '../../_icons/iconColor.png') ? path.join(__dirname, '../../_icons/iconColor.png')
@ -241,14 +242,18 @@ function runApp() {
}, },
show: false show: false
}) })
openedWindows.push(newWindow)
if (replaceMainWindow) {
mainWindow = newWindow
}
mainWindow.setBounds({ newWindow.setBounds({
width: 1200, width: 1200,
height: 800 height: 800
}) })
if (useProxy) { if (useProxy) {
mainWindow.webContents.session.setProxy({ newWindow.webContents.session.setProxy({
proxyRules: proxyUrl proxyRules: proxyUrl
}) })
} }
@ -260,7 +265,7 @@ function runApp() {
'http://youtube.com', 'http://youtube.com',
'https://youtube.com' 'https://youtube.com'
].forEach(url => { ].forEach(url => {
mainWindow.webContents.session.cookies.set({ newWindow.webContents.session.cookies.set({
url: url, url: url,
name: 'CONSENT', name: 'CONSENT',
value: 'YES+' value: 'YES+'
@ -284,7 +289,7 @@ function runApp() {
.reduce((accumulator, { size: { width } }) => accumulator + width, 0) .reduce((accumulator, { size: { width } }) => accumulator + width, 0)
if (allDisplaysSummaryWidth >= bounds.x) { if (allDisplaysSummaryWidth >= bounds.x) {
mainWindow.setBounds({ newWindow.setBounds({
x: bounds.x, x: bounds.x,
y: bounds.y, y: bounds.y,
width: bounds.width, width: bounds.width,
@ -292,19 +297,23 @@ function runApp() {
}) })
} }
if (maximized) { if (maximized) {
mainWindow.maximize() newWindow.maximize()
} }
}) })
// eslint-disable-next-line // If called multiple times
setMenu() // Duplicate menu items will be added
if (replaceMainWindow) {
// eslint-disable-next-line
setMenu()
}
// load root file/url // load root file/url
if (isDev) { if (isDev) {
mainWindow.loadURL('http://localhost:9080') newWindow.loadURL('http://localhost:9080')
} else { } else {
/* eslint-disable-next-line */ /* eslint-disable-next-line */
mainWindow.loadFile(`${__dirname}/index.html`) newWindow.loadFile(`${__dirname}/index.html`)
global.__static = path global.__static = path
.join(__dirname, '/static') .join(__dirname, '/static')
@ -312,74 +321,87 @@ function runApp() {
} }
// Show when loaded // Show when loaded
mainWindow.on('ready-to-show', () => { newWindow.on('ready-to-show', () => {
mainWindow.show() newWindow.show()
mainWindow.focus() newWindow.focus()
}) })
mainWindow.on('closed', () => { newWindow.on('closed', () => {
// Remove closed window
openedWindows = openedWindows.filter((window) => window !== newWindow)
if (newWindow === mainWindow) {
// Replace mainWindow to avoid accessing `mainWindow.webContents`
// Which raises "Object has been destroyed" error
mainWindow = openedWindows[0]
}
console.log('closed') console.log('closed')
}) })
ipcMain.on('setBounds', (_e, data) => {
const value = {
...mainWindow.getNormalBounds(),
maximized: mainWindow.isMaximized()
}
settingsDb.findOne({
_id: 'bounds'
}, function (err, doc) {
if (err) {
return
}
if (doc !== null) {
settingsDb.update({
_id: 'bounds'
}, {
$set: {
value
}
}, {})
} else {
settingsDb.insert({
_id: 'bounds',
value
})
}
})
})
ipcMain.on('appReady', () => {
if (startupUrl) {
mainWindow.webContents.send('openUrl', startupUrl)
}
})
ipcMain.on('disableSmoothScrolling', () => {
app.commandLine.appendSwitch('disable-smooth-scrolling')
mainWindow.close()
createWindow()
})
ipcMain.on('enableSmoothScrolling', () => {
app.commandLine.appendSwitch('enable-smooth-scrolling')
mainWindow.close()
createWindow()
})
ipcMain.on('enableProxy', (event, url) => {
console.log(url)
mainWindow.webContents.session.setProxy({
proxyRules: url
})
})
ipcMain.on('disableProxy', () => {
mainWindow.webContents.session.setProxy({})
})
} }
// Save closing window bounds & maximized status
ipcMain.on('setBounds', (_e, data) => {
const value = {
...mainWindow.getNormalBounds(),
maximized: mainWindow.isMaximized()
}
settingsDb.findOne({
_id: 'bounds'
}, function (err, doc) {
if (err) {
return
}
if (doc !== null) {
settingsDb.update({
_id: 'bounds'
}, {
$set: {
value
}
}, {})
} else {
settingsDb.insert({
_id: 'bounds',
value
})
}
})
})
ipcMain.on('appReady', () => {
if (startupUrl) {
mainWindow.webContents.send('openUrl', startupUrl)
}
})
ipcMain.on('disableSmoothScrolling', () => {
app.commandLine.appendSwitch('disable-smooth-scrolling')
mainWindow.close()
createWindow()
})
ipcMain.on('enableSmoothScrolling', () => {
app.commandLine.appendSwitch('enable-smooth-scrolling')
mainWindow.close()
createWindow()
})
ipcMain.on('enableProxy', (event, url) => {
console.log(url)
mainWindow.webContents.session.setProxy({
proxyRules: url
})
})
ipcMain.on('disableProxy', () => {
mainWindow.webContents.session.setProxy({})
})
ipcMain.on('createNewWindow', () => {
createWindow(false, '', false)
})
app.on('window-all-closed', () => { app.on('window-all-closed', () => {
if (process.platform !== 'darwin') { if (process.platform !== 'darwin') {
app.quit() app.quit()

View File

@ -6,6 +6,7 @@ import $ from 'jquery'
import router from '../../router/index.js' import router from '../../router/index.js'
import debounce from 'lodash.debounce' import debounce from 'lodash.debounce'
import ytSuggest from 'youtube-suggest' import ytSuggest from 'youtube-suggest'
const { ipcRenderer } = require('electron')
export default Vue.extend({ export default Vue.extend({
name: 'TopNav', name: 'TopNav',
@ -195,6 +196,10 @@ export default Vue.extend({
toggleSideNav: function () { toggleSideNav: function () {
this.$store.commit('toggleSideNav') this.$store.commit('toggleSideNav')
},
createNewWindow: function () {
ipcRenderer.send('createNewWindow')
} }
} }
}) })

View File

@ -66,6 +66,11 @@
@media only screen and (min-width: 681px) @media only screen and (min-width: 681px)
display: none display: none
.navNewWindowIcon
// Hidden when "History" menu item also hidden
@media only screen and (max-width: 279px)
display: none
.logo // parts that make up the logo .logo // parts that make up the logo
display: flex display: flex
align-items: center align-items: center

View File

@ -24,6 +24,11 @@
icon="search" icon="search"
@click="toggleSearchContainer" @click="toggleSearchContainer"
/> />
<font-awesome-icon
class="navNewWindowIcon navIcon"
icon="clone"
@click="createNewWindow"
/>
<div class="logo"> <div class="logo">
<div <div
class="logoIcon" class="logoIcon"