Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: redesign motrix scheme #716

Merged
merged 6 commits into from
Jun 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 14 additions & 20 deletions src/main/Application.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export default class Application extends EventEmitter {
this.upnp.map(dhtPort)
]
try {
await Promise.all(promises)
await Promise.allSettled(promises)
} catch (e) {
logger.warn('[Motrix] start UPnP mapping fail', e)
}
Expand All @@ -192,7 +192,7 @@ export default class Application extends EventEmitter {
this.upnp.unmap(dhtPort)
]
try {
await Promise.all(promises)
await Promise.allSettled(promises)
} catch (e) {
logger.warn('[Motrix] stop UPnP mapping fail', e)
}
Expand All @@ -214,7 +214,7 @@ export default class Application extends EventEmitter {
this.upnp.map(newValue)
]
try {
await Promise.all(promises)
await Promise.allSettled(promises)
} catch (e) {
logger.info('[Motrix] change UPnP port mapping failed:', e)
}
Expand Down Expand Up @@ -410,7 +410,7 @@ export default class Application extends EventEmitter {
this.themeManager = new ThemeManager()
this.themeManager.on('system-theme-change', (theme) => {
this.trayManager.changeIconTheme(theme)
this.sendCommandToAll('application:update-system-theme', theme)
this.sendCommandToAll('application:update-system-theme', { theme })
})
}

Expand All @@ -423,21 +423,13 @@ export default class Application extends EventEmitter {
}

initProtocolManager () {
if (is.dev() || is.mas()) {
return
}

const protocols = this.configManager.getUserConfig('protocols', {})
this.protocolManager = new ProtocolManager({
protocols
})
}

handleProtocol (url) {
if (is.dev() || is.mas()) {
return
}

this.show()

this.protocolManager.handle(url)
Expand All @@ -454,15 +446,17 @@ export default class Application extends EventEmitter {

this.show()

const fileName = basename(filePath)
const name = basename(filePath)
readFile(filePath, (err, data) => {
if (err) {
logger.warn(`[Motrix] read file error: ${filePath}`, err.message)
return
}
const file = Buffer.from(data).toString('base64')
const args = [fileName, file]
this.sendCommandToAll('application:new-bt-task-with-file', ...args)
const dataURL = Buffer.from(data).toString('base64')
this.sendCommandToAll('application:new-bt-task-with-file', {
name,
dataURL
})
})
}

Expand Down Expand Up @@ -557,11 +551,11 @@ export default class Application extends EventEmitter {
}
})

