electron#Display TypeScript Examples

The following examples show how to use electron#Display. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: wallpaper_manager.ts    From SpaceEye with MIT License 6 votes vote down vote up
/**
     * Get all non-internal monitors.
     *
     * @returns All non-internal monitors
     */
    private static getMonitors(): Display[] {
        const allMonitors = screen.getAllDisplays().filter(monitor => !monitor.internal)
        log.debug(
            'All monitors:',
            allMonitors.map(monitor => monitor.id)
        )
        return allMonitors
    }
Example #2
Source File: wallpaper_manager.ts    From SpaceEye with MIT License 6 votes vote down vote up
/**
     * Select the image source that best matches the monitor.
     *
     * @param monitor - Monitor in question
     * @param sources - Possible image sources
     * @returns Selected image
     */
    private static selectImageSourceForMonitor(
        monitor: Display,
        sources: ImageSource[]
    ): ImageSource {
        for (const source of sources) {
            if (
                source.dimensions[0] > monitor.scaleFactor * monitor.size.width &&
                source.dimensions[1] > monitor.scaleFactor * monitor.size.height
            ) {
                return source
            }
        }
        return sources[sources.length - 1]
    }
Example #3
Source File: wallpaper_manager.ts    From SpaceEye with MIT License 6 votes vote down vote up
/**
     * Given a view, determine the optimal image needed.
     *
     * Image is selected based on the resolution of available screens.
     *
     * @param view - Satellite view
     * @param monitors - Computer monitors currently available
     * @returns Selected image
     */
    private static getOptimalImageFromView(view: SatelliteView, monitors: Display[]): ImageSource {
        // Get the best image size for each monitor
        const possibleImages = monitors.map(monitor =>
            WallpaperManager.selectImageSourceForMonitor(monitor, view.imageSources)
        )
        // Pick the biggest one
        const bestImage = maxBy(possibleImages, image => image.dimensions[0] * image.dimensions[1])!
        log.debug(`Optimal image for view "${view.id}": ${bestImage.id}`)
        return bestImage
    }
Example #4
Source File: wallpaper_manager.ts    From SpaceEye with MIT License 6 votes vote down vote up
/**
     * Check if the monitor configuration has changed.
     *
     * @param oldMonitors - Old configuration of monitors
     * @param newMonitors - New configuration of monitors
     * @returns Whether the config has changed
     */
    private static haveMonitorsChanged(oldMonitors: Display[], newMonitors: Display[]): boolean {
        // TODO: Check monitor sizes too
        return oldMonitors.length !== newMonitors.length
    }
Example #5
Source File: window.ts    From SideQuest with MIT License 6 votes vote down vote up
private getWorkingArea(display: Display): WorkingArea | undefined {
        if (display.workArea.width > 0 && display.workArea.height > 0) {
            return display.workArea;
        }

        if (display.bounds.width > 0 && display.bounds.height > 0) {
            return display.bounds;
        }

        return undefined;
    }
Example #6
Source File: window.ts    From SideQuest with MIT License 5 votes vote down vote up
private validateWindowState(state: WindowState, displays: Display[]): WindowState {
        if (
            typeof state.x !== 'number' ||
            typeof state.y !== 'number' ||
            typeof state.width !== 'number' ||
            typeof state.height !== 'number'
        ) {
            return undefined;
        }

        if (state.width <= 0 || state.height <= 0) {
            return undefined;
        }

        if (displays.length === 1) {
            const displayWorkingArea = this.getWorkingArea(displays[0]);
            if (displayWorkingArea) {
                const ensureStateInDisplayWorkingArea = () => {
                    const isTooFarLeft = state.x < displayWorkingArea.x;
                    if (isTooFarLeft) {
                        state.x = displayWorkingArea.x;
                    }
                    const isTooFarUp = state.y < displayWorkingArea.y;
                    if (isTooFarUp) {
                        state.y = displayWorkingArea.y;
                    }
                };
                ensureStateInDisplayWorkingArea();
                const isTooWide = state.width > displayWorkingArea.width;
                if (isTooWide) {
                    state.width = displayWorkingArea.width;
                }
                const isTooTall = state.height > displayWorkingArea.height;
                if (isTooTall) {
                    state.height = displayWorkingArea.height;
                }
                const isTooFarRight = state.x > displayWorkingArea.x + displayWorkingArea.width - 128;
                if (isTooFarRight) {
                    state.x = displayWorkingArea.x + displayWorkingArea.width - 128;
                }
                const isTooFarDown = state.y > displayWorkingArea.y + displayWorkingArea.height - 128;
                if (isTooFarDown) {
                    state.y = displayWorkingArea.y + displayWorkingArea.height - 128;
                }
                ensureStateInDisplayWorkingArea();
            }
            return state;
        } else {
            const display = screen.getDisplayMatching({ x: state.x, y: state.y, width: state.width, height: state.height });
            const displayWorkingArea = this.getWorkingArea(display);
            if (
                display &&
                displayWorkingArea &&
                state.x + state.width > displayWorkingArea.x &&
                state.y + state.height > displayWorkingArea.y &&
                state.x < displayWorkingArea.x + displayWorkingArea.width &&
                state.y < displayWorkingArea.y + displayWorkingArea.height
            ) {
                return state;
            } else {
                return undefined;
            }
        }
    }
