Skip to content

Commit 47f1a28

Browse files
committed
Make sure active instance changed fires when switching groups
Fixes microsoft#127357
1 parent e9c4f85 commit 47f1a28

File tree

4 files changed

+15
-18
lines changed

4 files changed

+15
-18
lines changed

src/vs/workbench/contrib/terminal/browser/terminal.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export interface ITerminalGroup {
8282
focusNextPane(): void;
8383
resizePane(direction: Direction): void;
8484
resizePanes(relativeSizes: number[]): void;
85-
setActiveInstanceByIndex(index: number): void;
85+
setActiveInstanceByIndex(index: number, force?: boolean): void;
8686
attachToElement(element: HTMLElement): void;
8787
addInstance(instance: ITerminalInstance): void;
8888
removeInstance(instance: ITerminalInstance): void;
@@ -217,7 +217,6 @@ export interface ITerminalGroupService extends ITerminalInstanceHost, ITerminalF
217217
readonly groups: readonly ITerminalGroup[];
218218
activeGroup: ITerminalGroup | undefined;
219219
readonly activeGroupIndex: number;
220-
readonly activeInstanceIndex: number;
221220

222221
readonly onDidChangeActiveGroup: Event<ITerminalGroup | undefined>;
223222
readonly onDidDisposeGroup: Event<ITerminalGroup>;

src/vs/workbench/contrib/terminal/browser/terminalGroup.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -421,16 +421,18 @@ export class TerminalGroup extends Disposable implements ITerminalGroup {
421421
return terminalIndex;
422422
}
423423

424-
setActiveInstanceByIndex(index: number): void {
424+
setActiveInstanceByIndex(index: number, force?: boolean): void {
425425
// Check for invalid value
426426
if (index < 0 || index >= this._terminalInstances.length) {
427427
return;
428428
}
429429

430-
const oldActiveGroup = this.activeInstance;
430+
const oldActiveInstance = this.activeInstance;
431431
this._activeInstanceIndex = index;
432-
if (oldActiveGroup !== this.activeInstance) {
433-
this._onInstancesChanged.fire();
432+
if (force) {
433+
if (oldActiveInstance !== this.activeInstance) {
434+
this._onInstancesChanged.fire();
435+
}
434436
this._onDidChangeActiveInstance.fire(this.activeInstance);
435437
}
436438
}

src/vs/workbench/contrib/terminal/browser/terminalGroupService.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ export class TerminalGroupService extends Disposable implements ITerminalGroupSe
2828
get instances(): ITerminalInstance[] {
2929
return this.groups.reduce((p, c) => p.concat(c.terminalInstances), [] as ITerminalInstance[]);
3030
}
31-
activeInstanceIndex: number = -1;
3231

3332
private _terminalGroupCountContextKey: IContextKey<number>;
3433
private _terminalCountContextKey: IContextKey<number>;
@@ -105,10 +104,11 @@ export class TerminalGroupService extends Disposable implements ITerminalGroupSe
105104
}
106105
set activeGroup(value: ITerminalGroup | undefined) {
107106
if (value === undefined) {
108-
this.activeGroupIndex = -1;
107+
// Setting to undefined is not possible, this can only be done when removing the last group
109108
return;
110109
}
111-
this.activeGroupIndex = this.groups.findIndex(e => e === value);
110+
const index = this.groups.findIndex(e => e === value);
111+
this.setActiveGroupByIndex(index);
112112
}
113113

114114
get activeInstance(): ITerminalInstance | undefined {
@@ -233,13 +233,10 @@ export class TerminalGroupService extends Disposable implements ITerminalGroupSe
233233
}
234234

235235
this._onDidChangeInstances.fire();
236-
if (this.groups.length === 0) {
237-
this._onDidChangeActiveInstance.fire(undefined);
238-
}
239-
240236
this._onDidChangeGroups.fire();
241237
if (wasActiveGroup) {
242238
this._onDidChangeActiveGroup.fire(this.activeGroup);
239+
this._onDidChangeActiveInstance.fire(this.activeInstance);
243240
}
244241
}
245242

@@ -248,7 +245,7 @@ export class TerminalGroupService extends Disposable implements ITerminalGroupSe
248245
* group has been removed.
249246
*/
250247
setActiveGroupByIndex(index: number, force?: boolean) {
251-
if (index >= this.groups.length) {
248+
if (index < 0 || index >= this.groups.length) {
252249
return;
253250
}
254251
const oldActiveGroup = this.activeGroup;
@@ -287,16 +284,15 @@ export class TerminalGroupService extends Disposable implements ITerminalGroupSe
287284
return;
288285
}
289286

290-
this.activeInstanceIndex = instanceLocation.instanceIndex;
287+
const activeInstanceIndex = instanceLocation.instanceIndex;
291288

292289
if (this.activeGroupIndex !== instanceLocation.groupIndex) {
293290
this.activeGroupIndex = instanceLocation.groupIndex;
294291
this._onDidChangeActiveGroup.fire(this.activeGroup);
292+
instanceLocation.group.setActiveInstanceByIndex(activeInstanceIndex, true);
295293
}
296294
this.groups.forEach((g, i) => g.setVisible(i === instanceLocation.groupIndex));
297295

298-
instanceLocation.group.setActiveInstanceByIndex(this.activeInstanceIndex);
299-
this._onDidChangeActiveInstance.fire(newActiveInstance);
300296
}
301297

302298
setActiveGroupToNext() {

src/vs/workbench/contrib/terminal/browser/terminalView.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ class SingleTerminalTabActionViewItem extends MenuEntryActionViewItem {
433433

434434
// Register listeners to update the tab
435435
this._register(this._terminalService.onInstancePrimaryStatusChanged(e => this.updateLabel(e)));
436-
this._register(_terminalGroupService.onDidChangeActiveInstance(() => this.updateLabel()));
436+
this._register(this._terminalGroupService.onDidChangeActiveInstance(() => this.updateLabel()));
437437
this._register(this._terminalService.onInstanceIconChanged(e => this.updateLabel(e)));
438438
this._register(this._terminalService.onInstanceColorChanged(e => this.updateLabel(e)));
439439
this._register(this._terminalService.onInstanceTitleChanged(e => {

0 commit comments

Comments
 (0)