Repository navigation
feat(desktop): wire v2 terminal to user theme/font settings + addon parity #3155
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
apps/desktop/src/renderer/lib/terminal/appearance/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| import type { ITheme } from "@xterm/xterm"; | ||
| import { toXtermTheme } from "renderer/stores/theme/utils"; | ||
| import { | ||
| builtInThemes, | ||
| DEFAULT_THEME_ID, | ||
| getTerminalColors, | ||
| } from "shared/themes"; | ||
|
|
||
| export interface TerminalAppearance { | ||
| theme: ITheme; | ||
| background: string; | ||
| fontFamily: string; | ||
| fontSize: number; | ||
| } | ||
|
|
||
| const GENERIC_FONT_FAMILIES = new Set([ | ||
| "serif", | ||
| "sans-serif", | ||
| "monospace", | ||
| "cursive", | ||
| "fantasy", | ||
| "system-ui", | ||
| "ui-serif", | ||
| "ui-sans-serif", | ||
| "ui-monospace", | ||
| "ui-rounded", | ||
| "emoji", | ||
| "math", | ||
| "fangsong", | ||
| ]); | ||
|
|
||
| function serializeFontFamilyList(families: string[]): string { | ||
| return families | ||
| .map((family) => | ||
| GENERIC_FONT_FAMILIES.has(family) | ||
| ? family | ||
| : `"${family.replaceAll('"', '\\"')}"`, | ||
| ) | ||
| .join(", "); | ||
| } | ||
|
|
||
| export const DEFAULT_TERMINAL_FONT_FAMILIES = [ | ||
| "JetBrains Mono", | ||
| "JetBrainsMono Nerd Font", | ||
| "MesloLGM Nerd Font", | ||
| "MesloLGM NF", | ||
| "MesloLGS NF", | ||
| "MesloLGS Nerd Font", | ||
| "Hack Nerd Font", | ||
| "FiraCode Nerd Font", | ||
| "CaskaydiaCove Nerd Font", | ||
| "Menlo", | ||
| "Monaco", | ||
| "Courier New", | ||
| "monospace", | ||
| ] as const; | ||
|
|
||
| export const DEFAULT_TERMINAL_FONT_FAMILY = serializeFontFamilyList([ | ||
| ...DEFAULT_TERMINAL_FONT_FAMILIES, | ||
| ]); | ||
|
|
||
| export const DEFAULT_TERMINAL_FONT_SIZE = 14; | ||
|
|
||
| /** Reads localStorage theme cache for flash-free first paint. */ | ||
| export function getDefaultTerminalAppearance(): TerminalAppearance { | ||
| const theme = readCachedTerminalTheme(); | ||
| return { | ||
| theme, | ||
| background: theme.background ?? "#151110", | ||
| fontFamily: DEFAULT_TERMINAL_FONT_FAMILY, | ||
| fontSize: DEFAULT_TERMINAL_FONT_SIZE, | ||
| }; | ||
| } | ||
|
|
||
| function readCachedTerminalTheme(): ITheme { | ||
| try { | ||
| const cachedTerminal = localStorage.getItem("theme-terminal"); | ||
| if (cachedTerminal) { | ||
| return toXtermTheme(JSON.parse(cachedTerminal)); | ||
| } | ||
| const themeId = localStorage.getItem("theme-id") ?? DEFAULT_THEME_ID; | ||
| const theme = builtInThemes.find((t) => t.id === themeId); | ||
| if (theme) { | ||
| return toXtermTheme(getTerminalColors(theme)); | ||
| } | ||
| } catch {} | ||
| const defaultTheme = builtInThemes.find((t) => t.id === DEFAULT_THEME_ID); | ||
| return defaultTheme | ||
| ? toXtermTheme(getTerminalColors(defaultTheme)) | ||
| : { background: "#151110", foreground: "#eae8e6" }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import { ClipboardAddon } from "@xterm/addon-clipboard"; | ||
| import { ImageAddon } from "@xterm/addon-image"; | ||
| import { LigaturesAddon } from "@xterm/addon-ligatures"; | ||
| import { Unicode11Addon } from "@xterm/addon-unicode11"; | ||
| import { WebglAddon } from "@xterm/addon-webgl"; | ||
| import type { Terminal as XTerm } from "@xterm/xterm"; | ||
|
|
||
| // Once WebGL fails, skip it for all subsequent runtimes (VS Code pattern). | ||
| let suggestedRendererType: "webgl" | "dom" | undefined; | ||
|
|
||
| /** | ||
| * Load optional addons onto an already-opened terminal. Returns a cleanup | ||
| * function. WebGL is deferred to rAF to avoid racing with xterm's post-open | ||
| * viewport sync. | ||
| */ | ||
| export function loadAddons(terminal: XTerm): () => void { | ||
| let disposed = false; | ||
| let webglAddon: WebglAddon | null = null; | ||
|
|
||
| terminal.loadAddon(new ClipboardAddon()); | ||
|
|
||
| const unicode11 = new Unicode11Addon(); | ||
| terminal.loadAddon(unicode11); | ||
| terminal.unicode.activeVersion = "11"; | ||
|
|
||
| terminal.loadAddon(new ImageAddon()); | ||
|
|
||
| try { | ||
| terminal.loadAddon(new LigaturesAddon()); | ||
| } catch {} | ||
|
|
||
| const rafId = requestAnimationFrame(() => { | ||
| if (disposed || suggestedRendererType === "dom") return; | ||
|
|
||
| try { | ||
| webglAddon = new WebglAddon(); | ||
| webglAddon.onContextLoss(() => { | ||
| webglAddon?.dispose(); | ||
| webglAddon = null; | ||
| terminal.refresh(0, terminal.rows - 1); | ||
| }); | ||
| terminal.loadAddon(webglAddon); | ||
| } catch { | ||
| suggestedRendererType = "dom"; | ||
| webglAddon = null; | ||
| } | ||
| }); | ||
|
|
||
| return () => { | ||
| disposed = true; | ||
| cancelAnimationFrame(rafId); | ||
| try { | ||
| webglAddon?.dispose(); | ||
| } catch {} | ||
| webglAddon = null; | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.