Path: blob/main/plugins/default-browser-emulator/lib/Viewports.ts
1029 views
import IViewport from '@secret-agent/interfaces/IViewport';1import { IDataWindowFraming } from '../interfaces/IBrowserData';23const defaultWindowFraming = {4screenGapLeft: 0,5screenGapTop: 0,6screenGapRight: 0,7screenGapBottom: 0,8frameBorderWidth: 0,9frameBorderHeight: 0,10};1112export const defaultScreen = {13width: 1440,14height: 900,15};1617export default class Viewports {18static getDefault(windowBaseFraming: IDataWindowFraming, windowFraming: IDataWindowFraming) {19windowFraming = windowFraming || { ...defaultWindowFraming };20const base = windowBaseFraming || { ...defaultWindowFraming };21const screenWidth = defaultScreen.width;22const screenHeight = defaultScreen.height;2324const windowInnerWidth =25screenWidth - (base.screenGapLeft + base.screenGapRight + base.frameBorderWidth);26const windowWidth = windowInnerWidth + windowFraming.frameBorderWidth;2728const windowInnerHeight =29screenHeight - (base.screenGapTop + base.screenGapBottom + base.frameBorderHeight);30const windowHeight = windowInnerHeight + windowFraming.frameBorderHeight;3132const availableScreenWidth =33screenWidth - (windowFraming.screenGapLeft + windowFraming.screenGapRight);34const leftoverSpacingWidth = availableScreenWidth - windowWidth;35const positionX = randomIntFromInterval(36windowFraming.screenGapLeft,37windowFraming.screenGapLeft + leftoverSpacingWidth,38);3940const availableScreenHeight =41screenHeight - (windowFraming.screenGapTop + windowFraming.screenGapBottom);42const leftoverSpacingHeight = availableScreenHeight - windowHeight;43const positionY = randomIntFromInterval(44windowFraming.screenGapTop,45windowFraming.screenGapTop + leftoverSpacingHeight,46);4748return {49positionX,50positionY,51screenWidth,52screenHeight,53width: windowWidth,54height: windowHeight,55deviceScaleFactor: 1,56} as IViewport;57}58}5960// HELPERS6162function randomIntFromInterval(min, max) {63if (min === max) return min;64return Math.floor(Math.random() * (max - min + 1) + min);65}666768