forked from microsoft/vscode
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsidebarPart.ts
194 lines (163 loc) · 7.25 KB
/
sidebarPart.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
/*---------------------------------------------------------------------------------------------
* 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/sidebarpart';
import { TPromise } from 'vs/base/common/winjs.base';
import nls = require('vs/nls');
import { Registry } from 'vs/platform/registry/common/platform';
import { Action, IAction } from 'vs/base/common/actions';
import { CompositePart } from 'vs/workbench/browser/parts/compositePart';
import { Viewlet, ViewletRegistry, Extensions as ViewletExtensions } from 'vs/workbench/browser/viewlet';
import { IWorkbenchActionRegistry, Extensions as ActionExtensions } from 'vs/workbench/common/actions';
import { SyncActionDescriptor } from 'vs/platform/actions/common/actions';
import { Separator } from 'vs/base/browser/ui/actionbar/actionbar';
import { IViewletService } from 'vs/workbench/services/viewlet/browser/viewlet';
import { IPartService, Parts, Position as SideBarPosition } from 'vs/workbench/services/part/common/partService';
import { IViewlet } from 'vs/workbench/common/viewlet';
import { Scope } from 'vs/workbench/browser/actions';
import { IStorageService } from 'vs/platform/storage/common/storage';
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
import { IMessageService } from 'vs/platform/message/common/message';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { KeyMod, KeyCode } from 'vs/base/common/keyCodes';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import Event from 'vs/base/common/event';
import { IThemeService } from 'vs/platform/theme/common/themeService';
import { contrastBorder } from 'vs/platform/theme/common/colorRegistry';
import { SIDE_BAR_TITLE_FOREGROUND, SIDE_BAR_BACKGROUND, SIDE_BAR_FOREGROUND, SIDE_BAR_BORDER } from 'vs/workbench/common/theme';
import { ToggleSidebarVisibilityAction } from 'vs/workbench/browser/actions/toggleSidebarVisibility';
import { Dimension } from 'vs/base/browser/builder';
export class SidebarPart extends CompositePart<Viewlet> {
public static readonly activeViewletSettingsKey = 'workbench.sidebar.activeviewletid';
public _serviceBrand: any;
private blockOpeningViewlet: boolean;
constructor(
id: string,
@IMessageService messageService: IMessageService,
@IStorageService storageService: IStorageService,
@ITelemetryService telemetryService: ITelemetryService,
@IContextMenuService contextMenuService: IContextMenuService,
@IPartService partService: IPartService,
@IKeybindingService keybindingService: IKeybindingService,
@IInstantiationService instantiationService: IInstantiationService,
@IThemeService themeService: IThemeService
) {
super(
messageService,
storageService,
telemetryService,
contextMenuService,
partService,
keybindingService,
instantiationService,
themeService,
Registry.as<ViewletRegistry>(ViewletExtensions.Viewlets),
SidebarPart.activeViewletSettingsKey,
Registry.as<ViewletRegistry>(ViewletExtensions.Viewlets).getDefaultViewletId(),
'sideBar',
'viewlet',
Scope.VIEWLET,
SIDE_BAR_TITLE_FOREGROUND,
id,
{ hasTitle: true, borderWidth: () => (this.getColor(SIDE_BAR_BORDER) || this.getColor(contrastBorder)) ? 1 : 0 }
);
}
public get onDidViewletOpen(): Event<IViewlet> {
return this._onDidCompositeOpen.event as Event<IViewlet>;
}
public get onDidViewletClose(): Event<IViewlet> {
return this._onDidCompositeClose.event as Event<IViewlet>;
}
public updateStyles(): void {
super.updateStyles();
// Part container
const container = this.getContainer();
container.style('background-color', this.getColor(SIDE_BAR_BACKGROUND));
container.style('color', this.getColor(SIDE_BAR_FOREGROUND));
const borderColor = this.getColor(SIDE_BAR_BORDER) || this.getColor(contrastBorder);
const isPositionLeft = this.partService.getSideBarPosition() === SideBarPosition.LEFT;
container.style('border-right-width', borderColor && isPositionLeft ? '1px' : null);
container.style('border-right-style', borderColor && isPositionLeft ? 'solid' : null);
container.style('border-right-color', isPositionLeft ? borderColor : null);
container.style('border-left-width', borderColor && !isPositionLeft ? '1px' : null);
container.style('border-left-style', borderColor && !isPositionLeft ? 'solid' : null);
container.style('border-left-color', !isPositionLeft ? borderColor : null);
}
public openViewlet(id: string, focus?: boolean): TPromise<Viewlet> {
if (this.blockOpeningViewlet) {
return TPromise.as(null); // Workaround against a potential race condition
}
// First check if sidebar is hidden and show if so
let promise = TPromise.wrap<void>(null);
if (!this.partService.isVisible(Parts.SIDEBAR_PART)) {
try {
this.blockOpeningViewlet = true;
promise = this.partService.setSideBarHidden(false);
} finally {
this.blockOpeningViewlet = false;
}
}
return promise.then(() => this.openComposite(id, focus)) as TPromise<Viewlet>;
}
public getActiveViewlet(): IViewlet {
return <IViewlet>this.getActiveComposite();
}
public getLastActiveViewletId(): string {
return this.getLastActiveCompositetId();
}
public hideActiveViewlet(): TPromise<void> {
return this.hideActiveComposite().then(composite => void 0);
}
public layout(dimension: Dimension): Dimension[] {
if (!this.partService.isVisible(Parts.SIDEBAR_PART)) {
return [dimension];
}
return super.layout(dimension);
}
protected getTitleAreaContextMenuActions(): IAction[] {
const contextMenuActions = super.getTitleAreaContextMenuActions();
if (contextMenuActions.length) {
contextMenuActions.push(new Separator());
}
contextMenuActions.push(this.createHideSideBarAction());
return contextMenuActions;
}
private createHideSideBarAction(): IAction {
return <IAction>{
id: ToggleSidebarVisibilityAction.ID,
label: nls.localize('compositePart.hideSideBarLabel', "Hide Side Bar"),
enabled: true,
run: () => this.partService.setSideBarHidden(true)
};
}
}
class FocusSideBarAction extends Action {
public static readonly ID = 'workbench.action.focusSideBar';
public static readonly LABEL = nls.localize('focusSideBar', "Focus into Side Bar");
constructor(
id: string,
label: string,
@IViewletService private viewletService: IViewletService,
@IPartService private partService: IPartService
) {
super(id, label);
}
public run(): TPromise<any> {
// Show side bar
if (!this.partService.isVisible(Parts.SIDEBAR_PART)) {
return this.partService.setSideBarHidden(false);
}
// Focus into active viewlet
let viewlet = this.viewletService.getActiveViewlet();
if (viewlet) {
viewlet.focus();
}
return TPromise.as(true);
}
}
const registry = Registry.as<IWorkbenchActionRegistry>(ActionExtensions.WorkbenchActions);
registry.registerWorkbenchAction(new SyncActionDescriptor(FocusSideBarAction, FocusSideBarAction.ID, FocusSideBarAction.LABEL, {
primary: KeyMod.CtrlCmd | KeyCode.KEY_0
}), 'View: Focus into Side Bar', nls.localize('viewCategory', "View"));