Skip to content

Commit

Permalink
- add 10 second shutdown timer
Browse files Browse the repository at this point in the history
- update ingame window
- other minor changes
  • Loading branch information
antonk777 committed Nov 11, 2021
1 parent 70020aa commit e5396b6
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 60 deletions.
8 changes: 7 additions & 1 deletion native/css/header.css
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,21 @@
.app-header h1 {
padding-left: 8px;
margin: 0;
color: #8d8d8d;
font-style: normal;
font-weight: normal;
color: #8d8d8d;
font-size: 14px;
cursor: inherit;
}

.app-header .hotkey-text {
margin: 0 auto;
padding-left: 8px;
color: #8d8d8d;
font-style: normal;
font-weight: normal;
font-size: 14px;
cursor: inherit;
}

.app-header .hotkey-text kbd {
Expand Down
15 changes: 3 additions & 12 deletions native/scripts/services/windows-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export class WindowsService {
/**
* Close a window
* @param {string} name
* @returns {Promise<any>}
* @returns {Promise<void>}
*/
static async close(name) {
const state = await WindowsService.getWindowState(name);
Expand All @@ -113,16 +113,7 @@ export class WindowsService {

const { window } = await WindowsService.obtainWindow(name);

return new Promise((resolve, reject) => {
overwolf.windows.close(window.id, result => {
if (result.success) {
resolve();
} else {
console.warn('WindowsService.close(): error:', name, result);
reject(new Error(result.error));
}
});
});
await new Promise(resolve => overwolf.windows.close(window.id, resolve));
}

/**
Expand Down Expand Up @@ -194,7 +185,7 @@ export class WindowsService {
return new Promise((resolve, reject) => {
overwolf.windows.getWindowsStates(state => {
if (state.success) {
resolve(state);
resolve(state.resultV2);
} else {
reject(state);
}
Expand Down
61 changes: 47 additions & 14 deletions native/windows/background/background-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export class BackgroundController {
this.owEventBus = new EventBus();
this.owEventsStore = [];
this.owInfoUpdatesStore = [];
this.shutdownTimeout = null;
this.hasMultipleMonitors = null;
}

Expand Down Expand Up @@ -107,10 +108,10 @@ export class BackgroundController {
}

// Open in-game window
this._restoreGameWindow();
await this._restoreGameWindow();

// Close desktop window
WindowsService.close(kWindowNames.DESKTOP);
await WindowsService.close(kWindowNames.DESKTOP);
}

/**
Expand Down Expand Up @@ -141,7 +142,7 @@ export class BackgroundController {

// If app was not launched automatically, restore the a relevant game window
if (!BackgroundController._launchedWithGameEvent()) {
this._restoreGameWindow();
await this._restoreGameWindow();
}
}

Expand All @@ -153,9 +154,9 @@ export class BackgroundController {
const isGameRunning = await this.runningGameService.isGameRunning();

if (isGameRunning) {
this._restoreGameWindow();
await this._restoreGameWindow();
} else {
WindowsService.restore(kWindowNames.DESKTOP);
await WindowsService.restore(kWindowNames.DESKTOP);
}
}

Expand All @@ -178,23 +179,55 @@ export class BackgroundController {
* @private
*/
async _onWindowStateChanged() {
if (await this._canShutdown()) {
this._startShutdownTimeout();
} else {
this._stopShutdownTimeout();
}
}

/**
* Check whether we can safely close the app
* @private
*/
async _canShutdown() {
const isGameRunning = await this.runningGameService.isGameRunning();

// Never shut down the app when a game is running
if (isGameRunning) {
return;
return false;
}

const { resultV2: states } = await WindowsService.getWindowsStates();
const states = await WindowsService.getWindowsStates();

// If all UI (non-background) windows are closed, close the app
const shouldClose = Object.entries(states)
// If all UI (non-background) windows are closed, we can close the app
return Object.entries(states)
.filter(([windowName]) => (windowName !== kWindowNames.BACKGROUND))
.every(([windowName, windowState]) => (windowState === 'closed'));
}

// Close the whole app
if (shouldClose) {
window.close();
/**
* Start shutdown timeout, and close after 10 if possible
* @private
*/
_startShutdownTimeout() {
this._stopShutdownTimeout();

this.shutdownTimeout = setTimeout(async () => {
if (await this._canShutdown()) {
window.close(); // Close the whole app
}
}, 10000);
}

/**
* Stop shutdown timeout
* @private
*/
_stopShutdownTimeout() {
if (this.shutdownTimeout !== null) {
clearTimeout(this.shutdownTimeout);
this.shutdownTimeout = null;
}
}

Expand All @@ -217,7 +250,7 @@ export class BackgroundController {
* @private
*/
async _onHotkeyTogglePressed() {
const { resultV2: states } = await WindowsService.getWindowsStates();
const states = await WindowsService.getWindowsStates();

switch (states[kWindowNames.SECOND]) {
case 'normal':
Expand All @@ -240,7 +273,7 @@ export class BackgroundController {
* @private
*/
async _onHotkeySecondScreenPressed() {
const { resultV2: states } = await WindowsService.getWindowsStates();
const states = await WindowsService.getWindowsStates();

switch (states[kWindowNames.IN_GAME]) {
case 'normal':
Expand Down
62 changes: 53 additions & 9 deletions native/windows/in-game/in-game-controller.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,81 @@
import { InGameView } from '../../windows/in-game/in-game-view.js';
import { HotkeysService } from '../../scripts/services/hotkeys-service.js';
import { RunningGameService } from '../../scripts/services/running-game-service.js';
import { kHotkeyToggle } from '../../scripts/constants/hotkeys-ids.js';
import { kHotkeySecondScreen, kHotkeyToggle } from '../../scripts/constants/hotkeys-ids.js';

export class InGameController {
constructor() {
this.inGameView = new InGameView();
this.hotkeysService = new HotkeysService();
this.runningGameService = new RunningGameService();

this._eventListenerBound = this._eventListener.bind(this);

this.owEventBus = null;
}

run() {
// listen to events from the event bus from the main window,
// the callback will be run in the context of the current window
// Get the event bus instance from the background window
const { owEventBus } = overwolf.windows.getMainWindow();

owEventBus.addListener(this._eventListenerBound);
this.owEventBus = owEventBus;

this._readStoredData();

// This callback will run in the context of the current window
this.owEventBus.addListener(this._eventListenerBound);

// Update hotkey view and listen to changes:
this._updateHotkey();
this.hotkeysService.addHotkeyChangeListener(() => this._updateHotkey());

this._addBeforeCloseListener();
}

/**
* This removes in-game window's listener from the event bus when the window
* closes
*/
_addBeforeCloseListener() {
window.addEventListener('beforeunload', e => {
delete e.returnValue;

this.owEventBus.removeListener(this._eventListenerBound);
});
}

/**
* Read & render events and info updates that happened before this was opened
*/
_readStoredData() {
const {
owEventsStore,
owInfoUpdatesStore
} = overwolf.windows.getMainWindow();

owEventsStore.forEach(v => this._gameEventHandler(v));
owInfoUpdatesStore.forEach(v => this._infoUpdateHandler(v));
}

async _updateHotkey() {
const gameInfo = await this.runningGameService.getRunningGameInfo();

const hotkey = await this.hotkeysService.getHotkey(
kHotkeyToggle,
gameInfo.classId
);
const [
hotkeyToggle,
hotkeySecondScreen
] = await Promise.all([
this.hotkeysService.getHotkey(
kHotkeyToggle,
gameInfo.classId
),
this.hotkeysService.getHotkey(
kHotkeySecondScreen,
gameInfo.classId
)
]);

this.inGameView.updateHotkey(hotkey);
this.inGameView.updateToggleHotkey(hotkeyToggle);
this.inGameView.updateSecondHotkey(hotkeySecondScreen);
}

_eventListener(eventName, eventValue) {
Expand Down
24 changes: 12 additions & 12 deletions native/windows/in-game/in-game-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,11 @@ export class InGameView extends SampleAppView {
this._infoLog = document.getElementById('infoLog');
this._copyEventsButton = document.getElementById('copyEvents');
this._copyInfoButton = document.getElementById('copyInfo');
this._hotkey = document.getElementById('hotkey');
this._hotkeyToggle = document.getElementById('hotkey-toggle');
this._hotkeySecondScreen = document.getElementById('hotkey-second-screen');

this.logEvent = this.logEvent.bind(this);
this.logInfoUpdate = this.logInfoUpdate.bind(this);
this.updateHotkey = this.updateHotkey.bind(this);
this._copyEventsLog = this._copyEventsLog.bind(this);
this._copyInfoLog = this._copyInfoLog.bind(this);

this._copyEventsButton.addEventListener('click', this._copyEventsLog);
this._copyInfoButton.addEventListener('click', this._copyInfoLog);
this._copyEventsButton.addEventListener('click', e => this._copyEventsLog(e));
this._copyInfoButton.addEventListener('click', e => this._copyInfoLog(e));
}

// -- Public --
Expand All @@ -31,9 +26,14 @@ export class InGameView extends SampleAppView {
this._logLine(this._infoLog, string, isHighlight);
}

// Update hotkey header
updateHotkey(hotkey) {
this._hotkey.textContent = hotkey;
// Update toggle hotkey header
updateToggleHotkey(hotkey) {
this._hotkeyToggle.textContent = hotkey;
}

// Update second screen hotkey header
updateSecondHotkey(hotkey) {
this._hotkeySecondScreen.textContent = hotkey;
}

// -- Private --
Expand Down
10 changes: 7 additions & 3 deletions native/windows/in-game/in-game.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@
<header class="app-header">
<img src="../../img/header_icon.svg" />
<h1>Sample App / in-game window</h1>
<h1 class="hotkey-text">
<div class="hotkey-text">
Show/Hide Hotkey:
<kbd id="hotkey"></kbd>
</h1>
<kbd id="hotkey-toggle"></kbd>
</div>
<div class="hotkey-text">
Toggle Second Screen Hotkey:
<kbd id="hotkey-second-screen"></kbd>
</div>
<div class="window-controls-group">
<button id="minimizeButton" class="window-control window-control-minimize" />
<button id="closeButton" class="window-control window-control-close" />
Expand Down
12 changes: 8 additions & 4 deletions native/windows/second/second-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ export class SecondController {
this.secondView.updateSecondHotkey(hotkeySecondScreen);
}

/**
* Position & center this window on a secondary monitor
*/
async _positionWindow() {
const [
{ window },
Expand Down Expand Up @@ -133,10 +136,11 @@ export class SecondController {
});
}

await Promise.all([
WindowsService.maximize(kWindowNames.SECOND),
WindowsService.setTopmost(kWindowNames.SECOND, true)
]);
if (window.stateEx !== 'maximized') {
await WindowsService.maximize(kWindowNames.SECOND);
}

await WindowsService.setTopmost(kWindowNames.SECOND, true);
}

_eventListener(eventName, eventValue) {
Expand Down
10 changes: 5 additions & 5 deletions native/windows/second/second.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
<header class="app-header">
<img src="../../img/header_icon.svg" />
<h1>Sample App / second screen window</h1>
<h1 class="hotkey-text">
<div class="hotkey-text">
Show/Hide Hotkey:
<kbd id="hotkey-toggle"></kbd>
</h1>
<h1 class="hotkey-text">
Toggle Second screen Hotkey:
</div>
<div class="hotkey-text">
Toggle Second Screen Hotkey:
<kbd id="hotkey-second-screen"></kbd>
</h1>
</div>
<div class="window-controls-group">
<button id="minimizeButton" class="window-control window-control-minimize" />
<button id="maximizeButton" class="window-control window-control-maximize" />
Expand Down

0 comments on commit e5396b6

Please sign in to comment.