import { IXYRect } from 'polar-shared/src/util/rects/IXYRect' ; import { IXYRects } from 'polar-shared/src/util/rects/IXYRects' ; import { CaptureOpts , DefaultCaptureOpts , Screenshot , ScreenshotRequest } from '../Screenshot' ; import { ClientRects } from 'polar-shared/src/util/rects/ClientRects' ; import { Logger } from 'polar-shared/src/logger/Logger' ; import { IScreenshotDelegate , ScreenshotDelegate , WebContentsID } from './ScreenshotDelegate' ; import { remote } from 'electron' ; import { Promises } from '../../util/Promises' ; import { AnnotationToggler } from '../AnnotationToggler' ; import { AppRuntime } from 'polar-shared/src/util/AppRuntime' ; const log = Logger . create ( ) ; const MIN_PAINT_INTERVAL = 1000 / 60 ; ElectronRendererContext export class ElectronScreenshots { public static supported ( ) { return AppRuntime . isElectron ( ) ; } param target param opts return { Promise } public static async capture ( target , opts = new DefaultCaptureOpts ( ) ) { if ( ! this . supported ( ) ) { throw new Error ( "Captured screenshots not supported" ) ; } const screenshotRequest = await this . doCapture ( target , opts ) ; log . info ( "Sending screenshot request: " , screenshotRequest ) ; const id = this . getWebContentsID ( ) ; const annotationToggler = new AnnotationToggler ( ) ; await annotationToggler . hide ( ) ; const capturedScreenshot = await this . getRemoteDelegate ( ) . capture ( id , screenshotRequest ) ; await Promises . requestAnimationFrame ( ( ) => annotationToggler . show ( ) ) ; return capturedScreenshot ; } public static async captureToFile ( target , dest , opts ) { } private static getRemoteDelegate ( ) { return remote . getGlobal ( ScreenshotDelegate . DELEGATE_NAME ) ; } private static getWebContentsID ( ) { return remote . getCurrentWebContents ( ) . id ; } private static async doCapture ( target , opts ) { let rect ; if ( target instanceof HTMLElement ) { log . info ( "Using HTML element to build rect from bounding client rect." ) ; rect = IXYRects . createFromClientRect ( target . getBoundingClientRect ( ) ) ; } else if ( ClientRects . instanceOf ( target ) ) { rect = { x : target . left , y : target . top , width : target . width , height : target . height } ; log . info ( "Using client rect: " , rect ) ; } else if ( IXYRects . instanceOf ( target ) ) { log . info ( "Using IXYRect" ) ; rect = target ; } else { throw new Error ( "Unknown target type." ) ; } const screenshotRequest = { rect , resize : opts . resize , crop : opts . crop , type : opts . type } ; return screenshotRequest ; } } export type CaptureTarget = IXYRect | ClientRect ; export interface StyleRestore { readonly visibility : string | null ; } export interface AnnotationStyle { readonly element : HTMLElement ; readonly styleRestore : StyleRestore ; }