import { BrowserWindow , screen , shell } from "electron" ; import { Logger } from 'polar-shared/src/logger/Logger' ; import { ResourcePaths } from '../../electron/webresource/ResourcePaths' ; import { ElectronUserAgents } from "../electron_browser/ElectronUserAgents" ; import { ExternalNavigationBlock } from "../../electron/navigation/ExternalNavigationBlock" ; const log = Logger . create ( ) ; const WIDTH = 900 * 1.2 ; const HEIGHT = 1100 * 1.2 ; const SIDEBAR_BUFFER = 100 ; const DEFAULT_URL = ResourcePaths . resourceURLFromRelativeURL ( './apps/repository/index.html' ) ; export const MAIN_SESSION_PARTITION_NAME = 'persist:polar-app' ; export const APP_ICON = ResourcePaths . resourceURLFromRelativeURL ( './icon.png' ) ; export const BROWSER_WINDOW_OPTIONS = Object . freeze ( { backgroundColor : '#FFF' , width : WIDTH + SIDEBAR_BUFFER , height : HEIGHT , show : false , icon : APP_ICON , webPreferences : { nodeIntegration : true , defaultEncoding : 'UTF-8' , webSecurity : false , webaudio : true , /**
         * Use a persistent cookie session between restarts.  This is used so
         * that we keep user cookies including Google Analytics cookies.
         */ partition : MAIN_SESSION_PARTITION_NAME , } } ) ; Deprecated export class MainAppBrowserWindowFactory { public static createWindow ( browserWindowOptions = BROWSER_WINDOW_OPTIONS , url = DEFAULT_URL ) { browserWindowOptions = Object . assign ( { } , browserWindowOptions ) ; const position = this . computeXY ( ) ; if ( position ) { browserWindowOptions . x = position . x ; browserWindowOptions . y = position . y ; } const display = screen . getPrimaryDisplay ( ) ; interface DimensionMapping { readonly original : 'minWidth' | 'minHeight' | 'width' | 'height' ; readonly dimension : 'width' | 'height' ; readonly defaultValue ? : number ; } const dimensionMappings = [ { original : 'minHeight' , dimension : 'height' , defaultValue : 800 } , { original : 'minWidth' , dimension : 'width' , defaultValue : 600 } , { original : 'height' , dimension : 'height' } , { original : 'width' , dimension : 'width' } ] ; for ( const dimensionMapping of dimensionMappings ) { const current = browserWindowOptions [ dimensionMapping . original ] ! || dimensionMapping . defaultValue ! ; const max = display . size [ dimensionMapping . dimension ] ; browserWindowOptions [ dimensionMapping . original ] = Math . min ( current , max ) ; } const browserWindow = new BrowserWindow ( browserWindowOptions ) ; browserWindow . webContents . on ( 'new-window' , ( e , newURL ) => { if ( ExternalNavigationBlock . get ( ) ) { e . preventDefault ( ) ; shell . openExternal ( newURL ) . catch ( err => log . error ( "Could not open external URL" , err , newURL ) ) ; } else { log . notice ( "Allowing external navigation to new window URL: " + newURL ) ; } } ) ; browserWindow . webContents . on ( 'will-navigate' , ( e , navURL ) => { const parsedURL = new URL ( navURL ) ; const host = parsedURL . hostname ; if ( host === "localhost" ) { log . info ( "Always allowing localhost URL" ) ; return ; } if ( ExternalNavigationBlock . get ( ) ) { log . info ( "Attempt to navigate to new URL: " , navURL ) ; e . preventDefault ( ) ; shell . openExternal ( navURL ) . catch ( err => log . error ( "Cloud open external URL" , err , url ) ) ; } else { log . notice ( "Allowing external navigation to: " + navURL ) ; return ; } } ) ; const userAgent = ElectronUserAgents . computeUserAgentFromWebContents ( browserWindow . webContents ) ; log . info ( "Loading URL: " + url ) ; browserWindow . loadURL ( url , { userAgent } ) . catch ( err => log . error ( "Could not load URL " , err , url ) ) ; return new Promise < BrowserWindow > ( resolve => { browserWindow . once ( 'ready-to-show' , ( ) => { browserWindow . webContents . zoomFactor = 1.0 ; browserWindow . show ( ) ; resolve ( browserWindow ) ; } ) ; } ) ; } private static computeXY ( ) { const offset = 35 ; const focusedWindow = BrowserWindow . getFocusedWindow ( ) ; if ( focusedWindow ) { const position = focusedWindow . getPosition ( ) ; let x = position [ 0 ] ; let y = position [ 1 ] ; x += offset ; y += offset ; return { x , y } ; } return undefined ; } } interface Position { x : number ; y : number ; }