Bug 872078 - Added preview and info for fonts in Response view of Netmonitor. r=bomsy

A preview for fonts is added to the Response view of the Netmonitor. This view displays the name and MIME type of the font and generates a preview for it.

If no preview can be created, a hint is shown to the user. This is the case when the font isn't used within the page when generating the preview. For example, this is the case when loading the font via the Font Loading API.

Depends on D110167

Differential Revision: https://phabricator.services.mozilla.com/D110169
This commit is contained in:
Sebastian Zartner 2021-04-22 18:53:39 +00:00
parent 4adcd267a1
commit 12080be6d9
5 changed files with 164 additions and 4 deletions

View File

@ -1080,7 +1080,7 @@ netmonitor.summary.editAndResend=Edit and Resend
netmonitor.headers.raw=Raw
# LOCALIZATION NOTE (netmonitor.response.name): This is the label displayed
# in the network details response tab identifying an image's file name.
# in the network details response tab identifying an image's file name or font face's name.
netmonitor.response.name=Name:
# LOCALIZATION NOTE (netmonitor.response.dimensions): This is the label displayed
@ -1088,9 +1088,14 @@ netmonitor.response.name=Name:
netmonitor.response.dimensions=Dimensions:
# LOCALIZATION NOTE (netmonitor.response.mime): This is the label displayed
# in the network details response tab identifying an image's mime.
# in the network details response tab identifying an image's or font's MIME type.
netmonitor.response.mime=MIME Type:
# LOCALIZATION NOTE (netmonitor.response.fontPreviewFailed): This is the notice displayed
# in the network details response tab if the font preview could not be generated due to
# an error.
netmonitor.response.fontPreviewFailed=Font preview could not be generated
# LOCALIZATION NOTE (netmonitor.timings.blocked): This is the label displayed
# in the network details timings tab identifying the amount of time spent
# in a "blocked" state.

View File

@ -255,6 +255,21 @@
max-height: 100px;
}
.network-monitor #response-panel .response-font-box {
overflow-y: auto;
padding: 10px;
display: flex;
flex-direction: column;
justify-content: center;
}
.network-monitor .response-font {
margin-bottom: 10px;
width: 100%;
height: 100%;
object-fit: contain;
}
.network-monitor .tree-container .treeTable tr.response-preview-container {
flex: 1;
min-height: 0;

View File

@ -0,0 +1,129 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
const { gDevTools } = require("devtools/client/framework/devtools");
const { Component } = require("devtools/client/shared/vendor/react");
const PropTypes = require("devtools/client/shared/vendor/react-prop-types");
const { L10N } = require("devtools/client/netmonitor/src/utils/l10n");
const {
div,
img,
} = require("devtools/client/shared/vendor/react-dom-factories");
const { getColor } = require("devtools/client/shared/theme");
const FONT_NAME = L10N.getStr("netmonitor.response.name");
const FONT_MIME_TYPE = L10N.getStr("netmonitor.response.mime");
const FONT_PREVIEW_FAILED = L10N.getStr(
"netmonitor.response.fontPreviewFailed"
);
const FONT_PREVIEW_TEXT =
"ABCDEFGHIJKLM\nNOPQRSTUVWXYZ\nabcdefghijklm\nnopqrstuvwxyz\n0123456789";
class FontPreview extends Component {
static get propTypes() {
return {
connector: PropTypes.object.isRequired,
mimeType: PropTypes.string,
url: PropTypes.string,
};
}
constructor(props) {
super(props);
this.state = {
name: "",
dataURL: "",
};
this.onThemeChanged = this.onThemeChanged.bind(this);
}
componentDidMount() {
this.getPreview();
// Listen for theme changes as the color of the preview depends on the theme
gDevTools.on("theme-switched", this.onThemeChanged);
}
componentDidUpdate(prevProps) {
const { url } = this.props;
if (prevProps.url !== url) {
this.getPreview();
}
}
componentWillUnmount() {
gDevTools.off("theme-switched", this.onThemeChanged);
}
/**
* Handler for the "theme-switched" event.
*/
onThemeChanged(frame) {
if (frame === window) {
this.getPreview();
}
}
/**
* Generate the font preview and receives information about the font.
*/
async getPreview() {
const { connector } = this.props;
const toolbox = connector.getToolbox();
const inspectorFront = await toolbox.target.getFront("inspector");
const { pageStyle } = inspectorFront;
const pageFontFaces = await pageStyle.getAllUsedFontFaces({
includePreviews: true,
includeVariations: false,
previewText: FONT_PREVIEW_TEXT,
previewFillStyle: getColor("body-color"),
});
const fontFace = pageFontFaces.find(
pageFontFace => pageFontFace.URI === this.props.url
);
this.setState({
name: fontFace?.name ?? "",
dataURL: (await fontFace?.preview.data.string()) ?? "",
});
}
render() {
const { mimeType } = this.props;
const { name, dataURL } = this.state;
if (dataURL === "") {
return div({ className: "empty-notice" }, FONT_PREVIEW_FAILED);
}
return div(
{ className: "panel-container response-font-box devtools-monospace" },
img({
className: "response-font",
src: dataURL,
alt: "",
}),
div(
{ className: "response-summary" },
div({ className: "tabpanel-summary-label" }, FONT_NAME),
div({ className: "tabpanel-summary-value" }, name)
),
div(
{ className: "response-summary" },
div({ className: "tabpanel-summary-label" }, FONT_MIME_TYPE),
div({ className: "tabpanel-summary-value" }, mimeType)
)
);
}
}
module.exports = FontPreview;

View File

@ -3,5 +3,9 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
DevToolsModules(
"HtmlPreview.js", "ImagePreview.js", "SourcePreview.js", "UrlPreview.js"
"FontPreview.js",
"HtmlPreview.js",
"ImagePreview.js",
"SourcePreview.js",
"UrlPreview.js",
)

View File

@ -30,6 +30,9 @@ const PropertiesView = createFactory(
const ImagePreview = createFactory(
require("devtools/client/netmonitor/src/components/previews/ImagePreview")
);
const FontPreview = createFactory(
require("devtools/client/netmonitor/src/components/previews/FontPreview")
);
const SourcePreview = createFactory(
require("devtools/client/netmonitor/src/components/previews/SourcePreview")
);
@ -226,10 +229,14 @@ class ResponsePanel extends Component {
let { encoding, mimeType, text } = responseContent.content;
if (mimeType.includes("image/")) {
if (Filters.images({ mimeType })) {
return ImagePreview({ encoding, mimeType, text, url });
}
if (Filters.fonts({ url, mimeType })) {
return FontPreview({ connector, mimeType, url });
}
// Decode response if it's coming from JSONView.
if (mimeType.includes(JSON_VIEW_MIME_TYPE) && encoding === "base64") {
text = decodeUnicodeBase64(text);