Skip to content

Commit

Permalink
Check renderer schema when updating preference renderers (#12347)
Browse files Browse the repository at this point in the history
  • Loading branch information
colin-grant-work authored Mar 29, 2023
1 parent 6cd4223 commit f82e52b
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 27 deletions.
8 changes: 5 additions & 3 deletions packages/core/src/browser/icon-theme-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,11 @@ export class IconThemeService {
const preference = this.schemaProvider.getSchemaProperty(ICON_THEME_PREFERENCE_KEY);
if (preference) {
const sortedThemes = Array.from(this.definitions).sort((a, b) => a.label.localeCompare(b.label));
preference.enum = sortedThemes.map(e => e.id);
preference.enumItemLabels = sortedThemes.map(e => e.label);
this.schemaProvider.updateSchemaProperty(ICON_THEME_PREFERENCE_KEY, preference);
this.schemaProvider.updateSchemaProperty(ICON_THEME_PREFERENCE_KEY, {
...preference,
enum: sortedThemes.map(e => e.id),
enumItemLabels: sortedThemes.map(e => e.label)
});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,10 @@ export class PreferenceSchemaProvider extends PreferenceProvider {
return this.combinedSchema.properties[key];
}

/**
* {@link property} will be assigned to field {@link key} in the schema.
* Pass a new object to invalidate old schema.
*/
updateSchemaProperty(key: string, property: PreferenceDataProperty): void {
this.updateSchemaProps(key, property);
this.fireDidPreferenceSchemaChanged();
Expand Down
8 changes: 5 additions & 3 deletions packages/core/src/browser/theming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,11 @@ export class ThemeService {
const preference = this.schemaProvider.getSchemaProperty(COLOR_THEME_PREFERENCE_KEY);
if (preference) {
const sortedThemes = this.getThemes().sort((a, b) => a.label.localeCompare(b.label));
preference.enum = sortedThemes.map(e => e.id);
preference.enumItemLabels = sortedThemes.map(e => e.label);
this.schemaProvider.updateSchemaProperty(COLOR_THEME_PREFERENCE_KEY, preference);
this.schemaProvider.updateSchemaProperty(COLOR_THEME_PREFERENCE_KEY, {
...preference,
enum: sortedThemes.map(e => e.id),
enumItemLabels: sortedThemes.map(e => e.label)
});
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/preferences/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"@theia/userstorage": "1.35.0",
"@theia/workspace": "1.35.0",
"async-mutex": "^0.3.1",
"fast-deep-equal": "^3.1.3",
"jsonc-parser": "^2.2.0",
"p-debounce": "^2.1.0"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import { injectable, inject, postConstruct } from '@theia/core/shared/inversify';
import {
PreferenceService, ContextMenuRenderer, PreferenceInspection,
PreferenceScope, PreferenceProvider, codicon, OpenerService, open
PreferenceScope, PreferenceProvider, codicon, OpenerService, open, PreferenceDataProperty
} from '@theia/core/lib/browser';
import { Preference, PreferenceMenus } from '../../util/preference-types';
import { PreferenceTreeLabelProvider } from '../../util/preference-tree-label-provider';
Expand All @@ -39,6 +39,7 @@ export const SUBHEADER_CLASS = 'settings-section-subcategory-title';
export interface GeneralPreferenceNodeRenderer extends Disposable {
node: HTMLElement;
id: string;
schema?: PreferenceDataProperty;
group: string;
nodeId: string;
visible: boolean;
Expand Down Expand Up @@ -170,6 +171,10 @@ export abstract class PreferenceLeafNodeRenderer<ValueType extends JSONValue, In
protected isModifiedFromDefault = false;
protected markdownRenderer: markdownit;

get schema(): PreferenceDataProperty {
return this.preferenceNode.preference.data;
}

@postConstruct()
protected override init(): void {
this.setId();
Expand Down
24 changes: 4 additions & 20 deletions packages/preferences/src/browser/views/preference-editor-widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { postConstruct, injectable, inject } from '@theia/core/shared/inversify';
import throttle = require('@theia/core/shared/lodash.throttle');
import * as deepEqual from 'fast-deep-equal';
import {
PreferenceService,
CompositeTreeNode,
Expand All @@ -35,7 +36,6 @@ import { Preference } from '../util/preference-types';
import { COMMONLY_USED_SECTION_PREFIX } from '../util/preference-tree-generator';
import { PreferencesScopeTabBar } from './preference-scope-tabbar-widget';
import { PreferenceNodeRendererCreatorRegistry } from './components/preference-node-renderer-creator';
import stableJsonStringify = require('fast-json-stable-stringify');

export interface PreferencesEditorState {
firstVisibleChildID: string,
Expand Down Expand Up @@ -127,7 +127,7 @@ export class PreferencesEditorWidget extends BaseWidget implements StatefulWidge
protected handleSchemaChange(isFiltered: boolean): void {
for (const [id, renderer, collection] of this.allRenderers()) {
const node = this.model.getNode(renderer.nodeId);
if (!node || (Preference.LeafNode.is(node) && this.hasSchemaChanged(node))) {
if (!node || (Preference.LeafNode.is(node) && this.hasSchemaChanged(renderer, node))) {
renderer.dispose();
collection.delete(id);
}
Expand All @@ -143,9 +143,6 @@ export class PreferencesEditorWidget extends BaseWidget implements StatefulWidge
this.hideIfFailsFilters(renderer, isFiltered);
collection.set(id, renderer);
}
if (!this.preferenceDataKeys.has(node.id) && Preference.LeafNode.is(node)) {
this.setSchemaPropertyKey(node);
}
if (nextNode !== renderer.node) {
if (nextNode) {
renderer.insertBefore(nextNode);
Expand All @@ -169,21 +166,8 @@ export class PreferencesEditorWidget extends BaseWidget implements StatefulWidge
}
}

protected hasSchemaChanged(leafNode: Preference.LeafNode): boolean {
const oldKey = this.preferenceDataKeys.get(leafNode.id);
const newKey = this.setSchemaPropertyKey(leafNode);
return oldKey !== newKey;
}

protected setSchemaPropertyKey(leafNode: Preference.LeafNode): string | undefined {
const schemaProperty = this.schemaProvider.getSchemaProperty(leafNode.preferenceId);
if (schemaProperty) {
const key = stableJsonStringify(schemaProperty);
this.preferenceDataKeys.set(leafNode.id, key);
return key;
} else {
return undefined;
}
protected hasSchemaChanged(renderer: GeneralPreferenceNodeRenderer, node: Preference.LeafNode): boolean {
return !deepEqual(renderer.schema, node.preference.data);
}

protected handleSearchChange(isFiltered: boolean, leavesAreVisible: boolean): void {
Expand Down

0 comments on commit f82e52b

Please sign in to comment.