-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathnotifications.ts
More file actions
212 lines (183 loc) · 7.56 KB
/
notifications.ts
File metadata and controls
212 lines (183 loc) · 7.56 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import { Command, ConfigurationChangeEvent, ExtensionContext, commands, window, workspace } from 'vscode';
import { ExtensionApi } from '../backend';
import { ProcessStatus, ProcessStatusType, ProcessType, ScheduledProcess } from '../backend/executor';
import { SidebarContainer } from '../sidebar';
import { NotificationItem } from '../sidebar/views';
export enum NotificationType {
information,
warning,
error
}
export interface NotificationOptions {
showOnTray?: boolean | 'always' // always bypasses the 'Show notifications' setting
choices?: Command[]
showOnSidebar?: boolean,
sidebarMessage?: string, // Same as popup message if not defined
}
export class NotificationHandler {
private defaultOptions: NotificationOptions = {
showOnTray: true,
showOnSidebar: true,
};
private showTrayNotifications: boolean;
constructor(ctx: ExtensionContext) {
this.showTrayNotifications = workspace.getConfiguration('codechecker.editor')
.get('enableNotifications') ?? true;
ExtensionApi.executorManager.processStatusChange(this.onProcessStatusChange, this, ctx.subscriptions);
}
private activeNotifications = new Map<string, NotificationItem>();
// When a command option is clicked, the provided command in `options.choices` gets executed.
// If the tray notification was clicked, the choice is returned as well.
public async showNotification(
type: NotificationType, message: string, options?: NotificationOptions
): Promise<string | undefined> {
options = {
...this.defaultOptions,
...(options ?? {})
};
if (options.showOnSidebar) {
// When the constructor calls this function, the sidebar is not yet initialized
SidebarContainer.notificationView?.addNotification(
type, options.sidebarMessage ?? message, options.choices
);
}
if (options.showOnTray === 'always' || (options.showOnTray && this.showTrayNotifications)) {
const choiceTitles = options.choices?.map((command) => command.title) ?? [];
let choice: string | undefined;
switch (type) {
case NotificationType.information:
choice = await window.showInformationMessage(message, ...choiceTitles);
break;
case NotificationType.warning:
choice = await window.showWarningMessage(message, ...choiceTitles);
break;
case NotificationType.error:
choice = await window.showErrorMessage(message, ...choiceTitles);
break;
}
const choiceCommand = options.choices?.find((command) => command.title === choice);
if (choiceCommand !== undefined && choiceCommand.command !== '') {
await commands.executeCommand(choiceCommand.command, ...(choiceCommand.arguments ?? []));
}
return choice;
}
return;
}
onProcessStatusChange([status, process]: [ProcessStatus, ScheduledProcess]) {
// Only display sidebar entries for longer-running processes
const processType = process.processParameters.processType ?? 'Other';
const allowedProcessTypes: string[] = [ProcessType.analyze, ProcessType.log];
if (!allowedProcessTypes.includes(processType)) {
return;
}
const notification = this.activeNotifications.get(process.commandLine);
if (notification === undefined && status.type !== ProcessStatusType.queued) {
return;
}
const makeMessage = (message: string): Command => {
return {
title: `Process "${processType}" ${message}`,
command: '',
tooltip: `Full command line: ${process.commandLine}`
};
};
const makeReason = (): (Command)[] => {
if (!status.reason) {
return [];
}
return [{
title: `Reason: ${status.reason}`,
command: 'codechecker.executor.showOutput',
tooltip: `Reason: ${status.reason}\nSee the output log for full details`
},
{
title: 'Show process logs',
command: 'codechecker.executor.showOutput'
}];
};
switch (status.type) {
case ProcessStatusType.queued: {
const newNotification = SidebarContainer.notificationView.addNotification(
'browser', makeMessage('added to the process queue'), [
{
title: 'Run now',
command: 'codechecker.executor.forceRunProcess',
arguments: [process]
},
{
title: 'Remove from queue',
command: 'codechecker.executor.removeFromQueue',
arguments: [process]
}
]);
this.activeNotifications.set(process.commandLine, newNotification);
break;
}
case ProcessStatusType.running: {
notification!.silentUpdate({ choices: [] });
const newNotification = SidebarContainer.notificationView.addNotification(
'browser', makeMessage('is running...'), [
{
title: 'Kill process',
command: 'codechecker.executor.stopCodeChecker'
},
{
title: 'Show process logs',
command: 'codechecker.executor.showOutput'
}
]);
this.activeNotifications.set(process.commandLine, newNotification);
break;
}
case ProcessStatusType.killed: {
notification!.update({
message: makeMessage('was killed'),
choices: []
});
this.activeNotifications.delete(process.commandLine);
break;
}
case ProcessStatusType.finished: {
notification!.update({
message: makeMessage('finished running'),
choices: makeReason()
});
this.activeNotifications.delete(process.commandLine);
SidebarContainer.reportsView.updateStatus();
break;
}
case ProcessStatusType.warning: {
notification!.update({
message: makeMessage('finished with warnings'),
choices: makeReason()
});
this.activeNotifications.delete(process.commandLine);
break;
}
case ProcessStatusType.errored: {
notification!.update({
message: makeMessage('finished with errors'),
choices: makeReason()
});
this.activeNotifications.delete(process.commandLine);
break;
}
case ProcessStatusType.removed: {
notification!.update({
message: makeMessage('removed from the process queue'),
type: 'browser',
choices: []
});
this.activeNotifications.delete(process.commandLine);
break;
}
default: break;
}
}
onConfigChanged(event: ConfigurationChangeEvent) {
if (event.affectsConfiguration('codechecker.editor')) {
this.showTrayNotifications = workspace.getConfiguration('codechecker.editor')
.get('enableNotifications') ?? true;
}
}
}