this.on('application:show', (page) => {
this.on('application:show', ({ page }) => {
this.show(page)
})

this.on('application:hide', (page) => {
this.on('application:hide', ({ page }) => {
this.hide(page)
})

Expand All @@ -576,7 +570,7 @@ export default class Application extends EventEmitter {

this.on('application:change-theme', (theme) => {
this.themeManager.updateAppAppearance(theme)
this.sendCommandToAll('application:update-theme', theme)
this.sendCommandToAll('application:update-theme', { theme })
})

this.on('application:change-locale', (locale) => {
Expand Down Expand Up @@ -663,7 +657,7 @@ export default class Application extends EventEmitter {
}

handleConfigChange (configName) {
this.sendCommandToAll('application:update-preference-config', configName)
this.sendCommandToAll('application:update-preference-config', { configName })
}

handleEvents () {
Expand Down
22 changes: 14 additions & 8 deletions src/main/core/ProtocolManager.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

import { EventEmitter } from 'events'
import { app } from 'electron'
import is from 'electron-is'
import { parse } from 'querystring'

import logger from './Logger'
import protocolMap from '../configs/protocol'
Expand Down Expand Up @@ -28,6 +29,10 @@ export default class ProtocolManager extends EventEmitter {
}

setup (protocols) {
if (is.dev() || is.mas()) {
return
}

Object.keys(protocols).forEach((protocol) => {
const enabled = protocols[protocol]
if (enabled) {
Expand Down Expand Up @@ -63,23 +68,24 @@ export default class ProtocolManager extends EventEmitter {
return
}

global.application.sendCommandToAll('application:new-task', ADD_TASK_TYPE.URI, url)
global.application.sendCommandToAll('application:new-task', {
type: ADD_TASK_TYPE.URI,
uri: url
})
}

handleMoProtocol (url) {
const parsed = new URL(url)
const { host } = parsed
const { host, search } = parsed
logger.info('[Motrix] protocol parsed:', parsed, host)

const command = protocolMap[host]
if (!command) {
return
}

// @TODO 没想明白怎么传参数好
// 如果按顺序传递,那 url 的 query string 就要求有序的了
// const query = queryString.parse(parsed.query)
const args = []
global.application.sendCommandToAll(command, ...args)
const query = search.startsWith('?') ? search.replace('?', '') : search
const args = parse(query)
global.application.sendCommandToAll(command, args)
}
}
8 changes: 4 additions & 4 deletions src/main/menus/darwin.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"id": "menu.app",
"submenu": [
{ "id": "app.about", "command": "application:about", "command-before": "application:show,index" },
{ "id": "app.about", "command": "application:about", "command-before": "application:show?page=index" },
{ "type": "separator" },
{ "id": "app.preferences", "command": "application:preferences" },
{ "id": "app.check-for-updates", "command": "application:check-for-updates" },
Expand All @@ -17,9 +17,9 @@
{
"id": "menu.task",
"submenu": [
{ "id": "task.new-task", "command": "application:new-task", "command-after": "application:show,index" },
{ "id": "task.new-bt-task", "command": "application:new-bt-task", "command-arg": "torrent", "command-after": "application:show,index" },
{ "id": "task.open-file", "command": "application:open-file", "command-before": "application:show,index" },
{ "id": "task.new-task", "command": "application:new-task", "command-after": "application:show?page=index" },
{ "id": "task.new-bt-task", "command": "application:new-bt-task", "command-after": "application:show?page=index" },
{ "id": "task.open-file", "command": "application:open-file", "command-before": "application:show?page=index" },
{ "type": "separator" },
{ "id": "task.pause-task", "command": "application:pause-task" },
{ "id": "task.resume-task", "command": "application:resume-task" },
Expand Down
10 changes: 5 additions & 5 deletions src/main/menus/linux.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
{
"id": "menu.file",
"submenu": [
{ "id": "app.about", "command": "application:about", "command-before": "application:show,index" },
{ "id": "app.about", "command": "application:about", "command-before": "application:show?page=index" },
{ "type": "separator" },
{ "id": "app.preferences", "command": "application:preferences" },
{ "id": "app.check-for-updates", "command": "application:check-for-updates" },
{ "id": "app.show", "command": "application:show", "command-arg": "index" },
{ "id": "app.show", "command": "application:show", "command-arg": { "page": "index" } },
{ "type": "separator" },
{ "id": "app.quit", "command": "application:quit" }
]
},
{
"id": "menu.task",
"submenu": [
{ "id": "task.new-task", "command": "application:new-task", "command-after": "application:show,index" },
{ "id": "task.new-bt-task", "command": "application:new-bt-task", "command-arg": "torrent", "command-after": "application:show,index" },
{ "id": "task.open-file", "command": "application:open-file", "command-before": "application:show,index" },
{ "id": "task.new-task", "command": "application:new-task", "command-after": "application:show?page=index" },
{ "id": "task.new-bt-task", "command": "application:new-bt-task", "command-after": "application:show?page=index" },
{ "id": "task.open-file", "command": "application:open-file", "command-before": "application:show?page=index" },
{ "type": "separator" },
{ "id": "task.pause-task", "command": "application:pause-task" },
{ "id": "task.resume-task", "command": "application:resume-task" },
Expand Down
10 changes: 5 additions & 5 deletions src/main/menus/touchBar.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"type": "button", "icon": "new-task", "id": "task.new-task", "command": "application:new-task", "command-arg": "uri", "command-after": "application:show,index"
"type": "button", "icon": "new-task", "id": "task.new-task", "command": "application:new-task", "command-after": "application:show?page=index"
},
{
"type": "spacer", "size": "small"
Expand All @@ -10,13 +10,13 @@
"id": "task.task-list",
"items": [
{
"type": "button", "icon": "task-active", "command": "application:task-list", "command-arg": "active"
"type": "button", "icon": "task-active", "command": "application:task-list", "command-arg": { "status": "active" }
},
{
"type": "button", "icon": "task-waiting", "command": "application:task-list", "command-arg": "waiting"
"type": "button", "icon": "task-waiting", "command": "application:task-list", "command-arg": { "status": "waiting" }
},
{
"type": "button", "icon": "task-stopped", "command": "application:task-list", "command-arg": "stopped"
"type": "button", "icon": "task-stopped", "command": "application:task-list", "command-arg": { "status": "stopped" }
}
]
},
Expand All @@ -30,6 +30,6 @@
"type": "spacer", "size": "small"
},
{
"type": "button", "icon": "about", "id": "app.about", "command": "application:about", "command-before": "application:show,index"
"type": "button", "icon": "about", "id": "app.about", "command": "application:about", "command-before": "application:show?page=index"
}
]
10 changes: 5 additions & 5 deletions src/main/menus/tray.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[
{ "id": "task.new-task", "command": "application:new-task", "command-after": "application:show,index" },
{ "id": "task.new-bt-task", "command": "application:new-bt-task", "command-arg": "torrent", "command-after": "application:show,index" },
{ "id": "task.open-file", "command": "application:open-file", "command-before": "application:show,index" },
{ "id": "task.new-task", "command": "application:new-task", "command-after": "application:show?page=index" },
{ "id": "task.new-bt-task", "command": "application:new-bt-task", "command-arg": { "type": "torrent" }, "command-after": "application:show?page=index" },
{ "id": "task.open-file", "command": "application:open-file", "command-before": "application:show?page=index" },
{ "type": "separator" },
{ "id": "app.show", "command": "application:show", "command-arg": "index" },
{ "id": "app.show", "command": "application:show", "command-arg": { "page": "index" } },
{ "id": "help.manual", "command": "help:manual" },
{ "id": "app.check-for-updates", "command": "application:check-for-updates" },
{ "type": "separator" },
{ "id": "app.preferences", "command": "application:preferences", "command-before": "application:show,index" },
{ "id": "app.preferences", "command": "application:preferences", "command-before": "application:show?page=index" },
{ "id": "app.quit", "command": "application:quit" }
]
10 changes: 5 additions & 5 deletions src/main/menus/win32.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
{
"id": "menu.file",
"submenu": [
{ "id": "app.about", "command": "application:about", "command-before": "application:show,index" },
{ "id": "app.about", "command": "application:about", "command-before": "application:show?page=index" },
{ "type": "separator" },
{ "id": "app.preferences", "command": "application:preferences" },
{ "id": "app.check-for-updates", "command": "application:check-for-updates" },
{ "id": "app.show", "command": "application:show", "command-arg": "index" },
{ "id": "app.show", "command": "application:show", "command-arg": { "page": "index" } },
{ "type": "separator" },
{ "id": "app.quit", "command": "application:quit" }
]
},
{
"id": "menu.task",
"submenu": [
{ "id": "task.new-task", "command": "application:new-task", "command-after": "application:show,index" },
{ "id": "task.new-bt-task", "command": "application:new-bt-task", "command-arg": "torrent", "command-after": "application:show,index" },
{ "id": "task.open-file", "command": "application:open-file", "command-before": "application:show,index" },
{ "id": "task.new-task", "command": "application:new-task", "command-after": "application:show?page=index" },
{ "id": "task.new-bt-task", "command": "application:new-bt-task", "command-after": "application:show?page=index" },
{ "id": "task.open-file", "command": "application:open-file", "command-before": "application:show?page=index" },
{ "type": "separator" },
{ "id": "task.pause-task", "command": "application:pause-task" },
{ "id": "task.resume-task", "command": "application:resume-task" },
Expand Down
12 changes: 8 additions & 4 deletions src/main/utils/menu.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { parse } from 'querystring'

export function concat (template, submenu, submenuToAdd) {
submenuToAdd.forEach(sub => {
let relativeItem = null
Expand Down Expand Up @@ -115,16 +117,18 @@ function handleCommandBefore (item) {
if (!item['command-before']) {
return
}
const [command, ...args] = item['command-before'].split(',')
global.application.sendCommandToAll(command, ...args)
const [command, params] = item['command-before'].split('?')
const args = parse(params)
global.application.sendCommandToAll(command, args)
}

function handleCommandAfter (item) {
if (!item['command-after']) {
return
}
const [command, ...args] = item['command-after'].split(',')
global.application.sendCommandToAll(command, ...args)
const [command, params] = item['command-after'].split('?')
const args = parse(params)
global.application.sendCommandToAll(command, args)
}

function acceleratorForCommand (command, keystrokesByCommand) {
Expand Down
6 changes: 3 additions & 3 deletions src/renderer/components/Preference/Advanced.vue
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@
import '@/components/Icons/sync'
import '@/components/Icons/refresh'

const initialForm = (config) => {
const initForm = (config) => {
const {
allProxy,
allProxyBackup,
Expand Down Expand Up @@ -395,7 +395,7 @@
},
data () {
const { locale } = this.$store.state.preference.config
const form = initialForm(this.$store.state.preference.config)
const form = initForm(this.$store.state.preference.config)
const formOriginal = cloneDeep(form)

return {
Expand Down Expand Up @@ -524,7 +524,7 @@
syncFormConfig () {
this.$store.dispatch('preference/fetchPreference')
.then((config) => {
this.form = initialForm(config)
this.form = initForm(config)
this.formOriginal = cloneDeep(this.form)
})
},
Expand Down
6 changes: 3 additions & 3 deletions src/renderer/components/Preference/Basic.vue
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@
} from '@shared/utils'
import { APP_RUN_MODE } from '@shared/constants'

const initialForm = (config) => {
const initForm = (config) => {
const {
autoHideWindow,
dir,
Expand Down Expand Up @@ -269,7 +269,7 @@
},
data () {
const { locale } = this.$store.state.preference.config
const form = initialForm(this.$store.state.preference.config)
const form = initForm(this.$store.state.preference.config)
const formOriginal = cloneDeep(form)

return {
Expand Down Expand Up @@ -379,7 +379,7 @@
syncFormConfig () {
this.$store.dispatch('preference/fetchPreference')
.then((config) => {
this.form = initialForm(config)
this.form = initForm(config)
this.formOriginal = cloneDeep(this.form)
})
},
Expand Down
Loading