Skip to content

Commit

Permalink
Focus explicitly when creating terminals (#127672)
Browse files Browse the repository at this point in the history
Fixes #127648
  • Loading branch information
Tyriar authored Jun 30, 2021
1 parent 628b4d4 commit cae22b8
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/vs/workbench/contrib/terminal/browser/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ export interface ITerminalGroupService extends ITerminalInstanceHost, ITerminalF

setContainer(container: HTMLElement): void;

showPanel(focus?: boolean, force?: boolean): Promise<void>;
showPanel(focus?: boolean): Promise<void>;
hidePanel(): void;
focusTabs(): void;
showTabs(): void;
Expand Down
45 changes: 32 additions & 13 deletions src/vs/workbench/contrib/terminal/browser/terminalActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,14 @@ export function registerTerminalActions() {

if (instance) {
terminalService.setActiveInstance(instance);
if (instance.target === TerminalLocation.Editor) {
await instance.focusWhenReady(true);
} else {
await terminalGroupService.showPanel(true);
}
}

}
await terminalGroupService.showPanel(true);
}
});

Expand All @@ -211,10 +216,9 @@ export function registerTerminalActions() {
}
async run(accessor: ServicesAccessor) {
const terminalService = accessor.get(ITerminalService);
// TODO: Await openEditor
terminalService.createTerminal({
await terminalService.createTerminal({
target: TerminalLocation.Editor
});
}).focusWhenReady();
}
});

