Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Heejin committed Mar 15, 2016
0 parents commit 201c457
Show file tree
Hide file tree
Showing 51 changed files with 3,022 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["es2015", "react"]
}
20 changes: 20 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"extends": "airbnb/base",
"rules": {
"comma-dangle": ["error", "never"],
"no-unused-vars": "warn",
"camelcase": "off",
"object-shorthand": "off",
"guard-for-in": "warn",
"arrow-body-style": "off",
"consistent-return": "warn",
"no-throw-literal": "warn",
"quote-props": "off",
"new-cap": "warn",
"default-case": "off",
"no-empty": "warn",
"curly": "off",
"padded-blocks": "off",
"no-use-before-define": "warn"
}
}
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
node_modules
/.vscode
/main/
/_localStorage/
/dist/app.bundle.js

/platform-util/build
/platform-util/node_modules

/plugins/

/debug.log
Binary file added build/icon.ico
Binary file not shown.
17 changes: 17 additions & 0 deletions dist/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<html>

<head>
<title>fred</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
<script type="text/javascript">
window.$ = window.jQuery = require('jquery');
</script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery-scrollTo/2.1.0/jquery.scrollTo.min.js"></script>
</head>

<body style="margin: 55px 12px 12px 12px; overflow: hidden">
<div id="app"></div>
<script src="app.bundle.js"></script>
</body>

</html>
38 changes: 38 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict';

const gulp = require('gulp');
const gutil = require('gulp-util');
const babel = require('gulp-babel');
const sourcemaps = require('gulp-sourcemaps');
const shell = require('gulp-shell');

const webpack = require('webpack');

gulp.task('main', () => {
gulp.src('./main-es6/**/*.js')
.pipe(sourcemaps.init())
.pipe(babel({
presets: ['es2015', 'react']
}))
.pipe(sourcemaps.write())
.pipe(gulp.dest('main'));
gulp.src('./main-es6/**/*.json')
.pipe(gulp.dest('main'));
});

gulp.task('renderer', (complete) => {
webpack(require('./webpack.config.js'), (err, stats) => {
if (err) {
throw new gutil.PluginError('webpack', err);
}
gutil.log('webpack', stats.toString());
complete();
});
});

gulp.task('watch', ['main', 'renderer'], () => {
gulp.watch('./main-es6/**/*.js', ['main']);
gulp.watch('./renderer/**/*', ['renderer']);
});

gulp.task('default', ['main', 'renderer']);
Binary file added images/hain.ico
Binary file not shown.
Binary file added images/hain.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/tray_16.ico
Binary file not shown.
Binary file added images/tray_24.ico
Binary file not shown.
86 changes: 86 additions & 0 deletions main-es6/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
'use strict';

const _ = require('lodash');
const electron = require('electron');
const cp = require('child_process');

const dialog = electron.dialog;
const app = electron.app;
const globalShortcut = electron.globalShortcut;

const logger = require('./logger').create('main');

const window = require('./window');
const server = require('./server');
const iconProtocol = require('./icon-protocol');
const toast = require('./services/toast');
const autolaunch = require('./autolaunch');

function registerShortcut() {
globalShortcut.register('alt+space', () => {
if (window.isContentLoading() || server.isLoading()) {
logger.log('please wait a seconds, you can use shortcut after loaded');
return;
}
window.showWindowOnCenter();
});
}

let _isRestarting = false;

const shouldSetAutolaunch = (_.includes(process.argv, '--setautolaunch'));
if (shouldSetAutolaunch) {
autolaunch.activate();
}

const isRestarted = (_.includes(process.argv, '--restarted'));
const shouldQuit = app.makeSingleInstance((cmdLine, workingDir) => {
if (_isRestarting)
return;
window.showWindowOnCenter();
});

