Skip to content

Commit 02c4d0c

Browse files
authored
Perf/optimize rendering - 优化渲染模式 (#573)
* perf: 添加images长度校验,精简无用的code * perf: mix-blend-mode 默认值改为 unset * perf: css variable 从 :root 迁移到 body
1 parent 48bf13a commit 02c4d0c

File tree

9 files changed

+41
-26
lines changed

9 files changed

+41
-26
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "background",
33
"displayName": "background",
44
"description": "Bring background images to your vscode",
5-
"version": "2.0.7",
5+
"version": "2.0.8-alpha.5",
66
"scripts": {
77
"vscode:prepublish": "npm run compile",
88
"vscode:uninstall": "node ./out/uninstall",

src/background/Background.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export class Background implements Disposable {
8888
let content = await fs.promises.readFile(path.join(docDir, docName), ENCODING);
8989
// 替换图片内联为base64
9090
content = content.replace(/\.\.\/images[^\")]+/g, (relativePath: string) => {
91-
const imgPath = path.join(vscodePath.extensionRoot, 'images', relativePath);
91+
const imgPath = path.join(vscodePath.extRoot, 'images', relativePath);
9292

9393
return (
9494
`data:image/${path.extname(imgPath).slice(1) || 'png'};base64,` +

src/background/PatchGenerator/PatchGenerator.base.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import path from 'path';
21
import { pathToFileURL } from 'url';
32

43
import fg from 'fast-glob';
@@ -47,6 +46,9 @@ export function css(template: TemplateStringsArray, ...args: any[]) {
4746
export class AbsPatchGenerator<T extends { images: string[] }> {
4847
protected config: T;
4948

49+
/** 必须有图片 */
50+
protected imageRequired = true;
51+
5052
constructor(config: T) {
5153
const images = config?.images.filter(n => n.length) || [];
5254
this.config = {
@@ -168,20 +170,36 @@ container.appendChild(div);
168170
}
169171

170172
public create() {
173+
if (this.imageRequired && !this.config.images.length) {
174+
return '';
175+
}
176+
171177
const style = this.compileCSS(this.getStyle());
172178
const script = this.getScript().trim();
173179

174180
return [
175181
this.getPreload(),
176-
`
177-
var style = document.createElement("style");
182+
(() => {
183+
if (!style.length) {
184+
return '';
185+
}
186+
return `
187+
const style = document.createElement("style");
178188
style.textContent = ${JSON.stringify(style)};
179-
document.head.appendChild(style);
180-
`,
189+
document.head.appendChild(style);`;
190+
})(),
181191
script
182192
]
183193
.filter(n => !!n.length)
184194
.map(n => _.withIIFE(n))
185195
.join(';');
186196
}
187197
}
198+
199+
export class WithoutImagesPatchGenerator extends AbsPatchGenerator<any> {
200+
constructor() {
201+
super({ images: [] });
202+
}
203+
204+
protected readonly imageRequired = false;
205+
}

src/background/PatchGenerator/PatchGenerator.checksums.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AbsPatchGenerator, css } from './PatchGenerator.base';
1+
import { css, WithoutImagesPatchGenerator } from './PatchGenerator.base';
22

33
// https://github.com/Microsoft/vscode/blob/main/src/vs/workbench/services/integrity/electron-sandbox/integrityService.ts#L147
44
// https://github.com/microsoft/vscode-loc/blob/main/i18n/vscode-language-pack-zh-hans/translations/main.i18n.json#L14343
@@ -38,10 +38,7 @@ const Translations = [
3838
'安裝似乎已損毀。請重新安裝。'
3939
];
4040

41-
export class ChecksumsPatchGenerator extends AbsPatchGenerator<any> {
42-
constructor() {
43-
super({ images: [] });
44-
}
41+
export class ChecksumsPatchGenerator extends WithoutImagesPatchGenerator {
4542
/**
4643
* fix checksums with css. LOL
4744
*

src/background/PatchGenerator/PatchGenerator.fullscreen.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ function getNextImg() {
7373
}
7474
7575
function setNextImg() {
76-
document.documentElement.style.setProperty(cssvariable, 'url(' + getNextImg() + ')');
76+
document.body.style.setProperty(cssvariable, 'url(' + getNextImg() + ')');
7777
}
7878
7979
if (interval > 0) {

src/background/PatchGenerator/PatchGenerator.theme.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
import { AbsPatchGenerator, css } from './PatchGenerator.base';
2-
3-
export class ThemePatchGenerator extends AbsPatchGenerator<any> {
4-
constructor() {
5-
super({ images: [] });
6-
}
1+
import { css, WithoutImagesPatchGenerator } from './PatchGenerator.base';
72

3+
export class ThemePatchGenerator extends WithoutImagesPatchGenerator {
84
/**
95
* 混合模式使用 css variable
106
*
@@ -16,13 +12,13 @@ export class ThemePatchGenerator extends AbsPatchGenerator<any> {
1612
protected getStyle(): string {
1713
return css`
1814
// 浅色主题(默认)
19-
:root {
15+
body {
2016
// 不使用混合模式
21-
${ThemePatchGenerator.cssMixBlendMode}: normal;
17+
${ThemePatchGenerator.cssMixBlendMode}: unset;
2218
}
2319
24-
// 深色主题 (覆盖)
25-
:root:has(body > .monaco-workbench.vs-dark) {
20+
// 深色主题 (覆盖),避免使用 :root
21+
body:has(> .monaco-workbench.vs-dark) {
2622
// 使用混合模式
2723
${ThemePatchGenerator.cssMixBlendMode}: screen;
2824
}

src/background/PatchGenerator/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export class PatchGenerator {
3535
new PanelPatchGenerator(options.panel).create(), // panel
3636
new FullscreenPatchGenerator(options.fullscreen).create() // fullscreen
3737
]
38+
.filter(n => !!n.length)
3839
.map(n => _.withIIFE(n))
3940
.join(';');
4041

src/utils/vscodePath.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ export const vscodePath = {
4343
* 基础目录
4444
*/
4545
base,
46-
extensionRoot: path.join(__dirname, '../../'),
46+
/**
47+
* 扩展根目录
48+
*/
49+
extRoot: path.join(__dirname, '../../'),
4750
/**
4851
* css文件路径
4952
*/

0 commit comments

Comments
 (0)