Expand Down Expand Up @@ -987,7 +991,11 @@ export function registerTerminalActions() {
config: { attachPersistentProcess: selected.term }
});
terminalService.setActiveInstance(instance);
terminalGroupService.showPanel(true);
if (instance.target === TerminalLocation.Editor) {
await instance.focusWhenReady(true);
} else {
terminalGroupService.showPanel(true);
}
}
}
});
Expand Down Expand Up @@ -1205,8 +1213,12 @@ export function registerTerminalActions() {
return;
}
terminalService.setActiveInstance(instance);
if (instance.target === TerminalLocation.Editor) {
await instance.focusWhenReady(true);
} else {
return accessor.get(ITerminalGroupService).showPanel(true);
}
}
return accessor.get(ITerminalGroupService).showPanel(true);
}
});
registerAction2(class extends Action2 {
Expand Down Expand Up @@ -1462,8 +1474,12 @@ export function registerTerminalActions() {
return undefined;
}
const instance = terminalService.splitInstance(activeInstance, options?.config, cwd);
if (instance?.target !== TerminalLocation.Editor) {
return terminalGroupService.showPanel(true);
if (instance) {
if (instance.target === TerminalLocation.Editor) {
instance.focusWhenReady();
} else {
return terminalGroupService.showPanel(true);
}
}
}
});
Expand Down Expand Up @@ -1663,8 +1679,12 @@ export function registerTerminalActions() {
instance = terminalService.createTerminal(eventOrOptions);
}
terminalService.setActiveInstance(instance);
if (instance.target === TerminalLocation.Editor) {
await instance.focusWhenReady(true);
} else {
await terminalGroupService.showPanel(true);
}
}
await terminalGroupService.showPanel(true);
}
});
registerAction2(class extends Action2 {
Expand Down Expand Up @@ -1922,10 +1942,9 @@ export function registerTerminalActions() {
if (quickSelectProfiles) {
const profile = quickSelectProfiles.find(profile => profile.profileName === profileSelection);
if (profile) {
const instance = terminalService.createTerminal(
{
config: profile
});
const instance = terminalService.createTerminal({
config: profile
});
terminalService.setActiveInstance(instance);
} else {
console.warn(`No profile with name "${profileSelection}"`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class TerminalEditorInput extends EditorInput {

override copy(): IEditorInput {
const instance = this._copyInstance || this._terminalInstanceService.createInstance({}, TerminalLocation.Editor);
instance.focusWhenReady();
this._copyInstance = undefined;
return this._instantiationService.createInstance(TerminalEditorInput, instance);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,7 @@ export class TerminalEditorService extends Disposable implements ITerminalEditor
} else {
this._activeInstanceIndex = this.instances.findIndex(e => e === instance);
}
const newActiveInstance = this.activeInstance;
newActiveInstance?.focus();
this._onDidChangeActiveInstance.fire(newActiveInstance);
this._onDidChangeActiveInstance.fire(this.activeInstance);
}

async openEditor(instance: ITerminalInstance): Promise<void> {
Expand Down
33 changes: 13 additions & 20 deletions src/vs/workbench/contrib/terminal/browser/terminalGroupService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ import { FindReplaceState } from 'vs/editor/contrib/find/findState';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IContextKey, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IShellLaunchConfig, TerminalLocation, TerminalSettingId } from 'vs/platform/terminal/common/terminal';
import { IShellLaunchConfig, TerminalSettingId } from 'vs/platform/terminal/common/terminal';
import { IViewDescriptorService, IViewsService, ViewContainerLocation } from 'vs/workbench/common/views';
import { ITerminalFindHost, ITerminalGroup, ITerminalGroupService, ITerminalInstance } from 'vs/workbench/contrib/terminal/browser/terminal';
import { TerminalConfigHelper } from 'vs/workbench/contrib/terminal/browser/terminalConfigHelper';
import { TerminalGroup } from 'vs/workbench/contrib/terminal/browser/terminalGroup';
import { TerminalViewPane } from 'vs/workbench/contrib/terminal/browser/terminalView';
import { KEYBINDING_CONTEXT_GROUP_TERMINAL_COUNT, KEYBINDING_CONTEXT_TERMINAL_GROUP_COUNT, KEYBINDING_CONTEXT_TERMINAL_TABS_MOUSE, TERMINAL_VIEW_ID } from 'vs/workbench/contrib/terminal/common/terminal';
Expand All @@ -36,8 +35,6 @@ export class TerminalGroupService extends Disposable implements ITerminalGroupSe

private _findState: FindReplaceState;

private _configHelper: TerminalConfigHelper;

private readonly _onDidChangeActiveGroup = new Emitter<ITerminalGroup | undefined>();
readonly onDidChangeActiveGroup = this._onDidChangeActiveGroup.event;
private readonly _onDidDisposeGroup = new Emitter<ITerminalGroup>();
Expand Down Expand Up @@ -76,8 +73,6 @@ export class TerminalGroupService extends Disposable implements ITerminalGroupSe
this.onDidChangeInstances(() => this._terminalCountContextKey.set(this.instances.length));

this._findState = new FindReplaceState();

this._configHelper = _instantiationService.createInstance(TerminalConfigHelper);
}

hidePanel(): void {
Expand Down Expand Up @@ -163,20 +158,18 @@ export class TerminalGroupService extends Disposable implements ITerminalGroupSe
return group;
}

async showPanel(focus?: boolean, force?: boolean): Promise<void> {
if (this._configHelper.config.defaultLocation !== TerminalLocation.Editor || force) {
const pane = this._viewsService.getActiveViewWithId(TERMINAL_VIEW_ID)
?? await this._viewsService.openView(TERMINAL_VIEW_ID, focus);
pane?.setExpanded(true);

if (focus) {
// Do the focus call asynchronously as going through the
// command palette will force editor focus
await timeout(0);
const instance = this.activeInstance;
if (instance) {
await instance.focusWhenReady(true);
}
async showPanel(focus?: boolean): Promise<void> {
const pane = this._viewsService.getActiveViewWithId(TERMINAL_VIEW_ID)
?? await this._viewsService.openView(TERMINAL_VIEW_ID, focus);
pane?.setExpanded(true);

if (focus) {
// Do the focus call asynchronously as going through the
// command palette will force editor focus
await timeout(0);
const instance = this.activeInstance;
if (instance) {
await instance.focusWhenReady(true);
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/vs/workbench/contrib/terminal/browser/terminalInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ export class TerminalInstance extends Disposable implements ITerminalInstance {
private _areLinksReady: boolean = false;
private _initialDataEvents: string[] | undefined = [];
private _containerReadyBarrier: AutoOpenBarrier;
private _attachBarrier: AutoOpenBarrier;

private _messageTitleDisposable: IDisposable | undefined;

Expand Down Expand Up @@ -297,6 +298,7 @@ export class TerminalInstance extends Disposable implements ITerminalInstance {
this._register(toDisposable(() => this._dndObserver?.dispose()));

this._containerReadyBarrier = new AutoOpenBarrier(Constants.WaitForContainerThreshold);
this._attachBarrier = new AutoOpenBarrier(1000);
this._xtermReadyPromise = this._createXterm();
this._xtermReadyPromise.then(async () => {
// Wait for a period to allow a container to be ready
Expand Down Expand Up @@ -647,6 +649,8 @@ export class TerminalInstance extends Disposable implements ITerminalInstance {
return;
}

this._attachBarrier.open();

// Attach has not occurred yet
if (!this._wrapperElement) {
return this._attachToElement(container);
Expand Down Expand Up @@ -1017,6 +1021,7 @@ export class TerminalInstance extends Disposable implements ITerminalInstance {

async focusWhenReady(force?: boolean): Promise<void> {
await this._xtermReadyPromise;
await this._attachBarrier.wait();
this.focus(force);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ export class TerminalService implements ITerminalService {
// Fire events
this._onDidChangeInstances.fire();
this._onActiveGroupChanged.fire(this._terminalGroupService.activeGroup);
this._terminalGroupService.showPanel(true, true);
this._terminalGroupService.showPanel(true);
}

protected _initInstanceListeners(instance: ITerminalInstance): void {
Expand Down
3 changes: 2 additions & 1 deletion src/vs/workbench/contrib/terminal/browser/terminalView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ export class TerminalViewPane extends ViewPane {
run: () => {
const instance = this._terminalGroupService.activeInstance;
if (instance) {
return this._terminalService.splitInstance(instance);
const newInstance = this._terminalService.splitInstance(instance);
return newInstance?.focusWhenReady();
}
return;
}
Expand Down

0 comments on commit cae22b8

Please sign in to comment.