Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
panchilo committed Jul 24, 2018
1 parent bc9fba6 commit 50f5a7d
Show file tree
Hide file tree
Showing 14 changed files with 284 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.DS_Store
.eslintcache
.cache
node_modules
out
dist
npm-debug.log
Binary file added assets/offline.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 assets/[email protected]
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 assets/[email protected]
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 assets/online.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 assets/[email protected]
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 assets/[email protected]
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 build/icon.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 build/[email protected]
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 build/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Electron Hello Application</title>
<link rel="stylesheet" type="text/css" media="screen" href="main.css" />
</head>
<body>
<div class="main-container">
<div class="top"></div>
<div class="bottom">Bottom</div>
</div>

<script>
const {ipcRenderer} = require('electron');
const updateOnlineStatus = () => {
ipcRenderer.send('online-status-changed', navigator.onLine ? 'online' : 'offline');
}

window.addEventListener('online', updateOnlineStatus);
window.addEventListener('offline', updateOnlineStatus);

updateOnlineStatus();
</script>
</body>
</html>
30 changes: 30 additions & 0 deletions main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
html {
margin: 0;
padding: 0;
}

body {
margin: 0;
padding: 0;
}

.main-container {
display: flex;
flex-direction: column;

height: 100vh;
}

.main-container .top {
-webkit-app-region: drag;
-webkit-user-select: none;

height: 24px;

background-color: darkviolet;
}

.main-container .bottom {
flex-grow: 1;
background-color: white;
}
181 changes: 181 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
const { app, shell, clipboard, nativeImage, Menu, Tray, BrowserWindow, ipcMain } = require("electron");
const fetch = require("electron-fetch");
const path = require('path');

const onlineImagePath = path.join(process.resourcesPath, "online.png");
const offlineImagePath = path.join(process.resourcesPath, "offline.png");

const INITIALIZATION_DELAY = 3000;
const MENU_ACTION_DELAY = 200;
const REFRESH_DELAY = 800;

let onlineImage = nativeImage.createFromPath(onlineImagePath);
let offlineImage = nativeImage.createFromPath(offlineImagePath);
let initialized = false;
let showStatus = true;
let publicIP = null;

let mainWindow = null;
let tray = null;
let trayMenu = [
{
label: 'Refreshing',
icon: offlineImage,
enabled: false
},
{
label: 'Copy IP Address to Clipboard',
click: () => {
copyToClipboard();
},
visible: false
},
{
label: 'Refresh',
click: () => {
setTimeout(() => {
getIpAddress();
}, MENU_ACTION_DELAY);
},
enabled: false
},
{
label: 'Launch on System Startup',
enabled: false
},
{
label: 'Show Status in Menu Bar',
type: 'checkbox',
checked: showStatus,
click: () => {
setTimeout(() => {
showStatus = !showStatus;
toggleStatus();
}, MENU_ACTION_DELAY);
}
},
{type: 'separator'},
{
label: 'About PubIP',
click: () => {
mainWindow.show();
}
},
{
label: 'Check for Updates...',
enabled: false
},
{type: 'separator'},
{
label: 'Quit Public IP Tray',
click: () => {
app.quit();
}
}
];

function createTray() {
tray = new Tray(nativeImage.createEmpty());
}

function createWindow () {
mainWindow = new BrowserWindow({
modal: true,
width: 430,
height: 270,
// resizable: false,
maximizable: false,
minimizable: false,
alwaysOnTop: true,
center: true,
show: false,
titleBarStyle: "hidden"
});
mainWindow.loadFile("index.html");
// mainWindow.loadURL("https://www.google.com/");

// mainWindow.addEventListener('beforeunload', (e) => {
// e.returnValue = true;
// });

mainWindow.on("close", (e) => {
mainWindow.hide();
e.preventDefault();
});
}

function setInitialized() {
setTimeout(() => {
initialized = true;
}, INITIALIZATION_DELAY);
}

function copyToClipboard() {
if (publicIP) {
clipboard.writeText(publicIP);
}
}

function getIpAddress() {
toggleStatus("refreshing");
setTimeout(() => {
fetch.default("https://api.ipify.org?format=json")
.then(res => res.json())
.then(json => {
publicIP = json.ip
toggleStatus("online");
setInitialized();
})
.catch(() => {
publicIP = null;
toggleStatus("offline");
setInitialized();
});
}, REFRESH_DELAY);
}

function toggleStatus(status) {
if (status === "online") {
tray.setTitle(publicIP);
tray.setImage(onlineImage);
trayMenu[0].label = `Online: ${publicIP}`;
trayMenu[0].icon = onlineImage;
trayMenu[1].visible = true;
trayMenu[2].enabled = true;
} else if (status === "refreshing") {
tray.setTitle("Refreshing...");
tray.setImage(offlineImage);
trayMenu[0].label = `Refreshing`;
trayMenu[0].icon = offlineImage;
trayMenu[1].visible = false;
trayMenu[2].enabled = false;
} else if (status === "offline") {
tray.setTitle("Offline");
tray.setImage(offlineImage);
trayMenu[0].label = `Offline`;
trayMenu[0].icon = offlineImage;
trayMenu[1].visible = false;
trayMenu[2].enabled = true;
} else {
trayMenu[4].checked = showStatus;
tray.setTitle(showStatus ? publicIP || "Offline" : "");
}
tray.setContextMenu(Menu.buildFromTemplate(trayMenu));
}

app.on("ready", (x) => {
// Hide app dock icon.
app.dock.hide();
// Create Renderer Window
createWindow();
// Create System Tray entry and menu
createTray();
// Retrieve IP Address
getIpAddress();
});

ipcMain.on("online-status-changed", (event, status) => {
if (initialized) {
getIpAddress();
}
});
40 changes: 40 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "public-ip-tray",
"version": "1.0.0",
"description": "Shows Public IP Address in your System Tray",
"main": "main.js",
"scripts": {
"start": "electron .",
"pack": "electron-builder --dir",
"dist": "electron-builder"
},
"author": {
"name": "Blue Alba",
"email": "[email protected]"
},
"license": "MIT",
"build": {
"appId": "com.bluealba.public-ip-tray",
"productName": "Public IP Tray",
"copyright": "Copyright © 2018 ${author}",
"extraFiles": [
{
"from": "assets",
"to": "resources",
"filter": [ "**/*" ]
}
],
"mac": {
"category": "public.app-category.productivity",
"target": "dmg",
"icon": "build/icon.png"
}
},
"dependencies": {
"electron-fetch": "^1.2.1"
},
"devDependencies": {
"electron": "^2.0.5",
"electron-builder": "^20.24.4"
}
}

0 comments on commit 50f5a7d

Please sign in to comment.