Skip to content

Commit

Permalink
Better handling loading terminal fonts (#6913)
Browse files Browse the repository at this point in the history
* better handling loading terminal fonts timing issues, fix #5019

Signed-off-by: Roman <ixrock@gmail.com>

* refactoring, loading all terminal fonts to earliest stage app starting

Signed-off-by: Roman <ixrock@gmail.com>

* attempt to fix tests

Signed-off-by: Roman <ixrock@gmail.com>

Signed-off-by: Roman <ixrock@gmail.com>
  • Loading branch information
ixrock authored Jan 13, 2023
1 parent fd00515 commit 875972e
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1141,7 +1141,7 @@ exports[`preferences - navigation to terminal preferences given in preferences,
class="Select__single-value css-1dimb5e-singleValue"
>
<span
style="font-family: RobotoMono; font-size: 12px;"
style="font-family: RobotoMono, var(--font-terminal); font-size: 12px;"
>
RobotoMono
</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,29 @@ import { Select } from "../../../../../../renderer/components/select";
import type { Logger } from "../../../../../../common/logger";
import { action } from "mobx";
import loggerInjectable from "../../../../../../common/logger.injectable";
import {
terminalFontsInjectable,
} from "../../../../../../renderer/components/dock/terminal/terminal-fonts.injectable";

interface Dependencies {
userStore: UserStore;
logger: Logger;
terminalFonts: Map<string, string>;
}

const NonInjectedTerminalFontFamily = observer(
({ userStore, logger }: Dependencies) => {

// fonts must be declared in `fonts.scss` and at `template.html` (if early-preloading required)
const supportedCustomFonts: SelectOption<string>[] = [
"RobotoMono", "Anonymous Pro", "IBM Plex Mono", "JetBrains Mono", "Red Hat Mono",
"Source Code Pro", "Space Mono", "Ubuntu Mono",
].map(customFont => {
({ userStore, logger, terminalFonts }: Dependencies) => {
const bundledFonts: SelectOption<string>[] = Array.from(terminalFonts.keys()).map(font => {
const { fontFamily, fontSize } = userStore.terminalConfig;

return {
label: <span style={{ fontFamily: customFont, fontSize }}>{customFont}</span>,
value: customFont,
isSelected: fontFamily === customFont,
label: (
<span style={{ fontFamily: `${font}, var(--font-terminal)`, fontSize }}>
{font}
</span>
),
value: font,
isSelected: fontFamily === font,
};
});

Expand All @@ -50,21 +53,18 @@ const NonInjectedTerminalFontFamily = observer(
themeName="lens"
controlShouldRenderValue
value={userStore.terminalConfig.fontFamily}
options={supportedCustomFonts}
options={bundledFonts}
onChange={onFontFamilyChange as any}
/>
</section>
);
},
);

export const TerminalFontFamily = withInjectables<Dependencies>(
NonInjectedTerminalFontFamily,

{
getProps: (di) => ({
userStore: di.inject(userStoreInjectable),
logger: di.inject(loggerInjectable),
}),
},
);
export const TerminalFontFamily = withInjectables<Dependencies>(NonInjectedTerminalFontFamily, {
getProps: (di) => ({
userStore: di.inject(userStoreInjectable),
logger: di.inject(loggerInjectable),
terminalFonts: di.inject(terminalFontsInjectable),
}),
});
16 changes: 0 additions & 16 deletions src/renderer/components/app.scss
Original file line number Diff line number Diff line change
Expand Up @@ -232,22 +232,6 @@ iframe {
}
}

#fonts-preloading {
> span {
position: absolute;
visibility: hidden;
height: 0;

&:before {
width: 0;
display: block;
overflow: hidden;
content: "text-example"; // some text required to start applying/rendering font in document
font-family: inherit; // font-family must be specified via style="" (see: template.html)
}
}
}

// app's common loading indicator, displaying on the route transitions
#loading {
position: absolute;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import type { TabId } from "../dock/store";
import type { TerminalApi } from "../../../api/terminal-api";
import terminalSpawningPoolInjectable from "./terminal-spawning-pool.injectable";
import terminalConfigInjectable from "../../../../common/user-store/terminal-config.injectable";
import terminalCopyOnSelectInjectable from "../../../../common/user-store/terminal-copy-on-select.injectable";
import terminalCopyOnSelectInjectable
from "../../../../common/user-store/terminal-copy-on-select.injectable";
import isMacInjectable from "../../../../common/vars/is-mac.injectable";
import openLinkInBrowserInjectable from "../../../../common/utils/open-link-in-browser.injectable";
import xtermColorThemeInjectable from "../../../themes/terminal-colors.injectable";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getGlobalOverride } from "../../../../common/test-utils/get-global-override";
import { preloadAllTerminalFontsInjectable } from "./terminal-fonts.injectable";

export default getGlobalOverride(preloadAllTerminalFontsInjectable, () => {
return {
id: "",
async run() {
},
};
});
78 changes: 78 additions & 0 deletions src/renderer/components/dock/terminal/terminal-fonts.injectable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/

import { getInjectable } from "@ogre-tools/injectable";
import { beforeFrameStartsFirstInjectionToken } from "../../../before-frame-starts/tokens";
import RobotoMono from "../../../fonts/Roboto-Mono-nerd.ttf"; // patched font with icons
import AnonymousPro from "../../../fonts/AnonymousPro-Regular.ttf";
import IBMPlexMono from "../../../fonts/IBMPlexMono-Regular.ttf";
import JetBrainsMono from "../../../fonts/JetBrainsMono-Regular.ttf";
import RedHatMono from "../../../fonts/RedHatMono-Regular.ttf";
import SourceCodePro from "../../../fonts/SourceCodePro-Regular.ttf";
import SpaceMono from "../../../fonts/SpaceMono-Regular.ttf";
import UbuntuMono from "../../../fonts/UbuntuMono-Regular.ttf";

export const terminalFontsInjectable = getInjectable({
id: "terminalFontsInjectable",

instantiate() {
return new Map([
["RobotoMono", RobotoMono],
["Anonymous Pro", AnonymousPro],
["IBM Plex Mono", IBMPlexMono],
["JetBrains Mono", JetBrainsMono],
["Red Hat Mono", RedHatMono],
["Source Code Pro", SourceCodePro],
["Space Mono", SpaceMono],
["Ubuntu Mono", UbuntuMono],
]);
},
});


export const preloadTerminalFontInjectable = getInjectable({
id: "preloadTerminalFontInjectable",

instantiate(di) {
const terminalFonts = di.inject(terminalFontsInjectable);

return async function (fontFamily: string): Promise<void> {
const fontBundledPath = terminalFonts.get(fontFamily);
const fontLoaded = document.fonts.check(`10px ${fontFamily}`);

if (fontLoaded || !fontBundledPath) return;

const font = new FontFace(fontFamily, `url(${fontBundledPath})`);

document.fonts.add(font);
await font.load();
};
},

causesSideEffects: true,
});

export const preloadAllTerminalFontsInjectable = getInjectable({
id: "preloadAllTerminalFontsInjectable",

instantiate(di) {
const terminalFonts = di.inject(terminalFontsInjectable);
const preloadFont = di.inject(preloadTerminalFontInjectable);

return {
id: "preload-all-terminal-fonts",

async run() {
await Promise.allSettled(
Array.from(terminalFonts.keys()).map(preloadFont),
);
},
};
},

injectionToken: beforeFrameStartsFirstInjectionToken,

causesSideEffects: true,
});
55 changes: 0 additions & 55 deletions src/renderer/components/fonts.scss
Original file line number Diff line number Diff line change
Expand Up @@ -44,58 +44,3 @@
font-display: block;
src: url("../fonts/MaterialIcons-Regular.ttf") format("truetype");
}


// Terminal fonts (monospaced)
// Source: https://fonts.google.com/?category=Monospace
@font-face {
font-family: "Anonymous Pro";
src: local("Anonymous Pro"), url("../fonts/AnonymousPro-Regular.ttf") format("truetype");
font-display: block;
}

@font-face {
font-family: "IBM Plex Mono";
src: local("IBM Plex Mono"), url("../fonts/IBMPlexMono-Regular.ttf") format("truetype");
font-display: block;
}

@font-face {
font-family: "JetBrains Mono";
src: local("JetBrains Mono"), url("../fonts/JetBrainsMono-Regular.ttf") format("truetype");
font-display: block;
}

@font-face {
font-family: "Red Hat Mono";
src: local("Red Hat Mono"), url("../fonts/RedHatMono-Regular.ttf") format("truetype");
font-display: block;
}


@font-face {
font-family: "Source Code Pro";
src: local("Source Code Pro"), url("../fonts/SourceCodePro-Regular.ttf") format("truetype");
font-display: block;
}

@font-face {
font-family: "Space Mono";
src: local("Space Mono"), url("../fonts/SpaceMono-Regular.ttf") format("truetype");
font-display: block;
}

@font-face {
font-family: "Ubuntu Mono";
src: local("Ubuntu Mono"), url("../fonts/UbuntuMono-Regular.ttf") format("truetype");
font-display: block;
}

// Patched RobotoMono font with icons
// RobotoMono Windows Compatible for using in terminal
// https://github.com/ryanoasis/nerd-fonts/tree/master/patched-fonts/RobotoMono
@font-face {
font-family: "RobotoMono";
src: local("RobotoMono"), url("../fonts/Roboto-Mono-nerd.ttf") format("truetype");
font-display: block;
}
11 changes: 0 additions & 11 deletions src/renderer/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,6 @@
</head>
<body>
<div id="app"></div>
<div id="fonts-preloading">
<span style="font-family: 'Material Icons'" />
<span style="font-family: 'RobotoMono'" />
<span style="font-family: 'Anonymous Pro'" />
<span style="font-family: 'IBM Plex Mono'" />
<span style="font-family: 'JetBrains Mono'" />
<span style="font-family: 'Red Hat Mono'" />
<span style="font-family: 'Source Code Pro'" />
<span style="font-family: 'Space Mono'" />
<span style="font-family: 'Ubuntu Mono'" />
</div>
<div id="terminal-init"></div>
</body>
</html>

0 comments on commit 875972e

Please sign in to comment.