Skip to content

Commit

Permalink
Major refactor -- split windows into separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
feross committed May 27, 2016
1 parent 3757507 commit 001601b
Show file tree
Hide file tree
Showing 15 changed files with 464 additions and 358 deletions.
2 changes: 1 addition & 1 deletion main/announcement.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module.exports = {
}

var electron = require('electron')
var get = require('simple-get')

var config = require('../config')
var log = require('./log')
Expand All @@ -13,6 +12,7 @@ var ANNOUNCEMENT_URL = config.ANNOUNCEMENT_URL +
'&platform=' + process.platform

function init () {
var get = require('simple-get')
get.concat(ANNOUNCEMENT_URL, onResponse)
}

Expand Down
53 changes: 53 additions & 0 deletions main/dialog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
module.exports = {
openSeedFile,
openSeedDirectory,
openTorrentFile,
openTorrentAddress
}

var electron = require('electron')
var windows = require('./windows')

// Prompts the user for a file, then creates a torrent. Only allows a single file
// selection.
function openSeedFile () {
electron.dialog.showOpenDialog({
title: 'Select a file for the torrent file.',
properties: [ 'openFile' ]
}, function (selectedPaths) {
if (!Array.isArray(selectedPaths)) return
windows.main.send('dispatch', 'showCreateTorrent', selectedPaths)
})
}

// Prompts the user for a file or directory, then creates a torrent. Only allows a
// single selection. To create a multi-file torrent, the user must select a
// directory.
function openSeedDirectory () {
electron.dialog.showOpenDialog({
title: 'Select a file or folder for the torrent file.',
properties: [ 'openFile', 'openDirectory' ]
}, function (selectedPaths) {
if (!Array.isArray(selectedPaths)) return
windows.main.send('dispatch', 'showCreateTorrent', selectedPaths)
})
}

// Prompts the user to choose a torrent file, then adds it.
function openTorrentFile () {
electron.dialog.showOpenDialog(windows.main.win, {
title: 'Select a .torrent file to open.',
filters: [{ name: 'Torrent Files', extensions: ['torrent'] }],
properties: [ 'openFile', 'multiSelections' ]
}, function (selectedPaths) {
if (!Array.isArray(selectedPaths)) return
selectedPaths.forEach(function (selectedPath) {
windows.main.send('dispatch', 'addTorrent', selectedPath)
})
})
}

