Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
xludx committed Dec 3, 2017
1 parent d7e94d4 commit 269299f
Show file tree
Hide file tree
Showing 11 changed files with 233 additions and 93 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.DS_Store
node_modules
build
11 changes: 11 additions & 0 deletions .partbar.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"refresh": 300000,
"coins": [
"bitcoin",
"particl",
"neo",
"ada"
],
"currency": "$"
}

41 changes: 11 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# CoinWatch Touchbar
# Particl Touchbar

[![Join the chat at https://gitter.im/coinwatch/Lobby](https://badges.gitter.im/coinwatch/Lobby.svg)](https://gitter.im/coinwatch/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
Displays Particl & Bitcoin price on your Macbook's touchbar.

Check Bitcoin, Ethereum, Ripple & other cryptocurrencies anytime via your touchbar.
[Download Page](https://github.com/xludx/partbar/releases)

[Download Page](https://github.com/andrewrd/coinwatch/releases)
Based on:

## Prerequisites

Expand All @@ -14,39 +14,20 @@ Check Bitcoin, Ethereum, Ripple & other cryptocurrencies anytime via your touchb

Users have the option to define a custom configuration for this app.

Simply place a json file named `.coinwatch.json` in your home directory that has
Simply place a json file named `.partbar.json` in your home directory that has
the following structure.

```json
{
"refresh": 150000,
"refresh": 300000,
"coins": [
"bitcoin",
"ethereum",
"ripple"
]
"particl"
],
"currency": "$"
}
```

From here you can modify the refresh rate and the coins that are displayed in
your touch bar.
## Thanks

## How to Install

npm install

## How to Run

npm start

## Support Development

Bitcoin - 1JVSv65ZdEf6NQUHdGw7EafQwPnVUuM2PK

Ethereum - 0x007856928853Fb57f5d5dE3F695E003E11D7B716

Ripple - rM4pGL1G7WBAUBLsB9SPUrjdPXpz8nT8be

## Contributors
andrewrd, andrewrd.com
Noam Eppel, CleanForest.co
Particl Touchbar is based on [coinwatch](https://github.com/andrewrd/coinwatch/).
Binary file removed build/.DS_Store
Binary file not shown.
Binary file modified img/logo.icns
Binary file not shown.
Binary file modified img/logo.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 img/particl-tray.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 img/particl.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 2 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@

<div class="container">
<div class="welcome">
<h1>CoinWatch Touchbar</h1>
<p>Check Bitcoin, Ethereum, Ripple and other cryptocurrency prices anytime.</p>
<h1>Particl Touchbar</h1>
</div>

<div class="credit">
<p> andrewrd.com <p/>
<p>particl.io<p/>
</div>

</div>
Expand Down
255 changes: 201 additions & 54 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,96 +1,243 @@
const electron = require('electron');

const { app, BrowserWindow, TouchBar, session, ipcMain } = require('electron');
const { app, BrowserWindow, TouchBar, Tray, session, ipcMain } = require('electron');
const { TouchBarLabel, TouchBarButton, TouchBarSpacer } = TouchBar;
const expandTilde = require('expand-tilde');
const fs = require('fs');
const path = require('path');
const url = require('url');
const fetch = require('electron-fetch');

const defaultConfig = {
refresh: 150000, // 2.5 minutes
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow = null;
let tray = null;
let openDevTools = false;
let partPrice = 0.0;
let refreshFn = null;

let config = {
refresh: 60000*5,
coins: [
"bitcoin",
"ethereum",
"ripple"
]
"particl",
"bitcoin"
],
currency: '€' // or '$'
};

function button(coinName, price) {
var iconPath = path.join(__dirname, '/img/' + coinName + '.png');
let iconPath = path.join(__dirname, '/img/' + coinName + '.png');
if (!fs.existsSync(iconPath)) {
iconPath = path.join(__dirname, '/img/notfound.png');
}

return new TouchBarButton({
// label: coinName + ' $' + price,
label: '$' + price,
label: config.currency + price,
textColor: '#ABCDEF',
icon: iconPath,
iconPosition: 'left',
click: () => {
let win = new BrowserWindow({width: 900, height: 800})
win.on('closed', () => {
win = null
})
let win = new BrowserWindow({width: 900, height: 800});
win.on('closed', () => {
win = null
});

// Load a remote URL
win.loadURL('https://coinmarketcap.com/currencies/' + coinName)
// Load a remote URL
win.loadURL('https://coinmarketcap.com/currencies/' + coinName)
}
});
}

function touchBar(response, ids) {
var buttons =
response.filter(
(coin) => ids.includes(coin.id)
).map(
(coin) => button(coin.id, coin.price_usd));
function touchBar(coinMarketData) {
const buttons = coinMarketData.map( (coin) => {
if( config.currency === '€' ){
return button(coin.id, (coin.price_eur*1).toFixed(2));
} else {
return button(coin.id, (coin.price_usd*1).toFixed(2));
}
});

return new TouchBar(
buttons.filter((b) => b !== null));
return new TouchBar(buttons.filter((b) => b !== null));
}

/*
** initiates the Main Window
*/
function createWindow(config) {
// Create the browser window.
win = new BrowserWindow({ width: 500, height: 260, icon: path.join(__dirname, '/img/logo.png') });

// and load the index.html of the app.
win.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
}));

// Open the DevTools.
//win.webContents.openDevTools();

// Emitted when the window is closed.
win.on('closed', () => {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
win = null;

if (tray === null) {
tray = makeTray();
}

console.log('config:', config);

if (mainWindow === null){

// Create the browser window.
mainWindow = new BrowserWindow({
width: 500,
height: 260,
icon: path.join(__dirname, '/img/logo.png'), // doesn't work on osx :/
resizable: false,
// titleBarStyle: 'hidden',
frame: true
});

// and load the index.html of the app.
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
}));

// Open the DevTools.
if (openDevTools || config.devtools) {
mainWindow.webContents.openDevTools()
}

// Emitted when the window is closed.
mainWindow.on('closed', () => {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null;
});
}

if( refreshFn === null ){
refreshFn = () => {
fetch('https://api.coinmarketcap.com/v1/ticker/?limit=200&convert=EUR')
.then( (res) => {
return res.json();
})
.then( (json) => {
const coinMarketData = json.filter( (coin) => {
return config.coins.includes(coin.id);
});
if( mainWindow !== null ) {
mainWindow.setTouchBar(touchBar(coinMarketData));
}

const partMarketData = coinMarketData.filter( (coin) => {
return coin.id === 'particl';
});

if( config.currency === '€' ){
partPrice = partMarketData[0].price_eur;
} else {
partPrice = partMarketData[0].price_usd;
}

// console.log('partPrice: ', partPrice);
tray.setTitle( config.currency + (partPrice*1).toFixed(2));
});
};
setInterval(refreshFn, config.refresh);
}
refreshFn();
}

/*
** creates the tray icon and menu
*/
function makeTray() {

// Default tray image + icon
let trayImage = path.join(__dirname, 'img/particl-tray.png');

// The tray context menu
const contextMenu = electron.Menu.buildFromTemplate([
/*{
label: 'View',
submenu: [
{
label: 'Reload',
click () { mainWindow.webContents.reloadIgnoringCache(); }
},
{
label: 'Open Dev Tools',
click () { mainWindow.openDevTools(); }
}
]
},*/
{
role: 'window',
submenu: [
{
label: 'Close',
click () { app.quit() }
},
{
label: 'Hide',
click () { mainWindow.hide(); }
},
{
label: 'Show',
click () { mainWindow.show(); }
}
]
},
{
role: 'help',
submenu: [
/*{
label: 'About ' + app.getName(),
click () { electron.shell.openExternal('https://particl.io/#about'); }
},*/
{
label: 'Visit Particl.io',
click () { electron.shell.openExternal('https://particl.io'); }
},
{
label: 'Visit Electron',
click () { electron.shell.openExternal('https://electron.atom.io'); }
}
]
}
]);

// Create the tray icon
tray = new electron.Tray(trayImage);

// tray.setPressedImage(image);

// Set the tray icon
// tray.setToolTip('Particl');
// tray.setContextMenu(contextMenu);

// Always show window when tray icon clicked
tray.on('click', (event, bounds) => {
if (mainWindow === null) {
createWindow(config);
} else {
mainWindow.show();
}
});

var refresh = () => {
fetch('https://api.coinmarketcap.com/v1/ticker/?limit=20')
.then((res) => { return res.json(); }
).then((json) => { win.setTouchBar(touchBar(json, config.coins)); })
};
tray.on('mouse-enter', (event, position) => {
refreshFn();
});

refresh();
setInterval(refresh, config.refresh);
return tray;
}

// Load user-defined configuration (use default otherwise)
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', () => {
var configPath = expandTilde('~/.coinwatch.json');
const configPath = expandTilde('~/.partbar.json');
if (fs.existsSync(configPath)) {
createWindow(JSON.parse(fs.readFileSync(configPath)));
config = JSON.parse(fs.readFileSync(configPath));
}
createWindow(config);
});

app.on('activate', function () {
if (mainWindow === null) {
createWindow(config);
} else {
createWindow(defaultConfig);
mainWindow.show();
}
});

Expand All @@ -99,6 +246,6 @@ app.on('window-all-closed', () => {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
// app.quit();
app.quit();
}
});
Loading

0 comments on commit 269299f

Please sign in to comment.