-
Notifications
You must be signed in to change notification settings - Fork 27.9k
/
watermark.ts
225 lines (192 loc) · 10.4 KB
/
watermark.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
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import 'vs/css!./media/watermark';
import { Disposable, DisposableStore } from 'vs/base/common/lifecycle';
import { isMacintosh, isWeb, OS } from 'vs/base/common/platform';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import * as nls from 'vs/nls';
import { Registry } from 'vs/platform/registry/common/platform';
import { IConfigurationRegistry, Extensions as ConfigurationExtensions } from 'vs/platform/configuration/common/configurationRegistry';
import { IWorkspaceContextService, WorkbenchState } from 'vs/platform/workspace/common/workspace';
import { IWorkbenchContribution, IWorkbenchContributionsRegistry, Extensions as WorkbenchExtensions } from 'vs/workbench/common/contributions';
import { ILifecycleService, LifecyclePhase } from 'vs/workbench/services/lifecycle/common/lifecycle';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { OpenFolderAction, OpenFileFolderAction, OpenFileAction } from 'vs/workbench/browser/actions/workspaceActions';
import { OpenRecentAction } from 'vs/workbench/browser/actions/windowActions';
import { ShowAllCommandsAction } from 'vs/workbench/contrib/quickaccess/browser/commandsQuickAccess';
import { Parts, IWorkbenchLayoutService } from 'vs/workbench/services/layout/browser/layoutService';
import { FindInFilesActionId } from 'vs/workbench/contrib/search/common/constants';
import * as dom from 'vs/base/browser/dom';
import { KeybindingLabel } from 'vs/base/browser/ui/keybindingLabel/keybindingLabel';
import { IEditorGroupsService } from 'vs/workbench/services/editor/common/editorGroupsService';
import { CommandsRegistry } from 'vs/platform/commands/common/commands';
import { TerminalCommandId } from 'vs/workbench/contrib/terminal/common/terminal';
import { assertIsDefined } from 'vs/base/common/types';
import { workbenchConfigurationNodeBase } from 'vs/workbench/common/configuration';
import { NEW_UNTITLED_FILE_COMMAND_ID } from 'vs/workbench/contrib/files/browser/fileCommands';
import { DEBUG_START_COMMAND_ID } from 'vs/workbench/contrib/debug/browser/debugCommands';
import { IThemeService } from 'vs/platform/theme/common/themeService';
import { attachKeybindingLabelStyler } from 'vs/platform/theme/common/styler';
import { ContextKeyExpression, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { TerminalContextKeys } from 'vs/workbench/contrib/terminal/common/terminalContextKey';
const $ = dom.$;
interface WatermarkEntry {
text: string;
id: string;
mac?: boolean;
when?: ContextKeyExpression;
}
const showCommands: WatermarkEntry = { text: nls.localize('watermark.showCommands', "Show All Commands"), id: ShowAllCommandsAction.ID };
const quickAccess: WatermarkEntry = { text: nls.localize('watermark.quickAccess', "Go to File"), id: 'workbench.action.quickOpen' };
const openFileNonMacOnly: WatermarkEntry = { text: nls.localize('watermark.openFile', "Open File"), id: OpenFileAction.ID, mac: false };
const openFolderNonMacOnly: WatermarkEntry = { text: nls.localize('watermark.openFolder', "Open Folder"), id: OpenFolderAction.ID, mac: false };
const openFileOrFolderMacOnly: WatermarkEntry = { text: nls.localize('watermark.openFileFolder', "Open File or Folder"), id: OpenFileFolderAction.ID, mac: true };
const openRecent: WatermarkEntry = { text: nls.localize('watermark.openRecent', "Open Recent"), id: OpenRecentAction.ID };
const newUntitledFile: WatermarkEntry = { text: nls.localize('watermark.newUntitledFile', "New Untitled File"), id: NEW_UNTITLED_FILE_COMMAND_ID };
const newUntitledFileMacOnly: WatermarkEntry = Object.assign({ mac: true }, newUntitledFile);
const findInFiles: WatermarkEntry = { text: nls.localize('watermark.findInFiles', "Find in Files"), id: FindInFilesActionId };
const toggleTerminal: WatermarkEntry = { text: nls.localize({ key: 'watermark.toggleTerminal', comment: ['toggle is a verb here'] }, "Toggle Terminal"), id: TerminalCommandId.Toggle, when: TerminalContextKeys.processSupported };
const startDebugging: WatermarkEntry = { text: nls.localize('watermark.startDebugging', "Start Debugging"), id: DEBUG_START_COMMAND_ID, when: TerminalContextKeys.processSupported };
const toggleFullscreen: WatermarkEntry = { text: nls.localize({ key: 'watermark.toggleFullscreen', comment: ['toggle is a verb here'] }, "Toggle Full Screen"), id: 'workbench.action.toggleFullScreen', when: TerminalContextKeys.processSupported.toNegated() };
const showSettings: WatermarkEntry = { text: nls.localize('watermark.showSettings', "Show Settings"), id: 'workbench.action.openSettings', when: TerminalContextKeys.processSupported.toNegated() };
const noFolderEntries = [
showCommands,
openFileNonMacOnly,
openFolderNonMacOnly,
openFileOrFolderMacOnly,
openRecent,
newUntitledFileMacOnly
];
const folderEntries = [
showCommands,
quickAccess,
findInFiles,
startDebugging,
toggleTerminal,
toggleFullscreen,
showSettings
];
const WORKBENCH_TIPS_ENABLED_KEY = 'workbench.tips.enabled';
export class WatermarkContribution extends Disposable implements IWorkbenchContribution {
private watermark: HTMLElement | undefined;
private watermarkDisposable = this._register(new DisposableStore());
private enabled: boolean;
private workbenchState: WorkbenchState;
constructor(
@ILifecycleService private readonly lifecycleService: ILifecycleService,
@IWorkbenchLayoutService private readonly layoutService: IWorkbenchLayoutService,
@IKeybindingService private readonly keybindingService: IKeybindingService,
@IWorkspaceContextService private readonly contextService: IWorkspaceContextService,
@IContextKeyService private readonly contextKeyService: IContextKeyService,
@IConfigurationService private readonly configurationService: IConfigurationService,
@IEditorGroupsService private readonly editorGroupsService: IEditorGroupsService,
@IThemeService private readonly themeService: IThemeService,
@ITelemetryService private readonly telemetryService: ITelemetryService
) {
super();
this.workbenchState = contextService.getWorkbenchState();
this.enabled = this.configurationService.getValue<boolean>(WORKBENCH_TIPS_ENABLED_KEY);
this.registerListeners();
if (this.enabled) {
this.create();
}
}
private registerListeners(): void {
this.lifecycleService.onDidShutdown(() => this.dispose());
this._register(this.configurationService.onDidChangeConfiguration(e => {
if (e.affectsConfiguration(WORKBENCH_TIPS_ENABLED_KEY)) {
const enabled = this.configurationService.getValue<boolean>(WORKBENCH_TIPS_ENABLED_KEY);
if (enabled !== this.enabled) {
this.enabled = enabled;
if (this.enabled) {
this.create();
} else {
this.destroy();
}
}
}
}));
this._register(this.contextService.onDidChangeWorkbenchState(e => {
const previousWorkbenchState = this.workbenchState;
this.workbenchState = this.contextService.getWorkbenchState();
if (this.enabled && this.workbenchState !== previousWorkbenchState) {
this.recreate();
}
}));
const allEntriesWhenClauses = [...noFolderEntries, ...folderEntries].filter(entry => entry.when !== undefined).map(entry => entry.when!);
const allKeys = new Set<string>();
allEntriesWhenClauses.forEach(when => when.keys().forEach(key => allKeys.add(key)));
this._register(this.contextKeyService.onDidChangeContext(e => {
if (e.affectsSome(allKeys)) {
this.recreate();
}
}));
}
private create(): void {
const container = assertIsDefined(this.layoutService.getContainer(Parts.EDITOR_PART));
container.classList.add('has-watermark');
this.watermark = $('.watermark');
const box = dom.append(this.watermark, $('.watermark-box'));
const folder = this.workbenchState !== WorkbenchState.EMPTY;
const selected = (folder ? folderEntries : noFolderEntries)
.filter(entry => !('when' in entry) || this.contextKeyService.contextMatchesRules(entry.when))
.filter(entry => !('mac' in entry) || entry.mac === (isMacintosh && !isWeb))
.filter(entry => !!CommandsRegistry.getCommand(entry.id));
const keybindingLabelStylers = this.watermarkDisposable.add(new DisposableStore());
const update = () => {
dom.clearNode(box);
keybindingLabelStylers.clear();
selected.map(entry => {
const dl = dom.append(box, $('dl'));
const dt = dom.append(dl, $('dt'));
dt.textContent = entry.text;
const dd = dom.append(dl, $('dd'));
const keybinding = new KeybindingLabel(dd, OS, { renderUnboundKeybindings: true });
keybindingLabelStylers.add(attachKeybindingLabelStyler(keybinding, this.themeService));
keybinding.set(this.keybindingService.lookupKeybinding(entry.id));
});
};
update();
dom.prepend(container.firstElementChild as HTMLElement, this.watermark);
this.watermarkDisposable.add(this.keybindingService.onDidUpdateKeybindings(update));
this.watermarkDisposable.add(this.editorGroupsService.onDidLayout(dimension => this.handleEditorPartSize(container, dimension)));
this.handleEditorPartSize(container, this.editorGroupsService.contentDimension);
/* __GDPR__
"watermark:open" : { }
*/
this.telemetryService.publicLog('watermark:open');
}
private handleEditorPartSize(container: HTMLElement, dimension: dom.IDimension): void {
container.classList.toggle('max-height-478px', dimension.height <= 478);
}
private destroy(): void {
if (this.watermark) {
this.watermark.remove();
const container = this.layoutService.getContainer(Parts.EDITOR_PART);
if (container) {
container.classList.remove('has-watermark');
}
this.watermarkDisposable.clear();
}
}
private recreate(): void {
this.destroy();
this.create();
}
}
Registry.as<IWorkbenchContributionsRegistry>(WorkbenchExtensions.Workbench)
.registerWorkbenchContribution(WatermarkContribution, LifecyclePhase.Restored);
Registry.as<IConfigurationRegistry>(ConfigurationExtensions.Configuration)
.registerConfiguration({
...workbenchConfigurationNodeBase,
'properties': {
'workbench.tips.enabled': {
'type': 'boolean',
'default': true,
'description': nls.localize('tips.enabled', "When enabled, will show the watermark tips when no editor is open.")
},
}
});