Skip to content
This repository has been archived by the owner on Feb 1, 2022. It is now read-only.

Commit

Permalink
Hide to tray on minimize #27
Browse files Browse the repository at this point in the history
  • Loading branch information
Tustin committed Feb 18, 2019
1 parent 864d379 commit a58fe1d
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -3,5 +3,6 @@ dist/*
!dist/app.html
build/*
!build/installer.nsh
test/*
*.css
*.log
9 changes: 5 additions & 4 deletions package.json
Expand Up @@ -47,10 +47,11 @@
"compile": "npm run lint && tsc && sass ./assets/app.scss ./dist/app.css --style=compressed --no-source-map",
"watch": "tsc -w",
"start": "npm run compile && electron .",
"build-icons": "electron-icon-maker -i ./assets/images/logo.png -o ./build/",
"build-win": "npm run compile && npm run build-icons && electron-builder build --windows",
"build-mac": "npm run compile && npm run build-icons && electron-builder build --mac",
"release": "npm run compile && npm run build-icons && electron-builder build"
"build:icons": "electron-icon-maker -i ./assets/images/logo.png -o ./build/",
"build:win": "npm run compile && npm run build:icons && electron-builder build --windows",
"build:mac": "npm run compile && npm run build:icons && electron-builder build --mac",
"release": "npm run compile && npm run build:icons && electron-builder build",
"test": "npm run compile && npm run build:icons && electron-packager . --asar --overwrite --out=test && ./test/playstationdiscord-win32-x64/playstationdiscord.exe"
},
"repository": {
"type": "git",
Expand Down
60 changes: 56 additions & 4 deletions src/app.ts
@@ -1,4 +1,4 @@
import { app, BrowserWindow, dialog, ipcMain, nativeImage, shell } from 'electron';
import { app, BrowserWindow, dialog, ipcMain, nativeImage, shell, Tray, Menu, Notification } from 'electron';
import { IPresenceModel, IProfileModel } from './Model/ProfileModel';
import { IOAuthTokenCodeRequestModel, IOAuthTokenRefreshRequestModel, IOAuthTokenResponseModel, } from './Model/AuthenticationModel';
import { DiscordController, ps4ClientId, ps3ClientId, psVitaClientId } from './DiscordController';
Expand All @@ -21,7 +21,7 @@ const eventEmitter = new events.EventEmitter();

const sonyLoginUrl : string = 'https://id.sonyentertainmentnetwork.com/signin/?service_entity=urn:service-entity:psn&response_type=code&client_id=ba495a24-818c-472b-b12d-ff231c1b5745&redirect_uri=https://remoteplay.dl.playstation.net/remoteplay/redirect&scope=psn:clientapp&request_locale=en_US&ui=pr&service_logo=ps&layout_type=popup&smcid=remoteplay&PlatformPrivacyWs1=exempt&error=login_required&error_code=4165&error_description=User+is+not+authenticated#/signin?entry=%2Fsignin';

const logoIcon = nativeImage.createFromPath('./assets/images/logo.png');
const logoIcon = nativeImage.createFromPath(path.join(__dirname, '../assets/images/logo.png'));

let mainWindow : BrowserWindow = null;
let loginWindow : BrowserWindow = null;
Expand All @@ -37,6 +37,8 @@ if (!instanceLock)
app.quit();
}

app.setAppUserModelId(process.execPath);

function login(data: string) : Promise<IOAuthTokenResponseModel>
{
return new Promise<IOAuthTokenResponseModel>((resolve, reject) => {
Expand Down Expand Up @@ -163,6 +165,24 @@ function spawnLoginWindow() : void

function spawnMainWindow() : void
{
const contextMenu = Menu.buildFromTemplate([
{
label: 'Show Application',
click: () => mainWindow.show()
},
{
label: 'Quit',
click: () => {
mainWindow.destroy();
app.quit();
}
}
]);

const tray = new Tray(logoIcon);

tray.setContextMenu(contextMenu);

mainWindow = new BrowserWindow({
width: 512,
height: 512,
Expand All @@ -174,7 +194,7 @@ function spawnMainWindow() : void
webPreferences: {
nodeIntegration: true
},
frame: false
frame: false,
});

discordController = new DiscordController(ps4ClientId);
Expand Down Expand Up @@ -335,6 +355,38 @@ function spawnMainWindow() : void
mainWindow.on('closed', () => {
mainWindow = null;
});

mainWindow.on('minimize', () => {
if (process.platform !== 'win32')
{
return;
}

mainWindow.hide();

if (Notification.isSupported())
{
const notification = new Notification({
title: 'Still Here!',
body: 'PlayStationDiscord is still running in the tray. You can restore it by double clicking the icon in the tray.',
icon: logoIcon
});

notification.show();
}
else
{
log.warn('Tray notification not shown because notifications aren\'t supported on this platform', process.platform);
}

tray.on('double-click', () => {
if (!mainWindow.isVisible())
{
mainWindow.show();
mainWindow.focus();
}
});
});
}

function fetchProfile() : Promise<IProfileModel>
Expand Down Expand Up @@ -524,7 +576,7 @@ function toggleUpdateInfo(value: boolean) : void
}
}

app.on('second-instance', (event) => {
app.on('second-instance', () => {
if (!mainWindow)
{
return;
Expand Down

0 comments on commit a58fe1d

Please sign in to comment.