diff --git a/app/lib/app.ts b/app/lib/app.ts index ccda0768..59d99175 100644 --- a/app/lib/app.ts +++ b/app/lib/app.ts @@ -1,6 +1,6 @@ import { app, ipcMain, Menu, Tray, shell } from 'electron' import { loadConfig } from './config' -import { Window } from './window' +import { Window, WindowOptions } from './window' export class Application { private tray: Tray @@ -20,8 +20,8 @@ export class Application { app.commandLine.appendSwitch('disable-http-cache') } - async newWindow (): Promise { - let window = new Window() + async newWindow (options?: WindowOptions): Promise { + let window = new Window(options) this.windows.push(window) window.visible$.subscribe(visible => { if (visible) { diff --git a/app/lib/cli.ts b/app/lib/cli.ts index 4d0b09a9..5d68ac64 100644 --- a/app/lib/cli.ts +++ b/app/lib/cli.ts @@ -28,6 +28,10 @@ export function parseArgs (argv, cwd) { describe: 'Show DevTools on start', type: 'boolean' }) + .option('hidden', { + describe: 'Start minimized', + type: 'boolean' + }) .option('version', { alias: 'v', describe: 'Show version and exit', diff --git a/app/lib/index.ts b/app/lib/index.ts index 54aad9f7..28fe3c18 100644 --- a/app/lib/index.ts +++ b/app/lib/index.ts @@ -59,5 +59,5 @@ app.on('ready', () => { } ])) } - application.newWindow() + application.newWindow({ hidden: argv.hidden }) }) diff --git a/app/lib/window.ts b/app/lib/window.ts index 884c2280..75926843 100644 --- a/app/lib/window.ts +++ b/app/lib/window.ts @@ -14,6 +14,10 @@ if (process.platform === 'win32') { DwmEnableBlurBehindWindow = require('windows-blurbehind').DwmEnableBlurBehindWindow } +export interface WindowOptions { + hidden?: boolean +} + export class Window { ready: Promise private visible = new Subject() @@ -23,14 +27,16 @@ export class Window { get visible$ (): Observable { return this.visible } - constructor () { + constructor (options?: WindowOptions) { let configData = loadConfig() + options = options || {} + this.windowConfig = new ElectronConfig({ name: 'window' }) this.windowBounds = this.windowConfig.get('windowBoundaries') let maximized = this.windowConfig.get('maximized') - let options: Electron.BrowserWindowConstructorOptions = { + let bwOptions: Electron.BrowserWindowConstructorOptions = { width: 800, height: 600, title: 'Terminus', @@ -41,33 +47,36 @@ export class Window { show: false, backgroundColor: '#00000000' } - Object.assign(options, this.windowBounds) + Object.assign(bwOptions, this.windowBounds) if ((configData.appearance || {}).frame === 'native') { - options.frame = true + bwOptions.frame = true } else { if (process.platform === 'darwin') { - options.titleBarStyle = 'hiddenInset' + bwOptions.titleBarStyle = 'hiddenInset' } } if (process.platform === 'linux') { - options.backgroundColor = '#131d27' + bwOptions.backgroundColor = '#131d27' } - this.window = new BrowserWindow(options) + this.window = new BrowserWindow(bwOptions) this.window.once('ready-to-show', () => { if (process.platform === 'darwin') { this.window.setVibrancy('dark') } else if (process.platform === 'win32' && (configData.appearance || {}).vibrancy) { this.setVibrancy(true) } - if (maximized) { - this.window.maximize() - } else { - this.window.show() + + if (!options.hidden) { + if (maximized) { + this.window.maximize() + } else { + this.window.show() + } + this.window.focus() } - this.window.focus() }) this.window.loadURL(`file://${app.getAppPath()}/dist/index.html?${this.window.id}`, { extraHeaders: 'pragma: no-cache\n' }) diff --git a/terminus-core/src/components/appRoot.component.ts b/terminus-core/src/components/appRoot.component.ts index 115483c0..09943e28 100644 --- a/terminus-core/src/components/appRoot.component.ts +++ b/terminus-core/src/components/appRoot.component.ts @@ -122,7 +122,7 @@ export class AppRootComponent { }) this.hostApp.secondInstance$.subscribe(() => { - this.onGlobalHotkey() + this.presentWindow() }) this.hotkeys.globalHotkey.subscribe(() => { this.onGlobalHotkey() @@ -166,28 +166,35 @@ export class AppRootComponent { onGlobalHotkey () { if (this.hostApp.getWindow().isFocused()) { - // focused - this.electron.loseFocus() - this.hostApp.getWindow().blur() - if (this.hostApp.platform !== Platform.macOS) { + this.hideWindow() + } else { + this.presentWindow() + } + } + + presentWindow () { + if (!this.hostApp.getWindow().isVisible()) { + // unfocused, invisible + this.hostApp.getWindow().show() + this.hostApp.getWindow().focus() + } else { + if (this.config.store.appearance.dock === 'off') { + // not docked, visible + setTimeout(() => { + this.hostApp.getWindow().focus() + }) + } else { + // docked, visible this.hostApp.getWindow().hide() } - } else { - if (!this.hostApp.getWindow().isVisible()) { - // unfocused, invisible - this.hostApp.getWindow().show() - this.hostApp.getWindow().focus() - } else { - if (this.config.store.appearance.dock === 'off') { - // not docked, visible - setTimeout(() => { - this.hostApp.getWindow().focus() - }) - } else { - // docked, visible - this.hostApp.getWindow().hide() - } - } + } + } + + hideWindow () { + this.electron.loseFocus() + this.hostApp.getWindow().blur() + if (this.hostApp.platform !== Platform.macOS) { + this.hostApp.getWindow().hide() } } diff --git a/terminus-core/src/services/hostApp.service.ts b/terminus-core/src/services/hostApp.service.ts index 33fe5e6c..f091b5c2 100644 --- a/terminus-core/src/services/hostApp.service.ts +++ b/terminus-core/src/services/hostApp.service.ts @@ -85,6 +85,8 @@ export class HostAppService { text = shellEscape([text]) } this.cliPaste.next(text) + } else { + this.secondInstance.next() } }))