diff --git a/packages/api-electron/src/preload/window.ts b/packages/api-electron/src/preload/window.ts index c8788f0..51a6f80 100644 --- a/packages/api-electron/src/preload/window.ts +++ b/packages/api-electron/src/preload/window.ts @@ -348,6 +348,17 @@ class Window implements ssf.Window { wrappedWindow.id = String(win.id); return wrappedWindow; } + + static getById(id: string) { + return new Promise(resolve => { + if (!isNaN(parseInt(id))) { + const bw = BrowserWindow.fromId(parseInt(id)) + resolve(bw === null ? null : Window.wrap(bw)); + return; + } + resolve(null); + }); + } } export default Window; diff --git a/packages/api-openfin/src/window.ts b/packages/api-openfin/src/window.ts index b4e43ed..3db4cdf 100644 --- a/packages/api-openfin/src/window.ts +++ b/packages/api-openfin/src/window.ts @@ -242,9 +242,7 @@ class Window implements ssf.Window { fin.desktop.InterApplicationBus.unsubscribe('*' , 'ssf-child-windows', subscribeListener); const children = []; names.forEach((name) => { - const app = fin.desktop.Application.wrap(name); - const win = app.getWindow(); - const childWin = Window.wrap(win); + const childWin = Window.getById(name); children.push(childWin); }); resolve(children); @@ -282,9 +280,7 @@ class Window implements ssf.Window { resolve(null); return; } - const app = fin.desktop.Application.wrap(name); - const win = app.getWindow(); - const parentWin = Window.wrap(win); + const parentWin = Window.getById(name); resolve(parentWin); }; @@ -524,6 +520,36 @@ class Window implements ssf.Window { wrappedWin.id = win.uuid + ':' + win.name; return wrappedWin; } + + static getById(id: string) { + const appExists = (uuid): Promise => { + return new Promise(resolve => { + fin.desktop.System.getAllApplications((apps) => { + resolve(apps.filter((app) => app.uuid === uuid).length === 1); + }); + }); + }; + + const idRegex = '(' + // Start group 1 + '[\w\d-]+' + // At least 1 word character, digit or dash + ')' + // End group 1 + ':' + // colon + '\\1'; // Same string that was matched in group 1 + + let app = null; + let existsPromise: Promise; + const uuid = id.match(new RegExp(idRegex)) ? id.split(':')[0] : id; + return appExists(uuid).then((exists) => { + app = exists ? fin.desktop.Application.wrap(uuid) : null; + }).then(() => { + if (app) { + const win = app.getWindow(); + return Window.wrap(win); + } + + return null; + }) + } } export default Window; diff --git a/packages/api-specification/interface/window.ts b/packages/api-specification/interface/window.ts index 7775329..01355d8 100644 --- a/packages/api-specification/interface/window.ts +++ b/packages/api-specification/interface/window.ts @@ -461,5 +461,11 @@ declare namespace ssf { * @returns A promise that resolves to nothing when the window has unmaximized. */ unmaximize(): Promise; + + /** + * Get the window object with a particular id. Returns null if no window with that id exists. + * @param id The id of the window. + */ + static getById(id: string): Promise; } } diff --git a/packages/api-tests/test/window.core.spec.js b/packages/api-tests/test/window.core.spec.js index c769972..3074695 100644 --- a/packages/api-tests/test/window.core.spec.js +++ b/packages/api-tests/test/window.core.spec.js @@ -616,7 +616,7 @@ describe('WindowCore API', function(done) { return chainPromises(steps); }); - it.only('Should wrap a native window in a containerjs window object #ssf.Window.wrap', function() { + it('Should wrap a native window in a containerjs window object #ssf.Window.wrap', function() { const windowTitle = 'windownamewrap'; const windowOptions = getWindowOptions({ name: windowTitle diff --git a/packages/api-tests/test/window.spec.js b/packages/api-tests/test/window.spec.js index 580e476..b979241 100644 --- a/packages/api-tests/test/window.spec.js +++ b/packages/api-tests/test/window.spec.js @@ -109,6 +109,58 @@ if (process.env.MOCHA_CONTAINER !== 'browser') { return chainPromises(steps); }); + it('Should get a window by its id #ssf.Window.getById', function() { + const windowTitle = 'windownamegetbyid'; + const windowOptions = getWindowOptions({ + name: windowTitle + }); + + /* eslint-disable no-undef */ + const idScript = (callback) => { + ssf.app.ready().then(() => { + const id = window.newWin.getId(); + const win = ssf.Window.getById(id); + callback(win !== null); + }); + }; + /* eslint-enable no-undef */ + + const steps = [ + ...setupWindowSteps(windowOptions), + () => selectWindow(app.client, 0), + () => executeAsyncJavascript(app.client, idScript), + (result) => assert.equal(result.value, true) + ]; + + return chainPromises(steps); + }); + + it('Should return null if no window with id exists #ssf.Window.getById', function() { + const windowTitle = 'windownamegetbyidwrong'; + const windowOptions = getWindowOptions({ + name: windowTitle + }); + + /* eslint-disable no-undef */ + const idScript = (callback) => { + ssf.app.ready().then(() => { + ssf.Window.getById('thisiswrong').then(win => { + callback(win === null); + }); + }); + }; + /* eslint-enable no-undef */ + + const steps = [ + ...setupWindowSteps(windowOptions), + () => selectWindow(app.client, 0), + () => executeAsyncJavascript(app.client, idScript), + (result) => assert.equal(result.value, true) + ]; + + return chainPromises(steps); + }); + it('Should return the maximum width #ssf.Window.getMaximumSize', function() { const windowTitle = 'windownamemaxwidth'; const maxWidth = 500;