From 45fea0e4c220f3cc243826830562ba4d9b231421 Mon Sep 17 00:00:00 2001 From: Pierre Baize Date: Fri, 4 Oct 2019 15:19:30 -0400 Subject: [PATCH 1/2] removed Window.wrap --- src/browser/api/application.js | 2 +- .../api/notifications/subscriptions.ts | 3 +- src/browser/api/window.js | 31 ++++++++----------- 3 files changed, 16 insertions(+), 20 deletions(-) diff --git a/src/browser/api/application.js b/src/browser/api/application.js index 17f9c2312..c12bbbf22 100644 --- a/src/browser/api/application.js +++ b/src/browser/api/application.js @@ -398,7 +398,7 @@ Application.getInfo = function(identity, callback) { Application.getWindow = function(identity) { let uuid = identity.uuid; - return Window.wrap(uuid, uuid); + return coreState.getWindowByUuidName(uuid, uuid); }; Application.grantAccess = function() { diff --git a/src/browser/api/notifications/subscriptions.ts b/src/browser/api/notifications/subscriptions.ts index 48a07d87f..48dfc571f 100644 --- a/src/browser/api/notifications/subscriptions.ts +++ b/src/browser/api/notifications/subscriptions.ts @@ -18,6 +18,7 @@ import ofEvents from '../../of_events'; const {writeToLog} = require('../../log'); const _ = require('underscore'); import route from '../../../common/route'; +import { getWindowByUuidName } from '../../core_state'; const NOTE_APP_UUID = 'service:notifications'; @@ -622,7 +623,7 @@ function windowIsValid(identity: any): boolean { let isValid: boolean; try { - const openfinWindow = Window.wrap(identity.uuid, identity.name); + const openfinWindow = getWindowByUuidName(identity.uuid, identity.name); const browserWindow = openfinWindow && openfinWindow.browserWindow; if (!browserWindow) { diff --git a/src/browser/api/window.js b/src/browser/api/window.js index a238ffa97..ec4786307 100644 --- a/src/browser/api/window.js +++ b/src/browser/api/window.js @@ -231,7 +231,7 @@ let optionSetters = { let uuid = browserWin._options.uuid; let name = browserWin._options.name; - let openfinWindow = Window.wrap(uuid, name); + let openfinWindow = coreState.getWindowByUuidName(uuid, name); let hideOnCloseListener = openfinWindow.hideOnCloseListener; let closeEventString = route.window('close-requested', uuid, name); @@ -412,7 +412,7 @@ Window.create = function(id, opts) { let hideReason = 'hide'; let hideOnCloseListener = () => { - let openfinWindow = Window.wrap(uuid, name); + let openfinWindow = coreState.getWindowByUuidName(uuid, name); openfinWindow.hideReason = 'hide-on-close'; browserWindow.hide(); }; @@ -496,7 +496,7 @@ Window.create = function(id, opts) { // once the window is closed, be sure to close all the children // it may have and remove it from the browserWindow.on('close', (event) => { - let ofWindow = Window.wrap(uuid, name); + let ofWindow = coreState.getWindowByUuidName(uuid, name); let closeEventString = route.window('close-requested', uuid, name); let listenerCount = ofEvents.listenerCount(closeEventString); @@ -570,7 +570,7 @@ Window.create = function(id, opts) { // if the window has already been removed from core_state, // don't propagate anymore events - if (!Window.wrap(uuid, name)) { + if (!coreState.getWindowByUuidName(uuid, name)) { return; } @@ -877,11 +877,6 @@ Window.create = function(id, opts) { return winObj; }; - -Window.wrap = function(uuid, name) { - return coreState.getWindowByUuidName(uuid, name); -}; - Window.connected = function() {}; Window.isEmbedded = function() {}; @@ -1022,7 +1017,7 @@ Window.close = function(identity, force, callback = () => {}) { let defaultAction = () => { if (!browserWindow.isDestroyed()) { - let openfinWindow = Window.wrap(identity.uuid, identity.name); + let openfinWindow = coreState.getWindowByUuidName(identity.uuid, identity.name); openfinWindow.forceClose = true; browserWindow.close(); } @@ -1176,14 +1171,14 @@ Window.getGroup = function(identity) { return []; } - let openfinWindow = Window.wrap(identity.uuid, identity.name); + let openfinWindow = coreState.getWindowByUuidName(identity.uuid, identity.name); return WindowGroups.getGroup(openfinWindow.groupUuid); }; Window.getWindowInfo = function(identity) { const browserWindow = getElectronBrowserWindow(identity, 'get info for'); - const { preloadScripts } = Window.wrap(identity.uuid, identity.name); + const { preloadScripts } = coreState.getWindowByUuidName(identity.uuid, identity.name); const windowKey = genWindowKey(identity); const isUserMovementEnabled = !disabledFrameRef.has(windowKey) || disabledFrameRef.get(windowKey) === 0; const windowInfo = Object.assign({ @@ -1298,7 +1293,7 @@ Window.leaveGroup = function(identity) { return; } - let openfinWindow = Window.wrap(identity.uuid, identity.name); + let openfinWindow = coreState.getWindowByUuidName(identity.uuid, identity.name); return WindowGroups.leaveGroup(openfinWindow); }; @@ -1798,7 +1793,7 @@ function createWindowTearDown(identity, id, browserWindow, _boundsChangedHandler // Close all child windows // Wait for the close event. return function() { - let ofWindow = Window.wrap(identity.uuid, identity.name); + let ofWindow = coreState.getWindowByUuidName(identity.uuid, identity.name); let childWindows = coreState.getChildrenByWinId(id) || []; // remove from core state earlier rather than later coreState.removeChildById(id); @@ -2022,7 +2017,7 @@ function boundsChangeDecorator(payload, args) { payload[key] = boundsChangePayload[key]; }); - let _win = Window.wrap(payload.uuid, payload.name); + let _win = coreState.getWindowByUuidName(payload.uuid, payload.name); let _browserWin = _win && _win.browserWindow; setOptOnBrowserWin('x', payload.left, _browserWin); setOptOnBrowserWin('y', payload.top, _browserWin); @@ -2070,7 +2065,7 @@ function willMoveOrResizeDecorator(payload, args) { } function opacityChangedDecorator(payload, args) { - let _win = Window.wrap(payload.uuid, payload.name); + let _win = coreState.getWindowByUuidName(payload.uuid, payload.name); let _browserWin = _win && _win.browserWindow; setOptOnBrowserWin('opacity', args[1], _browserWin); return false; @@ -2095,7 +2090,7 @@ function visibilityChangedDecorator(payload, args) { coreState.setSentFirstHideSplashScreen(uuid, true); } } else { - let openfinWindow = Window.wrap(payload.uuid, payload.name); + let openfinWindow = coreState.getWindowByUuidName(payload.uuid, payload.name); const { hideReason } = openfinWindow; payload.type = 'hidden'; payload.reason = hideReason === 'hide' && closing ? 'closing' : hideReason; @@ -2255,7 +2250,7 @@ function handleCustomAlerts(id, opts) { //If unknown window AND `errDesc` provided, throw error; otherwise return (possibly undefined) browser window ref. export function getElectronBrowserWindow(identity, errDesc) { - let openfinWindow = Window.wrap(identity.uuid, identity.name); + let openfinWindow = coreState.getWindowByUuidName(identity.uuid, identity.name); let browserWindow = openfinWindow && openfinWindow.browserWindow; if (errDesc && !browserWindow) { From b7b9af89b27e0467fc85b77b8a2953c5fdac9670 Mon Sep 17 00:00:00 2001 From: Pierre Baize Date: Mon, 7 Oct 2019 10:56:46 -0400 Subject: [PATCH 2/2] changed to named exports --- index.js | 4 +- src/browser/api/application.js | 2 +- .../api/notifications/subscriptions.ts | 7 +- src/browser/api/window.js | 257 +++++++++--------- .../api_handlers/event_listener.ts | 2 +- .../middleware_entity_existence.ts | 2 +- .../api_protocol/api_handlers/window.ts | 4 +- src/browser/core_state.ts | 13 +- 8 files changed, 143 insertions(+), 148 deletions(-) diff --git a/index.js b/index.js index 57556864b..6cae4e8f6 100644 --- a/index.js +++ b/index.js @@ -21,7 +21,7 @@ let minimist = require('minimist'); // local modules let Application = require('./src/browser/api/application.js').Application; let System = require('./src/browser/api/system.js').System; -import { Window } from './src/browser/api/window'; +import { create } from './src/browser/api/window'; let apiProtocol = require('./src/browser/api_protocol'); import socketServer from './src/browser/transports/socket_server'; @@ -79,7 +79,7 @@ app.on('child-window-created', function(parentBwId, childBwId, childOptions) { console.warn('failed to add'); } - Window.create(childId, childOptions); + create(childId, childOptions); }); app.on('select-client-certificate', function(event, webContents, url, list, callback) { diff --git a/src/browser/api/application.js b/src/browser/api/application.js index c12bbbf22..6eb3f8873 100644 --- a/src/browser/api/application.js +++ b/src/browser/api/application.js @@ -21,7 +21,7 @@ let _ = require('underscore'); // local modules let System = require('./system.js').System; -import { Window } from './window'; +import * as Window from './window'; let convertOpts = require('../convert_options.js'); import * as coreState from '../core_state'; let externalApiBase = require('../api_protocol/api_handlers/api_protocol_base'); diff --git a/src/browser/api/notifications/subscriptions.ts b/src/browser/api/notifications/subscriptions.ts index 48dfc571f..48e09ce3f 100644 --- a/src/browser/api/notifications/subscriptions.ts +++ b/src/browser/api/notifications/subscriptions.ts @@ -7,11 +7,8 @@ import { PendingNote } from './shapes'; -// required tor the ts compiler, theses should be made imports eventually -declare var require: any; - const {System} = require('../system'); -const {Window} = require('../window'); +import * as Window from '../window'; const {Application} = require('../application'); const {sendToIdentity} = require('../../api_protocol/api_handlers/api_protocol_base'); import ofEvents from '../../of_events'; @@ -128,7 +125,7 @@ ofEvents.on(route.application('window-end-load', '*'), (e: any) => { try { Window.close({ uuid, - name: Window.QUEUE_COUNTER_NAME + name: undefined }); } catch (e) { writeToLog('info', e); diff --git a/src/browser/api/window.js b/src/browser/api/window.js index ec4786307..dfce21663 100644 --- a/src/browser/api/window.js +++ b/src/browser/api/window.js @@ -53,7 +53,6 @@ import { WINDOWS_MESSAGE_MAP } from '../../common/windows_messages'; const subscriptionManager = new SubscriptionManager(); const isWin32 = process.platform === 'win32'; const windowPosCacheFolder = 'winposCache'; -export const Window = {}; // jshint ignore:line const disabledFrameRef = new Map(); let browserWindowEventMap = { @@ -396,7 +395,7 @@ let optionSetters = { }; -Window.create = function(id, opts) { +export function create(id, opts) { let name = opts.name; let uuid = opts.uuid; let identity = { @@ -798,7 +797,7 @@ Window.create = function(id, opts) { //we show as soon as we restore the window position instead of waiting for the connected event if (_options.autoShow && (!_options.waitForPageLoad)) { // Need to go through Window.show here so that the show-requested logic comes into play - Window.show(identity); + show(identity); } } else if (_options.waitForPageLoad) { browserWindow.once('ready-to-show', () => { @@ -810,7 +809,7 @@ Window.create = function(id, opts) { //instead of waiting for the connected event if (_options.autoShow) { // Need to go through Window.show here so that the show-requested logic comes into play - Window.show(identity); + show(identity); } observer.next(); }); @@ -822,7 +821,7 @@ Window.create = function(id, opts) { const constructorCallbackMessage = event[0]; if (_options.autoShow || _options.toShowOnRun) { if (!browserWindow.isVisible()) { - Window.show(identity); + show(identity); } } @@ -875,13 +874,13 @@ Window.create = function(id, opts) { WebContents.setIframeHandlers(browserWindow.webContents, winObj, uuid, name); return winObj; -}; +} -Window.connected = function() {}; +export function connected() {} -Window.isEmbedded = function() {}; +export function isEmbedded() {} -Window.addEventListener = function(identity, targetIdentity, type, listener) { +export function addEventListener(identity, targetIdentity, type, listener) { // TODO this leaves it up the the app to unsubscribe and is a potential // leak. perhaps we need a way to unhook when an app disconnects // automatically @@ -926,9 +925,9 @@ Window.addEventListener = function(identity, targetIdentity, type, listener) { ofEvents.removeListener(eventString, safeListener); }; return unsubscribe; -}; +} -Window.animate = function(identity, transitions, options = {}, callback = () => {}, errorCallback = () => {}) { +export function animate(identity, transitions, options = {}, callback = () => {}, errorCallback = (_) => {}) { let browserWindow = getElectronBrowserWindow(identity); if (!browserWindow) { @@ -972,9 +971,9 @@ Window.animate = function(identity, transitions, options = {}, callback = () => } else { errorCallback(new Error(`Proposed window bounds violate size constraints for uuid: ${identity.uuid} name: ${identity.name}`)); } -}; +} -Window.blur = function(identity) { +export function blur(identity) { let browserWindow = getElectronBrowserWindow(identity); if (!browserWindow) { @@ -982,28 +981,28 @@ Window.blur = function(identity) { } browserWindow.blur(); -}; +} -Window.bringToFront = function(identity) { +export function bringToFront(identity) { const browserWindow = getElectronBrowserWindow(identity); if (!browserWindow) { return; } NativeWindow.bringToFront(browserWindow); -}; +} -Window.center = function(identity) { +export function center(identity) { const browserWindow = getElectronBrowserWindow(identity); if (!browserWindow) { return; } NativeWindow.center(browserWindow); -}; +} // TODO investigate the close sequence, there appears to be a case were you // try to wrap and close an already closed window -Window.close = function(identity, force, callback = () => {}) { +export function close(identity, force, callback = () => {}) { let browserWindow = getElectronBrowserWindow(identity); if (!browserWindow) { @@ -1028,7 +1027,7 @@ Window.close = function(identity, force, callback = () => {}) { }); handleForceActions(identity, force, 'close-requested', payload, defaultAction); -}; +} function disabledFrameUnsubDecorator(identity) { const windowKey = genWindowKey(identity); @@ -1037,12 +1036,12 @@ function disabledFrameUnsubDecorator(identity) { if (refCount > 1) { disabledFrameRef.set(windowKey, --refCount); } else { - Window.enableUserMovement(identity); + enableUserMovement(identity); } }; } -Window.disableUserMovement = function(requestorIdentity, windowIdentity) { +export function disableUserMovement(requestorIdentity, windowIdentity) { const browserWindow = getElectronBrowserWindow(windowIdentity); const windowKey = genWindowKey(windowIdentity); @@ -1054,9 +1053,9 @@ Window.disableUserMovement = function(requestorIdentity, windowIdentity) { disabledFrameRef.set(windowKey, ++dframeRefCount); subscriptionManager.registerSubscription(disabledFrameUnsubDecorator(windowIdentity), requestorIdentity, `disable-frame-${windowKey}`); browserWindow.setUserMovementEnabled(false); -}; +} -Window.embed = function(identity, parentHwnd) { +export function embed(identity, parentHwnd) { let browserWindow = getElectronBrowserWindow(identity); if (!browserWindow) { @@ -1076,9 +1075,9 @@ Window.embed = function(identity, parentHwnd) { name: identity.name, uuid: identity.uuid }); -}; +} -Window.enableUserMovement = function(identity) { +export function enableUserMovement(identity) { const windowKey = genWindowKey(identity); let browserWindow = getElectronBrowserWindow(identity); @@ -1092,9 +1091,9 @@ Window.enableUserMovement = function(identity) { } browserWindow.setUserMovementEnabled(true); -}; +} -Window.executeJavascript = function(identity, code, callback = () => {}) { +export function executeJavascript(identity, code, callback = () => {}) { let browserWindow = getElectronBrowserWindow(identity); if (!browserWindow) { @@ -1103,33 +1102,33 @@ Window.executeJavascript = function(identity, code, callback = () => {}) { } WebContents.executeJavascript(browserWindow.webContents, code, callback); -}; +} -Window.flash = function(identity) { +export function flash(identity) { const browserWindow = getElectronBrowserWindow(identity); if (!browserWindow) { return; } NativeWindow.flash(browserWindow); -}; +} -Window.stopFlashing = function(identity) { +export function stopFlashing(identity) { const browserWindow = getElectronBrowserWindow(identity); if (!browserWindow) { return; } NativeWindow.stopFlashing(browserWindow); -}; +} -Window.focus = function(identity) { +export function focus(identity) { const browserWindow = getElectronBrowserWindow(identity); if (!browserWindow) { return; } NativeWindow.focus(browserWindow); -}; +} -Window.getAllFrames = function(identity) { +export function getAllFrames(identity) { const openfinWindow = coreState.getWindowByUuidName(identity.uuid, identity.name); if (!openfinWindow) { @@ -1144,9 +1143,9 @@ Window.getAllFrames = function(identity) { } return framesArr.concat(subFrames); -}; +} -Window.getBounds = function(identity) { +export function getBounds(identity) { const browserWindow = getElectronBrowserWindow(identity); if (!browserWindow) { @@ -1161,10 +1160,10 @@ Window.getBounds = function(identity) { } return NativeWindow.getBounds(browserWindow); -}; +} -Window.getGroup = function(identity) { +export function getGroup(identity) { let browserWindow = getElectronBrowserWindow(identity); if (!browserWindow) { @@ -1173,10 +1172,10 @@ Window.getGroup = function(identity) { let openfinWindow = coreState.getWindowByUuidName(identity.uuid, identity.name); return WindowGroups.getGroup(openfinWindow.groupUuid); -}; +} -Window.getWindowInfo = function(identity) { +export function getWindowInfo(identity) { const browserWindow = getElectronBrowserWindow(identity, 'get info for'); const { preloadScripts } = coreState.getWindowByUuidName(identity.uuid, identity.name); const windowKey = genWindowKey(identity); @@ -1186,25 +1185,25 @@ Window.getWindowInfo = function(identity) { isUserMovementEnabled }, WebContents.getInfo(browserWindow.webContents)); return windowInfo; -}; +} -Window.getAbsolutePath = function(identity, path) { +export function getAbsolutePath(identity, path) { let browserWindow = getElectronBrowserWindow(identity, 'get URL for'); return (path || path === 0) ? WebContents.getAbsolutePath(browserWindow.webContents, path) : ''; -}; +} -Window.getNativeId = function(identity) { +export function getNativeId(identity) { let browserWindow = getElectronBrowserWindow(identity, 'get ID for'); return browserWindow.nativeId; -}; +} -Window.getNativeWindow = function() {}; +export function getNativeWindow() {} -Window.getOptions = function(identity) { +export function getOptions(identity) { // In the case that the identity passed does not exist, or is not a window, // return the entity info object. The fail case is used for frame identity on spin up. try { @@ -1212,11 +1211,11 @@ Window.getOptions = function(identity) { } catch (e) { return System.getEntityInfo(identity); } -}; +} -Window.getParentWindow = function() {}; +export function getParentWindow() {} -Window.getSnapshot = (opts) => { +export const getSnapshot = (opts) => { return new Promise((resolve, reject) => { const { identity, payload: { area } } = opts; const browserWindow = getElectronBrowserWindow(identity); @@ -1250,43 +1249,43 @@ Window.getSnapshot = (opts) => { }; -Window.getState = function(identity) { +export function getState(identity) { let browserWindow = getElectronBrowserWindow(identity); if (!browserWindow) { return 'normal'; } return NativeWindow.getState(browserWindow); -}; +} -Window.hide = function(identity) { +export function hide(identity) { let browserWindow = getElectronBrowserWindow(identity); if (!browserWindow) { return; } NativeWindow.hide(browserWindow); -}; +} -Window.isNotification = function(name) { +export function isNotification(name) { const noteGuidRegex = /^A21B62E0-16B1-4B10-8BE3-BBB6B489D862/; return noteGuidRegex.test(name); -}; +} -Window.isShowing = function(identity) { +export function isShowing(identity) { let browserWindow = getElectronBrowserWindow(identity); if (!browserWindow) { return false; } return NativeWindow.isVisible(browserWindow); -}; +} -Window.joinGroup = function(identity, grouping) { +export function joinGroup(identity, grouping) { return WindowGroups.joinGroup({ uuid: identity.uuid, name: identity.name }, { uuid: grouping.uuid, name: grouping.name }); -}; +} -Window.leaveGroup = function(identity) { +export function leaveGroup(identity) { let browserWindow = getElectronBrowserWindow(identity); if (!browserWindow) { @@ -1295,78 +1294,78 @@ Window.leaveGroup = function(identity) { let openfinWindow = coreState.getWindowByUuidName(identity.uuid, identity.name); return WindowGroups.leaveGroup(openfinWindow); -}; +} -Window.maximize = function(identity) { +export function maximize(identity) { let browserWindow = getElectronBrowserWindow(identity, 'maximize'); let maximizable = getOptFromBrowserWin('maximizable', browserWindow, true); if (maximizable) { NativeWindow.maximize(browserWindow); } -}; +} -Window.mergeGroups = function(identity, grouping) { +export function mergeGroups(identity, grouping) { return WindowGroups.mergeGroups({ uuid: identity.uuid, name: identity.name }, { uuid: grouping.uuid, name: grouping.name }); -}; +} -Window.minimize = function(identity) { +export function minimize(identity) { let browserWindow = getElectronBrowserWindow(identity, 'minimize'); let minimizable = getOptFromBrowserWin('minimizable', browserWindow, true); if (minimizable) { NativeWindow.minimize(browserWindow); } -}; +} -Window.moveBy = function(identity, deltaLeft, deltaTop) { +export function moveBy(identity, deltaLeft, deltaTop) { const browserWindow = getElectronBrowserWindow(identity); if (!browserWindow) { return; } NativeWindow.moveBy(browserWindow, { deltaLeft, deltaTop }); -}; +} -Window.moveTo = function(identity, left, top) { +export function moveTo(identity, left, top) { const browserWindow = getElectronBrowserWindow(identity); if (!browserWindow) { return; } NativeWindow.moveTo(browserWindow, { left, top }); -}; +} -Window.navigate = function(identity, url) { +export function navigate(identity, url) { const browserWindow = getElectronBrowserWindow(identity, 'navigate'); return WebContents.navigate(browserWindow.webContents, url); -}; +} -Window.navigateBack = function(identity) { +export function navigateBack(identity) { const browserWindow = getElectronBrowserWindow(identity, 'navigate back'); return WebContents.navigateBack(browserWindow.webContents); -}; +} -Window.navigateForward = function(identity) { +export function navigateForward(identity) { const browserWindow = getElectronBrowserWindow(identity, 'navigate forward'); return WebContents.navigateForward(browserWindow.webContents); -}; +} -Window.reload = function(identity, ignoreCache = false) { +export function reload(identity, ignoreCache = false) { const browserWindow = getElectronBrowserWindow(identity, 'reload'); WebContents.reload(browserWindow.webContents, ignoreCache); -}; +} -Window.stopNavigation = function(identity) { +export function stopNavigation(identity) { const browserWindow = getElectronBrowserWindow(identity, 'stop navigating'); WebContents.stopNavigation(browserWindow.webContents); -}; +} -Window.removeEventListener = function(identity, type, listener) { +export function removeEventListener(identity, type, listener) { let browserWindow = getElectronBrowserWindow(identity, 'remove event listener for'); ofEvents.removeListener(route.window(type, browserWindow.webContents.id), listener); -}; +} function areNewBoundsWithinConstraints(options, width, height) { const { @@ -1399,7 +1398,7 @@ function areNewBoundsWithinConstraints(options, width, height) { return acceptableWidth && acceptableHeight && (aspectRatio <= 0 || roundedProposedRatio === roundedAspectRatio); } -Window.resizeBy = function(identity, deltaWidth, deltaHeight, anchor, callback, errorCallback) { +export function resizeBy(identity, deltaWidth, deltaHeight, anchor, callback, errorCallback) { const browserWindow = getElectronBrowserWindow(identity); const opts = { anchor, deltaHeight, deltaWidth }; if (!browserWindow) { @@ -1422,10 +1421,10 @@ Window.resizeBy = function(identity, deltaWidth, deltaHeight, anchor, callback, } else { errorCallback(new Error(`Proposed window bounds violate size constraints for uuid: ${identity.uuid} name: ${identity.name}`)); } -}; +} -Window.resizeTo = function(identity, width, height, anchor, callback, errorCallback) { +export function resizeTo(identity, width, height, anchor, callback, errorCallback) { const browserWindow = getElectronBrowserWindow(identity); const opts = { anchor, height, width }; if (!browserWindow) { @@ -1445,25 +1444,25 @@ Window.resizeTo = function(identity, width, height, anchor, callback, errorCallb } else { errorCallback(new Error(`Proposed window bounds violate size constraints for uuid: ${identity.uuid} name: ${identity.name}`)); } -}; +} -Window.restore = function(identity) { +export function restore(identity) { const browserWindow = getElectronBrowserWindow(identity, 'restore'); NativeWindow.restore(browserWindow); -}; +} -Window.setAsForeground = function(identity) { +export function setAsForeground(identity) { const browserWindow = getElectronBrowserWindow(identity); if (!browserWindow) { return; } NativeWindow.setAsForeground(browserWindow); -}; +} -Window.setBounds = function(identity, left, top, width, height, callback, errorCallback) { +export function setBounds(identity, left, top, width, height, callback, errorCallback) { const browserWindow = getElectronBrowserWindow(identity, 'set window bounds for'); const opts = { height, left, top, width }; if (!browserWindow) { @@ -1483,10 +1482,10 @@ Window.setBounds = function(identity, left, top, width, height, callback, errorC } else { errorCallback(new Error(`Proposed window bounds violate size constraints for uuid: ${identity.uuid} name: ${identity.name}`)); } -}; +} -Window.show = function(identity, force = false) { +export function show(identity, force = false) { const browserWindow = getElectronBrowserWindow(identity); if (!browserWindow) { @@ -1497,10 +1496,10 @@ Window.show = function(identity, force = false) { const defaultAction = () => NativeWindow.show(browserWindow); handleForceActions(identity, force, 'show-requested', payload, defaultAction); -}; +} -Window.showAt = function(identity, left, top, force = false) { +export function showAt(identity, left, top, force = false) { const browserWindow = getElectronBrowserWindow(identity); if (!browserWindow) { @@ -1513,9 +1512,9 @@ Window.showAt = function(identity, left, top, force = false) { const defaultAction = () => NativeWindow.showAt(browserWindow, { left, top }); handleForceActions(identity, force, 'show-requested', payload, defaultAction); -}; +} -Window.showMenu = function(identity, x, y, editable, hasSelectedText) { +export function showMenu(identity, x, y, editable, hasSelectedText) { let browserWindow = getElectronBrowserWindow(identity); if (!browserWindow) { @@ -1572,7 +1571,7 @@ Window.showMenu = function(identity, x, y, editable, hasSelectedText) { const app = Application.wrap(identity.uuid); Application.getChildWindows(identity).forEach(childWin => { - Window.close({ + close({ name: childWin.name, uuid: childWin.uuid }, true); @@ -1601,12 +1600,12 @@ Window.showMenu = function(identity, x, y, editable, hasSelectedText) { currentContextMenu = null; } }); -}; +} -Window.defineDraggableArea = function() {}; +export function defineDraggableArea() {} -Window.updateOptions = function(identity, updateObj) { +export function updateOptions(identity, updateObj) { let browserWindow = getElectronBrowserWindow(identity, 'update settings for'); let { uuid, name } = identity; let diff = {}, @@ -1637,13 +1636,13 @@ Window.updateOptions = function(identity, updateObj) { } catch (e) { console.log(e.message); } -}; +} -Window.exists = function(identity) { +export function exists(identity) { return coreState.windowExists(identity.uuid, identity.name); -}; +} -Window.getBoundsFromDisk = function(identity, callback, errorCallback) { +export function getBoundsFromDisk(identity, callback, errorCallback) { getBoundsCacheSafeFileName(identity, cacheFile => { try { fs.readFile(cacheFile, 'utf8', (err, data) => { @@ -1661,9 +1660,9 @@ Window.getBoundsFromDisk = function(identity, callback, errorCallback) { errorCallback(err); } }, errorCallback); -}; +} -Window.authenticate = function(identity, username, password, callback) { +export function authenticate(identity, username, password, callback) { let { authCallback } = getPendingAuthRequest(identity); @@ -1675,30 +1674,28 @@ Window.authenticate = function(identity, username, password, callback) { } else { callback(new Error('No authentication request pending for window')); } -}; +} -Window.getZoomLevel = function(identity, callback) { +export function getZoomLevel(identity, callback) { let browserWindow = getElectronBrowserWindow(identity, 'get zoom level for'); WebContents.getZoomLevel(browserWindow.webContents, callback); -}; +} -Window.setZoomLevel = function(identity, level) { +export function setZoomLevel(identity, level) { let browserWindow = getElectronBrowserWindow(identity, 'set zoom level for'); WebContents.setZoomLevel(browserWindow.webContents, level); -}; +} -Window.onUnload = (identity) => { +export const onUnload = (identity) => { ofEvents.emit(route.window('unload', identity.uuid, identity.name, false), identity); ofEvents.emit(route.window('init-subscription-listeners'), identity); }; -Window.registerWindowName = (identity) => { +export const registerWindowName = (identity) => { coreState.registerPendingWindowName(identity.uuid, identity.name); }; -Window.getViews = getViews; - -function getViews({ uuid, name }) { +export function getViews({ uuid, name }) { return coreState.getAllViews() .filter(v => v.target.uuid === uuid && v.target.name === name) .map(({ uuid, name }) => ({ uuid, name })); @@ -1757,7 +1754,7 @@ function createWindowTearDown(identity, id, browserWindow, _boundsChangedHandler uuid: child.uuid }; - Window.close(childIdentity, true, () => { + close(childIdentity, true, () => { resolve(); }); } else { @@ -2261,7 +2258,7 @@ export function getElectronBrowserWindow(identity, errDesc) { } function restoreWindowPosition(identity, cb) { - Window.getBoundsFromDisk(identity, savedBounds => { + getBoundsFromDisk(identity, savedBounds => { const monitorInfo = System.getMonitorInfo(); @@ -2281,16 +2278,16 @@ function restoreWindowPosition(identity, cb) { switch (savedBounds.windowState) { case 'maximized': - Window.maximize(identity); + maximize(identity); break; case 'minimized': - Window.minimize(identity); + minimize(identity); break; } // set zoom level const { zoomLevel } = savedBounds; - Window.setZoomLevel(identity, zoomLevel); + setZoomLevel(identity, zoomLevel); cb(); }, (err) => { //We care about errors but lets keep window creation going. @@ -2298,6 +2295,14 @@ function restoreWindowPosition(identity, cb) { cb(); }); } +export function getWindowMeta({ openfinWindow }) { + const identity = getIdentityFromObject(openfinWindow); + const bounds = getBounds(identity); + bounds.name = openfinWindow.name; + bounds.state = getState(identity); + bounds.isShowing = isShowing(identity); + return bounds; +} function intersectsRect(bounds, rect) { return !(bounds.left > rect.right || (bounds.left + bounds.width) < rect.left || bounds.top > rect.bottom || (bounds.top + bounds.height) < rect.top); diff --git a/src/browser/api_protocol/api_handlers/event_listener.ts b/src/browser/api_protocol/api_handlers/event_listener.ts index a53e0a97e..3e01e547b 100644 --- a/src/browser/api_protocol/api_handlers/event_listener.ts +++ b/src/browser/api_protocol/api_handlers/event_listener.ts @@ -11,7 +11,7 @@ import { GlobalHotkey } from '../../api/global_hotkey'; import { Identity as NoteIdentity } from '../../api/notifications/shapes'; import { noop } from '../../../common/main'; import { System } from '../../api/system'; -import { Window } from '../../api/window'; +import * as Window from '../../api/window'; import * as _ from 'underscore'; import * as apiProtocolBase from './api_protocol_base'; import ofEvents from '../../of_events'; diff --git a/src/browser/api_protocol/api_handlers/middleware_entity_existence.ts b/src/browser/api_protocol/api_handlers/middleware_entity_existence.ts index 5ef071199..d3a3d9b41 100644 --- a/src/browser/api_protocol/api_handlers/middleware_entity_existence.ts +++ b/src/browser/api_protocol/api_handlers/middleware_entity_existence.ts @@ -3,7 +3,7 @@ import RequestHandler from '../transport_strategy/base_handler'; import { appByUuid, windowExists, getBrowserViewByIdentity, viewExists } from '../../core_state'; import { applicationApiMap } from './application.js'; import { MessagePackage } from '../transport_strategy/api_transport_base'; -import { windowApiMap } from './window.js'; +import { windowApiMap } from './window'; import { browserViewActionMap } from './browser_view'; import { webContentsApiMap } from './webcontents'; diff --git a/src/browser/api_protocol/api_handlers/window.ts b/src/browser/api_protocol/api_handlers/window.ts index 708a5df49..145cb6ae6 100644 --- a/src/browser/api_protocol/api_handlers/window.ts +++ b/src/browser/api_protocol/api_handlers/window.ts @@ -1,5 +1,5 @@ -const { Window } = require('../../api/window'); +import * as Window from '../../api/window'; import { getGroupingWindowIdentity, getTargetWindowIdentity, @@ -468,7 +468,7 @@ function centerWindow(identity: Identity, message: APIMessage, ack: Acker): void ack(successAck); } -function getViews(identity: Identity, message: APIMessage, ack: Acker): void { +function getViews(identity: Identity, message: APIMessage, ack: Acker) { const { payload } = message; const windowIdentity = getTargetWindowIdentity(payload); return Window.getViews(windowIdentity); diff --git a/src/browser/core_state.ts b/src/browser/core_state.ts index 598c9d6b9..2d3481b86 100644 --- a/src/browser/core_state.ts +++ b/src/browser/core_state.ts @@ -20,6 +20,7 @@ import * as electronIPC from './transports/electron_ipc'; import { getIdentityFromObject, isEnableChromiumBuild } from '../common/main'; import { BrowserViewOpts } from './api/browser_view'; import { Identity } from './api_protocol/transport_strategy/api_transport_base'; +import { getWindowMeta } from './api/window'; interface ProxySettingsArgs { proxyAddress?: string; @@ -581,8 +582,7 @@ export function getAllAppObjects(): Shapes.AppObj[] { .map(app => app.appObj); //and return same } -export function getAllWindows(): WindowMeta[] { - const windowApi = require('./api/window.js').Window; // do not move this line! +export function getAllWindows() { // Filter out apps where main window has already been destroyed const aliveApps = apps.filter(({ children }) => { @@ -595,14 +595,7 @@ export function getAllWindows(): WindowMeta[] { return aliveApps.map(({ uuid, children }) => { const childWindows = children - .map(({ openfinWindow }) => { - const identity = getIdentityFromObject(openfinWindow); - const bounds = windowApi.getBounds(identity); - bounds.name = openfinWindow.name; - bounds.state = windowApi.getState(identity); - bounds.isShowing = windowApi.isShowing(identity); - return bounds; - }); + .map(getWindowMeta); const mainWindow = childWindows.shift() || {};