-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathextension-old-unused.js
298 lines (264 loc) · 9.28 KB
/
extension-old-unused.js
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
293
294
295
296
297
298
// Note: This is the older version that used an external process.
const vscode = require('vscode');
const os = require('os')
const path = require('path');
const fs = require('fs');
const child_process = require('child_process');
const title = 'Toggle Light/Dark Theme';
let terminating = false;
let process = null;
let outBuffer = "";
let pendingAction = null;
const lockFilePath = path.join(os.tmpdir(), 'vscode-auto-dark-mode-windows.lock');
function spawnProcess(context) {
try {
const options = {
stdio: [
null,
'pipe',
'pipe',
]
}
// Executable that efficiently waits for a change
let watcherExecutable = null;
if (os.platform() === 'win32') {
watcherExecutable = 'watcher-win.exe';
} else if (os.platform() === 'darwin') {
watcherExecutable = 'watcher-mac';
} else if (os.platform() === 'linux') {
watcherExecutable = 'watcher-lin';
}
if (!watcherExecutable) {
console.error(`${title}: Unsupported platform for watching system scheme: ${os.platform()}`);
return false;
}
const waitRegistryCommand = context.asAbsolutePath(watcherExecutable);
if (fs.existsSync(waitRegistryCommand)) {
process = child_process.spawn(waitRegistryCommand, [], options);
} else {
console.error(`${title}: External watch program not found: ${waitRegistryCommand}`);
return false;
}
process.stdout.on('data', (data) => {
outBuffer += data;
let idx;
while ((idx = outBuffer.indexOf('\n')) >= 0) {
const line = outBuffer.substr(0, idx).trim();
outBuffer = outBuffer.substr(idx + 1);
const value = parseInt(line);
if (value !== 0 || line === "0") {
matchDarkMode(value == 0);
} else {
console.warn(`${title}: unexpected stdout: ${data}`);
}
}
});
process.stderr.on('data', (data) => {
console.error(`${title}: stderr: ${data}`);
});
process.on('close', (code) => {
if (!terminating) {
console.error(`${title}: unexpected exit: ${code}`);
vscode.window.showErrorMessage(`${title}: unexpected process exit: ${code}`);
}
});
return true;
} catch (e) {
console.error(e);
return false;
}
}
async function killProcess() {
if (process !== null) {
terminating = true;
process.kill('SIGINT'); // 'SIGHUP', 'SIGINT', 'SIGTERM', 'SIGKILL'
process = null;
}
}
function getTheme(light) {
// Get the new, standard preferred theme setting
const workbenchConfiguration = vscode.workspace.getConfiguration('workbench');
let theme = workbenchConfiguration.get(light ? 'preferredLightColorTheme' : 'preferredDarkColorTheme');
if (!theme) {
// Get the old extension-specific configuration
const themeConfiguration = vscode.workspace.getConfiguration('autoDarkMode');
theme = themeConfiguration.get(light ? 'lightTheme' : 'darkTheme');
}
if (!theme) {
// Give up and return current theme
theme = workbenchConfiguration.get('colorTheme');
}
return theme;
}
function isDarkTheme() {
const currentTheme = vscode.workspace.getConfiguration('workbench').get('colorTheme');
return currentTheme === getTheme(false);
}
async function setTheme(isDark, delay, complete) {
// cope with change request during settle delay
pendingAction = { isDark, delay, complete };
// Use a lock-file to make sure we only do this in one instance of VS Code
let lockFile = null;
try {
// Try to clean a stale lock file (will fail if held by another)
try {
if (fs.existsSync(lockFilePath)) {
fs.unlinkSync(lockFilePath);
}
} catch(e) { ; }
lockFile = fs.openSync(lockFilePath, 'wx+');
for (;;) {
const action = pendingAction;
const currentTheme = vscode.workspace.getConfiguration('workbench').get('colorTheme');
let newTheme = getTheme(!action.isDark);
if (newTheme !== currentTheme) {
vscode.workspace.getConfiguration('workbench').update('colorTheme', newTheme, vscode.ConfigurationTarget.Global);
if (action.complete) { action.complete(action.isDark); }
if (action.delay) {
// Delay a little while in the lock so other instances will lose the race
await new Promise((resolve) => setTimeout(resolve, 5000));
}
}
// Check no other changes while we were busy
if (action === pendingAction) {
return action.isDark;
}
}
} catch(e) {
// Could not lock (pending action)
return undefined;
} finally {
try { if (lockFile != null) { fs.closeSync(lockFile); } } catch (e) { ; }
if (fs.existsSync(lockFilePath)) {
try { if (lockFile != null) { fs.unlinkSync(lockFilePath); } } catch (e) { ; }
}
}
}
async function matchDarkMode(dark) {
try {
function switched(isDark) {
statusMessage(`${isDark ? 'Dark' : 'Light'}`);
}
const result = await setTheme(dark, true, switched);
if (result === undefined) { // another process is switching
statusMessage(`${dark ? 'Dark' : 'Light'}`);
}
} catch(e) {
console.error(e);
vscode.window.showErrorMessage(`Problem while matching theme to ${dark ? 'dark' : 'light'}`);
}
}
async function toggleTheme() {
try {
const dark = !isDarkTheme();
function toggled(isDark) {
statusMessage(`${isDark ? 'Dark' : 'Light'}`);
}
await setTheme(dark, false, toggled);
} catch(e) {
console.error(e);
vscode.window.showErrorMessage(`Error toggling theme`);
}
}
let statusBarItem = null;
function activate(context) {
//console.info(`${title}: activated`);
const commandId = 'auto-dark-mode-windows.toggle';
let disposable = vscode.commands.registerCommand(commandId, toggleTheme);
context.subscriptions.push(disposable);
// create a new status bar item that we can now manage
statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 100);
statusBarItem.command = commandId;
context.subscriptions.push(statusBarItem);
context.subscriptions.push({ dispose: killProcess() });
let useBuiltIn = false;
const autoDetectColorScheme = vscode.workspace.getConfiguration('window').get('autoDetectColorScheme');
if (autoDetectColorScheme) {
useBuiltIn = true;
//context.subscriptions.push(vscode.window.onDidChangeTheme(updateStatusBarItem));
//vscode.window.showInformationMessage(`${title}: Using built-in theme switching.`);
}
// Placeholder code for possible cross-platform, method of detecting system dark mode changes
/*
// OLD POSSIBILITY 1: 'nativeTheme' module has the property: nativeTheme.shouldUseDarkColors / nativeTheme.themeSource
// OLD POSSIBILITY 2: const { systemPreferences } = require('electron'); systemPreferences.isDarkMode();
// OLD POSSIBILITY 3: If extensions could get a real 'window' object, could use matchMedia()
const windowObject = vscode.window; // This is not a real 'window' object
if (windowObject && windowObject.matchMedia) {
const matchMediaDark = windowObject.matchMedia('(prefers-color-scheme: dark)');
if (matchMediaDark.media !== 'not all') {
const listener = (isDark) => {
//vscode.window.showInformationMessage('BUILT-IN: state =', isDark.matches);
matchDarkMode(isDark.matches);
}
useBuiltIn = true;
//vscode.window.showInformationMessage('BUILT-IN: addListener');
matchMediaDark.addListener(listener);
context.subscriptions.push({ dispose: async () => {
//vscode.window.showInformationMessage('BUILT-IN: removeListener');
matchMediaDark.removeListener(listener);
}});
listener(matchDarkMode);
}
}
//vscode.window.showErrorMessage(`BUILT-IN: Using=${useBuiltIn}`);
*/
if (!useBuiltIn) {
const preference = 'window.autoDetectColorScheme';
vscode.window.showInformationMessage(
`VS Code can now match the system dark mode itself -- please set the setting: '${preference}'. This extension may still be used as a shortcut to manually toggle the theme (default: Cmd/Ctrl+Alt+Shift+T)`,
'Settings...'
).then((item) => {
if (!item) return;
vscode.commands.executeCommand('workbench.action.openSettings', preference);
});
const release = os.release().split('.').map(x => parseInt(x));
if (os.platform() !== 'win32' || release.length < 3 || release[0] < 10 || (release[0] === 10 && release[1] === 0 && release[2] < 17763)) {
vscode.window.showWarningMessage(`${title}: This extension can only monitor Dark Mode on Windows 10 (after October 2018 update).`);
} else if (!spawnProcess(context)) {
vscode.window.showErrorMessage(`${title}: Error spawning monitoring process`);
}
}
updateStatusBarItem();
}
let currentStatusMessage = null;
let currentStatusTimeout = null;
function updateStatusBarItem() {
if (!statusBarItem) return;
// https://code.visualstudio.com/api/references/icons-in-labels
// $(color-mode) $(activate-breakpoints) $(symbol-null) $(light-bulb) $(circle-filled)/$(circle-outline) $(star-full)/$(star-empty) $(eye)/$(eye-closed)
let text = `$(color-mode)`;
if (currentStatusMessage) {
text = text + ' ' + currentStatusMessage;
}
statusBarItem.text = text;
statusBarItem.tooltip = `Toggle dark/light theme.`;
statusBarItem.show();
}
function statusMessage(message) {
const timeout = 2000;
if (currentStatusTimeout) {
clearTimeout(currentStatusTimeout);
currentStatusTimeout = null;
}
if (!statusBarItem) {
if (!message) {
vscode.window.setStatusBarMessage(message, timeout);
}
return;
}
currentStatusMessage = message;
updateStatusBarItem();
currentStatusTimeout = setTimeout(() => {
statusMessage(null);
}, timeout);
}
async function deactivate() {
//console.info(`${title}: deactivating`);
statusBarItem = null;
await killProcess();
}
module.exports = {
activate,
deactivate
}