-
Notifications
You must be signed in to change notification settings - Fork 29.9k
/
navigationActions.ts
209 lines (169 loc) · 7.56 KB
/
navigationActions.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as nls from 'vs/nls';
import { Registry } from 'vs/platform/registry/common/platform';
import { Action } from 'vs/base/common/actions';
import { IEditorGroupsService, GroupDirection, GroupLocation, IFindGroupScope } from 'vs/workbench/services/editor/common/editorGroupsService';
import { IPanelService } from 'vs/workbench/services/panel/common/panelService';
import { IWorkbenchLayoutService, Parts } from 'vs/workbench/services/layout/browser/layoutService';
import { IViewletService } from 'vs/workbench/services/viewlet/browser/viewlet';
import { IViewlet } from 'vs/workbench/common/viewlet';
import { IPanel } from 'vs/workbench/common/panel';
import { SyncActionDescriptor } from 'vs/platform/actions/common/actions';
import { IWorkbenchActionRegistry, Extensions } from 'vs/workbench/common/actions';
import { Direction } from 'vs/base/browser/ui/grid/grid';
abstract class BaseNavigationAction extends Action {
constructor(
id: string,
label: string,
protected direction: Direction,
@IEditorGroupsService protected editorGroupService: IEditorGroupsService,
@IPanelService protected panelService: IPanelService,
@IWorkbenchLayoutService protected layoutService: IWorkbenchLayoutService,
@IViewletService protected viewletService: IViewletService
) {
super(id, label);
}
async run(): Promise<boolean | IViewlet | IPanel> {
const isEditorFocus = this.layoutService.hasFocus(Parts.EDITOR_PART);
const isPanelFocus = this.layoutService.hasFocus(Parts.PANEL_PART);
const isSidebarFocus = this.layoutService.hasFocus(Parts.SIDEBAR_PART);
let neighborPart: Parts | undefined;
if (isEditorFocus) {
const didNavigate = this.navigateAcrossEditorGroup(this.toGroupDirection(this.direction));
if (didNavigate) {
return true;
}
neighborPart = this.layoutService.getVisibleNeighborPart(Parts.EDITOR_PART, this.direction);
}
if (isPanelFocus) {
neighborPart = this.layoutService.getVisibleNeighborPart(Parts.PANEL_PART, this.direction);
}
if (isSidebarFocus) {
neighborPart = this.layoutService.getVisibleNeighborPart(Parts.SIDEBAR_PART, this.direction);
}
if (neighborPart === Parts.EDITOR_PART) {
return this.navigateToEditorGroup(this.direction === Direction.Right ? GroupLocation.FIRST : GroupLocation.LAST);
}
if (neighborPart === Parts.SIDEBAR_PART) {
return this.navigateToSidebar();
}
if (neighborPart === Parts.PANEL_PART) {
return this.navigateToPanel();
}
return false;
}
private async navigateToPanel(): Promise<IPanel | boolean> {
if (!this.layoutService.isVisible(Parts.PANEL_PART)) {
return false;
}
const activePanel = this.panelService.getActivePanel();
if (!activePanel) {
return false;
}
const activePanelId = activePanel.getId();
const res = await this.panelService.openPanel(activePanelId, true);
if (!res) {
return false;
}
return res;
}
private async navigateToSidebar(): Promise<IViewlet | boolean> {
if (!this.layoutService.isVisible(Parts.SIDEBAR_PART)) {
return false;
}
const activeViewlet = this.viewletService.getActiveViewlet();
if (!activeViewlet) {
return false;
}
const activeViewletId = activeViewlet.getId();
const viewlet = await this.viewletService.openViewlet(activeViewletId, true);
return !!viewlet;
}
private navigateAcrossEditorGroup(direction: GroupDirection): boolean {
return this.doNavigateToEditorGroup({ direction });
}
private navigateToEditorGroup(location: GroupLocation): boolean {
return this.doNavigateToEditorGroup({ location });
}
private toGroupDirection(direction: Direction): GroupDirection {
switch (direction) {
case Direction.Down: return GroupDirection.DOWN;
case Direction.Left: return GroupDirection.LEFT;
case Direction.Right: return GroupDirection.RIGHT;
case Direction.Up: return GroupDirection.UP;
}
}
private doNavigateToEditorGroup(scope: IFindGroupScope): boolean {
const targetGroup = this.editorGroupService.findGroup(scope, this.editorGroupService.activeGroup);
if (targetGroup) {
targetGroup.focus();
return true;
}
return false;
}
}
class NavigateLeftAction extends BaseNavigationAction {
static readonly ID = 'workbench.action.navigateLeft';
static readonly LABEL = nls.localize('navigateLeft', "Navigate to the View on the Left");
constructor(
id: string,
label: string,
@IEditorGroupsService editorGroupService: IEditorGroupsService,
@IPanelService panelService: IPanelService,
@IWorkbenchLayoutService layoutService: IWorkbenchLayoutService,
@IViewletService viewletService: IViewletService
) {
super(id, label, Direction.Left, editorGroupService, panelService, layoutService, viewletService);
}
}
class NavigateRightAction extends BaseNavigationAction {
static readonly ID = 'workbench.action.navigateRight';
static readonly LABEL = nls.localize('navigateRight', "Navigate to the View on the Right");
constructor(
id: string,
label: string,
@IEditorGroupsService editorGroupService: IEditorGroupsService,
@IPanelService panelService: IPanelService,
@IWorkbenchLayoutService layoutService: IWorkbenchLayoutService,
@IViewletService viewletService: IViewletService
) {
super(id, label, Direction.Right, editorGroupService, panelService, layoutService, viewletService);
}
}
class NavigateUpAction extends BaseNavigationAction {
static readonly ID = 'workbench.action.navigateUp';
static readonly LABEL = nls.localize('navigateUp', "Navigate to the View Above");
constructor(
id: string,
label: string,
@IEditorGroupsService editorGroupService: IEditorGroupsService,
@IPanelService panelService: IPanelService,
@IWorkbenchLayoutService layoutService: IWorkbenchLayoutService,
@IViewletService viewletService: IViewletService
) {
super(id, label, Direction.Up, editorGroupService, panelService, layoutService, viewletService);
}
}
class NavigateDownAction extends BaseNavigationAction {
static readonly ID = 'workbench.action.navigateDown';
static readonly LABEL = nls.localize('navigateDown', "Navigate to the View Below");
constructor(
id: string,
label: string,
@IEditorGroupsService editorGroupService: IEditorGroupsService,
@IPanelService panelService: IPanelService,
@IWorkbenchLayoutService layoutService: IWorkbenchLayoutService,
@IViewletService viewletService: IViewletService
) {
super(id, label, Direction.Down, editorGroupService, panelService, layoutService, viewletService);
}
}
const registry = Registry.as<IWorkbenchActionRegistry>(Extensions.WorkbenchActions);
const viewCategory = nls.localize('view', "View");
registry.registerWorkbenchAction(SyncActionDescriptor.create(NavigateUpAction, NavigateUpAction.ID, NavigateUpAction.LABEL, undefined), 'View: Navigate to the View Above', viewCategory);
registry.registerWorkbenchAction(SyncActionDescriptor.create(NavigateDownAction, NavigateDownAction.ID, NavigateDownAction.LABEL, undefined), 'View: Navigate to the View Below', viewCategory);
registry.registerWorkbenchAction(SyncActionDescriptor.create(NavigateLeftAction, NavigateLeftAction.ID, NavigateLeftAction.LABEL, undefined), 'View: Navigate to the View on the Left', viewCategory);
registry.registerWorkbenchAction(SyncActionDescriptor.create(NavigateRightAction, NavigateRightAction.ID, NavigateRightAction.LABEL, undefined), 'View: Navigate to the View on the Right', viewCategory);