Example #7
Source File: ipcMainHandlers.ts    From deskreen with GNU Affero General Public License v3.0 4 votes vote down vote up
export default function initIpcMainHandlers(mainWindow: BrowserWindow) {
  ipcMain.on('client-changed-language', async (_, newLangCode) => {
    i18n.changeLanguage(newLangCode);
    if (store.has(ElectronStoreKeys.AppLanguage)) {
      if (store.get(ElectronStoreKeys.AppLanguage) === newLangCode) {
        return;
      }
      store.delete(ElectronStoreKeys.AppLanguage);
    }
    store.set(ElectronStoreKeys.AppLanguage, newLangCode);
  });

  ipcMain.handle('get-signaling-server-port', () => {
    if (mainWindow === null) return;
    mainWindow.webContents.send('sending-port-from-main', signalingServer.port);
  });

  ipcMain.handle('get-all-displays', () => {
    return screen.getAllDisplays();
  });

  ipcMain.handle('get-display-size-by-display-id', (_, displayID: string) => {
    const display = screen.getAllDisplays().find((d: Display) => {
      return `${d.id}` === displayID;
    });

    if (display) {
      return display.size;
    }
    return undefined;
  });

  ipcMain.handle('main-window-onbeforeunload', () => {
    const deskreenGlobal = getDeskreenGlobal();
    deskreenGlobal.connectedDevicesService = new ConnectedDevicesService();
    deskreenGlobal.roomIDService = new RoomIDService();
    deskreenGlobal.sharingSessionService.sharingSessions.forEach(
      (sharingSession: SharingSession) => {
        sharingSession.denyConnectionForPartner();
        sharingSession.destroy();
      }
    );

    deskreenGlobal.rendererWebrtcHelpersService.helpers.forEach(
      (helperWindow) => {
        helperWindow.close();
      }
    );

    deskreenGlobal.sharingSessionService.waitingForConnectionSharingSession = null;
    deskreenGlobal.rendererWebrtcHelpersService.helpers.clear();
    deskreenGlobal.sharingSessionService.sharingSessions.clear();
  });

  ipcMain.handle('get-latest-version', () => {
    return getDeskreenGlobal().latestAppVersion;
  });

  ipcMain.handle('get-current-version', () => {
    return getDeskreenGlobal().currentAppVersion;
  });

  ipcMain.handle('get-local-lan-ip', async () => {
    if (
      process.env.RUN_MODE === 'dev' ||
      process.env.NODE_ENV === 'production'
    ) {
      const ip = await v4IPGetter();
      return ip;
    }
    return '255.255.255.255';
  });

  ipcMain.handle(IpcEvents.GetAppPath, () => {
    const deskreenGlobal = getDeskreenGlobal();
    return deskreenGlobal.appPath;
  });

  ipcMain.handle(IpcEvents.UnmarkRoomIDAsTaken, (_, roomID) => {
    const deskreenGlobal = getDeskreenGlobal();
    deskreenGlobal.roomIDService.unmarkRoomIDAsTaken(roomID);
  });

  function onDeviceConnectedCallback(device: Device): void {
    getDeskreenGlobal().connectedDevicesService.setPendingConnectionDevice(
      device
    );
    mainWindow.webContents.send(IpcEvents.SetPendingConnectionDevice, device);
  }

  ipcMain.handle(IpcEvents.CreateWaitingForConnectionSharingSession, () => {
    getDeskreenGlobal()
      .sharingSessionService.createWaitingForConnectionSharingSession()
      // eslint-disable-next-line promise/always-return
      .then((waitingForConnectionSharingSession) => {
        waitingForConnectionSharingSession.setOnDeviceConnectedCallback(
          onDeviceConnectedCallback
        );
      })
      .catch((e) => log.error(e));
  });

  ipcMain.handle(IpcEvents.ResetWaitingForConnectionSharingSession, () => {
    const sharingSession = getDeskreenGlobal().sharingSessionService
      .waitingForConnectionSharingSession;
    sharingSession?.disconnectByHostMachineUser();
    sharingSession?.destroy();
    sharingSession?.setStatus(SharingSessionStatusEnum.NOT_CONNECTED);
    getDeskreenGlobal().sharingSessionService.sharingSessions.delete(
      sharingSession?.id as string
    );
    getDeskreenGlobal().sharingSessionService.waitingForConnectionSharingSession = null;
  });

  ipcMain.handle(IpcEvents.SetDeviceConnectedStatus, () => {
    if (
      getDeskreenGlobal().sharingSessionService
        .waitingForConnectionSharingSession !== null
    ) {
      const sharingSession = getDeskreenGlobal().sharingSessionService
        .waitingForConnectionSharingSession;
      sharingSession?.setStatus(SharingSessionStatusEnum.CONNECTED);
    }
  });

  ipcMain.handle(
    IpcEvents.GetSourceDisplayIDByDesktopCapturerSourceID,
    (_, sourceId) => {
      return getDeskreenGlobal().desktopCapturerSourcesService.getSourceDisplayIDByDisplayCapturerSourceID(
        sourceId
      );
    }
  );

  ipcMain.handle(
    IpcEvents.DisconnectPeerAndDestroySharingSessionBySessionID,
    (_, sessionId) => {
      const sharingSession = getDeskreenGlobal().sharingSessionService.sharingSessions.get(
        sessionId
      );
      if (sharingSession) {
        getDeskreenGlobal().connectedDevicesService.disconnectDeviceByID(
          sharingSession.deviceID
        );
      }
      sharingSession?.disconnectByHostMachineUser();
      sharingSession?.destroy();
      getDeskreenGlobal().sharingSessionService.sharingSessions.delete(
        sessionId
      );
    }
  );

  ipcMain.handle(
    IpcEvents.GetDesktopCapturerSourceIdBySharingSessionId,
    (_, sessionId) => {
      return getDeskreenGlobal().sharingSessionService.sharingSessions.get(
        sessionId
      )?.desktopCapturerSourceID;
    }
  );

  ipcMain.handle(IpcEvents.GetConnectedDevices, () => {
    return getDeskreenGlobal().connectedDevicesService.getDevices();
  });

  ipcMain.handle(IpcEvents.DisconnectDeviceById, (_, id) => {
    getDeskreenGlobal().connectedDevicesService.disconnectDeviceByID(id);
  });

  ipcMain.handle(IpcEvents.DisconnectAllDevices, () => {
    getDeskreenGlobal().connectedDevicesService.disconnectAllDevices();
  });

  ipcMain.handle(IpcEvents.AppLanguageChanged, (_, newLang) => {
    if (store.has(ElectronStoreKeys.AppLanguage)) {
      store.delete(ElectronStoreKeys.AppLanguage);
    }
    store.set(ElectronStoreKeys.AppLanguage, newLang);
    getDeskreenGlobal().sharingSessionService.sharingSessions.forEach(
      (sharingSession) => {
        sharingSession?.appLanguageChanged();
      }
    );
  });

  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(),
          name: source?.source.name,
        },
      };
    }
    return res;
  });

  ipcMain.handle(
    IpcEvents.GetWaitingForConnectionSharingSessionSourceId,
    () => {
      return getDeskreenGlobal().sharingSessionService
        .waitingForConnectionSharingSession?.desktopCapturerSourceID;
    }
  );

  ipcMain.handle(
    IpcEvents.StartSharingOnWaitingForConnectionSharingSession,
    () => {
      const sharingSession = getDeskreenGlobal().sharingSessionService
        .waitingForConnectionSharingSession;
      if (sharingSession !== null) {
        sharingSession.callPeer();
        sharingSession.status = SharingSessionStatusEnum.SHARING;
      }
      getDeskreenGlobal().connectedDevicesService.addDevice(
        getDeskreenGlobal().connectedDevicesService.pendingConnectionDevice
      );
      getDeskreenGlobal().connectedDevicesService.resetPendingConnectionDevice();
    }
  );

  ipcMain.handle(IpcEvents.GetPendingConnectionDevice, () => {
    return getDeskreenGlobal().connectedDevicesService.pendingConnectionDevice;
  });

  ipcMain.handle(IpcEvents.GetWaitingForConnectionSharingSessionRoomId, () => {
    if (
      getDeskreenGlobal().sharingSessionService
        .waitingForConnectionSharingSession === null
    ) {
      return undefined;
    }
    return getDeskreenGlobal().sharingSessionService
      .waitingForConnectionSharingSession?.roomID;
  });

  ipcMain.handle(
    IpcEvents.GetDesktopSharingSourceIds,
    (_, { isEntireScreenToShareChosen }) => {
      if (isEntireScreenToShareChosen === true) {
        return getDeskreenGlobal()
          .desktopCapturerSourcesService.getScreenSources()
          .map((source) => source.id);
      }
      return getDeskreenGlobal()
        .desktopCapturerSourcesService.getAppWindowSources()
        .map((source) => source.id);
    }
  );

  ipcMain.handle(IpcEvents.SetDesktopCapturerSourceId, (_, id) => {
    getDeskreenGlobal().sharingSessionService.waitingForConnectionSharingSession?.setDesktopCapturerSourceID(
      id
    );
  });

  ipcMain.handle(IpcEvents.NotifyAllSessionsWithAppThemeChanged, () => {
    getDeskreenGlobal().sharingSessionService.sharingSessions.forEach(
      (sharingSession) => {
        sharingSession?.appThemeChanged();
      }
    );
  });

  ipcMain.handle(IpcEvents.GetIsFirstTimeAppStart, () => {
    if (store.has(ElectronStoreKeys.IsNotFirstTimeAppStart)) {
      return false;
    }
    return true;
  });

  ipcMain.handle(IpcEvents.SetAppStartedOnce, () => {
    if (store.has(ElectronStoreKeys.IsNotFirstTimeAppStart)) {
      store.delete(ElectronStoreKeys.IsNotFirstTimeAppStart);
    }
    store.set(ElectronStoreKeys.IsNotFirstTimeAppStart, true);
  });

  ipcMain.handle(IpcEvents.GetIsAppDarkTheme, () => {
    if (store.has(ElectronStoreKeys.IsAppDarkTheme)) {
      return store.get(ElectronStoreKeys.IsAppDarkTheme);
    }
    return false;
  });

  ipcMain.handle(IpcEvents.SetIsAppDarkTheme, (_, isDarkTheme) => {
    if (store.has(ElectronStoreKeys.IsAppDarkTheme)) {
      store.delete(ElectronStoreKeys.IsAppDarkTheme);
    }
    store.set(ElectronStoreKeys.IsAppDarkTheme, isDarkTheme);
  });

  ipcMain.handle(IpcEvents.GetAppLanguage, () => {
    if (store.has(ElectronStoreKeys.AppLanguage)) {
      return store.get(ElectronStoreKeys.AppLanguage);
    }
    return 'en';
  });

  ipcMain.handle(IpcEvents.DestroySharingSessionById, (_, id) => {
    const sharingSession = getDeskreenGlobal().sharingSessionService.sharingSessions.get(
      id
    );
    sharingSession?.setStatus(SharingSessionStatusEnum.DESTROYED);
    sharingSession?.destroy();
    getDeskreenGlobal().sharingSessionService.sharingSessions.delete(id);
  });
}