diff --git a/src/background/events.js b/src/background/events.js new file mode 100644 index 0000000..b62f833 --- /dev/null +++ b/src/background/events.js @@ -0,0 +1,17 @@ +import { BrowserWindow } from 'electron' + +import { sendToWindows } from './server/externals' +import { eventsList } from '../vendor' +import { Logger } from './server/utils' + +const logger = new Logger('External Open') + +export default function (e, args) { + e && e.preventDefault() + + logger.info('Opening', args) + + sendToWindows(eventsList.externalOpen.success, Array.isArray(args) ? args : [args]) + + setTimeout(BrowserWindow.getAllWindows()[0].focus, 100) +} diff --git a/src/background/index.js b/src/background/index.js index 4866640..3a5986a 100644 --- a/src/background/index.js +++ b/src/background/index.js @@ -12,6 +12,7 @@ import './startUp' import './server' import { dir } from './server/utils' import { localFiles } from './server/externals' +import external from './events' import menuTemplate from './menu' const isDevelopment = process.env.NODE_ENV !== 'production' @@ -172,6 +173,11 @@ app.on('quit', () => { }) }) +if (process.argv.length) external(null, process.argv) + +app.on('open-file', external) +app.on('open-url', external) + app.on('activate', () => { win === null ? createWindow() diff --git a/src/background/server/externals/index.js b/src/background/server/externals/index.js index d7cfa13..1def49e 100644 --- a/src/background/server/externals/index.js +++ b/src/background/server/externals/index.js @@ -4,3 +4,4 @@ export { default as mal } from './mal' export { default as localFiles } from './localFiles' export { default as parseSubtitles } from './subtitles' export { getCreds, setupCreds } from './vault' +export { default as sendToWindows } from './sendToWindows' diff --git a/src/background/server/externals/sendToWindows.js b/src/background/server/externals/sendToWindows.js new file mode 100644 index 0000000..9e756d0 --- /dev/null +++ b/src/background/server/externals/sendToWindows.js @@ -0,0 +1,8 @@ +import { BrowserWindow } from 'electron' + +export default function (eventName, data) { + BrowserWindow.getAllWindows() + .forEach((win) => { + win.webContents.send(eventName, data) + }) +} diff --git a/src/components/settings/sections/torrent.vue b/src/components/settings/sections/torrent.vue index ac4e95e..f87ecd3 100644 --- a/src/components/settings/sections/torrent.vue +++ b/src/components/settings/sections/torrent.vue @@ -25,11 +25,14 @@ export default { data: () => ({ isDefault: false, - defaultLoading: false + defaultLoading: false, + protocols: ['magnet', 'stream-magnet'] }), - async mounted () { - this.isDefault = (await this.$axios.get('protocols/isDefault')).data + mounted () { + const { app } = this.$electron.remote + + this.isDefault = this.protocols.some((protocol) => app.isDefaultProtocolClient(protocol)) }, computed: { @@ -54,13 +57,16 @@ export default { if (path) this.path = path }, - async setDefault () { + setDefault () { this.$log('Registering as default torrent app...') this.defaultLoading = true + const { app } = this.$electron.remote - const { status } = await this.$axios.post('protocols/register') + const isOk = this.protocols + .map((protocol) => app.setAsDefaultProtocolClient(protocol)) + .every(Boolean) - if (status === 200) { + if (isOk) { this.$log('Success!') this.isDefault = true } else { diff --git a/src/components/torrents/dialog.vue b/src/components/torrents/dialog.vue index e8a397b..6ec9f2b 100644 --- a/src/components/torrents/dialog.vue +++ b/src/components/torrents/dialog.vue @@ -50,13 +50,15 @@ export default { torrents: [{ torrent: '', show: false }] }), - mounted () { - const { torrent } = this.$route.query + created () { + this.$ipc.on(this.$eventsList.externalOpen.success, (e, args) => { + const toAdd = args.filter((e) => ['magnet', '.torrent'].includes(e)) - if (torrent) { - this.addTorrentsFromPath([torrent]) - this.show = true - } + if (toAdd.length) { + this.addTorrentsFromPath(toAdd) + this.show = true + } + }) }, computed: { diff --git a/src/mixins/video/hover.js b/src/mixins/video/hover.js index f47242c..2fc2751 100644 --- a/src/mixins/video/hover.js +++ b/src/mixins/video/hover.js @@ -1,5 +1,3 @@ -import { debounce } from 'lodash' - export default { data: () => ({ hover: { @@ -21,7 +19,7 @@ export default { }, methods: { - onMouseOver: debounce(function (e) { + onMouseOver (e) { const { value, isInsideTrack } = this.parseMouseMove(e) if (isInsideTrack) { @@ -31,7 +29,7 @@ export default { } else { this.hover.show = false } - }, 50), + }, onMouseEnter () { this.hover.show = true }, diff --git a/src/vendor/events.js b/src/vendor/events.js index fffa674..451b060 100644 --- a/src/vendor/events.js +++ b/src/vendor/events.js @@ -19,7 +19,8 @@ const getOnly = [ // Internal 'env', - 'isOnline' + 'isOnline', + 'externalOpen' ] const updatable = [