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

feat: open web ui on second instance #1175

Merged
merged 1 commit into from
Oct 7, 2019
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
31 changes: 15 additions & 16 deletions src/argv-files-handler.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
import { app } from 'electron'
import fs from 'fs-extra'
import addToIpfs from './add-to-ipfs'

export default async function (ctx) {
const handleArgv = async argv => {
for (const arg of argv.slice(1)) {
if (!arg.startsWith('--add')) {
continue
}
export async function argvHandler (argv, ctx) {
let handled = false

for (const arg of argv.slice(1)) {
if (!arg.startsWith('--add')) {
continue
}

const filename = arg.slice(6)
const filename = arg.slice(6)

if (await fs.pathExists(filename)) {
await addToIpfs(ctx, filename)
}
if (await fs.pathExists(filename)) {
await addToIpfs(ctx, filename)
handled = true
}
}

// Works for Windows context menu
app.on('second-instance', (_, argv) => {
handleArgv(argv)
})
return handled
}

export default async function (ctx) {
// Checks current proccess
await handleArgv(process.argv)
await argvHandler(process.argv, ctx)
}
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import setupAutoUpdater from './auto-updater'
import setupTray from './tray'
import setupIpfsOnPath from './ipfs-on-path'
import setupAnalytics from './analytics'
import setupSecondInstance from './second-instance'

// Hide Dock
if (app.dock) app.dock.hide()
Expand Down Expand Up @@ -66,6 +67,7 @@ async function run () {
await Promise.all([
setupArgvFilesHandler(ctx),
setupAutoLaunch(ctx),
setupSecondInstance(ctx),
// Setup global shortcuts
setupDownloadHash(ctx),
setupTakeScreenshot(ctx),
Expand Down
34 changes: 20 additions & 14 deletions src/protocol-handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import toUri from 'multiaddr-to-uri'

function openLink (protocol, part, base) {
shell.openExternal(`${base}/${protocol}/${part}`)
return true
}

function parseAddr (addr) {
Expand All @@ -18,33 +19,38 @@ async function parseUrl (url, ctx) {
}

if (url.startsWith('ipfs://')) {
openLink('ipfs', url.slice(7), base)
return openLink('ipfs', url.slice(7), base)
} else if (url.startsWith('ipns://')) {
openLink('ipns', url.slice(7), base)
return openLink('ipns', url.slice(7), base)
} else if (url.startsWith('dweb:/ipfs/')) {
openLink('ipfs', url.slice(11), base)
return openLink('ipfs', url.slice(11), base)
} else if (url.startsWith('dweb:/ipns/')) {
openLink('ipns', url.slice(11), base)
return openLink('ipns', url.slice(11), base)
}

return false
}

export async function argvHandler (argv, ctx) {
let handled = false

for (const arg of argv) {
if (await parseUrl(arg, ctx)) {
handled = true
}
}

return handled
}

export default function (ctx) {
// Handle if the app started running now, and a link
// was sent to be handled.
for (const arg of process.argv) {
parseUrl(arg, ctx)
}
argvHandler(process.argv, ctx)

// Handle URLs in macOS
app.on('open-url', (event, url) => {
event.preventDefault()
parseUrl(url, ctx)
})

// Handle URLs on Windows (hopefully on Linux too)
app.on('second-instance', (_, argv) => {
for (const arg of argv) {
parseUrl(arg, ctx)
}
})
}
17 changes: 17 additions & 0 deletions src/second-instance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { app } from 'electron'
import { argvHandler as protocolHandler } from './protocol-handlers'
import { argvHandler as filesHandler } from './argv-files-handler'

export default async function (ctx) {
app.on('second-instance', async (_, argv) => {
if (await protocolHandler(argv, ctx)) {
return
}

if (await filesHandler(argv, ctx)) {
return
}

ctx.launchWebUI()
})
}
8 changes: 6 additions & 2 deletions src/webui/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,12 @@ export default async function (ctx) {
ctx.webui = window

ctx.launchWebUI = (url, { focus = true } = {}) => {
logger.info(`[web ui] navigate to ${url}`)
window.webContents.send('updatedPage', url)
if (!url) {
logger.info('[web ui] launching web ui')
} else {
logger.info(`[web ui] navigate to ${url}`)
window.webContents.send('updatedPage', url)
}

if (focus) {
window.show()
Expand Down