-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.js
More file actions
292 lines (244 loc) · 10.3 KB
/
extension.js
File metadata and controls
292 lines (244 loc) · 10.3 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
const vscode = require('vscode');
let statusBarReminder, statusBarInfo, reminderIntervals = {};
const thankYouMessage = 'Great! 👍';
let config = vscode.workspace.getConfiguration('mindfulCoding');
let reminderType = config.get('general.reminderType', 'None');
function activate(context) {
context.subscriptions.push(
vscode.commands.registerCommand('mindfulCoding.openSettings', () => {
vscode.commands.executeCommand('workbench.action.openSettings', 'mindfulCoding');
statusBarInfo.hide();
}),
vscode.commands.registerCommand('mindfulCoding.dismissReminder', () => {
statusBarReminder.hide();
displayStatusBarMessage(thankYouMessage);
})
);
setupStatusBarItems(context);
if (!context.globalState.get('hasBeenPromptedForReminderSettings', false)) {
promptForSettingsReset(context).then(() => {
context.globalState.update('hasBeenPromptedForReminderSettings', true);
});
} else {
setupReminders(context);
}
context.subscriptions.push(vscode.workspace.onDidChangeConfiguration(e => {
if (['windowGaze.interval', 'stretch.interval', 'general.reminderType', 'windowGaze.enable', 'stretch.enable', 'customReminders.enable', 'customReminders.list']
.some(setting => e.affectsConfiguration(`mindfulCoding.${setting}`))) {
setupReminders(context, true);
}
}));
context.subscriptions.push(
vscode.commands.registerCommand('mindfulCoding.manageCustomReminders', () => {
manageCustomReminders(context);
})
);
}
function setupStatusBarItems(context) {
statusBarInfo = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 99);
statusBarInfo.command = "mindfulCoding.openSettings";
statusBarInfo.tooltip = "Click to customize Mindful Coding settings";
context.subscriptions.push(statusBarInfo);
statusBarReminder = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 100);
statusBarReminder.command = "mindfulCoding.dismissReminder";
statusBarReminder.tooltip = "Click when done";
context.subscriptions.push(statusBarReminder);
}
async function promptForSettingsReset(context) {
const reminderTypeConfig = vscode.workspace.getConfiguration('mindfulCoding').get('general.reminderType');
if (reminderTypeConfig !== undefined) {
const selection = await vscode.window.showInformationMessage('You have existing settings for Mindful Coding. Would you like to keep them or reset to defaults?', 'Keep', 'Reset');
if (selection === 'Reset') {
await resetSettings();
askReminderModeNotification(context);
}
}
}
async function resetSettings() {
config = vscode.workspace.getConfiguration('mindfulCoding');
await Promise.all([
'general.reminderType', 'windowGaze.interval', 'stretch.interval', 'windowGaze.enable', 'stretch.enable', 'customReminders.enable', 'customReminders.list'
].map(setting => config.update(setting, undefined, vscode.ConfigurationTarget.Global)));
}
function setupReminders(context, updatedSettings = false) {
config = vscode.workspace.getConfiguration('mindfulCoding');
reminderType = config.get('general.reminderType', 'None');
Object.values(reminderIntervals).forEach(clearInterval);
if (reminderType === 'None') {
displayStatusBarInfo('$(gear) Mindful Coding is disabled');
return;
}
if (config.get('windowGaze.enable')) {
reminderIntervals['windowGaze'] = setupInterval('Time to gaze out of a window. 🌳', config.get('windowGaze.interval'), context);
}
if (config.get('stretch.enable')) {
reminderIntervals['stretch'] = setupInterval('Time to stretch. 😺', config.get('stretch.interval'), context);
}
if (config.get('customReminders.enable', true)) {
const customReminders = config.get('customReminders.list', []);
customReminders.forEach((reminder, index) => {
reminderIntervals[`custom_${index}`] = setupInterval(reminder.text, reminder.interval, context);
});
}
if (updatedSettings) {
displayStatusBarInfo('$(gear) Mindful Coding settings updated');
} else {
displayStatusBarInfo('$(gear) Mindful Coding is active. Click to customize.');
}
}
function setupInterval(message, intervalInMinutes, context) {
const interval = Math.max(intervalInMinutes * 60000, 60000);
return setInterval(() => showReminder(message, context), interval);
}
const lastPopupTimestampKey = 'mindfulCoding.lastPopupTimestamp';
function showReminder(message, context) {
if (reminderType === 'None') {
return;
}
if (!context || !context.globalState) {
console.error('Context or global state is undefined');
return;
}
const now = new Date().getTime();
const lastPopupTimestamp = context.globalState.get(lastPopupTimestampKey, 0);
const popupCooldown = 50000;
if (reminderType === 'Status Bar') {
statusBarReminder.text = `$(clock) ${message}`;
statusBarReminder.tooltip = "Click when done";
statusBarReminder.show();
} else if (reminderType === 'Annoying Popup') {
if (now - lastPopupTimestamp > popupCooldown) {
vscode.window.showInformationMessage(message, { modal: true }, 'Done').then(selection => {
if (selection === 'Done') {
displayStatusBarMessage(thankYouMessage);
context.globalState.update(lastPopupTimestampKey, now);
}
});
}
} else {
vscode.window.showInformationMessage(message, 'Done').then(selection => {
if (selection === 'Done' && reminderType === 'Notification') {
displayStatusBarMessage(thankYouMessage);
}
});
}
}
let statusBarInfoTimeout, statusBarMessageTimeout;
function displayStatusBarInfo(message) {
statusBarInfo.text = message;
statusBarInfo.show();
if (statusBarInfoTimeout) {
clearTimeout(statusBarInfoTimeout);
}
statusBarInfoTimeout = setTimeout(() => {
statusBarInfo.hide();
}, 5000);
}
function displayStatusBarMessage(message) {
statusBarReminder.text = `$(check) ${message}`;
statusBarReminder.show();
if (statusBarMessageTimeout) {
clearTimeout(statusBarMessageTimeout);
}
statusBarMessageTimeout = setTimeout(() => {
statusBarReminder.hide();
}, 5000);
}
function manageCustomReminders(context) {
const config = vscode.workspace.getConfiguration('mindfulCoding');
const customReminders = config.get('customReminders.list', []);
const quickPickItems = customReminders.map((reminder, index) => ({
label: `${reminder.text} (every ${reminder.interval} minutes)`,
description: `Custom Reminder ${index + 1}`,
reminder: reminder,
index: index
}));
quickPickItems.push({ label: '$(add) Add new custom reminder', description: 'Create a new custom reminder' });
vscode.window.showQuickPick(quickPickItems, {
placeHolder: 'Select a custom reminder to edit, delete, or add a new one'
}).then(selected => {
if (selected) {
if (selected.reminder) {
vscode.window.showQuickPick(['Edit', 'Delete'], {
placeHolder: 'Edit or delete this reminder?'
}).then(action => {
if (action === 'Edit') {
editCustomReminder(context, selected.index);
} else if (action === 'Delete') {
deleteCustomReminder(context, selected.index);
}
});
} else {
addCustomReminder(context);
}
}
});
}
async function addCustomReminder(context) {
const text = await vscode.window.showInputBox({
prompt: 'Enter the reminder text',
validateInput: validateReminderText
});
if (!text) return;
const interval = await vscode.window.showInputBox({
prompt: 'Enter the interval in minutes (minimum 1 minute)',
validateInput: validateNumber
});
if (!interval) return;
const config = vscode.workspace.getConfiguration('mindfulCoding');
const customReminders = config.get('customReminders.list', []);
customReminders.push({ text, interval: parseInt(interval) });
await config.update('customReminders.list', customReminders, vscode.ConfigurationTarget.Global);
setupReminders(context, true);
}
async function editCustomReminder(context, index) {
const config = vscode.workspace.getConfiguration('mindfulCoding');
const customReminders = config.get('customReminders.list', []);
const reminder = customReminders[index];
const text = await vscode.window.showInputBox({
prompt: 'Enter the new reminder text',
value: reminder.text,
validateInput: validateReminderText
});
if (!text) return;
const interval = await vscode.window.showInputBox({
prompt: 'Enter the new interval in minutes (minimum 1 minute)',
value: reminder.interval.toString(),
validateInput: validateNumber
});
if (!interval) return;
customReminders[index] = { text, interval: parseInt(interval) };
await config.update('customReminders.list', customReminders, vscode.ConfigurationTarget.Global);
setupReminders(context, true);
}
async function deleteCustomReminder(context, index) {
const config = vscode.workspace.getConfiguration('mindfulCoding');
const customReminders = config.get('customReminders.list', []);
customReminders.splice(index, 1);
await config.update('customReminders.list', customReminders, vscode.ConfigurationTarget.Global);
setupReminders(context, true);
vscode.window.showInformationMessage('Custom reminder deleted successfully.');
}
function validateNumber(value) {
const num = parseInt(value);
if (isNaN(num)) {
return 'Please enter a valid number';
}
if (num < 1) {
return 'The minimum interval is 1 minute';
}
return null;
}
function validateReminderText(value) {
if (!value || value.trim() === '') {
return 'The reminder message cannot be empty';
}
return null;
}
function deactivate() {
Object.values(reminderIntervals).forEach(clearInterval);
}
module.exports = {
activate,
deactivate
};