Skip to content
This repository was archived by the owner on Sep 7, 2018. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/api-electron/src/preload/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,17 @@ class Window implements ssf.Window {
wrappedWindow.id = String(win.id);
return wrappedWindow;
}

static getById(id: string) {
return new Promise<Window>(resolve => {
if (!isNaN(parseInt(id))) {
const bw = BrowserWindow.fromId(parseInt(id))
resolve(bw === null ? null : Window.wrap(bw));
return;
}
resolve(null);
});
}
}

export default Window;
38 changes: 32 additions & 6 deletions packages/api-openfin/src/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
};

Expand Down Expand Up @@ -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<boolean> => {
return new Promise<boolean>(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<void>;
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;
6 changes: 6 additions & 0 deletions packages/api-specification/interface/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,5 +461,11 @@ declare namespace ssf {
* @returns A promise that resolves to nothing when the window has unmaximized.
*/
unmaximize(): Promise<void>;

/**
* 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<Window>;
}
}
2 changes: 1 addition & 1 deletion packages/api-tests/test/window.core.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
52 changes: 52 additions & 0 deletions packages/api-tests/test/window.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down