Skip to content

Commit

Permalink
explorer input black magic
Browse files Browse the repository at this point in the history
fixes #78153
  • Loading branch information
isidorn committed Aug 16, 2019
1 parent 5352ba3 commit b8974b0
Showing 1 changed file with 20 additions and 30 deletions.
50 changes: 20 additions & 30 deletions src/vs/workbench/contrib/files/browser/views/explorerViewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,56 +218,46 @@ export class FilesRenderer implements ITreeRenderer<ExplorerItem, FuzzyScore, IF
const lastDot = value.lastIndexOf('.');

inputBox.value = value;
inputBox.focus();
inputBox.select({ start: 0, end: lastDot > 0 && !stat.isDirectory ? lastDot : value.length });

let isFinishableDisposeEvent = false;
setTimeout(() => {
// Check if disposed
if (!inputBox.inputElement) {
return;
}
inputBox.focus();
inputBox.select({ start: 0, end: lastDot > 0 && !stat.isDirectory ? lastDot : value.length });
isFinishableDisposeEvent = true;
}, 0);

const done = once(async (success: boolean) => {
const done = once(async (success: boolean, blur: boolean) => {
label.element.style.display = 'none';
const value = inputBox.value;
dispose(toDispose);
label.element.remove();
// Timeout: once done rendering only then re-render #70902
setTimeout(() => editableData.onFinish(value, success), 0);
container.removeChild(label.element);
editableData.onFinish(value, success);
});

const blurDisposable = DOM.addDisposableListener(inputBox.inputElement, DOM.EventType.BLUR, () => {
done(inputBox.isInputValid());
});
// It can happen that the tree re-renders this node. When that happens,
// we're gonna get a blur event first and only after an element disposable.
// Because of that, we should setTimeout the blur handler to differentiate
// between the blur happening because of a unrender or because of a user action.
let ignoreBlur = false;

const toDispose = [
inputBox,
DOM.addStandardDisposableListener(inputBox.inputElement, DOM.EventType.KEY_DOWN, (e: IKeyboardEvent) => {
if (e.equals(KeyCode.Enter)) {
if (inputBox.validate()) {
done(true);
done(true, false);
}
} else if (e.equals(KeyCode.Escape)) {
done(false);
done(false, false);
}
}),
blurDisposable,
DOM.addDisposableListener(inputBox.inputElement, DOM.EventType.BLUR, () => {
setTimeout(() => {
if (!ignoreBlur) {
done(inputBox.isInputValid(), true);
}
}, 0);
}),
label,
styler
];

return toDisposable(() => {
if (isFinishableDisposeEvent) {
done(false);
}
else {
dispose(toDispose);
label.element.remove();
}
});
return toDisposable(() => ignoreBlur = true);
}

disposeElement?(element: ITreeNode<ExplorerItem, FuzzyScore>, index: number, templateData: IFileTemplateData): void {
Expand Down

0 comments on commit b8974b0

Please sign in to comment.