if (shouldQuit && !isRestarted) {
app.quit();
} else {
app.on('ready', () => {
window.createTray();
server.start().then(() => {
registerShortcut();
window.createWindow(() => {
if (isRestarted) {
window.showWindowOnCenter();
toast('restarted');
}
});
}).catch((err) => {
logger.log(err);
dialog.showErrorBox('unhandled error occured!', err);
app.quit();
});
});

app.on('will-quit', () => {
globalShortcut.unregisterAll();
window.destroyRefs();
});

iconProtocol.register();
}

function restart() {
if (_isRestarting)
return;
_isRestarting = true;

const argv = [].concat(process.argv);
if (!_.includes(argv, '--restarted')) {
argv.push('--restarted');
}
cp.exec(argv.join(' '));
setTimeout(() => app.quit(), 500);
}

module.exports = {
restart: restart
};
54 changes: 54 additions & 0 deletions main-es6/autolaunch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'use strict';

const co = require('co');
const Registry = require('winreg');
const regKey = new Registry({
hive: Registry.HKCU,
key: '\\Software\\Microsoft\\Windows\\CurrentVersion\\Run'
});

const VALUE_NAME = 'Hain';

function activate() {
return new Promise((resolve, reject) => {
regKey.set(VALUE_NAME, Registry.REG_SZ, `"${process.execPath}"`, (err) => {
if (err)
return reject(err);
return resolve();
});
});
}

function deactivate() {
return new Promise((resolve, reject) => {
regKey.remove(VALUE_NAME, (err) => {
if (err)
return reject(err);
return resolve();
});
});
}

function isActivated() {
return new Promise((resolve, reject) => {
regKey.get(VALUE_NAME, (err, item) => {
return resolve(item !== null);
});
});
}

function* toggle() {
const activated = yield isActivated();
if (activated) {
yield deactivate();
} else {
yield activate();
}
}

module.exports = {
activate,
deactivate,
isActivated,
toggle: co.wrap(toggle)
};
65 changes: 65 additions & 0 deletions main-es6/iconprotocol.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
'use strict';

const _ = require('lodash');
const path = require('path');
const SimpleCache = require('simple-lru-cache');

const electron = require('electron');
const app = electron.app;

const platformUtil = require('../platform-util');
const logger = require('./logger').create('iconprotocol');

function register() {
app.on('ready', () => {
const cache = new SimpleCache({ maxSize: 100 });

const protocol = electron.protocol;
protocol.registerBufferProtocol('icon', (req, callback) => {
let filePath = null;
try {
const path_base64 = req.url.substr(7);
filePath = new Buffer(path_base64, 'base64').toString();
} catch (e) {
// error
return callback();
}

if (filePath === null || filePath.length === 0) {
// error
return callback();
}

let cacheKey = filePath;
const extName = path.extname(filePath).toLowerCase();

if (extName.length > 0 && extName !== '.exe' && extName !== '.lnk') {
cacheKey = extName;
} else {
cacheKey = filePath;
}

const buffer = cache.get(cacheKey);
if (buffer === undefined) {
platformUtil.fetchFileIconAsPng(filePath, (err, buf) => {
if (err || buf === null) {
logger.log(`internal error ${err}`);
return callback();
}
cache.set(cacheKey, buf);
callback({ mimeType: 'image/png', data: buf });
});
} else {
callback({ mimeType: 'image/png', data: buffer });
}
}, (err) => {
if (err) {
logger.log('failed to register protocol: icon');
}
});
});
}

module.exports = {
register: register
};
24 changes: 24 additions & 0 deletions main-es6/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';

const winston = require('winston');
const logger = new (winston.Logger)({
level: 'debug',
transports: [
new (winston.transports.Console)({ timestamp: true, prettyPrint: true }),
new (winston.transports.File)({ filename: 'debug.log', json: false, prettyPrint: true })
]
});

class Logger {
constructor(tag) {
this.tag = tag;
}

log(msg) {
logger.debug(this.tag, msg);
}
}

module.exports = {
create: (tag) => new Logger(tag)
};
Loading

0 comments on commit 201c457

Please sign in to comment.