// Prompts the user for the URL of a torrent file, then downloads and adds it
function openTorrentAddress () {
windows.main.send('showOpenTorrentAddress')
}
43 changes: 20 additions & 23 deletions main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var ipcMain = electron.ipcMain
var announcement = require('./announcement')
var config = require('../config')
var crashReporter = require('../crash-reporter')
var dialog = require('./dialog')
var handlers = require('./handlers')
var ipc = require('./ipc')
var log = require('./log')
Expand Down Expand Up @@ -60,8 +61,8 @@ function init () {
app.on('ready', function () {
isReady = true

windows.createMainWindow()
windows.createWebTorrentHiddenWindow()
windows.main.create()
windows.webtorrent.create()
menu.init()

// To keep app startup fast, some code is delayed.
Expand All @@ -79,13 +80,13 @@ function init () {

app.isQuitting = true
e.preventDefault()
windows.main.send('dispatch', 'saveState') /* try to save state on exit */
windows.main.send('dispatch', 'saveState') // try to save state on exit
ipcMain.once('savedState', () => app.quit())
setTimeout(() => app.quit(), 2000) /* quit after 2 secs, at most */
setTimeout(() => app.quit(), 2000) // quit after 2 secs, at most
})

app.on('activate', function () {
if (isReady) windows.createMainWindow()
if (isReady) windows.main.create()
})
}

Expand All @@ -101,11 +102,11 @@ function onOpen (e, torrentId) {

if (app.ipcReady) {
windows.main.send('dispatch', 'onOpen', torrentId)
// Magnet links opened from Chrome won't focus the app without a setTimeout. The
// confirmation dialog Chrome shows causes Chrome to steal back the focus.
// Magnet links opened from Chrome won't focus the app without a setTimeout.
// The confirmation dialog Chrome shows causes Chrome to steal back the focus.
// Electron issue: https://github.com/atom/electron/issues/4338
setTimeout(function () {
windows.focusWindow(windows.main)
windows.main.focus()
}, 100)
} else {
argv.push(torrentId)
Expand All @@ -114,10 +115,11 @@ function onOpen (e, torrentId) {

function onAppOpen (newArgv) {
newArgv = sliceArgv(newArgv)
console.log(newArgv)

if (app.ipcReady) {
log('Second app instance opened, but was prevented:', newArgv)
windows.focusWindow(windows.main)
windows.main.focus()

processArgv(newArgv)
} else {
Expand All @@ -130,27 +132,22 @@ function sliceArgv (argv) {
}

function processArgv (argv) {
var pathsToOpen = []
var paths = []
argv.forEach(function (arg) {
if (arg === '-n') {
menu.showOpenSeedFiles()
dialog.openSeedDirectory()
} else if (arg === '-o') {
menu.showOpenTorrentFile()
dialog.openTorrentFile()
} else if (arg === '-u') {
menu.showOpenTorrentAddress()
dialog.openTorrentAddress()
} else if (arg.startsWith('-psn')) {
// Ignore OS X launchd "process serial number" argument
// More: https://github.com/feross/webtorrent-desktop/issues/214
// Issue: https://github.com/feross/webtorrent-desktop/issues/214
} else {
pathsToOpen.push(arg)
paths.push(arg)
}
})
if (pathsToOpen.length > 0) openFilePaths(pathsToOpen)
}

// Send files to the renderer process
// Opening files means either adding torrents, creating and seeding a torrent
// from files, or adding subtitles
function openFilePaths (paths) {
windows.main.send('dispatch', 'onOpen', paths)
if (paths.length > 0) {
windows.main.send('dispatch', 'onOpen', paths)
}
}
60 changes: 22 additions & 38 deletions main/ipc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,23 @@ module.exports = {
var electron = require('electron')

var app = electron.app
var ipcMain = electron.ipcMain

var log = require('./log')
var menu = require('./menu')
var windows = require('./windows')
var powerSaveBlocker = require('./power-save-blocker')
var shortcuts = require('./shortcuts')
var vlc = require('./vlc')
var windows = require('./windows')

// has to be a number, not a boolean, and undefined throws an error
var powerSaveBlockerId = 0

// messages from the main process, to be sent once the WebTorrent process starts
// Messages from the main process, to be sent once the WebTorrent process starts
var messageQueueMainToWebTorrent = []

// holds a ChildProcess while we're playing a video in VLC, null otherwise
var vlcProcess

function init () {
var ipcMain = electron.ipcMain

ipcMain.on('ipcReady', function (e) {
windows.main.show()
app.ipcReady = true
Expand All @@ -39,7 +38,7 @@ function init () {
})
})

ipcMain.on('showOpenTorrentFile', menu.showOpenTorrentFile)
ipcMain.on('showOpenTorrentFile', () => menu.showOpenTorrentFile())

ipcMain.on('setBounds', function (e, bounds, maximize) {
setBounds(bounds, maximize)
Expand All @@ -58,11 +57,11 @@ function init () {
})

ipcMain.on('toggleFullScreen', function (e, flag) {
menu.toggleFullScreen(flag)
windows.main.toggleFullScreen(flag)
})

ipcMain.on('setTitle', function (e, title) {
windows.main.setTitle(title)
windows.main.win.setTitle(title)
})

ipcMain.on('openItem', function (e, path) {
Expand All @@ -75,9 +74,8 @@ function init () {
electron.shell.showItemInFolder(path)
})

ipcMain.on('blockPowerSave', blockPowerSave)

ipcMain.on('unblockPowerSave', unblockPowerSave)
ipcMain.on('blockPowerSave', () => powerSaveBlocker.start())
ipcMain.on('unblockPowerSave', () => powerSaveBlocker.stop())

ipcMain.on('onPlayerOpen', function () {
menu.onPlayerOpen()
Expand Down Expand Up @@ -174,50 +172,49 @@ function init () {

function setBounds (bounds, maximize) {
// Do nothing in fullscreen
if (!windows.main || windows.main.isFullScreen()) {
if (!windows.main.win || windows.main.win.isFullScreen()) {
log('setBounds: not setting bounds because we\'re in full screen')
return
}

// Maximize or minimize, if the second argument is present
var willBeMaximized
if (maximize === true) {
if (!windows.main.isMaximized()) {
if (!windows.main.win.isMaximized()) {
log('setBounds: maximizing')
windows.main.maximize()
windows.main.win.maximize()
}
willBeMaximized = true
} else if (maximize === false) {
if (windows.main.isMaximized()) {
if (windows.main.win.isMaximized()) {
log('setBounds: unmaximizing')
windows.main.unmaximize()
windows.main.win.unmaximize()
}
willBeMaximized = false
} else {
willBeMaximized = windows.main.isMaximized()
willBeMaximized = windows.main.win.isMaximized()
}

// Assuming we're not maximized or maximizing, set the window size
if (!willBeMaximized) {
log('setBounds: setting bounds to ' + JSON.stringify(bounds))
if (bounds.x === null && bounds.y === null) {
// X and Y not specified? By default, center on current screen
var scr = electron.screen.getDisplayMatching(windows.main.getBounds())
var scr = electron.screen.getDisplayMatching(windows.main.win.getBounds())
bounds.x = Math.round(scr.bounds.x + scr.bounds.width / 2 - bounds.width / 2)
bounds.y = Math.round(scr.bounds.y + scr.bounds.height / 2 - bounds.height / 2)
log('setBounds: centered to ' + JSON.stringify(bounds))
}
windows.main.setBounds(bounds, true)
windows.main.win.setBounds(bounds, true)
} else {
log('setBounds: not setting bounds because of window maximization')
}
}

function setAspectRatio (aspectRatio) {
log('setAspectRatio %o', aspectRatio)
if (windows.main) {
windows.main.setAspectRatio(aspectRatio)
}
if (!windows.main.win) return
windows.main.win.setAspectRatio(aspectRatio)
}

// Display string in dock badging area (OS X)
Expand All @@ -231,19 +228,6 @@ function setBadge (text) {
// Show progress bar. Valid range is [0, 1]. Remove when < 0; indeterminate when > 1.
function setProgress (progress) {
log('setProgress %s', progress)
if (windows.main) {
windows.main.setProgressBar(progress)
}
}

function blockPowerSave () {
powerSaveBlockerId = electron.powerSaveBlocker.start('prevent-display-sleep')
log('blockPowerSave %d', powerSaveBlockerId)
}

function unblockPowerSave () {
if (electron.powerSaveBlocker.isStarted(powerSaveBlockerId)) {
electron.powerSaveBlocker.stop(powerSaveBlockerId)
log('unblockPowerSave %d', powerSaveBlockerId)
}
if (!windows.main.win) return
windows.main.win.setProgressBar(progress)
}
5 changes: 5 additions & 0 deletions main/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ module.exports.error = error

var electron = require('electron')

var config = require('../config')
var windows = require('./windows')

var app = electron.app

function log (...args) {
if (!config.IS_PRODUCTION) {
// In development, also log to the console
console.log(...args)
}
if (app.ipcReady) {
windows.main.send('log', ...args)
} else {
Expand Down
Loading

0 comments on commit 001601b

Please sign in to comment.