|
| 1 | +import { env, Uri, window } from "vscode"; |
| 2 | + |
| 3 | +/** |
| 4 | + * Opens a modal dialog for the user to make a yes/no choice. |
| 5 | + * |
| 6 | + * @param message The message to show. |
| 7 | + * @param modal If true (the default), show a modal dialog box, otherwise dialog is non-modal and can |
| 8 | + * be closed even if the user does not make a choice. |
| 9 | + * @param yesTitle The text in the box indicating the affirmative choice. |
| 10 | + * @param noTitle The text in the box indicating the negative choice. |
| 11 | + * |
| 12 | + * @return |
| 13 | + * `true` if the user clicks 'Yes', |
| 14 | + * `false` if the user clicks 'No' or cancels the dialog, |
| 15 | + * `undefined` if the dialog is closed without the user making a choice. |
| 16 | + */ |
| 17 | +export async function showBinaryChoiceDialog( |
| 18 | + message: string, |
| 19 | + modal = true, |
| 20 | + yesTitle = "Yes", |
| 21 | + noTitle = "No", |
| 22 | +): Promise<boolean | undefined> { |
| 23 | + const yesItem = { title: yesTitle, isCloseAffordance: false }; |
| 24 | + const noItem = { title: noTitle, isCloseAffordance: true }; |
| 25 | + const chosenItem = await window.showInformationMessage( |
| 26 | + message, |
| 27 | + { modal }, |
| 28 | + yesItem, |
| 29 | + noItem, |
| 30 | + ); |
| 31 | + if (!chosenItem) { |
| 32 | + return undefined; |
| 33 | + } |
| 34 | + return chosenItem?.title === yesItem.title; |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Opens a modal dialog for the user to make a yes/no choice. |
| 39 | + * |
| 40 | + * @param message The message to show. |
| 41 | + * @param modal If true (the default), show a modal dialog box, otherwise dialog is non-modal and can |
| 42 | + * be closed even if the user does not make a choice. |
| 43 | + * |
| 44 | + * @return |
| 45 | + * `true` if the user clicks 'Yes', |
| 46 | + * `false` if the user clicks 'No' or cancels the dialog, |
| 47 | + * `undefined` if the dialog is closed without the user making a choice. |
| 48 | + */ |
| 49 | +export async function showBinaryChoiceWithUrlDialog( |
| 50 | + message: string, |
| 51 | + url: string, |
| 52 | +): Promise<boolean | undefined> { |
| 53 | + const urlItem = { title: "More Information", isCloseAffordance: false }; |
| 54 | + const yesItem = { title: "Yes", isCloseAffordance: false }; |
| 55 | + const noItem = { title: "No", isCloseAffordance: true }; |
| 56 | + let chosenItem; |
| 57 | + |
| 58 | + // Keep the dialog open as long as the user is clicking the 'more information' option. |
| 59 | + // To prevent an infinite loop, if the user clicks 'more information' 5 times, close the dialog and return cancelled |
| 60 | + let count = 0; |
| 61 | + do { |
| 62 | + chosenItem = await window.showInformationMessage( |
| 63 | + message, |
| 64 | + { modal: true }, |
| 65 | + urlItem, |
| 66 | + yesItem, |
| 67 | + noItem, |
| 68 | + ); |
| 69 | + if (chosenItem === urlItem) { |
| 70 | + await env.openExternal(Uri.parse(url, true)); |
| 71 | + } |
| 72 | + count++; |
| 73 | + } while (chosenItem === urlItem && count < 5); |
| 74 | + |
| 75 | + if (!chosenItem || chosenItem.title === urlItem.title) { |
| 76 | + return undefined; |
| 77 | + } |
| 78 | + return chosenItem.title === yesItem.title; |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * Show an information message with a customisable action. |
| 83 | + * @param message The message to show. |
| 84 | + * @param actionMessage The call to action message. |
| 85 | + * |
| 86 | + * @return `true` if the user clicks the action, `false` if the user cancels the dialog. |
| 87 | + */ |
| 88 | +export async function showInformationMessageWithAction( |
| 89 | + message: string, |
| 90 | + actionMessage: string, |
| 91 | +): Promise<boolean> { |
| 92 | + const actionItem = { title: actionMessage, isCloseAffordance: false }; |
| 93 | + const chosenItem = await window.showInformationMessage(message, actionItem); |
| 94 | + return chosenItem === actionItem; |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * Opens a modal dialog for the user to make a choice between yes/no/never be asked again. |
| 99 | + * |
| 100 | + * @param message The message to show. |
| 101 | + * @param modal If true (the default), show a modal dialog box, otherwise dialog is non-modal and can |
| 102 | + * be closed even if the user does not make a choice. |
| 103 | + * @param yesTitle The text in the box indicating the affirmative choice. |
| 104 | + * @param noTitle The text in the box indicating the negative choice. |
| 105 | + * @param neverTitle The text in the box indicating the opt out choice. |
| 106 | + * |
| 107 | + * @return |
| 108 | + * `Yes` if the user clicks 'Yes', |
| 109 | + * `No` if the user clicks 'No' or cancels the dialog, |
| 110 | + * `No, and never ask me again` if the user clicks 'No, and never ask me again', |
| 111 | + * `undefined` if the dialog is closed without the user making a choice. |
| 112 | + */ |
| 113 | +export async function showNeverAskAgainDialog( |
| 114 | + message: string, |
| 115 | + modal = true, |
| 116 | + yesTitle = "Yes", |
| 117 | + noTitle = "No", |
| 118 | + neverAskAgainTitle = "No, and never ask me again", |
| 119 | +): Promise<string | undefined> { |
| 120 | + const yesItem = { title: yesTitle, isCloseAffordance: true }; |
| 121 | + const noItem = { title: noTitle, isCloseAffordance: false }; |
| 122 | + const neverAskAgainItem = { |
| 123 | + title: neverAskAgainTitle, |
| 124 | + isCloseAffordance: false, |
| 125 | + }; |
| 126 | + const chosenItem = await window.showInformationMessage( |
| 127 | + message, |
| 128 | + { modal }, |
| 129 | + yesItem, |
| 130 | + noItem, |
| 131 | + neverAskAgainItem, |
| 132 | + ); |
| 133 | + |
| 134 | + return chosenItem?.title; |
| 135 | +} |
0 commit comments