Initial implementation (#1363)

This commit is contained in:
Robin Westberg 2022-03-15 17:02:26 +01:00 committed by GitHub
parent 41f0ca4e7e
commit 08198aa86b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 47 additions and 17 deletions

View File

@ -58,6 +58,7 @@ jest.mock('../src/app/window-handler', () => {
jest.mock('../src/app/window-utils', () => {
return {
downloadManagerAction: jest.fn(),
getWindowByName: jest.fn(),
isValidWindow: jest.fn(() => true),
sanitize: jest.fn(),
setDataUrl: jest.fn(),
@ -471,17 +472,35 @@ describe('main api handler', () => {
});
it('should call `getNativeWindowHandle` correctly', () => {
const fromWebContentsMocked = {
getNativeWindowHandle: jest.fn(),
const windows = {
main: {
getNativeWindowHandle: jest.fn(),
},
popout1: {
getNativeWindowHandle: jest.fn(),
},
popout2: {
getNativeWindowHandle: jest.fn(),
},
};
jest.spyOn(BrowserWindow, 'fromWebContents').mockImplementation(() => {
return fromWebContentsMocked;
});
const value = {
jest
.spyOn(utils, 'getWindowByName')
.mockImplementation((windowName: string) => {
return windows[windowName];
});
ipcMain.send(apiName.symphonyApi, {
cmd: apiCmds.getNativeWindowHandle,
};
ipcMain.send(apiName.symphonyApi, value);
expect(fromWebContentsMocked.getNativeWindowHandle).toBeCalledTimes(1);
windowName: 'main',
});
expect(windows['main'].getNativeWindowHandle).toBeCalledTimes(1);
ipcMain.send(apiName.symphonyApi, {
cmd: apiCmds.getNativeWindowHandle,
windowName: 'popout1',
});
expect(windows['popout1'].getNativeWindowHandle).toBeCalledTimes(1);
expect(windows['popout2'].getNativeWindowHandle).toBeCalledTimes(0);
});
});
});

View File

@ -31,6 +31,7 @@ import { activate, handleKeyPress } from './window-actions';
import { ICustomBrowserWindow, windowHandler } from './window-handler';
import {
downloadManagerAction,
getWindowByName,
isValidView,
isValidWindow,
sanitize,
@ -419,9 +420,7 @@ ipcMain.handle(
}
break;
case apiCmds.getNativeWindowHandle:
const browserWin = BrowserWindow.fromWebContents(
event.sender,
) as ICustomBrowserWindow;
const browserWin = getWindowByName(arg.windowName);
if (browserWin && windowExists(browserWin)) {
const windowHandle = browserWin.getNativeWindowHandle();
return getContentWindowHandle(windowHandle);

View File

@ -279,7 +279,10 @@
<hr />
<p>Native Window Handle:</p>
<button id="get-window-handle">Get window handle</button>
<button id="get-window-handle">
Get window handle (optionally enter a window name)
</button>
<input type="text" id="text-window-handle-name" />
<input type="text" id="text-window-handle" />
<hr />
<br />
@ -1200,12 +1203,15 @@
.join('');
document.getElementById('text-window-handle').value = handleStr;
};
const windowName = document.getElementById('text-window-handle-name')
.value;
if (window.ssf) {
window.ssf.getNativeWindowHandle().then(resultCallback);
window.ssf.getNativeWindowHandle(windowName).then(resultCallback);
} else if (window.manaSSF) {
window.manaSSF.getNativeWindowHandle().then(resultCallback);
window.manaSSF.getNativeWindowHandle(windowName).then(resultCallback);
} else {
postRequest(apiCmds.getNativeWindowHandle, null, {
windowName,
successCallback: resultCallback,
});
}

View File

@ -737,12 +737,18 @@ export class SSFApi {
}
/**
* Get native window handle of the window where the renderer is displayed
* Get native window handle of the window, by default where the renderer is displayed,
* or optionally another window identified by its name.
* @param windowName optional window name, defaults to current renderer window
* @returns the platform-specific handle of the window.
*/
public getNativeWindowHandle(): Promise<Buffer> {
public getNativeWindowHandle(windowName?: string): Promise<Buffer> {
if (!windowName) {
windowName = window.name || 'main';
}
return ipcRenderer.invoke(apiName.symphonyApi, {
cmd: apiCmds.getNativeWindowHandle,
windowName,
});
}