349 lines
7.6 KiB
JavaScript
349 lines
7.6 KiB
JavaScript
var startTime = Date.now()
|
|
|
|
require('debug/browser')
|
|
|
|
var debug = require('debug')('index')
|
|
var electron = require('electron')
|
|
var path = require('path')
|
|
|
|
var app = electron.app
|
|
var mainWindow
|
|
|
|
// report crashes
|
|
// require('crash-reporter').start({
|
|
// productName: 'WebTorrent',
|
|
// companyName: 'WebTorrent',
|
|
// submitURL: 'https://webtorrent.io/crash-report',
|
|
// autoSubmit: true
|
|
// })
|
|
|
|
app.on('open-file', onOpen)
|
|
app.on('open-url', onOpen)
|
|
|
|
app.on('ready', function () {
|
|
mainWindow = createMainWindow()
|
|
|
|
var menu = electron.Menu.buildFromTemplate(template)
|
|
electron.Menu.setApplicationMenu(menu)
|
|
})
|
|
|
|
app.on('activate', function () {
|
|
if (mainWindow) {
|
|
mainWindow.show()
|
|
} else {
|
|
mainWindow = createMainWindow()
|
|
}
|
|
})
|
|
|
|
app.on('window-all-closed', function () {
|
|
if (process.platform !== 'darwin') {
|
|
app.quit()
|
|
}
|
|
})
|
|
|
|
var isQuitting = false
|
|
app.on('before-quit', function () {
|
|
isQuitting = true
|
|
})
|
|
|
|
electron.ipcMain.on('addTorrentFromPaste', function (e) {
|
|
addTorrentFromPaste()
|
|
})
|
|
|
|
electron.ipcMain.on('setBounds', function (e, bounds) {
|
|
setBounds(bounds)
|
|
})
|
|
|
|
electron.ipcMain.on('setAspectRatio', function (e, aspectRatio, extraSize) {
|
|
setAspectRatio(aspectRatio, extraSize)
|
|
})
|
|
|
|
|
|
electron.ipcMain.on('setProgress', function (e, progress) {
|
|
setProgress(progress)
|
|
})
|
|
|
|
function createMainWindow () {
|
|
var win = new electron.BrowserWindow({
|
|
backgroundColor: '#282828',
|
|
darkTheme: true,
|
|
minWidth: 375,
|
|
minHeight: 158,
|
|
show: false,
|
|
title: 'WebTorrent',
|
|
titleBarStyle: 'hidden-inset',
|
|
width: 450,
|
|
height: 300
|
|
})
|
|
win.loadURL('file://' + path.join(__dirname, 'main', 'index.html'))
|
|
win.webContents.on('did-finish-load', function () {
|
|
setTimeout(function () {
|
|
debug('startup time: %sms', Date.now() - startTime)
|
|
win.show()
|
|
}, 50)
|
|
})
|
|
win.on('close', function (e) {
|
|
if (process.platform === 'darwin' && !isQuitting) {
|
|
e.preventDefault()
|
|
win.hide()
|
|
}
|
|
})
|
|
win.once('closed', function () {
|
|
mainWindow = null
|
|
})
|
|
return win
|
|
}
|
|
|
|
function onOpen (e, torrentId) {
|
|
e.preventDefault()
|
|
mainWindow.send('addTorrent', torrentId)
|
|
}
|
|
|
|
function addTorrentFromPaste () {
|
|
debug('addTorrentFromPaste')
|
|
var torrentIds = electron.clipboard.readText().split('\n')
|
|
torrentIds.forEach(function (torrentId) {
|
|
torrentId = torrentId.trim()
|
|
if (torrentId.length === 0) return
|
|
mainWindow.send('addTorrent', torrentId)
|
|
})
|
|
}
|
|
|
|
function setBounds (bounds) {
|
|
debug('setBounds %o', bounds)
|
|
if (mainWindow) {
|
|
mainWindow.setBounds(bounds, true)
|
|
}
|
|
}
|
|
|
|
function setAspectRatio (aspectRatio, extraSize) {
|
|
debug('setAspectRatio %o %o', aspectRatio, extraSize)
|
|
if (mainWindow) {
|
|
mainWindow.setAspectRatio(aspectRatio, extraSize)
|
|
}
|
|
}
|
|
|
|
// Show progress bar. Valid range is [0, 1]. Remove when < 0; indeterminate when > 1.
|
|
function setProgress (progress) {
|
|
if (mainWindow) {
|
|
mainWindow.setProgressBar(progress)
|
|
}
|
|
}
|
|
|
|
function toggleDevTools (win) {
|
|
debug('toggleDevTools')
|
|
win = win || electron.BrowserWindow.getFocusedWindow()
|
|
|
|
if (win) {
|
|
win.toggleDevTools()
|
|
}
|
|
}
|
|
|
|
function reloadWindow (win) {
|
|
debug('reloadWindow')
|
|
win = win || electron.BrowserWindow.getFocusedWindow()
|
|
|
|
if (win) {
|
|
startTime = Date.now()
|
|
win.webContents.reloadIgnoringCache()
|
|
}
|
|
}
|
|
|
|
var template = [
|
|
{
|
|
label: 'File',
|
|
submenu: [
|
|
{
|
|
label: 'Create New Torrent...',
|
|
accelerator: 'CmdOrCtrl+N',
|
|
click: function () {
|
|
electron.dialog.showOpenDialog({
|
|
title: 'Select a file or folder for the torrent file.',
|
|
properties: [ 'openFile', 'openDirectory', 'multiSelections' ]
|
|
}, function (filenames) {
|
|
if (!Array.isArray(filenames)) return
|
|
mainWindow.send('seed', filenames)
|
|
})
|
|
}
|
|
},
|
|
{
|
|
label: 'Open Torrent File...',
|
|
accelerator: 'CmdOrCtrl+O',
|
|
click: function () {
|
|
electron.dialog.showOpenDialog(mainWindow, {
|
|
title: 'Select a .torrent file to open.',
|
|
properties: [ 'openFile', 'multiSelections' ]
|
|
}, function (filenames) {
|
|
if (!Array.isArray(filenames)) return
|
|
filenames.forEach(function (filename) {
|
|
mainWindow.send('addTorrent', filename)
|
|
})
|
|
})
|
|
}
|
|
},
|
|
{
|
|
label: 'Open Torrent Address...',
|
|
accelerator: 'CmdOrCtrl+U',
|
|
click: function () { electron.dialog.showMessageBox({ message: 'TODO', buttons: ['OK'] }) }
|
|
},
|
|
{
|
|
type: 'separator'
|
|
},
|
|
{
|
|
label: 'Close Window',
|
|
accelerator: 'CmdOrCtrl+W',
|
|
role: 'close'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
label: 'Edit',
|
|
submenu: [
|
|
{
|
|
label: 'Cut',
|
|
accelerator: 'CmdOrCtrl+X',
|
|
role: 'cut'
|
|
},
|
|
{
|
|
label: 'Copy',
|
|
accelerator: 'CmdOrCtrl+C',
|
|
role: 'copy'
|
|
},
|
|
{
|
|
label: 'Paste Torrent Address',
|
|
accelerator: 'CmdOrCtrl+V',
|
|
role: 'paste'
|
|
},
|
|
{
|
|
label: 'Select All',
|
|
accelerator: 'CmdOrCtrl+A',
|
|
role: 'selectall'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
label: 'View',
|
|
submenu: [
|
|
{
|
|
label: 'Toggle Full Screen',
|
|
accelerator: (function () {
|
|
if (process.platform === 'darwin') return 'Ctrl+Command+F'
|
|
else return 'F11'
|
|
})(),
|
|
click: function (item, focusedWindow) {
|
|
if (focusedWindow) focusedWindow.setFullScreen(!focusedWindow.isFullScreen())
|
|
}
|
|
},
|
|
{
|
|
type: 'separator'
|
|
},
|
|
{
|
|
label: 'Reload',
|
|
accelerator: 'CmdOrCtrl+R',
|
|
click: function (item, focusedWindow) {
|
|
reloadWindow(focusedWindow)
|
|
}
|
|
},
|
|
{
|
|
label: 'Toggle Developer Tools',
|
|
accelerator: (function () {
|
|
if (process.platform === 'darwin') return 'Alt+Command+I'
|
|
else return 'Ctrl+Shift+I'
|
|
})(),
|
|
click: function (item, focusedWindow) {
|
|
toggleDevTools(focusedWindow)
|
|
}
|
|
}
|
|
]
|
|
},
|
|
{
|
|
label: 'Window',
|
|
role: 'window',
|
|
submenu: [
|
|
{
|
|
label: 'Minimize',
|
|
accelerator: 'CmdOrCtrl+M',
|
|
role: 'minimize'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
label: 'Help',
|
|
role: 'help',
|
|
submenu: [
|
|
{
|
|
label: 'Learn more about WebTorrent',
|
|
click: function () { electron.shell.openExternal('https://webtorrent.io') }
|
|
},
|
|
{
|
|
label: 'Contribute on GitHub',
|
|
click: function () { electron.shell.openExternal('https://github.com/feross/webtorrent-app') }
|
|
},
|
|
{
|
|
type: 'separator'
|
|
},
|
|
{
|
|
label: 'Report an Issue...',
|
|
click: function () { electron.shell.openExternal('https://github.com/feross/webtorrent-app/issues') }
|
|
}
|
|
]
|
|
}
|
|
]
|
|
|
|
if (process.platform === 'darwin') {
|
|
var name = app.getName()
|
|
template.unshift({
|
|
label: name,
|
|
submenu: [
|
|
{
|
|
label: 'About ' + name,
|
|
role: 'about'
|
|
},
|
|
{
|
|
type: 'separator'
|
|
},
|
|
{
|
|
label: 'Services',
|
|
role: 'services',
|
|
submenu: []
|
|
},
|
|
{
|
|
type: 'separator'
|
|
},
|
|
{
|
|
label: 'Hide ' + name,
|
|
accelerator: 'Command+H',
|
|
role: 'hide'
|
|
},
|
|
{
|
|
label: 'Hide Others',
|
|
accelerator: 'Command+Alt+H',
|
|
role: 'hideothers'
|
|
},
|
|
{
|
|
label: 'Show All',
|
|
role: 'unhide'
|
|
},
|
|
{
|
|
type: 'separator'
|
|
},
|
|
{
|
|
label: 'Quit',
|
|
accelerator: 'Command+Q',
|
|
click: function () { app.quit() }
|
|
}
|
|
]
|
|
})
|
|
|
|
// Window menu
|
|
template[4].submenu.push(
|
|
{
|
|
type: 'separator'
|
|
},
|
|
{
|
|
label: 'Bring All to Front',
|
|
role: 'front'
|
|
}
|
|
)
|
|
}
|