add window option: always on top of other windows (#41)

* add window option: always on Top

* updated import path

* changes requested
This commit is contained in:
Tobirexy 2019-06-03 14:29:29 +02:00 committed by Kyle Spearrin
parent 38fc0432c3
commit 697e7ef632
2 changed files with 13 additions and 1 deletions

View File

@ -3,4 +3,5 @@ export class ElectronConstants {
static readonly enableCloseToTrayKey: string = 'enableCloseToTray'; static readonly enableCloseToTrayKey: string = 'enableCloseToTray';
static readonly enableTrayKey: string = 'enableTray'; static readonly enableTrayKey: string = 'enableTray';
static readonly enableStartToTrayKey: string = 'enableStartToTrayKey'; static readonly enableStartToTrayKey: string = 'enableStartToTrayKey';
static readonly enableAlwaysOnTopKey: string = 'enableAlwaysOnTopKey';
} }

View File

@ -1,9 +1,10 @@
import { app, BrowserWindow, screen } from 'electron'; import { app, BrowserWindow, screen } from 'electron';
import { ElectronConstants } from './electronConstants';
import * as path from 'path'; import * as path from 'path';
import * as url from 'url'; import * as url from 'url';
import { isDev, isMacAppStore, isSnapStore } from './utils'; import { isDev, isMacAppStore, isSnapStore } from './utils';
import { StorageService } from '../abstractions/storage.service'; import { StorageService } from '../abstractions/storage.service';
const WindowEventHandlingDelay = 100; const WindowEventHandlingDelay = 100;
@ -14,6 +15,7 @@ const Keys = {
export class WindowMain { export class WindowMain {
win: BrowserWindow; win: BrowserWindow;
isQuitting: boolean = false; isQuitting: boolean = false;
enableAlwaysOnTop: boolean = false;
private windowStateChangeTimer: NodeJS.Timer; private windowStateChangeTimer: NodeJS.Timer;
private windowStates: { [key: string]: any; } = {}; private windowStates: { [key: string]: any; } = {};
@ -84,6 +86,7 @@ export class WindowMain {
async createWindow(): Promise<void> { async createWindow(): Promise<void> {
this.windowStates[Keys.mainWindowSize] = await this.getWindowState(Keys.mainWindowSize, this.defaultWidth, this.windowStates[Keys.mainWindowSize] = await this.getWindowState(Keys.mainWindowSize, this.defaultWidth,
this.defaultHeight); this.defaultHeight);
this.enableAlwaysOnTop = await this.storageService.get<boolean>(ElectronConstants.enableAlwaysOnTopKey);
// Create the browser window. // Create the browser window.
this.win = new BrowserWindow({ this.win = new BrowserWindow({
@ -97,6 +100,7 @@ export class WindowMain {
icon: process.platform === 'linux' ? path.join(__dirname, '/images/icon.png') : undefined, icon: process.platform === 'linux' ? path.join(__dirname, '/images/icon.png') : undefined,
titleBarStyle: this.hideTitleBar && process.platform === 'darwin' ? 'hiddenInset' : undefined, titleBarStyle: this.hideTitleBar && process.platform === 'darwin' ? 'hiddenInset' : undefined,
show: false, show: false,
alwaysOnTop: this.enableAlwaysOnTop,
}); });
if (this.windowStates[Keys.mainWindowSize].isMaximized) { if (this.windowStates[Keys.mainWindowSize].isMaximized) {
@ -147,6 +151,13 @@ export class WindowMain {
this.win.on('move', () => { this.win.on('move', () => {
this.windowStateChangeHandler(Keys.mainWindowSize, this.win); this.windowStateChangeHandler(Keys.mainWindowSize, this.win);
}); });
}
async toggleAlwaysOnTop(){
this.enableAlwaysOnTop = !this.win.isAlwaysOnTop();
this.win.setAlwaysOnTop(this.enableAlwaysOnTop);
await this.storageService.save(ElectronConstants.enableAlwaysOnTopKey, this.enableAlwaysOnTop);
} }
private windowStateChangeHandler(configKey: string, win: BrowserWindow) { private windowStateChangeHandler(configKey: string, win: BrowserWindow) {