UNPKG

@yuebai008/cli

Version:

Command line interface for rapid qg-minigame development

1 lines 3.98 kB
import*as Root from"../../core/root/root.js";import*as SDK from"../../core/sdk/sdk.js";import*as UI from"../../ui/legacy/legacy.js";import*as EmulationModel from"../../models/emulation/emulation.js";import{DeviceModeView}from"./DeviceModeView.js";let deviceModeWrapperInstance,actionDelegateInstance;export class DeviceModeWrapper extends UI.Widget.VBox{inspectedPagePlaceholder;deviceModeView;toggleDeviceModeAction;showDeviceModeSetting;constructor(e){super(),this.inspectedPagePlaceholder=e,this.deviceModeView=null,this.toggleDeviceModeAction=UI.ActionRegistry.ActionRegistry.instance().action("emulation.toggle-device-mode");const t=EmulationModel.DeviceModeModel.DeviceModeModel.instance();this.showDeviceModeSetting=t.enabledSetting(),this.showDeviceModeSetting.setRequiresUserAction(Boolean(Root.Runtime.Runtime.queryParam("hasOtherClients"))),this.showDeviceModeSetting.addChangeListener(this.update.bind(this,!1)),SDK.TargetManager.TargetManager.instance().addModelListener(SDK.OverlayModel.OverlayModel,SDK.OverlayModel.Events.ScreenshotRequested,this.screenshotRequestedFromOverlay,this),this.update(!0)}static instance(e={forceNew:null,inspectedPagePlaceholder:null}){const{forceNew:t,inspectedPagePlaceholder:i}=e;if(!deviceModeWrapperInstance||t){if(!i)throw new Error(`Unable to create DeviceModeWrapper: inspectedPagePlaceholder must be provided: ${(new Error).stack}`);deviceModeWrapperInstance=new DeviceModeWrapper(i)}return deviceModeWrapperInstance}toggleDeviceMode(){this.showDeviceModeSetting.set(!this.showDeviceModeSetting.get())}isDeviceModeOn(){return this.showDeviceModeSetting.get()}captureScreenshot(e,t){return this.deviceModeView||(this.deviceModeView=new DeviceModeView),this.deviceModeView.setNonEmulatedAvailableSize(this.inspectedPagePlaceholder.element),e?this.deviceModeView.captureFullSizeScreenshot():t?this.deviceModeView.captureAreaScreenshot(t):this.deviceModeView.captureScreenshot(),!0}screenshotRequestedFromOverlay(e){const t=e.data;this.captureScreenshot(!1,t)}update(e){if(this.toggleDeviceModeAction&&this.toggleDeviceModeAction.setToggled(this.showDeviceModeSetting.get()),!e){const e=this.deviceModeView&&this.deviceModeView.isShowing();if(this.showDeviceModeSetting.get()===e)return}this.showDeviceModeSetting.get()?(this.deviceModeView||(this.deviceModeView=new DeviceModeView),this.deviceModeView.show(this.element),this.inspectedPagePlaceholder.clearMinimumSize(),this.inspectedPagePlaceholder.show(this.deviceModeView.element)):(this.deviceModeView&&(this.deviceModeView.exitHingeMode(),this.deviceModeView.detach()),this.inspectedPagePlaceholder.restoreMinimumSize(),this.inspectedPagePlaceholder.show(this.element))}}export class ActionDelegate{handleAction(e,t){if(DeviceModeWrapper.instance())switch(t){case"emulation.capture-screenshot":return DeviceModeWrapper.instance().captureScreenshot();case"emulation.capture-node-screenshot":{const i=UI.Context.Context.instance().flavor(SDK.DOMModel.DOMNode);if(!i)return!0;async function o(){if(!i)return;const e=await i.resolveToObject();if(!e)return;const t=await e.callFunction((function(){const e=this.getBoundingClientRect(),t=this.ownerDocument.documentElement.getBoundingClientRect();return JSON.stringify({x:e.left-t.left,y:e.top-t.top,width:e.width,height:e.height,scale:1})}));if(!t.object)throw new Error("Clipping error: could not get object data.");const o=JSON.parse(t.object.value),n=await i.domModel().target().pageAgent().invoke_getLayoutMetrics(),c=!n.getError()&&n.visualViewport.zoom||1;o.x*=c,o.y*=c,o.width*=c,o.height*=c,DeviceModeWrapper.instance().captureScreenshot(!1,o)}return o(),!0}case"emulation.capture-full-height-screenshot":return DeviceModeWrapper.instance().captureScreenshot(!0);case"emulation.toggle-device-mode":return DeviceModeWrapper.instance().toggleDeviceMode(),!0}return!1}static instance(e={forceNew:null}){const{forceNew:t}=e;return actionDelegateInstance&&!t||(actionDelegateInstance=new ActionDelegate),actionDelegateInstance}}