Skip to content

Commit eedfed0

Browse files
author
Benjamin Pasero
committed
web state - persist font info in browser local storage
1 parent 520423b commit eedfed0

4 files changed

Lines changed: 54 additions & 32 deletions

File tree

src/vs/editor/browser/config/configuration.ts

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import { CommonEditorConfiguration, IEnvConfiguration } from 'vs/editor/common/c
1414
import { IEditorOptions } from 'vs/editor/common/config/editorOptions';
1515
import { BareFontInfo, FontInfo } from 'vs/editor/common/config/fontInfo';
1616
import { IDimension } from 'vs/editor/common/editorCommon';
17-
import { IStorageService, StorageScope } from 'vs/platform/storage/common/storage';
1817
import { IAccessibilityService } from 'vs/platform/accessibility/common/accessibility';
1918

2019
class CSSBasedConfigurationCache {
@@ -62,28 +61,17 @@ export function readFontInfo(bareFontInfo: BareFontInfo): FontInfo {
6261
return CSSBasedConfiguration.INSTANCE.readConfiguration(bareFontInfo);
6362
}
6463

65-
export function restoreFontInfo(storageService: IStorageService): void {
66-
const strStoredFontInfo = storageService.get('editorFontInfo', StorageScope.GLOBAL);
67-
if (typeof strStoredFontInfo !== 'string') {
68-
return;
69-
}
70-
let storedFontInfo: ISerializedFontInfo[] | null = null;
71-
try {
72-
storedFontInfo = JSON.parse(strStoredFontInfo);
73-
} catch (err) {
74-
return;
75-
}
76-
if (!Array.isArray(storedFontInfo)) {
77-
return;
78-
}
79-
CSSBasedConfiguration.INSTANCE.restoreFontInfo(storedFontInfo);
64+
export function restoreFontInfo(fontInfo: ISerializedFontInfo[]): void {
65+
CSSBasedConfiguration.INSTANCE.restoreFontInfo(fontInfo);
8066
}
8167

82-
export function saveFontInfo(storageService: IStorageService): void {
83-
const knownFontInfo = CSSBasedConfiguration.INSTANCE.saveFontInfo();
84-
if (knownFontInfo.length > 0) {
85-
storageService.store('editorFontInfo', JSON.stringify(knownFontInfo), StorageScope.GLOBAL);
68+
export function serializeFontInfo(): ISerializedFontInfo[] | null {
69+
const fontInfo = CSSBasedConfiguration.INSTANCE.saveFontInfo();
70+
if (fontInfo.length > 0) {
71+
return fontInfo;
8672
}
73+
74+
return null;
8775
}
8876

8977
export interface ISerializedFontInfo {

src/vs/platform/storage/browser/storageService.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,10 @@ export class BrowserStorageService extends Disposable implements IStorageService
125125

126126
return logStorage(result[0], result[1], this.globalStorageFile.toString(), this.workspaceStorageFile.toString());
127127
}
128+
129+
close(): void {
130+
131+
// Signal as event so that clients can still store data
132+
this._onWillSaveState.fire({ reason: WillSaveStateReason.SHUTDOWN });
133+
}
128134
}

src/vs/workbench/browser/web.main.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,13 @@ class CodeRendererMain extends Disposable {
6969

7070
// Workbench Lifecycle
7171
this._register(this.workbench.onShutdown(() => this.dispose()));
72+
this._register(this.workbench.onWillShutdown(() => services.storageService.close()));
7273

7374
// Startup
7475
this.workbench.startup();
7576
}
7677

77-
private async initServices(): Promise<{ serviceCollection: ServiceCollection, logService: ILogService }> {
78+
private async initServices(): Promise<{ serviceCollection: ServiceCollection, logService: ILogService, storageService: BrowserStorageService }> {
7879
const serviceCollection = new ServiceCollection();
7980

8081
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
@@ -137,7 +138,7 @@ class CodeRendererMain extends Disposable {
137138
fileService.registerProvider(Schemas.userData, userDataProvider);
138139
}
139140

140-
await Promise.all([
141+
const services = await Promise.all([
141142
this.createWorkspaceService(payload, environmentService, fileService, remoteAgentService, logService).then(service => {
142143

143144
// Workspace
@@ -158,10 +159,10 @@ class CodeRendererMain extends Disposable {
158159
})
159160
]);
160161

161-
return { serviceCollection, logService };
162+
return { serviceCollection, logService, storageService: services[1] };
162163
}
163164

164-
private async createStorageService(payload: IWorkspaceInitializationPayload, environmentService: IWorkbenchEnvironmentService, fileService: IFileService, logService: ILogService): Promise<IStorageService> {
165+
private async createStorageService(payload: IWorkspaceInitializationPayload, environmentService: IWorkbenchEnvironmentService, fileService: IFileService, logService: ILogService): Promise<BrowserStorageService> {
165166
const storageService = new BrowserStorageService(environmentService, fileService);
166167

167168
try {

src/vs/workbench/browser/workbench.ts

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ import { getZoomLevel } from 'vs/base/browser/browser';
1313
import { mark } from 'vs/base/common/performance';
1414
import { onUnexpectedError, setUnexpectedErrorHandler } from 'vs/base/common/errors';
1515
import { Registry } from 'vs/platform/registry/common/platform';
16-
import { isWindows, isLinux, isWeb } from 'vs/base/common/platform';
16+
import { isWindows, isLinux, isWeb, isNative } from 'vs/base/common/platform';
1717
import { IWorkbenchContributionsRegistry, Extensions as WorkbenchExtensions } from 'vs/workbench/common/contributions';
1818
import { IEditorInputFactoryRegistry, Extensions as EditorExtensions } from 'vs/workbench/common/editor';
1919
import { IActionBarRegistry, Extensions as ActionBarExtensions } from 'vs/workbench/browser/actions';
2020
import { getServices } from 'vs/platform/instantiation/common/extensions';
2121
import { Position, Parts, IWorkbenchLayoutService } from 'vs/workbench/services/layout/browser/layoutService';
22-
import { IStorageService } from 'vs/platform/storage/common/storage';
22+
import { IStorageService, WillSaveStateReason, StorageScope } from 'vs/platform/storage/common/storage';
2323
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
2424
import { IViewletService } from 'vs/workbench/services/viewlet/browser/viewlet';
2525
import { IPanelService } from 'vs/workbench/services/panel/common/panelService';
@@ -36,7 +36,7 @@ import { NotificationsToasts } from 'vs/workbench/browser/parts/notifications/no
3636
import { IEditorService, IResourceEditor } from 'vs/workbench/services/editor/common/editorService';
3737
import { IEditorGroupsService } from 'vs/workbench/services/editor/common/editorGroupsService';
3838
import { setARIAContainer } from 'vs/base/browser/ui/aria/aria';
39-
import { restoreFontInfo, readFontInfo, saveFontInfo } from 'vs/editor/browser/config/configuration';
39+
import { readFontInfo, restoreFontInfo, serializeFontInfo } from 'vs/editor/browser/config/configuration';
4040
import { BareFontInfo } from 'vs/editor/common/config/fontInfo';
4141
import { ILogService } from 'vs/platform/log/common/log';
4242
import { toErrorMessage } from 'vs/base/common/errorMessage';
@@ -217,9 +217,6 @@ export class Workbench extends Layout {
217217
this.dispose();
218218
}));
219219

220-
// Storage
221-
this._register(storageService.onWillSaveState(() => saveFontInfo(storageService)));
222-
223220
// Configuration changes
224221
this._register(configurationService.onDidChangeConfiguration(() => this.setFontAliasing(configurationService)));
225222
}
@@ -243,6 +240,37 @@ export class Workbench extends Layout {
243240
}
244241
}
245242

243+
private warmUpFontCache(storageService: IStorageService, configurationService: IConfigurationService): void {
244+
const key = 'editorFontInfo';
245+
246+
// Restore (native: use storage service, web: use browser specific local storage)
247+
const storedFontInfoRaw = isNative ? storageService.get(key, StorageScope.GLOBAL) : window.localStorage.getItem(key);
248+
if (storedFontInfoRaw) {
249+
try {
250+
const storedFontInfo = JSON.parse(storedFontInfoRaw);
251+
if (Array.isArray(storedFontInfo)) {
252+
restoreFontInfo(storedFontInfo);
253+
}
254+
} catch (err) {
255+
/* ignore */
256+
}
257+
}
258+
259+
readFontInfo(BareFontInfo.createFromRawSettings(configurationService.getValue('editor'), getZoomLevel()));
260+
261+
// Persist on shutdown (native: use storage service, web: use browser specific local storage)
262+
this._register(storageService.onWillSaveState(e => {
263+
if (e.reason === WillSaveStateReason.SHUTDOWN) {
264+
const serializedFontInfo = serializeFontInfo();
265+
if (serializedFontInfo) {
266+
const serializedFontInfoRaw = JSON.stringify(serializedFontInfo);
267+
268+
isNative ? storageService.store(key, serializedFontInfoRaw, StorageScope.GLOBAL) : window.localStorage.setItem(key, serializedFontInfoRaw);
269+
}
270+
}
271+
}));
272+
}
273+
246274
private renderWorkbench(instantiationService: IInstantiationService, notificationService: NotificationService, storageService: IStorageService, configurationService: IConfigurationService): void {
247275

248276
// State specific classes
@@ -268,8 +296,7 @@ export class Workbench extends Layout {
268296
this.setFontAliasing(configurationService);
269297

270298
// Warm up font cache information before building up too many dom elements
271-
restoreFontInfo(storageService);
272-
readFontInfo(BareFontInfo.createFromRawSettings(configurationService.getValue('editor'), getZoomLevel()));
299+
this.warmUpFontCache(storageService, configurationService);
273300

274301
// Create Parts
275302
[

0 commit comments

Comments
 (0)