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

Fix FontMeasurementsImpl to work in a web environment #2260

Merged
merged 9 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/cursorless-vscode/resources/font_measurements.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const letter = document.querySelector("#letter");
const container = document.querySelector("#container");
const baselineHeight =
letter.offsetTop +
letter.offsetHeight -
container.offsetHeight -
container.offsetTop;
const vscode = acquireVsCodeApi();
vscode.postMessage({
widthRatio: letter.offsetWidth / 1000,
heightRatio: (letter.offsetHeight - baselineHeight) / 1000,
});
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class FontMeasurementsImpl implements FontMeasurements {
}>("fontRatios");

if (fontRatiosCache == null || fontRatiosCache.fontFamily !== fontFamily) {
const fontRatios = await getFontRatios();
const fontRatios = await getFontRatios(this.extensionContext);
this.extensionContext.globalState.update("fontRatios", {
...fontRatios,
fontFamily,
Expand All @@ -57,7 +57,7 @@ export class FontMeasurementsImpl implements FontMeasurements {
*
* @returns The width and height ratios of the font
*/
function getFontRatios() {
function getFontRatios(extensionContext: vscode.ExtensionContext) {
const panel = vscode.window.createWebviewPanel(
"cursorless.loading",
"Cursorless",
Expand All @@ -70,7 +70,14 @@ function getFontRatios() {
},
);

panel.webview.html = getWebviewContent();
const font_measurement_js = panel.webview.asWebviewUri(
vscode.Uri.joinPath(
extensionContext.extensionUri,
"resources",
"font_measurements.js",
),
);
panel.webview.html = getWebviewContent(panel.webview, font_measurement_js);

interface FontRatios {
/**
Expand Down Expand Up @@ -103,25 +110,23 @@ function getFontFamily() {
return config.get<string>("fontFamily")!;
}

function getWebviewContent() {
function getWebviewContent(
webview: vscode.Webview,
font_measurement_js: vscode.Uri,
) {
// baseline adjustment based on https://stackoverflow.com/a/27295528
return `<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Security-Policy" content="script-src ${webview.cspSource};" />
</head>

<body>
<h1>Loading Cursorless...</h1>
<div id="container">
<span id="letter" style="line-height: 0; visibility:hidden; font-size: 1000px; font-family: var(--vscode-editor-font-family); font-weight: var(--vscode-editor-font-weight);">A</span>
</div>
<script>
const letter = document.querySelector('#letter');
const container = document.querySelector('#container');
const baselineHeight = letter.offsetTop + letter.offsetHeight - container.offsetHeight - container.offsetTop;
const vscode = acquireVsCodeApi();
vscode.postMessage({
widthRatio: letter.offsetWidth / 1000,
heightRatio: (letter.offsetHeight - baselineHeight) / 1000
});
</script>
<script src="${font_measurement_js}"></script>
</body>
</html>`;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export const assets: Asset[] = [
{ source: "../../images/hats", destination: "images/hats" },
{ source: "./images/logo.png", destination: "images/logo.png" },
{ source: "../../images/logo.svg", destination: "images/logo.svg" },
{
source: "resources/font_measurements.js",
destination: "resources/font_measurements.js",
},
{ source: "../../schemas", destination: "schemas" },
{
source: "../../third-party-licenses.csv",
Expand Down
Loading