UNPKG

@sinotron/core

Version:

Simple framework for Typescript Electron projects

155 lines (154 loc) 4.56 kB
// noinspection JSUnusedGlobalSymbols import { screen } from 'electron'; import { DEVTOOLS_WIDTH_APPROX } from './values.js'; export class WinHelper { win; constructor(win) { this.win = win; } openDevTools(opts) { const mOpts = { mode: 'right', activate: true, ...opts }; let devtoolsOpenedListener = () => { if (this.win) { const currentBounds = this.win.getBounds(); const newWidth = currentBounds.width + DEVTOOLS_WIDTH_APPROX; this.setWidth(newWidth); } }; this.win.webContents.removeListener('devtools-opened', devtoolsOpenedListener); this.win.webContents.addListener('devtools-opened', devtoolsOpenedListener); let devtoolsClosedListener = () => { if (this.win) { const currentBounds = this.win.getBounds(); // Revert to the original application width const originalWidth = currentBounds.width - DEVTOOLS_WIDTH_APPROX; this.setWidth(originalWidth); } }; this.win.webContents.removeListener('devtools-closed', devtoolsClosedListener); this.win.webContents.addListener('devtools-closed', devtoolsClosedListener); this.win.webContents.openDevTools(mOpts); } /** * Set the window width. * * @param width */ setWidth(width) { const currentBounds = this.win.getBounds(); this.win.setBounds({ x: currentBounds.x, y: currentBounds.y, width: width, height: currentBounds.height }); return this; } /** * Set the window height. * * @param height */ setHeight(height) { const currentBounds = this.win.getBounds(); this.win.setBounds({ x: currentBounds.x, y: currentBounds.y, width: currentBounds.width, height: height }); return this; } /** * Set the window size. * * @param width * @param height */ setSize(width, height) { const currentBounds = this.win.getBounds(); this.win.setBounds({ x: currentBounds.x, y: currentBounds.y, width: width, height: height }); return this; } /** * Position the window in the center of the screen. */ centerWindow() { const bounds = this.win.getBounds(); const d = screen.getPrimaryDisplay(); const screenWidth = d.size.width; const screenHeight = d.size.height; const winWidth = bounds.width; const winHeight = bounds.height; const newWinX = (screenWidth - winWidth) / 2; const newWinY = (screenHeight - winHeight) / 2; this.win.setBounds({ x: newWinX, y: newWinY, width: winWidth, height: winHeight }); return this; } /** * Set the window size as percentage of screen * * @param widthPercent * @param heightPercent */ setSizePercent(widthPercent, heightPercent) { const d = screen.getPrimaryDisplay(); const displayWidth = d.size.width; const displayHeight = d.size.height; const width = (widthPercent / 100) * displayWidth; const height = (heightPercent / 100) * displayHeight; return this.setSize(width, height); } /** * Set window width as percentage of screen * * @param widthPercent */ setWidthPercent(widthPercent) { const d = screen.getPrimaryDisplay(); const width = (widthPercent / 100) * d.size.width; return this.setWidth(width); } /** * Set window height as percentage of screen * @param heightPercent */ setHeightPercent(heightPercent) { const d = screen.getPrimaryDisplay(); const height = (heightPercent / 100) * d.size.height; return this.setHeight(height); } /** * Broadcast a channel message using webContents.send(...) * * @param channelName * @param args */ sendChannelMessage(channelName, ...args) { this.win.webContents.send(channelName, args); } } /** * Get typed property of the global window object. * @param prop */ export function _windowProp(prop) { const win = window; if (typeof win[prop] !== 'undefined') { return win[prop]; } }