greatTeacherOnizuka
greatTeacherOnizuka

Reputation: 565

VScode extension add cancellation event to showInputBox

I have an input box that asks the user to enter their username in my extension. I would like that when user click on esc or cancel the extensions stop running.

This is what I tried so far but no luck (it seems that onCancellationRequested is expecting an Event and not a method..

await window.showInputBox(
{prompt: "UserName: ",
    placeHolder: "UserName"}, 
{isCancellationRequested: true, onCancellationRequested: (cancelled)=> {throw new Error('cancelled')}})

Upvotes: 2

Views: 1580

Answers (1)

Gama11
Gama11

Reputation: 34158

This is not what the CancellationToken is for. It does pretty much the opposite of what you want, it can be used to cancel the input popup programatically before ther user has performed any input.

If you want to find out whether the user has cancelled the input box, you need to check the return value of the Thenable as mentioned in the showInputBox() docs:

The returned value will be undefined if the input box was canceled (e.g. pressing ESC). [...]

vscode.window.showInputBox({prompt: 'UserName', placeHolder: 'UserName'}).then(value => {
    if (value === undefined) {
        throw new Error('cancelled');
    }
    // handle valid values
});

Upvotes: 3

Related Questions