forked from LeetCode-OpenSource/vscode-leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuiUtils.ts
126 lines (113 loc) · 4.53 KB
/
uiUtils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright (c) jdneo. All rights reserved.
// Licensed under the MIT license.
import * as vscode from "vscode";
import { getLeetCodeEndpoint } from "../commands/plugin";
import { leetCodeChannel } from "../leetCodeChannel";
import { getWorkspaceConfiguration } from "./settingUtils";
export namespace DialogOptions {
export const open: vscode.MessageItem = { title: "Open" };
export const yes: vscode.MessageItem = { title: "Yes" };
export const no: vscode.MessageItem = { title: "No", isCloseAffordance: true };
export const never: vscode.MessageItem = { title: "Never" };
export const singUp: vscode.MessageItem = { title: "Sign up" };
}
export async function promptForOpenOutputChannel(message: string, type: DialogType): Promise<void> {
let result: vscode.MessageItem | undefined;
switch (type) {
case DialogType.info:
result = await vscode.window.showInformationMessage(message, DialogOptions.open, DialogOptions.no);
break;
case DialogType.warning:
result = await vscode.window.showWarningMessage(message, DialogOptions.open, DialogOptions.no);
break;
case DialogType.error:
result = await vscode.window.showErrorMessage(message, DialogOptions.open, DialogOptions.no);
break;
default:
break;
}
if (result === DialogOptions.open) {
leetCodeChannel.show();
}
}
export async function promptForSignIn(): Promise<void> {
const choice: vscode.MessageItem | undefined = await vscode.window.showInformationMessage(
"Please sign in to LeetCode.",
DialogOptions.yes,
DialogOptions.no,
DialogOptions.singUp,
);
switch (choice) {
case DialogOptions.yes:
await vscode.commands.executeCommand("leetcode.signin");
break;
case DialogOptions.singUp:
if (getLeetCodeEndpoint()) {
openUrl("https://leetcode-cn.com");
} else {
openUrl("https://leetcode.com");
}
break;
default:
break;
}
}
export async function promptHintMessage(config: string, message: string, choiceConfirm: string, onConfirm: () => Promise<any>): Promise<void> {
if (getWorkspaceConfiguration().get<boolean>(config)) {
const choiceNoShowAgain: string = "Don't show again";
const choice: string | undefined = await vscode.window.showInformationMessage(
message, choiceConfirm, choiceNoShowAgain,
);
if (choice === choiceConfirm) {
await onConfirm();
} else if (choice === choiceNoShowAgain) {
await getWorkspaceConfiguration().update(config, false, true /* UserSetting */);
}
}
}
export async function openSettingsEditor(query?: string): Promise<void> {
await vscode.commands.executeCommand("workbench.action.openSettings", query);
}
export async function openKeybindingsEditor(query?: string): Promise<void> {
await vscode.commands.executeCommand("workbench.action.openGlobalKeybindings", query);
}
export async function showFileSelectDialog(fsPath?: string): Promise<vscode.Uri[] | undefined> {
const defaultUri: vscode.Uri | undefined = getBelongingWorkspaceFolderUri(fsPath);
const options: vscode.OpenDialogOptions = {
defaultUri,
canSelectFiles: true,
canSelectFolders: false,
canSelectMany: false,
openLabel: "Select",
};
return await vscode.window.showOpenDialog(options);
}
function getBelongingWorkspaceFolderUri(fsPath: string | undefined): vscode.Uri | undefined {
let defaultUri: vscode.Uri | undefined;
if (fsPath) {
const workspaceFolder: vscode.WorkspaceFolder | undefined = vscode.workspace.getWorkspaceFolder(vscode.Uri.file(fsPath));
if (workspaceFolder) {
defaultUri = workspaceFolder.uri;
}
}
return defaultUri;
}
export async function showDirectorySelectDialog(fsPath?: string): Promise<vscode.Uri[] | undefined> {
const defaultUri: vscode.Uri | undefined = getBelongingWorkspaceFolderUri(fsPath);
const options: vscode.OpenDialogOptions = {
defaultUri,
canSelectFiles: false,
canSelectFolders: true,
canSelectMany: false,
openLabel: "Select",
};
return await vscode.window.showOpenDialog(options);
}
export async function openUrl(url: string): Promise<void> {
vscode.commands.executeCommand("vscode.open", vscode.Uri.parse(url));
}
export enum DialogType {
info = "info",
warning = "warning",
error = "error",
}