emptyView.ts 6.8 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

6
import * as nls from 'vs/nls';
7
import * as errors from 'vs/base/common/errors';
8
import * as DOM from 'vs/base/browser/dom';
9
import { IAction } from 'vs/base/common/actions';
J
Johannes Rieken 已提交
10
import { Button } from 'vs/base/browser/ui/button/button';
11
import { IViewletViewOptions } from 'vs/workbench/browser/parts/views/viewsViewlet';
12
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
I
isidor 已提交
13
import { OpenFolderAction, AddRootFolderAction } from 'vs/workbench/browser/actions/workspaceActions';
14 15
import { attachButtonStyler } from 'vs/platform/theme/common/styler';
import { IThemeService } from 'vs/platform/theme/common/themeService';
16 17
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
18
import { IWorkspaceContextService, WorkbenchState } from 'vs/platform/workspace/common/workspace';
19
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
20
import { ViewletPanel, IViewletPanelOptions } from 'vs/workbench/browser/parts/views/panelViewlet';
I
isidor 已提交
21
import { ResourcesDropHandler, DragAndDropObserver } from 'vs/workbench/browser/dnd';
I
isidor 已提交
22 23
import { listDropBackground } from 'vs/platform/theme/common/colorRegistry';
import { SIDE_BAR_BACKGROUND } from 'vs/workbench/common/theme';
24 25 26
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
import { ILabelService } from 'vs/platform/label/common/label';
import { Schemas } from 'vs/base/common/network';
27
import { isWeb } from 'vs/base/common/platform';
S
Sandeep Somavarapu 已提交
28
import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
E
Erich Gamma 已提交
29

