@sinotron/core
Version:
Simple framework for Typescript Electron projects
101 lines (100 loc) • 2.81 kB
JavaScript
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;
}
/**
* 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];
}
}