From a33fc1468685f48fc63f0c0b1a7758c869e3a9b6 Mon Sep 17 00:00:00 2001 From: Pavlo Buidenkov Date: Mon, 30 May 2022 18:12:53 +0200 Subject: [PATCH] add ipcRenderer.invoke to preview source displays --- .../SharingSourcePreviewCard/index.tsx | 24 +++++++++---------- app/main/IpcEvents.enum.ts | 1 + app/main/ipcMainHandlers.ts | 19 +++++++++++++++ 3 files changed, 32 insertions(+), 12 deletions(-) diff --git a/app/components/SharingSourcePreviewCard/index.tsx b/app/components/SharingSourcePreviewCard/index.tsx index 99c221e..edbeff8 100644 --- a/app/components/SharingSourcePreviewCard/index.tsx +++ b/app/components/SharingSourcePreviewCard/index.tsx @@ -1,12 +1,8 @@ import React, { useEffect, useState } from 'react'; -import { remote } from 'electron'; +import { ipcRenderer } from 'electron'; import { Text, Card, Spinner } from '@blueprintjs/core'; import { Row, Col } from 'react-flexbox-grid'; -import DesktopCapturerSources from '../../features/DesktopCapturerSourcesService'; - -const desktopCapturerSourcesService = remote.getGlobal( - 'desktopCapturerSourcesService' -) as DesktopCapturerSources; +import { IpcEvents } from '../../main/IpcEvents.enum'; class SharingSourcePreviewCardProps { sharingSourceID: string | undefined = ''; @@ -26,19 +22,23 @@ export default function SharingSourcePreviewCard( const [isHovered, setIsHovered] = useState(false); useEffect(() => { setTimeout(async () => { - const sources = desktopCapturerSourcesService.getSourcesMap(); + const sources = await ipcRenderer.invoke( + IpcEvents.GetDesktopCapturerServiceSourcesMap + ); - if (sources && sharingSourceID && sources.get(sharingSourceID)) { + if (sources && sharingSourceID && sources[sharingSourceID]) { setSourceImage( - sources.get(sharingSourceID)?.source.thumbnail.toDataURL() || '' + ((sources[sharingSourceID]?.source.thumbnail as unknown) as string) || + '' ); - if (sources.get(sharingSourceID)?.source.appIcon != null) { + if (sources[sharingSourceID]?.source.appIcon != null) { setAppIconSourceImage( - sources.get(sharingSourceID)?.source.appIcon.toDataURL() || '' + ((sources[sharingSourceID]?.source.appIcon as unknown) as string) || + '' ); } setSourceName( - sources.get(sharingSourceID)?.source.name || + sources[sharingSourceID]?.source.name || 'Failed to get source name...' ); } diff --git a/app/main/IpcEvents.enum.ts b/app/main/IpcEvents.enum.ts index 4670e84..debad58 100644 --- a/app/main/IpcEvents.enum.ts +++ b/app/main/IpcEvents.enum.ts @@ -13,4 +13,5 @@ export enum IpcEvents { DisconnectDeviceById = 'disconnect-device-by-id', DisconnectAllDevices = 'disconnect-all-devices', AppLanguageChanged = 'app-language-changed', + GetDesktopCapturerServiceSourcesMap = 'get-desktop-capturer-service-sources-map', } diff --git a/app/main/ipcMainHandlers.ts b/app/main/ipcMainHandlers.ts index 44b4af0..15339ca 100644 --- a/app/main/ipcMainHandlers.ts +++ b/app/main/ipcMainHandlers.ts @@ -1,3 +1,4 @@ +/* eslint-disable no-restricted-syntax */ import { Display, ipcMain, BrowserWindow, screen } from 'electron'; import settings from 'electron-settings'; import i18n from '../configs/i18next.config'; @@ -187,4 +188,22 @@ export default function initIpcMainHandlers( } ); }); + + ipcMain.handle(IpcEvents.GetDesktopCapturerServiceSourcesMap, () => { + const map = getDeskreenGlobal().desktopCapturerSourcesService.getSourcesMap(); + const res = {}; + // eslint-disable-next-line guard-for-in + for (const key of map.keys()) { + const source = map.get(key); + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + res[key] = { + source: { + thumbnail: source?.source.thumbnail?.toDataURL(), + appIcon: source?.source.appIcon?.toDataURL(), + }, + }; + } + return res; + }); }