Skip to content

Commit

Permalink
update UI if user cancels
Browse files Browse the repository at this point in the history
  • Loading branch information
colin-grant-work committed Dec 3, 2021
1 parent ce1596f commit 164daa2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { MonacoEditorModel } from '@theia/monaco/lib/browser/monaco-editor-model
import { MonacoWorkspace } from '@theia/monaco/lib/browser/monaco-workspace';
import { Deferred } from '@theia/core/lib/common/promise-util';
import { FileService } from '@theia/filesystem/lib/browser/file-service';
import { nls } from '@theia/core';
import { CancellationError, nls } from '@theia/core';
import { EditorManager } from '@theia/editor/lib/browser';

export interface FilePreferenceProviderLocks {
Expand Down Expand Up @@ -140,15 +140,15 @@ export abstract class AbstractResourcePreferenceProvider extends PreferenceProvi
if (!this.model || !(path = this.getPath(key)) || !this.contains(resourceUri)) {
return false;
}
if (!locks) { // Action cancelled by user. Consider it complete.
return true;
if (!locks) {
throw new CancellationError();
}
if (shouldSave) {
if (this.model.dirty) {
shouldSave = await this.handleDirtyEditor();
}
if (!shouldSave) { // Action cancelled by user. Consider it complete.
return true;
if (!shouldSave) {
throw new CancellationError();
}
}
const editOperations = this.getEditOperations(path, value);
Expand All @@ -157,6 +157,9 @@ export abstract class AbstractResourcePreferenceProvider extends PreferenceProvi
}
return this.pendingTransaction.promise;
} catch (e) {
if (e instanceof CancellationError) {
throw e;
}
const message = `Failed to update the value of '${key}' in '${this.getUri()}'.`;
this.messageService.error(`${message} Please check if it is corrupted.`);
console.error(`${message}`, e);
Expand Down Expand Up @@ -200,9 +203,10 @@ export abstract class AbstractResourcePreferenceProvider extends PreferenceProvi
this.pendingTransaction.resolve(success);
}
}));
} else { // User cancelled the operation.
} else { // User canceled the operation.
this.singleChangeLock.cancel();
locks.releaseTransaction!();
this.pendingTransaction.resolve(false);
}
}
locks?.releaseChange();
Expand Down Expand Up @@ -342,7 +346,9 @@ export abstract class AbstractResourcePreferenceProvider extends PreferenceProvi
const saveAndRetry = nls.localizeByDefault('Save and Retry');
const open = nls.localizeByDefault('Open File');
const msg = await this.messageService.error(
nls.localizeByDefault('Unable to write preference change because the settings file is dirty. Please save the file and try again.'),
nls.localizeByDefault('Unable to write into {0} settings because the file has unsaved changes. Please save the {0} settings file first and then try again.',
nls.localizeByDefault(PreferenceScope[this.getScope()].toLocaleLowerCase())
),
saveAndRetry, open);

if (this.model) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,8 @@ export abstract class PreferenceLeafNodeRenderer<ValueType extends JSONValue, In
protected setPreferenceWithDebounce = debounce(this.setPreferenceImmediately.bind(this), 500, { leading: false, trailing: true });

protected setPreferenceImmediately(value: ValueType | undefined): Promise<void> {
return this.preferenceService.set(this.id, value, this.scopeTracker.currentScope.scope, this.scopeTracker.currentScope.uri);
return this.preferenceService.set(this.id, value, this.scopeTracker.currentScope.scope, this.scopeTracker.currentScope.uri)
.catch(() => this.handleValueChange());
}

handleSearchChange(isFiltered = this.model.isFiltered): void {
Expand All @@ -420,7 +421,18 @@ export abstract class PreferenceLeafNodeRenderer<ValueType extends JSONValue, In
this.updateHeadline();
}

/**
* Should create an HTML element that the user can interact with to change the value of the preference.
* @param container the parent element for the interactable. This method is responsible for adding the new element to its parent.
*/
protected abstract createInteractable(container: HTMLElement): void;
/**
* @returns a fallback default value for a preference of the type implemented by a concrete leaf renderer
* This function is only called if the default value for a given preference is not specified in its schema.
*/
protected abstract getFallbackValue(): ValueType;
/**
* This function is responsible for reconciling the display of the preference value with the value reported by the PreferenceService.
*/
protected abstract doHandleValueChange(): void;
}

0 comments on commit 164daa2

Please sign in to comment.