Skip to content

Commit

Permalink
Move unrelated code out of menu.js and ipc.js
Browse files Browse the repository at this point in the history
  • Loading branch information
feross committed May 29, 2016
1 parent d4efebd commit 62cb304
Show file tree
Hide file tree
Showing 16 changed files with 460 additions and 394 deletions.
18 changes: 12 additions & 6 deletions main/dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ module.exports = {
openTorrentAddress
}

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

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

/**
* Show open dialog to create a single-file torrent.
*/
function openSeedFile () {
if (!windows.main.win) return
log('openSeedFile')
var opts = {
title: 'Select a file for the torrent.',
properties: [ 'openFile' ]
Expand All @@ -22,7 +25,7 @@ function openSeedFile () {
electron.dialog.showOpenDialog(windows.main.win, opts, function (selectedPaths) {
resetTitle()
if (!Array.isArray(selectedPaths)) return
windows.main.send('dispatch', 'showCreateTorrent', selectedPaths)
windows.main.dispatch('showCreateTorrent', selectedPaths)
})
}

Expand All @@ -33,6 +36,7 @@ function openSeedFile () {
*/
function openSeedDirectory () {
if (!windows.main.win) return
log('openSeedDirectory')
var opts = process.platform === 'darwin'
? {
title: 'Select a file or folder for the torrent.',
Expand All @@ -46,7 +50,7 @@ function openSeedDirectory () {
electron.dialog.showOpenDialog(windows.main.win, opts, function (selectedPaths) {
resetTitle()
if (!Array.isArray(selectedPaths)) return
windows.main.send('dispatch', 'showCreateTorrent', selectedPaths)
windows.main.dispatch('showCreateTorrent', selectedPaths)
})
}

Expand All @@ -55,6 +59,7 @@ function openSeedDirectory () {
*/
function openTorrentFile () {
if (!windows.main.win) return
log('openTorrentFile')
var opts = {
title: 'Select a .torrent file to open.',
filters: [{ name: 'Torrent Files', extensions: ['torrent'] }],
Expand All @@ -65,7 +70,7 @@ function openTorrentFile () {
resetTitle()
if (!Array.isArray(selectedPaths)) return
selectedPaths.forEach(function (selectedPath) {
windows.main.send('dispatch', 'addTorrent', selectedPath)
windows.main.dispatch('addTorrent', selectedPath)
})
})
}
Expand All @@ -74,15 +79,16 @@ function openTorrentFile () {
* Show modal dialog to open a torrent URL (magnet uri, http torrent link, etc.)
*/
function openTorrentAddress () {
windows.main.send('showOpenTorrentAddress')
log('openTorrentAddress')
windows.main.dispatch('openTorrentAddress')
}

/**
* Dialogs on do not show a title on OS X, so the window title is used instead.
*/
function setTitle (title) {
if (process.platform === 'darwin') {
windows.main.send('dispatch', 'setTitle', title)
windows.main.dispatch('setTitle', title)
}
}

Expand Down
59 changes: 59 additions & 0 deletions main/dock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
module.exports = {
downloadFinished,
init,
setBadge
}

var electron = require('electron')

var app = electron.app

var dialog = require('./dialog')
var log = require('./log')

/**
* Add a right-click menu to the dock icon. (OS X)
*/
function init () {
if (!app.dock) return
var menu = electron.Menu.buildFromTemplate(getMenuTemplate())
app.dock.setMenu(menu)
}

/**
* Bounce the Downloads stack if `path` is inside the Downloads folder. (OS X)
*/
function downloadFinished (path) {
if (!app.dock) return
log(`downloadFinished: ${path}`)
app.dock.downloadFinished(path)
}

/**
* Display string in dock badging area. (OS X)
*/
function setBadge (text) {
if (!app.dock) return
log(`setBadge: ${text}`)
app.dock.setBadge(String(text))
}

function getMenuTemplate () {
return [
{
label: 'Create New Torrent...',
accelerator: 'CmdOrCtrl+N',
click: () => dialog.openSeedDirectory()
},
{
label: 'Open Torrent File...',
accelerator: 'CmdOrCtrl+O',
click: () => dialog.openTorrentFile()
},
{
label: 'Open Torrent Address...',
accelerator: 'CmdOrCtrl+U',
click: () => dialog.openTorrentAddress()
}
]
}
16 changes: 9 additions & 7 deletions main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var announcement = require('./announcement')
var config = require('../config')
var crashReporter = require('../crash-reporter')
var dialog = require('./dialog')
var dock = require('./dock')
var handlers = require('./handlers')
var ipc = require('./ipc')
var log = require('./log')
Expand Down Expand Up @@ -61,8 +62,8 @@ function init () {
app.on('ready', function () {
isReady = true

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

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

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

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

function delayedInit () {
announcement.init()
tray.init()
dock.init()
handlers.install()
tray.init()
updater.init()
}

function onOpen (e, torrentId) {
e.preventDefault()

if (app.ipcReady) {
windows.main.send('dispatch', 'onOpen', torrentId)
windows.main.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.
// Electron issue: https://github.com/atom/electron/issues/4338
Expand Down Expand Up @@ -148,6 +150,6 @@ function processArgv (argv) {
}
})
if (paths.length > 0) {
windows.main.send('dispatch', 'onOpen', paths)
windows.main.dispatch('onOpen', paths)
}
}
Loading

0 comments on commit 62cb304

Please sign in to comment.