-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
vsx-extensions-contribution.ts
188 lines (174 loc) · 8.69 KB
/
vsx-extensions-contribution.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
/********************************************************************************
* Copyright (C) 2020 TypeFox and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
import { injectable, inject } from 'inversify';
import { Command, CommandRegistry } from '@theia/core/lib/common/command';
import { AbstractViewContribution } from '@theia/core/lib/browser/shell/view-contribution';
import { VSXExtensionsViewContainer } from './vsx-extensions-view-container';
import { Widget } from '@theia/core/lib/browser/widgets/widget';
import { VSXExtensionsModel } from './vsx-extensions-model';
import { ColorContribution } from '@theia/core/lib/browser/color-application-contribution';
import { ColorRegistry, Color } from '@theia/core/lib/browser/color-registry';
import { TabBarToolbarContribution, TabBarToolbarItem, TabBarToolbarRegistry } from '@theia/core/lib/browser/shell/tab-bar-toolbar';
import { FrontendApplicationContribution, FrontendApplication } from '@theia/core/lib/browser/frontend-application';
import { MessageService, Mutable } from '@theia/core/lib/common';
import { FileDialogService, OpenFileDialogProps } from '@theia/filesystem/lib/browser';
import { PluginServer } from '@theia/plugin-ext/lib/common';
import URI from '@theia/core/lib/common/uri';
import { LabelProvider } from '@theia/core/lib/browser';
import { VscodeCommands } from '@theia/plugin-ext-vscode/lib/browser/plugin-vscode-commands-contribution';
export namespace VSXExtensionsCommands {
export const CLEAR_ALL: Command = {
id: 'vsxExtensions.clearAll',
category: 'Extensions',
label: 'Clear Search Results',
iconClass: 'clear-all'
};
export const INSTALL_FROM_VSIX: Command & { dialogLabel: string } = {
id: 'vsxExtensions.installFromVSIX',
category: 'Extensions',
label: 'Install from VSIX...',
dialogLabel: 'Install from VSIX'
};
}
@injectable()
export class VSXExtensionsContribution extends AbstractViewContribution<VSXExtensionsViewContainer>
implements ColorContribution, FrontendApplicationContribution, TabBarToolbarContribution {
@inject(VSXExtensionsModel) protected readonly model: VSXExtensionsModel;
@inject(CommandRegistry) protected readonly commandRegistry: CommandRegistry;
@inject(PluginServer) protected readonly pluginServer: PluginServer;
@inject(TabBarToolbarRegistry) protected readonly tabbarToolbarRegistry: TabBarToolbarRegistry;
@inject(FileDialogService) protected readonly fileDialogService: FileDialogService;
@inject(MessageService) protected readonly messageService: MessageService;
@inject(LabelProvider) protected readonly labelProvider: LabelProvider;
constructor() {
super({
widgetId: VSXExtensionsViewContainer.ID,
widgetName: VSXExtensionsViewContainer.LABEL,
defaultWidgetOptions: {
area: 'left',
rank: 500
},
toggleCommandId: 'vsxExtensions.toggle',
toggleKeybinding: 'ctrlcmd+shift+x'
});
}
async initializeLayout(app: FrontendApplication): Promise<void> {
await this.openView({ activate: false });
}
registerCommands(commands: CommandRegistry): void {
super.registerCommands(commands);
commands.registerCommand(VSXExtensionsCommands.CLEAR_ALL, {
execute: w => this.withWidget(w, () => this.model.search.query = ''),
isEnabled: w => this.withWidget(w, () => !!this.model.search.query),
isVisible: w => this.withWidget(w, () => true)
});
commands.registerCommand(VSXExtensionsCommands.INSTALL_FROM_VSIX, {
execute: () => this.installFromVSIX()
});
}
registerToolbarItems(registry: TabBarToolbarRegistry): void {
registry.registerItem({
id: VSXExtensionsCommands.CLEAR_ALL.id,
command: VSXExtensionsCommands.CLEAR_ALL.id,
tooltip: VSXExtensionsCommands.CLEAR_ALL.label,
priority: 1,
onDidChange: this.model.onDidChange
});
this.registerMoreToolbarItem({
id: VSXExtensionsCommands.INSTALL_FROM_VSIX.id,
command: VSXExtensionsCommands.INSTALL_FROM_VSIX.id,
tooltip: VSXExtensionsCommands.INSTALL_FROM_VSIX.label,
group: 'other_1'
});
}
/**
* Register commands to the `More Actions...` extensions toolbar item.
*/
public registerMoreToolbarItem = (item: Mutable<TabBarToolbarItem>) => {
const commandId = item.command;
const id = 'vsxExtensions.tabbar.toolbar.' + commandId;
const command = this.commandRegistry.getCommand(commandId);
this.commandRegistry.registerCommand({ id, iconClass: command && command.iconClass }, {
execute: (w, ...args) => w instanceof VSXExtensionsViewContainer
&& this.commandRegistry.executeCommand(commandId, ...args),
isEnabled: (w, ...args) => w instanceof VSXExtensionsViewContainer
&& this.commandRegistry.isEnabled(commandId, ...args),
isVisible: (w, ...args) => w instanceof VSXExtensionsViewContainer
&& this.commandRegistry.isVisible(commandId, ...args),
isToggled: (w, ...args) => w instanceof VSXExtensionsViewContainer
&& this.commandRegistry.isToggled(commandId, ...args),
});
item.command = id;
this.tabbarToolbarRegistry.registerItem(item);
};
registerColors(colors: ColorRegistry): void {
// VS Code colors should be aligned with https://code.visualstudio.com/api/references/theme-color#extensions
colors.register(
{
id: 'extensionButton.prominentBackground', defaults: {
dark: '#327e36',
light: '#327e36'
}, description: 'Button background color for actions extension that stand out (e.g. install button).'
},
{
id: 'extensionButton.prominentForeground', defaults: {
dark: Color.white,
light: Color.white
}, description: 'Button foreground color for actions extension that stand out (e.g. install button).'
},
{
id: 'extensionButton.prominentHoverBackground', defaults: {
dark: '#28632b',
light: '#28632b'
}, description: 'Button background hover color for actions extension that stand out (e.g. install button).'
}
);
}
protected withWidget<T>(widget: Widget | undefined = this.tryGetWidget(), fn: (widget: VSXExtensionsViewContainer) => T): T | false {
if (widget instanceof VSXExtensionsViewContainer && widget.id === VSXExtensionsViewContainer.ID) {
return fn(widget);
}
return false;
}
/**
* Installs a local .vsix file after prompting the `Open File` dialog. Resolves to the URI of the file.
*/
protected async installFromVSIX(): Promise<URI | undefined> {
const props: OpenFileDialogProps = {
title: VSXExtensionsCommands.INSTALL_FROM_VSIX.dialogLabel,
openLabel: 'Install',
filters: { 'VSIX Extensions (*.vsix)': ['vsix'] },
canSelectMany: false
};
const extensionUri = await this.fileDialogService.showOpenDialog(props);
if (extensionUri) {
if (extensionUri.path.ext === '.vsix') {
const extensionName = this.labelProvider.getName(extensionUri);
try {
await this.commandRegistry.executeCommand(VscodeCommands.INSTALL_FROM_VSIX.id, extensionUri);
this.messageService.info(`Completed installing ${extensionName} from VSIX.`);
} catch (e) {
this.messageService.error(`Failed to install ${extensionName} from VSIX.`);
console.warn(e);
}
} else {
this.messageService.error('The selected file is not a valid "*.vsix" plugin.');
}
return extensionUri;
}
return undefined;
}
}