1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

refactoring, loading all terminal fonts to earliest stage app starting

Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
Roman 2023-01-12 15:21:52 +02:00
parent a1b36b688d
commit e3961ad2a8
5 changed files with 26 additions and 35 deletions

View File

@ -2,7 +2,7 @@
* Copyright (c) OpenLens Authors. All rights reserved. * Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information. * Licensed under MIT License. See LICENSE in root directory for more information.
*/ */
import React, { useEffect } from "react"; import React from "react";
import { SubTitle } from "../../../../../../renderer/components/layout/sub-title"; import { SubTitle } from "../../../../../../renderer/components/layout/sub-title";
import { withInjectables } from "@ogre-tools/injectable-react"; import { withInjectables } from "@ogre-tools/injectable-react";
import type { UserStore } from "../../../../../../common/user-store"; import type { UserStore } from "../../../../../../common/user-store";
@ -14,7 +14,6 @@ import type { Logger } from "../../../../../../common/logger";
import { action } from "mobx"; import { action } from "mobx";
import loggerInjectable from "../../../../../../common/logger.injectable"; import loggerInjectable from "../../../../../../common/logger.injectable";
import { import {
preloadAllTerminalFontsInjectable,
terminalFontsInjectable, terminalFontsInjectable,
} from "../../../../../../renderer/components/dock/terminal/terminal-fonts.injectable"; } from "../../../../../../renderer/components/dock/terminal/terminal-fonts.injectable";
@ -22,15 +21,10 @@ interface Dependencies {
userStore: UserStore; userStore: UserStore;
logger: Logger; logger: Logger;
terminalFonts: Map<string, string>; terminalFonts: Map<string, string>;
preloadFonts: () => Promise<void>;
} }
const NonInjectedTerminalFontFamily = observer( const NonInjectedTerminalFontFamily = observer(
({ userStore, logger, terminalFonts, preloadFonts }: Dependencies) => { ({ userStore, logger, terminalFonts }: Dependencies) => {
useEffect(() => {
preloadFonts(); // preload all fonts to show preview in select-box
}, []);
const bundledFonts: SelectOption<string>[] = Array.from(terminalFonts.keys()).map(font => { const bundledFonts: SelectOption<string>[] = Array.from(terminalFonts.keys()).map(font => {
const { fontFamily, fontSize } = userStore.terminalConfig; const { fontFamily, fontSize } = userStore.terminalConfig;
@ -67,15 +61,10 @@ const NonInjectedTerminalFontFamily = observer(
}, },
); );
export const TerminalFontFamily = withInjectables<Dependencies>( export const TerminalFontFamily = withInjectables<Dependencies>(NonInjectedTerminalFontFamily, {
NonInjectedTerminalFontFamily, getProps: (di) => ({
userStore: di.inject(userStoreInjectable),
{ logger: di.inject(loggerInjectable),
getProps: (di) => ({ terminalFonts: di.inject(terminalFontsInjectable),
userStore: di.inject(userStoreInjectable), }),
logger: di.inject(loggerInjectable), });
terminalFonts: di.inject(terminalFontsInjectable),
preloadFonts: di.inject(preloadAllTerminalFontsInjectable),
}),
},
);

View File

@ -15,7 +15,6 @@ import isMacInjectable from "../../../../common/vars/is-mac.injectable";
import openLinkInBrowserInjectable from "../../../../common/utils/open-link-in-browser.injectable"; import openLinkInBrowserInjectable from "../../../../common/utils/open-link-in-browser.injectable";
import xtermColorThemeInjectable from "../../../themes/terminal-colors.injectable"; import xtermColorThemeInjectable from "../../../themes/terminal-colors.injectable";
import loggerInjectable from "../../../../common/logger.injectable"; import loggerInjectable from "../../../../common/logger.injectable";
import { preloadTerminalFontInjectable } from "./terminal-fonts.injectable";
export type CreateTerminal = (tabId: TabId, api: TerminalApi) => Terminal; export type CreateTerminal = (tabId: TabId, api: TerminalApi) => Terminal;
@ -30,7 +29,6 @@ const createTerminalInjectable = getInjectable({
openLinkInBrowser: di.inject(openLinkInBrowserInjectable), openLinkInBrowser: di.inject(openLinkInBrowserInjectable),
xtermColorTheme: di.inject(xtermColorThemeInjectable), xtermColorTheme: di.inject(xtermColorThemeInjectable),
logger: di.inject(loggerInjectable), logger: di.inject(loggerInjectable),
preloadFont: di.inject(preloadTerminalFontInjectable),
}; };
return (tabId, api) => new Terminal(dependencies, { tabId, api }); return (tabId, api) => new Terminal(dependencies, { tabId, api });

View File

@ -4,6 +4,7 @@
*/ */
import { getInjectable } from "@ogre-tools/injectable"; 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 RobotoMono from "../../../fonts/Roboto-Mono-nerd.ttf"; // patched font with icons
import AnonymousPro from "../../../fonts/AnonymousPro-Regular.ttf"; import AnonymousPro from "../../../fonts/AnonymousPro-Regular.ttf";
import IBMPlexMono from "../../../fonts/IBMPlexMono-Regular.ttf"; import IBMPlexMono from "../../../fonts/IBMPlexMono-Regular.ttf";
@ -60,12 +61,18 @@ export const preloadAllTerminalFontsInjectable = getInjectable({
const terminalFonts = di.inject(terminalFontsInjectable); const terminalFonts = di.inject(terminalFontsInjectable);
const preloadFont = di.inject(preloadTerminalFontInjectable); const preloadFont = di.inject(preloadTerminalFontInjectable);
return async function (): Promise<any> { return {
return Promise.allSettled( id: "preload-all-terminal-fonts",
Array.from(terminalFonts.keys()).map(preloadFont),
); async run() {
await Promise.allSettled(
Array.from(terminalFonts.keys()).map(preloadFont),
);
},
}; };
}, },
injectionToken: beforeFrameStartsFirstInjectionToken,
causesSideEffects: true, causesSideEffects: true,
}); });

View File

@ -28,7 +28,6 @@ export interface TerminalDependencies {
readonly xtermColorTheme: IComputedValue<Record<string, string>>; readonly xtermColorTheme: IComputedValue<Record<string, string>>;
readonly logger: Logger; readonly logger: Logger;
openLinkInBrowser: OpenLinkInBrowser; openLinkInBrowser: OpenLinkInBrowser;
preloadFont: (fontFamily: string) => Promise<void>;
} }
export interface TerminalArguments { export interface TerminalArguments {
@ -54,8 +53,7 @@ export class Terminal {
return this.elem.querySelector(".xterm-viewport")!; return this.elem.querySelector(".xterm-viewport")!;
} }
async attachTo(parentElem: HTMLElement) { attachTo(parentElem: HTMLElement) {
await this.dependencies.preloadFont(this.fontFamily);
assert(this.elem, "Terminal should always be mounted somewhere"); assert(this.elem, "Terminal should always be mounted somewhere");
parentElem.appendChild(this.elem); parentElem.appendChild(this.elem);
this.onActivate(); this.onActivate();
@ -208,10 +206,9 @@ export class Terminal {
this.fit(); this.fit();
}; };
setFontFamily = async (fontFamily: string) => { setFontFamily = (fontFamily: string) => {
this.dependencies.logger.info(`[TERMINAL]: set fontFamily to ${fontFamily}`); this.dependencies.logger.info(`[TERMINAL]: set fontFamily to ${fontFamily}`);
await this.dependencies.preloadFont(fontFamily);
this.xterm.options.fontFamily = fontFamily; this.xterm.options.fontFamily = fontFamily;
this.fit(); this.fit();

View File

@ -34,14 +34,14 @@ class NonInjectedTerminalWindow extends React.Component<TerminalWindowProps & De
public elem: HTMLElement | null = null; public elem: HTMLElement | null = null;
public terminal!: Terminal; public terminal!: Terminal;
async componentDidMount() { componentDidMount() {
this.props.terminalStore.connect(this.props.tab); this.props.terminalStore.connect(this.props.tab);
const terminal = this.props.terminalStore.getTerminal(this.props.tab.id); const terminal = this.props.terminalStore.getTerminal(this.props.tab.id);
assert(terminal, "Terminal must be created for tab before mounting"); assert(terminal, "Terminal must be created for tab before mounting");
this.terminal = terminal; this.terminal = terminal;
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
await this.terminal.attachTo(this.elem!); this.terminal.attachTo(this.elem!);
disposeOnUnmount(this, [ disposeOnUnmount(this, [
// refresh terminal available space (cols/rows) when <Dock/> resized // refresh terminal available space (cols/rows) when <Dock/> resized
@ -51,7 +51,7 @@ class NonInjectedTerminalWindow extends React.Component<TerminalWindowProps & De
]); ]);
} }
async componentDidUpdate() { componentDidUpdate(): void {
this.terminal.detach(); this.terminal.detach();
this.props.terminalStore.connect(this.props.tab); this.props.terminalStore.connect(this.props.tab);
const terminal = this.props.terminalStore.getTerminal(this.props.tab.id); const terminal = this.props.terminalStore.getTerminal(this.props.tab.id);
@ -59,7 +59,7 @@ class NonInjectedTerminalWindow extends React.Component<TerminalWindowProps & De
assert(terminal, "Terminal must be created for tab before mounting"); assert(terminal, "Terminal must be created for tab before mounting");
this.terminal = terminal; this.terminal = terminal;
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
await this.terminal.attachTo(this.elem!); this.terminal.attachTo(this.elem!);
} }
componentWillUnmount(): void { componentWillUnmount(): void {