Skip to content

Commit

Permalink
"Restore" window when launching from tray to prevent blank window fro…
Browse files Browse the repository at this point in the history
…m opening?
  • Loading branch information
anther committed Oct 3, 2018
1 parent 621ad2d commit 7ffb277
Showing 1 changed file with 109 additions and 115 deletions.
224 changes: 109 additions & 115 deletions app/main.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,22 @@
*
* @flow
*/
import { app, BrowserWindow, shell, ipcMain, Menu, Tray, nativeImage } from 'electron';
import {
app,
BrowserWindow,
shell,
ipcMain,
Menu,
Tray,
nativeImage
} from 'electron';
import { autoUpdater } from 'electron-updater';
import _ from 'lodash';
import AutoLaunch from 'auto-launch';
import electronSettings from 'electron-settings';
import path from "path";
import path from 'path';
import Files from './utils/Files';


let mainWindow = null;

const LAUNCH_AT_STARTUP_KEY = 'launchAtStartup';
Expand Down Expand Up @@ -96,35 +103,30 @@ if (isSecondInstance) {
app.quit();
}


let tray = null;
let debugTray = { send: () => {} };
app.on('ready', async () => {
const imagePath = Files.createApplicationPath('./external/android-icon-36x36.png');
const trayImage = nativeImage.createFromPath(imagePath);
ipcMain.on('debugTray', (event) => {
debugTray = event.sender;
event.sender.send('debugTray', imagePath);
});

const imagePath = Files.createApplicationPath(
'./external/android-icon-36x36.png'
);
const trayImage = nativeImage.createFromPath(imagePath);
ipcMain.on('debugTray', event => {
debugTray = event.sender;
event.sender.send('debugTray', imagePath);
});

tray = new Tray(trayImage);
updateTray();
tray = new Tray(trayImage);
updateTray();

tray.on('click', () => {
if(mainWindow.isMinimized())
{
mainWindow.show();
}
else
{
mainWindow.show();
}
});
tray.on('double-click', () => {
mainWindow.show();
});
tray.setToolTip('SmashLadder Dolphin Launcher');
tray.on('click', () => {
mainWindow.show();
mainWindow.restore();
mainWindow.focus();
});
tray.on('double-click', () => {
mainWindow.show();
});
tray.setToolTip('SmashLadder Dolphin Launcher');
});
app.on('ready', async () => {
if (
Expand All @@ -148,115 +150,107 @@ app.on('ready', async () => {
shell.openExternal(url);
});

mainWindow.on('minimize', (event) => {
event.preventDefault();
mainWindow.hide();
mainWindow.on('minimize', event => {
event.preventDefault();
mainWindow.hide();
});

mainWindow.webContents.on('did-finish-load', () => {
if (!mainWindow) {
throw new Error('"mainWindow" is not defined');
}
const autoLaunched = (process.argv || []).indexOf('--hidden') !== -1;
if (process.env.START_MINIMIZED) {
if (process.env.START_MINIMIZED) {
mainWindow.minimize();
} else if(!autoLaunched){
} else if (!autoLaunched) {
mainWindow.show();
mainWindow.focus();
} if(autoLaunched){
}
if (autoLaunched) {
mainWindow.hide();
mainWindow.minimize();
}

});

mainWindow.on('closed', () => {
mainWindow = null;
});

});


const setupAutoLaunch = async () => {
const autoLaunch = new AutoLaunch({
name: 'SmashLadder Dolphin Launcher',
path: app.getPath('exe'),
isHidden: true,
});
let launchAtStartup = await electronSettings.get(LAUNCH_AT_STARTUP_KEY);
if(launchAtStartup === undefined)
{
try{
const isEnabled = await autoLaunch.isEnabled();
if(!isEnabled)
{
autoLaunch.enable();
launchAtStartup = true;
electronSettings.set(LAUNCH_AT_STARTUP_KEY, launchAtStartup);
}
}
catch (error) {
console.error(error);
}
}
return {
autoLaunch,
launchAtStartup,
};
const autoLaunch = new AutoLaunch({
name: 'SmashLadder Dolphin Launcher',
path: app.getPath('exe'),
isHidden: true
});
let launchAtStartup = await electronSettings.get(LAUNCH_AT_STARTUP_KEY);
if (launchAtStartup === undefined) {
try {
const isEnabled = await autoLaunch.isEnabled();
if (!isEnabled) {
autoLaunch.enable();
launchAtStartup = true;
electronSettings.set(LAUNCH_AT_STARTUP_KEY, launchAtStartup);
}
} catch (error) {
console.error(error);
}
}
return {
autoLaunch,
launchAtStartup
};
};

const updateTray = async () => {
const { autoLaunch, launchAtStartup } = await setupAutoLaunch();
const contextMenu = Menu.buildFromTemplate([
{
label: app.getVersion(),
enabled: false,
},
{
type: 'separator',
},
{
label: 'Launch At Startup',
checked: !!launchAtStartup,
type: 'checkbox',
click: () => {
debugTray.send('debugTray', `launch at startup ${launchAtStartup}`);
if(launchAtStartup)
{
autoLaunch.disable()
.then(()=>{
electronSettings.set(LAUNCH_AT_STARTUP_KEY, false);
updateTray();
})
.catch((error)=>{
if(debugTray)
{
debugTray.send('debugTray', error);
}
});
}
else
{
autoLaunch.enable()
.then(()=>{
electronSettings.set(LAUNCH_AT_STARTUP_KEY, true);
updateTray();
})
.catch((error)=>{
if(debugTray)
{
debugTray.send('debugTray', error);
}
});
}
}
},
{
label: 'Quit',
click: () => {
app.quit();
}
}
]);
tray.setContextMenu(contextMenu);
};
const { autoLaunch, launchAtStartup } = await setupAutoLaunch();
const contextMenu = Menu.buildFromTemplate([
{
label: app.getVersion(),
enabled: false
},
{
type: 'separator'
},
{
label: 'Launch At Startup',
checked: !!launchAtStartup,
type: 'checkbox',
click: () => {
debugTray.send('debugTray', `launch at startup ${launchAtStartup}`);
if (launchAtStartup) {
autoLaunch
.disable()
.then(() => {
electronSettings.set(LAUNCH_AT_STARTUP_KEY, false);
updateTray();
})
.catch(error => {
if (debugTray) {
debugTray.send('debugTray', error);
}
});
} else {
autoLaunch
.enable()
.then(() => {
electronSettings.set(LAUNCH_AT_STARTUP_KEY, true);
updateTray();
})
.catch(error => {
if (debugTray) {
debugTray.send('debugTray', error);
}
});
}
}
},
{
label: 'Quit',
click: () => {
app.quit();
}
}
]);
tray.setContextMenu(contextMenu);
};

0 comments on commit 7ffb277

Please sign in to comment.