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

User options and auto launch #564

Merged
merged 20 commits into from
Dec 26, 2017
Merged
Show file tree
Hide file tree
Changes from 18 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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"description": "IPFS Native Application",
"main": "src/index.js",
"dependencies": {
"auto-launch": "^5.0.5",
"electron-compile": "^6.4.2",
"electron-is-dev": "^0.3.0",
"electron-menubar": "^1.0.1",
Expand Down
22 changes: 19 additions & 3 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import fs from 'fs'
import os from 'os'
import isDev from 'electron-is-dev'
import {app, dialog} from 'electron'

import FileHistory from './utils/file-history'
import KeyValueStore from './utils/key-value-store'

// Set up what to do on Uncaught Exceptions
process.on('uncaughtException', (error) => {
Expand Down Expand Up @@ -43,6 +45,7 @@ const ipfsAppData = (() => {

const ipfsPathFile = path.join(ipfsAppData, 'app-node-path')
const ipfsFileHistoryFile = path.join(ipfsAppData, 'file-history.json')
const userConfigFile = path.join(ipfsAppData, 'config.json')

const ipfsPath = (() => {
let pathIPFS
Expand Down Expand Up @@ -83,10 +86,25 @@ if (isDev) {
export default {
logger: logger,
fileHistory: new FileHistory(ipfsFileHistoryFile),
settingsStore: new KeyValueStore(userConfigFile),
logo: {
ice: logo('ice'),
black: logo('black')
},
ipfsPath: ipfsPath,
ipfsPathFile: ipfsPathFile,
// Will be replaced by a BrowserWindow instance.
settingsWindow: {
index: `file://${__dirname}/views/settings.html`,
title: 'IPFS Station Settings',
show: false,
icon: logo('ice'),
backgroundColor: '#252525',
resizable: false,
width: 450,
height: 450
},
// Will be replaced by a Menubar instance.
menubar: {
index: `file://${__dirname}/views/menubar.html`,
icon: logo('black'),
Expand All @@ -103,7 +121,5 @@ export default {
webSecurity: false
}
}
},
ipfsPath: ipfsPath,
ipfsPathFile: ipfsPathFile
}
}
21 changes: 21 additions & 0 deletions src/controls/main/auto-launch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const AutoLaunch = require('auto-launch')

const settingsOption = 'autoLaunch'
const autoLauncher = new AutoLaunch({
name: 'IPFS Station'
})

export default function (opts) {
let {logger, settingsStore} = opts

let activate = (value) => {
if (value === true) {
autoLauncher.enable().catch(logger.error)
} else {
autoLauncher.disable().catch(logger.error)
}
}

activate(settingsStore.get(settingsOption))
settingsStore.on(settingsOption, activate)
}
6 changes: 4 additions & 2 deletions src/controls/main/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import autoLaunch from './auto-launch'
import fileHistory from './file-history'
import openFileDialog from './open-file-dialog'
import openSettings from './open-settings'
import openWebUI from './open-webui'
import settings from './settings'
import takeScreenshot from './take-screenshot'
import toggleSticky from './toggle-sticky'
import uploadFiles from './upload-files'

export default function (opts) {
autoLaunch(opts)
fileHistory(opts)
openFileDialog(opts)
openSettings(opts)
openWebUI(opts)
settings(opts)
takeScreenshot(opts)
toggleSticky(opts)
uploadFiles(opts)
Expand Down
2 changes: 1 addition & 1 deletion src/controls/main/open-file-dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {uploadFiles} from '../utils'
function openFileDialog (opts, dir = false) {
let window = opts.window

return (event, callback) => {
return (event) => {
dialog.showOpenDialog(window, {
properties: [dir ? 'openDirectory' : 'openFile', 'multiSelections']
}, (files) => {
Expand Down
8 changes: 0 additions & 8 deletions src/controls/main/open-settings.js

This file was deleted.

55 changes: 55 additions & 0 deletions src/controls/main/settings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import {join} from 'path'
import {shell, ipcMain, BrowserWindow} from 'electron'

function setupWindow (opts) {
const options = opts.settingsWindow
const window = new BrowserWindow(options)

window.setMenu(null)
window.loadURL(options.index)
window.on('close', (event) => {
event.preventDefault()
window.hide()
})

// Replace the window settings by the actual
// instance of BrowserWindow
opts.settingsWindow = window
}

function open (opts) {
const {settingsWindow, settingsStore} = opts

return () => {
settingsWindow.webContents.send('settings', settingsStore.toObject())
settingsWindow.show()
settingsWindow.focus()
}
}

function openNodeConfig (opts) {
const {ipfsPath} = opts
return () => {
shell.openExternal(join(ipfsPath, 'config'))
}
}

function updateSettings (opts) {
const {settingsStore} = opts

return (event, settings) => {
for (const key in settings) {
if (settings.hasOwnProperty(key)) {
settingsStore.set(key, settings[key])
}
}
}
}

export default function (opts) {
setupWindow(opts)

ipcMain.on('open-settings', open(opts))
ipcMain.on('update-settings', updateSettings(opts))
ipcMain.on('open-node-settings', openNodeConfig(opts))
}
26 changes: 21 additions & 5 deletions src/controls/main/take-screenshot.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import {clipboard, ipcMain, globalShortcut} from 'electron'

const settingsOption = 'screenshotShortcut'
const shortcut = 'CommandOrControl+Alt+S'

function handleScreenshot (opts) {
let {logger, fileHistory, ipfs} = opts

Expand All @@ -8,6 +11,11 @@ function handleScreenshot (opts) {

logger.info('Screenshot taken')

if (!ipfs()) {
logger.info('Daemon not running. Aborting screenshot upload.')
return
}

ipfs()
.add(Buffer.from(base64Data, 'base64'))
.then((res) => {
Expand All @@ -23,12 +31,20 @@ function handleScreenshot (opts) {
}

export default function (opts) {
let {send, logger} = opts
let {send, logger, settingsStore} = opts

globalShortcut.register('CommandOrControl+Alt+S', () => {
logger.info('Taking Screenshot')
send('screenshot')
})
let activate = (value) => {
if (value === true) {
globalShortcut.register(shortcut, () => {
logger.info('Taking Screenshot')
send('screenshot')
})
} else {
globalShortcut.unregister(shortcut)
}
}

activate(settingsStore.get(settingsOption))
settingsStore.on(settingsOption, activate)
ipcMain.on('screenshot', handleScreenshot(opts))
}
Loading