Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(trace-viewer): Add setting for display canvas content in snapshots #34010

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
21 changes: 14 additions & 7 deletions packages/trace-viewer/src/sw/snapshotRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,9 @@ declare global {

function snapshotScript(viewport: ViewportSize, ...targetIds: (string | undefined)[]) {
function applyPlaywrightAttributes(unwrapPopoutUrl: (url: string) => string, viewport: ViewportSize, ...targetIds: (string | undefined)[]) {
const isUnderTest = new URLSearchParams(location.search).has('isUnderTest');
const searchParams = new URLSearchParams(location.search);
const shouldPopulateCanvasFromScreenshot = searchParams.has('shouldPopulateCanvasFromScreenshot');
const isUnderTest = searchParams.has('isUnderTest');

// info to recursively compute canvas position relative to the top snapshot frame.
// Before rendering each iframe, its parent extracts the '__playwright_canvas_render_info__' attribute
Expand Down Expand Up @@ -512,15 +514,20 @@ function snapshotScript(viewport: ViewportSize, ...targetIds: (string | undefine

drawCheckerboard(context, canvas);

context.drawImage(img, boundingRect.left * img.width, boundingRect.top * img.height, (boundingRect.right - boundingRect.left) * img.width, (boundingRect.bottom - boundingRect.top) * img.height, 0, 0, canvas.width, canvas.height);
if (shouldPopulateCanvasFromScreenshot) {
context.drawImage(img, boundingRect.left * img.width, boundingRect.top * img.height, (boundingRect.right - boundingRect.left) * img.width, (boundingRect.bottom - boundingRect.top) * img.height, 0, 0, canvas.width, canvas.height);

if (partiallyUncaptured)
canvas.title = `Playwright couldn't capture full canvas contents because it's located partially outside the viewport.`;
else
canvas.title = `Canvas contents are displayed on a best-effort basis based on viewport screenshots taken during test execution.`;
} else {
canvas.title = 'Canvas content display is disabled.';
Copy link
Contributor Author

@agg23 agg23 Dec 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Open to comments/suggestions on these strings

}

if (isUnderTest)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This belongs to the conditional block above.

// eslint-disable-next-line no-console
console.log(`canvas drawn:`, JSON.stringify([boundingRect.left, boundingRect.top, (boundingRect.right - boundingRect.left), (boundingRect.bottom - boundingRect.top)].map(v => Math.floor(v * 100))));

if (partiallyUncaptured)
canvas.title = `Playwright couldn't capture full canvas contents because it's located partially outside the viewport.`;
else
canvas.title = `Canvas contents are displayed on a best-effort basis based on viewport screenshots taken during test execution.`;
}
};
img.onerror = () => {
Expand Down
42 changes: 42 additions & 0 deletions packages/trace-viewer/src/ui/defaultSettingsView.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as React from 'react';
import { SettingsView } from './settingsView';
import { useDarkModeSetting } from '@web/theme';
import { useShouldPopulateCanvasFromScreenshot } from './settings/useShouldPopulateCanvasFromScreenshot';

export const DefaultSettingsView: React.FC<{}> = () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is default settings view and how is it different from generic settings view? Put them into one file and clarify the difference?

const [
shouldPopulateCanvasFromScreenshot,
setShouldPopulateCanvasFromScreenshot,
] = useShouldPopulateCanvasFromScreenshot();
const [darkMode, setDarkMode] = useDarkModeSetting();

return (
<SettingsView
settings={[
{ value: darkMode, set: setDarkMode, name: 'Dark mode' },
{
value: shouldPopulateCanvasFromScreenshot,
set: setShouldPopulateCanvasFromScreenshot,
name: 'Display canvas content',
title: 'Attempt to display the captured canvas appearance in the snapshot preview. May not be accurate.'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Open to comments/suggestions on these strings

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Best effort canvas element visualization." ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That string is very long. I'd prefer it be shorter so it fits better in the settings UI. I do like the content though.

},
]}
/>
);
};
3 changes: 2 additions & 1 deletion packages/trace-viewer/src/ui/recorder/recorderView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,8 @@ const TraceView: React.FunctionComponent<{
return snapshot.action || snapshot.after || snapshot.before;
}, [action]);
const snapshotUrls = React.useMemo(() => {
return snapshot ? extendSnapshot(snapshot) : undefined;
// TODO: Use actual setting. Requires settings UI to be wired up
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean the patch is not complete?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seemed to me that this Recorder trace view was not complete. It needs its own settings UI to integrate the canvas setting, and the menu doesn't trivially integrate with Recorder trace view because of annoying CSS issues.

return snapshot ? extendSnapshot(snapshot, false) : undefined;
}, [snapshot]);

return <SnapshotView
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { useSetting } from '@web/uiUtils';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a little too fine-grained to me, inline?


export const useShouldPopulateCanvasFromScreenshot = (): [
boolean,
(value: boolean) => void
] => useSetting('shouldPopulateCanvasFromScreenshot', false);
21 changes: 21 additions & 0 deletions packages/trace-viewer/src/ui/settingsToolbar.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
Copyright (c) Microsoft Corporation.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

.settings-toolbar-dialog {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

background-color: var(--vscode-sideBar-background);

padding: 4px 8px;
}
49 changes: 49 additions & 0 deletions packages/trace-viewer/src/ui/settingsToolbarButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Copyright (c) Microsoft Corporation.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import * as React from 'react';
import { Dialog } from './shared/dialog';
import { ToolbarButton } from '@web/components/toolbarButton';
import { DefaultSettingsView } from './defaultSettingsView';
import './settingsToolbar.css';

export const SettingsToolbarButton: React.FC<{}> = () => {
const hostingRef = React.useRef<HTMLButtonElement>(null);

const [open, setOpen] = React.useState(false);

return (
<>
<ToolbarButton
ref={hostingRef}
icon='settings-gear'
title='Settings'
onClick={() => setOpen(current => !current)}
/>
<Dialog
className='settings-toolbar-dialog'
open={open}
width={200}
// TODO: Temporary spacing until design of toolbar buttons is revisited
verticalOffset={8}
requestClose={() => setOpen(false)}
hostingElement={hostingRef}
>
<DefaultSettingsView />
</Dialog>
</>
);
};
20 changes: 11 additions & 9 deletions packages/trace-viewer/src/ui/settingsView.css
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,27 @@
*/

.settings-view {
display: flex;
flex: none;
margin-top: 4px;
padding: 4px 0px;
row-gap: 8px;
user-select: none;
}

.settings-view .setting label {
.settings-view .setting {
display: flex;
flex-direction: row;
align-items: center;
margin: 4px 2px;
}

.settings-view .setting:first-of-type label {
margin-top: 2px;
}
.settings-view .setting label {
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;

.settings-view .setting:last-of-type label {
margin-bottom: 2px;
cursor: pointer;
}

.settings-view .setting input {
margin-right: 5px;
flex-shrink: 0;
}
38 changes: 24 additions & 14 deletions packages/trace-viewer/src/ui/settingsView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,32 @@ import * as React from 'react';
import './settingsView.css';

export type Setting<T> = {
value: T,
set: (value: T) => void,
title: string
value: T;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Land this little refactoring separately, pre-work

set: (value: T) => void;
name: string;
title?: string;
};

export const SettingsView: React.FunctionComponent<{
settings: Setting<boolean>[],
settings: Setting<boolean>[];
}> = ({ settings }) => {
return <div className='vbox settings-view'>
{settings.map(({ value, set, title }) => {
return <div key={title} className='setting'>
<label>
<input type='checkbox' checked={value} onChange={() => set(!value)}/>
{title}
</label>
</div>;
})}
</div>;
return (
<div className='vbox settings-view'>
{settings.map(({ value, set, name, title }) => {
const labelId = `setting-${name}`;

return (
<div key={name} className='setting' title={title}>
<input
type='checkbox'
id={labelId}
checked={value}
onChange={() => set(!value)}
/>
<label htmlFor={labelId}>{name}</label>
</div>
);
})}
</div>
);
};
Loading
Loading