2020-01-03 15:01:15 +01:00
|
|
|
import fs from "fs";
|
|
|
|
import path from "path";
|
2020-01-03 22:47:10 +01:00
|
|
|
import {app, BrowserWindow, ipcMain, Menu, shell, Tray} from "electron";
|
2020-01-03 15:01:15 +01:00
|
|
|
|
|
|
|
import Config from "./Config";
|
2019-10-13 22:59:28 +02:00
|
|
|
|
|
|
|
const resourcesDir = path.resolve(__dirname, '../resources');
|
2020-01-03 15:01:15 +01:00
|
|
|
const iconPath = path.resolve(resourcesDir, 'logo.png');
|
2019-10-13 22:59:28 +02:00
|
|
|
|
|
|
|
const config = new Config();
|
|
|
|
|
2020-01-03 13:01:50 +01:00
|
|
|
const devMode = process.argv.length > 2 && process.argv[2] === '--dev';
|
|
|
|
|
2019-10-13 22:59:28 +02:00
|
|
|
let selectedService = 0;
|
|
|
|
|
2020-01-03 13:01:50 +01:00
|
|
|
let tray;
|
2019-10-13 22:59:28 +02:00
|
|
|
let window;
|
|
|
|
let addServiceWindow;
|
|
|
|
|
2020-01-03 13:01:50 +01:00
|
|
|
function toggleMainWindow() {
|
|
|
|
if (window != null) {
|
|
|
|
if (!window.isFocused()) {
|
|
|
|
window.show();
|
|
|
|
} else {
|
|
|
|
window.hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-13 22:59:28 +02:00
|
|
|
function createWindow() {
|
2020-01-03 13:01:50 +01:00
|
|
|
// System tray
|
2020-01-03 15:01:15 +01:00
|
|
|
tray = new Tray(iconPath);
|
2020-01-03 13:01:50 +01:00
|
|
|
tray.setToolTip('Tabs');
|
|
|
|
tray.setContextMenu(Menu.buildFromTemplate([
|
|
|
|
{label: 'Tabs', enabled: false},
|
|
|
|
{label: 'Open Tabs', click: () => window.show()},
|
|
|
|
{type: 'separator'},
|
|
|
|
{label: 'Quit', role: 'quit'}
|
|
|
|
]));
|
|
|
|
tray.on('click', () => toggleMainWindow());
|
|
|
|
|
2019-10-13 22:59:28 +02:00
|
|
|
// Create the browser window.
|
|
|
|
window = new BrowserWindow({
|
|
|
|
webPreferences: {
|
|
|
|
nodeIntegration: true,
|
|
|
|
enableRemoteModule: true,
|
|
|
|
webviewTag: true,
|
|
|
|
},
|
|
|
|
autoHideMenuBar: true,
|
2020-01-03 15:01:15 +01:00
|
|
|
icon: iconPath,
|
2019-10-13 22:59:28 +02:00
|
|
|
});
|
|
|
|
window.maximize();
|
|
|
|
window.on('closed', () => {
|
|
|
|
window = null;
|
|
|
|
});
|
|
|
|
|
2020-01-03 13:01:50 +01:00
|
|
|
if (devMode) {
|
2019-10-13 22:59:28 +02:00
|
|
|
window.webContents.openDevTools({
|
|
|
|
mode: 'right'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-01-03 22:47:10 +01:00
|
|
|
// Open external links in default OS browser
|
|
|
|
app.on('web-contents-created', (e, contents) => {
|
|
|
|
if (contents.getType() === 'webview') {
|
|
|
|
contents.on('new-window', (e, url) => {
|
|
|
|
e.preventDefault();
|
|
|
|
shell.openExternal(url);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2019-10-13 22:59:28 +02:00
|
|
|
|
|
|
|
// Sync services with navigation
|
|
|
|
window.webContents.on('dom-ready', sendServices);
|
|
|
|
|
|
|
|
// Load navigation view
|
|
|
|
window.loadFile(path.resolve(resourcesDir, 'index.html'))
|
|
|
|
.catch(console.error);
|
|
|
|
|
|
|
|
// Load active service
|
|
|
|
ipcMain.on('setActiveService', (event, index) => {
|
|
|
|
setActiveService(index);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Set a service's favicon
|
|
|
|
ipcMain.on('setServiceFavicon', (event, index, favicon) => {
|
|
|
|
config.services[index].favicon = favicon;
|
|
|
|
config.save();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Open add service window
|
|
|
|
ipcMain.on('openAddServiceWindow', () => {
|
|
|
|
if (!addServiceWindow) {
|
|
|
|
addServiceWindow = new BrowserWindow({
|
2020-01-03 13:01:50 +01:00
|
|
|
webPreferences: {
|
|
|
|
nodeIntegration: true,
|
|
|
|
enableRemoteModule: true,
|
|
|
|
webviewTag: true,
|
|
|
|
},
|
2019-10-13 22:59:28 +02:00
|
|
|
parent: window,
|
|
|
|
modal: true,
|
|
|
|
autoHideMenuBar: true,
|
|
|
|
});
|
|
|
|
addServiceWindow.on('close', () => {
|
|
|
|
addServiceWindow = null;
|
|
|
|
});
|
2020-01-03 13:01:50 +01:00
|
|
|
if (devMode) {
|
|
|
|
addServiceWindow.webContents.openDevTools({
|
|
|
|
mode: 'right'
|
|
|
|
});
|
|
|
|
}
|
2020-01-03 15:01:15 +01:00
|
|
|
addServiceWindow.webContents.on('dom-ready', () => {
|
|
|
|
addServiceWindow.webContents.send('syncIcons', listIcons('brands'), listIcons('solid'));
|
|
|
|
});
|
2019-10-13 22:59:28 +02:00
|
|
|
addServiceWindow.loadFile(path.resolve(resourcesDir, 'add-service.html'))
|
|
|
|
.catch(console.error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function sendServices() {
|
|
|
|
window.webContents.send('services', config.services, selectedService);
|
|
|
|
}
|
|
|
|
|
|
|
|
function setActiveService(index) {
|
|
|
|
selectedService = index;
|
|
|
|
}
|
|
|
|
|
2020-01-03 15:01:15 +01:00
|
|
|
function listIcons(set) {
|
|
|
|
const directory = path.resolve(resourcesDir, 'icons/' + set);
|
|
|
|
const icons = [];
|
|
|
|
fs.readdirSync(directory).forEach(i => icons.push(i.split('.svg')[0]));
|
|
|
|
return icons;
|
|
|
|
}
|
|
|
|
|
2019-10-13 22:59:28 +02:00
|
|
|
app.on('ready', createWindow);
|