Skip to content

Commit

Permalink
Using requestSingleInstanceLock instead of makeSingleInstance as it i…
Browse files Browse the repository at this point in the history
…s deprecated in the latest beta.
  • Loading branch information
ShadyThGod committed Dec 17, 2018
1 parent 4396137 commit 151057c
Showing 1 changed file with 171 additions and 148 deletions.
319 changes: 171 additions & 148 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,17 @@ const url = require('url');
const path = require('path');
const fs = require('fs');

const { app, BrowserWindow, Menu, ipcMain, shell, Notification, globalShortcut, Tray, dialog } = electron;
const {
app,
BrowserWindow,
Menu,
ipcMain,
shell,
Notification,
globalShortcut,
Tray,
dialog
} = electron;

//Defining the window variables
let mainWindow;
Expand All @@ -13,177 +23,186 @@ let prefWindow;
let trayIcon;

//Make application single-instance only
var shouldQuit = app.makeSingleInstance(function(argv, dir) {
if (mainWindow) {
if (mainWindow.isMinimized()) {
mainWindow.restore();
}
mainWindow.focus();
}
});

if (shouldQuit) {
app.quit();
return;
}
const singleInstanceLock = app.requestSingleInstanceLock();

//App.on ready function
app.on('ready', function() {
mainWindow = new BrowserWindow({
title: 'Altus',
icon: "./img/icon.png"
}); //Creates the main window
mainWindow.maximize(); //Maximizing the main window always
mainWindow.loadURL(url.format({ //Loads the mainwindow html file
pathname: path.join(__dirname, 'windows', 'mainWindow.html'),
protocol: 'file:',
slashes: true
}));
const mainMenu = Menu.buildFromTemplate(mainMenuTemp); //Applies the main menu template
Menu.setApplicationMenu(mainMenu); //Sets the main menu
mainWindow.on('closed', function() { //Quits app when main window is closed
mainWindow = null
app.quit();
});

ipcMain.on('theme:change', function(e, data) { //Runs when the user wants to change the theme
mainWindow.webContents.send('theme:change', data);
themeWindow.close(); //Closes the theme window if open
if (!singleInstanceLock) {
app.quit()
} else {
app.on('second-instance', (e, cmd, pwd) => {
if (mainWindow) {
if (mainWindow.isMinimized()) mainWindow.restore()
mainWindow.focus()
}
});

ipcMain.on('linkOpen', function(e, data) {
shell.openExternal(data);
});
//App.on ready function
app.on('ready', function() {
mainWindow = new BrowserWindow({
title: 'Altus',
icon: "./img/icon.png"
}); //Creates the main window
mainWindow.maximize(); //Maximizing the main window always
mainWindow.loadURL(url.format({ //Loads the mainwindow html file
pathname: path.join(__dirname, 'windows', 'mainWindow.html'),
protocol: 'file:',
slashes: true
}));
const mainMenu = Menu.buildFromTemplate(mainMenuTemp); //Applies the main menu template
Menu.setApplicationMenu(mainMenu); //Sets the main menu
mainWindow.on('closed', function() { //Quits app when main window is closed
mainWindow = null
app.quit();
});

ipcMain.on('settingsChanged', function(e, f) {
mainWindow.webContents.send('settingsChanged', true);
prefWindow.close();
});
ipcMain.on('theme:change', function(e, data) { //Runs when the user wants to change the theme
mainWindow.webContents.send('theme:change', data);
themeWindow.close(); //Closes the theme window if open
});

globalShortcut.register('CmdOrCtrl+Shift+F12', function() {
var focusedWindow = BrowserWindow.getFocusedWindow();
focusedWindow.webContents.openDevTools();
});
ipcMain.on('linkOpen', function(e, data) {
shell.openExternal(data);
});

globalShortcut.register('CmdOrCtrl+Shift+R', function() {
var focusedWindow = BrowserWindow.getFocusedWindow();
focusedWindow.webContents.reload();
});
ipcMain.on('settingsChanged', function(e, f) {
mainWindow.webContents.send('settingsChanged', true);
prefWindow.close();
});

init();
globalShortcut.register('CmdOrCtrl+Shift+F12', function() {
var focusedWindow = BrowserWindow.getFocusedWindow();
focusedWindow.webContents.openDevTools();
});

ipcMain.on('notification-process-1', function(e, i) {
mainWindow.webContents.send('notification-process-1', i);
});
globalShortcut.register('CmdOrCtrl+Shift+R', function() {
var focusedWindow = BrowserWindow.getFocusedWindow();
focusedWindow.webContents.reload();
});

function init() {
var theme, persistTheme, toggleNotifications, toggleSound, toggleTray;
ipcMain.on('preferences', function(e, pref) {
theme = pref.theme || { name: 'default-theme', css: '' };
init();

if (typeof pref.toggleTray !== "undefined") {
toggleTray = pref.toggleTray;
} else {
toggleTray = true;
}
ipcMain.on('notification-process-1', function(e, i) {
mainWindow.webContents.send('notification-process-1', i);
});

if (typeof pref.persistTheme !== "undefined") {
persistTheme = pref.persistTheme;
} else {
persistTheme = true;
}
function init() {
var theme, persistTheme, toggleNotifications, toggleSound, toggleTray;
ipcMain.on('preferences', function(e, pref) {
theme = pref.theme || {
name: 'default-theme',
css: ''
};

if (typeof pref.toggleNotifications !== "undefined") {
toggleNotifications = pref.toggleNotifications;
} else {
toggleNotifications = true;
}
if (typeof pref.toggleTray !== "undefined") {
toggleTray = pref.toggleTray;
} else {
toggleTray = true;
}

if (typeof pref.toggleSound !== "undefined") {
toggleSound = pref.toggleSound;
} else {
toggleSound = true;
}
if (typeof pref.persistTheme !== "undefined") {
persistTheme = pref.persistTheme;
} else {
persistTheme = true;
}

if (theme.name == undefined || theme.css == undefined) {
theme.name = 'default-theme';
theme.css = '';
}
if (persistTheme == undefined) {
persistTheme = true;
}
if (persistTheme) setTheme();
setSound(toggleSound);
if (typeof pref.toggleNotifications !== "undefined") {
toggleNotifications = pref.toggleNotifications;
} else {
toggleNotifications = true;
}

if (toggleTray) {
setTray();
} else {
trayIcon.destroy();
trayIcon = null;
trayIcon = undefined;
}
});
if (typeof pref.toggleSound !== "undefined") {
toggleSound = pref.toggleSound;
} else {
toggleSound = true;
}

function setTray() {
//Tray icon
var trayIconPath = path.join(__dirname, 'img/icon.png');
const trayContextMenu = Menu.buildFromTemplate([{
label: 'Maximize',
click() {
if (mainWindow) {
mainWindow.show();
mainWindow.focus();
}
if (theme.name == undefined || theme.css == undefined) {
theme.name = 'default-theme';
theme.css = '';
}
}, {
label: 'Minimize to Tray',
click() {
mainWindow.hide();
if (persistTheme == undefined) {
persistTheme = true;
}
}, {
label: 'Exit',
click() {
dialog.showMessageBox({
type: 'question',
buttons: ["OK", "Cancel"],
title: "Exit",
message: "Are you sure you want to exit?"
}, function(res) {
if (res == 0) {
app.quit();
return;
if (persistTheme) setTheme();
setSound(toggleSound);

if (toggleTray) {
setTray();
} else {
trayIcon.destroy();
trayIcon = null;
trayIcon = undefined;
}
});

function setTray() {
//Tray icon
var trayIconPath = path.join(__dirname, 'img/icon.png');
const trayContextMenu = Menu.buildFromTemplate([{
label: 'Maximize',
click() {
if (mainWindow) {
mainWindow.show();
mainWindow.focus();
}
}
}, {
label: 'Minimize to Tray',
click() {
mainWindow.hide();
}
}, {
label: 'Exit',
click() {
dialog.showMessageBox({
type: 'question',
buttons: ["OK", "Cancel"],
title: "Exit",
message: "Are you sure you want to exit?"
}, function(res) {
if (res == 0) {
app.quit();
return;
}
});
}
}]);
if (process.platform !== "darwin") {
trayIcon = new Tray(trayIconPath);
trayIcon.setToolTip('Altus');
trayIcon.setContextMenu(trayContextMenu);
} else {
app.dock.setMenu(trayContextMenu);
}
}

function setTheme() {
if (theme.name == 'default-theme') {
mainWindow.webContents.send('theme:change', {
name: 'default-theme'
});
} else if (theme.name == 'dark-theme') {
mainWindow.webContents.send('theme:change', {
name: 'dark-theme'
});
} else {
mainWindow.webContents.send('theme:change', {
name: theme.name,
css: theme.css
});
}
}]);
if (process.platform !== "darwin") {
trayIcon = new Tray(trayIconPath);
trayIcon.setToolTip('Altus');
trayIcon.setContextMenu(trayContextMenu);
} else {
app.dock.setMenu(trayContextMenu);
}
}

function setTheme() {
if (theme.name == 'default-theme') {
mainWindow.webContents.send('theme:change', { name: 'default-theme' });
} else if (theme.name == 'dark-theme') {
mainWindow.webContents.send('theme:change', { name: 'dark-theme' });
} else {
mainWindow.webContents.send('theme:change', { name: theme.name, css: theme.css });
function setSound(toggleSound) {
mainWindow.webContents.send('muteSound', toggleSound);
}
}

function setSound(toggleSound) {
mainWindow.webContents.send('muteSound', toggleSound);
mainWindow.webContents.on('did-finish-load', function() {
mainWindow.webContents.send('sendPreferencesBool', true);
});
}

mainWindow.webContents.on('did-finish-load', function() {
mainWindow.webContents.send('sendPreferencesBool', true);
});
}
});
});
}

function createAboutWindow() {
if (typeof aboutWindow == 'object') {
Expand Down Expand Up @@ -272,14 +291,18 @@ const mainMenuTemp = [{ //The main menu template
label: 'Default Theme',
accelerator: 'CommandOrControl+Shift+D',
click() {
mainWindow.webContents.send('theme:change', { name: 'default-theme' });
mainWindow.webContents.send('theme:change', {
name: 'default-theme'
});
}
},
{
label: 'Dark/Night Theme',
accelerator: 'CommandOrControl+Shift+N',
click() {
mainWindow.webContents.send('theme:change', { name: 'dark-theme' });
mainWindow.webContents.send('theme:change', {
name: 'dark-theme'
});
}
}
]
Expand Down

0 comments on commit 151057c

Please sign in to comment.