forked from ChromeDevTools/devtools-frontend
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathDeviceModeWrapper.ts
More file actions
188 lines (167 loc) · 6.85 KB
/
DeviceModeWrapper.ts
File metadata and controls
188 lines (167 loc) · 6.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import type * as Common from '../../core/common/common.js';
import * as Root from '../../core/root/root.js';
import * as SDK from '../../core/sdk/sdk.js';
import type * as Protocol from '../../generated/protocol.js';
import * as EmulationModel from '../../models/emulation/emulation.js';
import * as UI from '../../ui/legacy/legacy.js';
import {DeviceModeView} from './DeviceModeView.js';
import type {InspectedPagePlaceholder} from './InspectedPagePlaceholder.js';
let deviceModeWrapperInstance: DeviceModeWrapper;
export class DeviceModeWrapper extends UI.Widget.VBox {
private readonly inspectedPagePlaceholder: InspectedPagePlaceholder;
private deviceModeView: DeviceModeView|null;
private readonly toggleDeviceModeAction: UI.ActionRegistration.Action;
private showDeviceModeSetting: Common.Settings.Setting<boolean>;
private constructor(inspectedPagePlaceholder: InspectedPagePlaceholder) {
super();
this.inspectedPagePlaceholder = inspectedPagePlaceholder;
this.deviceModeView = null;
this.toggleDeviceModeAction = UI.ActionRegistry.ActionRegistry.instance().getAction('emulation.toggle-device-mode');
const model = EmulationModel.DeviceModeModel.DeviceModeModel.instance();
this.showDeviceModeSetting = model.enabledSetting();
this.showDeviceModeSetting.setRequiresUserAction(Boolean(Root.Runtime.Runtime.queryParam('hasOtherClients')));
this.showDeviceModeSetting.addChangeListener(this.update.bind(this, false));
SDK.TargetManager.TargetManager.instance().addModelListener(
SDK.OverlayModel.OverlayModel, SDK.OverlayModel.Events.SCREENSHOT_REQUESTED,
this.screenshotRequestedFromOverlay, this);
this.update(true);
}
static instance(opts: {
forceNew: boolean|null,
inspectedPagePlaceholder: InspectedPagePlaceholder|null,
} = {forceNew: null, inspectedPagePlaceholder: null}): DeviceModeWrapper {
const {forceNew, inspectedPagePlaceholder} = opts;
if (!deviceModeWrapperInstance || forceNew) {
if (!inspectedPagePlaceholder) {
throw new Error(
`Unable to create DeviceModeWrapper: inspectedPagePlaceholder must be provided: ${new Error().stack}`);
}
deviceModeWrapperInstance = new DeviceModeWrapper(inspectedPagePlaceholder);
}
return deviceModeWrapperInstance;
}
toggleDeviceMode(): void {
this.showDeviceModeSetting.set(!this.showDeviceModeSetting.get());
}
isDeviceModeOn(): boolean {
return this.showDeviceModeSetting.get();
}
captureScreenshot(fullSize?: boolean, clip?: Protocol.Page.Viewport): boolean {
if (!this.deviceModeView) {
this.deviceModeView = new DeviceModeView();
}
this.deviceModeView.setNonEmulatedAvailableSize(this.inspectedPagePlaceholder.element);
if (fullSize) {
void this.deviceModeView.captureFullSizeScreenshot();
} else if (clip) {
void this.deviceModeView.captureAreaScreenshot(clip);
} else {
void this.deviceModeView.captureScreenshot();
}
return true;
}
private screenshotRequestedFromOverlay(event: Common.EventTarget.EventTargetEvent<Protocol.Page.Viewport>): void {
const clip = event.data;
this.captureScreenshot(false, clip);
}
update(force?: boolean): void {
this.toggleDeviceModeAction.setToggled(this.showDeviceModeSetting.get());
const shouldShow = this.showDeviceModeSetting.get();
if (!force && shouldShow === this.deviceModeView?.isShowing()) {
return;
}
if (shouldShow) {
if (!this.deviceModeView) {
this.deviceModeView = new DeviceModeView();
}
this.deviceModeView.show(this.element);
this.inspectedPagePlaceholder.clearMinimumSize();
this.inspectedPagePlaceholder.show(this.deviceModeView.element);
} else {
if (this.deviceModeView) {
this.deviceModeView.exitHingeMode();
this.deviceModeView.detach();
}
this.inspectedPagePlaceholder.restoreMinimumSize();
this.inspectedPagePlaceholder.show(this.element);
}
}
}
export class ActionDelegate implements UI.ActionRegistration.ActionDelegate {
handleAction(context: UI.Context.Context, actionId: string): boolean {
switch (actionId) {
case 'emulation.capture-screenshot':
if (deviceModeWrapperInstance) {
return DeviceModeWrapper.instance().captureScreenshot();
}
void captureScreenshotDirect();
return true;
case 'emulation.capture-node-screenshot': {
const node = context.flavor(SDK.DOMModel.DOMNode);
if (!node) {
return true;
}
async function captureClip(): Promise<void> {
if (!node) {
return;
}
const object = await node.resolveToObject();
if (!object) {
return;
}
const result = await object.callFunction(function(this: Element) {
const rect = this.getBoundingClientRect();
const docRect = this.ownerDocument.documentElement.getBoundingClientRect();
return JSON.stringify({
x: rect.left - docRect.left,
y: rect.top - docRect.top,
width: rect.width,
height: rect.height,
scale: 1,
});
});
if (!result.object) {
throw new Error('Clipping error: could not get object data.');
}
const clip = (JSON.parse((result.object.value as string)));
const response = await node.domModel().target().pageAgent().invoke_getLayoutMetrics();
const error = response.getError();
const zoom = !error && response.visualViewport.zoom || 1;
clip.x *= zoom;
clip.y *= zoom;
clip.width *= zoom;
clip.height *= zoom;
DeviceModeWrapper.instance().captureScreenshot(false, clip);
}
void captureClip();
return true;
}
case 'emulation.capture-full-height-screenshot':
return DeviceModeWrapper.instance().captureScreenshot(true);
case 'emulation.toggle-device-mode':
DeviceModeWrapper.instance().toggleDeviceMode();
return true;
}
return false;
}
}
async function captureScreenshotDirect(): Promise<void> {
const target = SDK.TargetManager.TargetManager.instance().primaryPageTarget();
const screenCaptureModel = target?.model(SDK.ScreenCaptureModel.ScreenCaptureModel);
if (!screenCaptureModel) {
return;
}
const screenshot = await screenCaptureModel.captureScreenshot(
'png' as Protocol.Page.CaptureScreenshotRequestFormat, 100,
SDK.ScreenCaptureModel.ScreenshotMode.FROM_VIEWPORT);
if (!screenshot) {
return;
}
const link = document.createElement('a');
link.download = 'screenshot.png';
link.href = 'data:image/png;base64,' + screenshot;
link.click();
}