30
export class EmptyView extends ViewletPanel {
E
Erich Gamma 已提交
31

32 33
	static readonly ID: string = 'workbench.explorer.emptyView';
	static readonly NAME = nls.localize('noWorkspace', "No Folder Opened");
34

35
	private button: Button;
36 37
	private messageElement: HTMLElement;
	private titleElement: HTMLElement;
38

39
	constructor(
S
#27823  
Sandeep Somavarapu 已提交
40
		options: IViewletViewOptions,
41 42
		@IThemeService private readonly themeService: IThemeService,
		@IInstantiationService private readonly instantiationService: IInstantiationService,
43
		@IKeybindingService keybindingService: IKeybindingService,
44
		@IContextMenuService contextMenuService: IContextMenuService,
45
		@IWorkspaceContextService private readonly contextService: IWorkspaceContextService,
46 47
		@IConfigurationService configurationService: IConfigurationService,
		@IWorkbenchEnvironmentService private environmentService: IWorkbenchEnvironmentService,
S
Sandeep Somavarapu 已提交
48 49
		@ILabelService private labelService: ILabelService,
		@IContextKeyService contextKeyService: IContextKeyService
50
	) {
S
Sandeep Somavarapu 已提交
51
		super({ ...(options as IViewletPanelOptions), ariaHeaderLabel: nls.localize('explorerSection', "Files Explorer Section") }, keybindingService, contextMenuService, configurationService, contextKeyService);
52 53
		this._register(this.contextService.onDidChangeWorkbenchState(() => this.setLabels()));
		this._register(this.labelService.onDidChangeFormatters(() => this.setLabels()));
E
Erich Gamma 已提交
54 55
	}

56
	renderHeader(container: HTMLElement): void {
57 58 59 60 61 62 63
		const titleContainer = document.createElement('div');
		DOM.addClass(titleContainer, 'title');
		container.appendChild(titleContainer);

		this.titleElement = document.createElement('span');
		this.titleElement.textContent = name;
		titleContainer.appendChild(this.titleElement);
E
Erich Gamma 已提交
64 65
	}

J
Joao Moreno 已提交
66
	protected renderBody(container: HTMLElement): void {
E
Erich Gamma 已提交
67
		DOM.addClass(container, 'explorer-empty-view');
I
isidor 已提交
68
		container.tabIndex = 0;
E
Erich Gamma 已提交
69

70 71 72
		const messageContainer = document.createElement('div');
		DOM.addClass(messageContainer, 'section');
		container.appendChild(messageContainer);
E
Erich Gamma 已提交
73

74 75
		this.messageElement = document.createElement('p');
		messageContainer.appendChild(this.messageElement);
E
Erich Gamma 已提交
76

77
		this.button = new Button(messageContainer);
78
		attachButtonStyler(this.button, this.themeService);
J
Joao Moreno 已提交
79

M
Matt Bierner 已提交
80
		this._register(this.button.onDidClick(() => {
M
Matt Bierner 已提交
81 82 83
			if (!this.actionRunner) {
				return;
			}
I
isidor 已提交
84
			const actionClass = this.contextService.getWorkbenchState() === WorkbenchState.WORKSPACE ? AddRootFolderAction : OpenFolderAction;
85
			const action = this.instantiationService.createInstance<string, string, IAction>(actionClass, actionClass.ID, actionClass.LABEL);
86
			this.actionRunner.run(action).then(() => {
87 88 89 90 91
				action.dispose();
			}, err => {
				action.dispose();
				errors.onUnexpectedError(err);
			});
I
isidor 已提交
92
		}));
93

M
Matt Bierner 已提交
94
		this._register(new DragAndDropObserver(container, {
I
isidor 已提交
95
			onDrop: e => {
M
Matt Bierner 已提交
96 97
				const color = this.themeService.getTheme().getColor(SIDE_BAR_BACKGROUND);
				container.style.backgroundColor = color ? color.toString() : '';
I
isidor 已提交
98 99 100 101
				const dropHandler = this.instantiationService.createInstance(ResourcesDropHandler, { allowWorkspaceOpen: true });
				dropHandler.handleDrop(e, () => undefined, targetGroup => undefined);
			},
			onDragEnter: (e) => {
M
Matt Bierner 已提交
102 103
				const color = this.themeService.getTheme().getColor(listDropBackground);
				container.style.backgroundColor = color ? color.toString() : '';
I
isidor 已提交
104 105
			},
			onDragEnd: () => {
M
Matt Bierner 已提交
106 107
				const color = this.themeService.getTheme().getColor(SIDE_BAR_BACKGROUND);
				container.style.backgroundColor = color ? color.toString() : '';
I
isidor 已提交
108 109
			},
			onDragLeave: () => {
M
Matt Bierner 已提交
110 111
				const color = this.themeService.getTheme().getColor(SIDE_BAR_BACKGROUND);
				container.style.backgroundColor = color ? color.toString() : '';
I
isidor 已提交
112 113
			},
			onDragOver: e => {
M
Matt Bierner 已提交
114
				e.dataTransfer!.dropEffect = 'copy';
I
isidor 已提交
115
			}
I
isidor 已提交
116
		}));
117

I
isidor 已提交
118
		this.setLabels();
119 120
	}

I
isidor 已提交
121 122
	private setLabels(): void {
		if (this.contextService.getWorkbenchState() === WorkbenchState.WORKSPACE) {
123
			this.messageElement.textContent = nls.localize('noWorkspaceHelp', "You have not yet added a folder to the workspace.");
I
isidor 已提交
124 125 126
			if (this.button) {
				this.button.label = nls.localize('addFolder', "Add Folder");
			}
127
			this.titleElement.textContent = EmptyView.NAME;
I
isidor 已提交
128
		} else {
129
			if (this.environmentService.configuration.remoteAuthority && !isWeb) {
130 131 132 133 134
				const hostLabel = this.labelService.getHostLabel(Schemas.vscodeRemote, this.environmentService.configuration.remoteAuthority);
				this.messageElement.textContent = hostLabel ? nls.localize('remoteNoFolderHelp', "Connected to {0}", hostLabel) : nls.localize('connecting', "Connecting...");
			} else {
				this.messageElement.textContent = nls.localize('noFolderHelp', "You have not yet opened a folder.");
			}
I
isidor 已提交
135 136 137
			if (this.button) {
				this.button.label = nls.localize('openFolder', "Open Folder");
			}
138
			this.titleElement.textContent = this.title;
139
		}
E
Erich Gamma 已提交
140 141
	}

142
	layoutBody(size: number): void {
J
Joao Moreno 已提交
143 144 145
		// no-op
	}

146
	focusBody(): void {
147
		if (this.button) {
148
			this.button.element.focus();
B
Benjamin Pasero 已提交
149
		}
E
Erich Gamma 已提交
150
	}
151
}