implement BrowserWindow-API functions

This commit is contained in:
Gregor Biswanger
2017-10-16 16:53:35 +02:00
parent 828963ae9f
commit c5229d1d00
12 changed files with 1358 additions and 21 deletions

View File

@@ -1,7 +1,13 @@
import { BrowserWindow } from "electron";
import { BrowserWindow, Menu } from "electron";
const windows: Electron.BrowserWindow[] = []
module.exports = (socket: SocketIO.Server) => {
socket.on('register-browserWindow-ready-to-show', (id) => {
getWindowById(id).on('ready-to-show', () => {
socket.emit('browserWindow-ready-to-show');
});
});
socket.on('createBrowserWindow', (options, loadUrl) => {
let window = new BrowserWindow(options);
@@ -13,6 +19,10 @@ module.exports = (socket: SocketIO.Server) => {
} catch (error) {
if (error.message === 'Object has been destroyed') {
windows.splice(index, 1);
const ids = [];
windows.forEach(x => ids.push(x.id));
socket.emit('BrowserWindowClosed', ids);
}
}
}
@@ -222,6 +232,254 @@ module.exports = (socket: SocketIO.Server) => {
getWindowById(id).setMaximizable(maximizable);
});
socket.on('browserWindow-isMaximizable', (id) => {
const maximizable = getWindowById(id).isMaximizable();
socket.emit('browserWindow-isMaximizable-completed', maximizable);
});
socket.on('browserWindow-setFullScreenable', (id, fullscreenable) => {
getWindowById(id).setFullScreenable(fullscreenable);
});
socket.on('browserWindow-isFullScreenable', (id) => {
const fullscreenable = getWindowById(id).isFullScreenable();
socket.emit('browserWindow-isFullScreenable-completed', fullscreenable);
});
socket.on('browserWindow-setClosable', (id, closable) => {
getWindowById(id).setClosable(closable);
});
socket.on('browserWindow-isClosable', (id) => {
const closable = getWindowById(id).isClosable();
socket.emit('browserWindow-isClosable-completed', closable);
});
socket.on('browserWindow-setAlwaysOnTop', (id, flag, level, relativeLevel) => {
getWindowById(id).setAlwaysOnTop(flag, level, relativeLevel);
});
socket.on('browserWindow-isAlwaysOnTop', (id) => {
const isAlwaysOnTop = getWindowById(id).isAlwaysOnTop();
socket.emit('browserWindow-isAlwaysOnTop-completed', isAlwaysOnTop);
});
socket.on('browserWindow-center', (id) => {
getWindowById(id).center();
});
socket.on('browserWindow-setPosition', (id, x, y, animate) => {
getWindowById(id).setPosition(x, y, animate);
});
socket.on('browserWindow-getPosition', (id) => {
const position = getWindowById(id).getPosition();
socket.emit('browserWindow-getPosition-completed', position);
});
socket.on('browserWindow-setTitle', (id, title) => {
getWindowById(id).setTitle(title);
});
socket.on('browserWindow-getTitle', (id) => {
const title = getWindowById(id).getTitle();
socket.emit('browserWindow-getTitle-completed', title);
});
socket.on('browserWindow-setTitle', (id, title) => {
getWindowById(id).setTitle(title);
});
socket.on('browserWindow-setSheetOffset', (id, offsetY, offsetX) => {
if(offsetX) {
getWindowById(id).setSheetOffset(offsetY, offsetX);
} else {
getWindowById(id).setSheetOffset(offsetY);
}
});
socket.on('browserWindow-flashFrame', (id, flag) => {
getWindowById(id).flashFrame(flag);
});
socket.on('browserWindow-setSkipTaskbar', (id, skip) => {
getWindowById(id).setSkipTaskbar(skip);
});
socket.on('browserWindow-setKiosk', (id, flag) => {
getWindowById(id).setKiosk(flag);
});
socket.on('browserWindow-isKiosk', (id) => {
const isKiosk = getWindowById(id).isKiosk();
socket.emit('browserWindow-isKiosk-completed', isKiosk);
});
socket.on('browserWindow-setRepresentedFilename', (id, filename) => {
getWindowById(id).setRepresentedFilename(filename);
});
socket.on('browserWindow-getRepresentedFilename', (id) => {
const pathname = getWindowById(id).getRepresentedFilename();
socket.emit('browserWindow-getRepresentedFilename-completed', pathname);
});
socket.on('browserWindow-setDocumentEdited', (id, edited) => {
getWindowById(id).setDocumentEdited(edited);
});
socket.on('browserWindow-isDocumentEdited', (id) => {
const edited = getWindowById(id).isDocumentEdited();
socket.emit('browserWindow-isDocumentEdited-completed', edited);
});
socket.on('browserWindow-focusOnWebView', (id) => {
getWindowById(id).focusOnWebView();
});
socket.on('browserWindow-blurWebView', (id) => {
getWindowById(id).blurWebView();
});
socket.on('browserWindow-loadURL', (id, url, options) => {
getWindowById(id).loadURL(url, options);
});
socket.on('browserWindow-reload', (id) => {
getWindowById(id).reload();
});
socket.on('browserWindow-setMenu', (id, menuItems) => {
let menu = null;
if(menuItems) {
menu = Menu.buildFromTemplate(menuItems);
addMenuItemClickConnector(menu.items, (id) => {
socket.emit("windowMenuItemClicked", id);
});
}
getWindowById(id).setMenu(menu);
});
function addMenuItemClickConnector(menuItems, callback) {
menuItems.forEach((item) => {
if(item.submenu && item.submenu.items.length > 0) {
addMenuItemClickConnector(item.submenu.items, callback);
}
if("id" in item && item.id) {
item.click = () => { callback(item.id); };
}
});
}
socket.on('browserWindow-setProgressBar', (id, progress) => {
getWindowById(id).setProgressBar(progress);
});
socket.on('browserWindow-setHasShadow', (id, hasShadow) => {
getWindowById(id).setHasShadow(hasShadow);
});
socket.on('browserWindow-hasShadow', (id) => {
const hasShadow = getWindowById(id).hasShadow();
socket.emit('browserWindow-hasShadow-completed', hasShadow);
});
socket.on('browserWindow-setAppDetails', (id, options) => {
getWindowById(id).setAppDetails(options);
});
socket.on('browserWindow-showDefinitionForSelection', (id) => {
getWindowById(id).showDefinitionForSelection();
});
socket.on('browserWindow-setAutoHideMenuBar', (id, hide) => {
getWindowById(id).setAutoHideMenuBar(hide);
});
socket.on('browserWindow-isMenuBarAutoHide', (id) => {
const isMenuBarAutoHide = getWindowById(id).isMenuBarAutoHide();
socket.emit('browserWindow-isMenuBarAutoHide-completed', isMenuBarAutoHide);
});
socket.on('browserWindow-setMenuBarVisibility', (id, visible) => {
getWindowById(id).setMenuBarVisibility(visible);
});
socket.on('browserWindow-isMenuBarVisible', (id) => {
const isMenuBarVisible = getWindowById(id).isMenuBarVisible();
socket.emit('browserWindow-isMenuBarVisible-completed', isMenuBarVisible);
});
socket.on('browserWindow-setVisibleOnAllWorkspaces', (id, visible) => {
getWindowById(id).setVisibleOnAllWorkspaces(visible);
});
socket.on('browserWindow-isVisibleOnAllWorkspaces', (id) => {
const isVisibleOnAllWorkspaces = getWindowById(id).isVisibleOnAllWorkspaces();
socket.emit('browserWindow-isVisibleOnAllWorkspaces-completed', isVisibleOnAllWorkspaces);
});
socket.on('browserWindow-setIgnoreMouseEvents', (id, ignore) => {
getWindowById(id).setIgnoreMouseEvents(ignore);
});
socket.on('browserWindow-setContentProtection', (id, enable) => {
getWindowById(id).setContentProtection(enable);
});
socket.on('browserWindow-setFocusable', (id, focusable) => {
getWindowById(id).setFocusable(focusable);
});
socket.on('browserWindow-setParentWindow', (id, parent) => {
const browserWindow = BrowserWindow.fromId(parent.id);
getWindowById(id).setParentWindow(browserWindow);
});
socket.on('browserWindow-getParentWindow', (id) => {
const browserWindow = getWindowById(id).getParentWindow();
socket.emit('browserWindow-getParentWindow-completed', browserWindow.id);
});
socket.on('browserWindow-getChildWindows', (id) => {
const browserWindows = getWindowById(id).getChildWindows();
const ids = [];
browserWindows.forEach(x => {
ids.push(x.id);
})
socket.emit('browserWindow-getChildWindows-completed', ids);
});
socket.on('browserWindow-setAutoHideCursor', (id, autoHide) => {
getWindowById(id).setAutoHideCursor(autoHide);
});
socket.on('browserWindow-setVibrancy', (id, type) => {
getWindowById(id).setVibrancy(type);
});
function getWindowById(id: number): Electron.BrowserWindow {
for (var index = 0; index < windows.length; index++) {
var element = windows[index];