Skip to content

Commit

Permalink
Don't let console steal focus from the user (#2806)
Browse files Browse the repository at this point in the history
  • Loading branch information
lionel- authored Apr 23, 2024
1 parent 362be65 commit 1abacb3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ import { EmptyHistoryMatchStrategy, HistoryMatch, HistoryMatchStrategy } from 'v
import { HistoryPrefixMatchStrategy } from 'vs/workbench/contrib/positronConsole/common/historyPrefixMatchStrategy';
import { HistoryInfixMatchStrategy } from 'vs/workbench/contrib/positronConsole/common/historyInfixMatchStrategy';
import { CodeEditorWidget } from 'vs/editor/browser/widget/codeEditor/codeEditorWidget';
import { getActiveElement } from 'vs/base/browser/dom';
import { EditorContextKeys } from 'vs/editor/common/editorContextKeys';
import { InQuickPickContextKey } from 'vs/workbench/browser/quickaccess';
import { TerminalContextKeys } from 'vs/workbench/contrib/terminal/common/terminalContextKey';

// Position enumeration.
const enum Position {
Expand Down Expand Up @@ -672,14 +676,31 @@ export const ConsoleInput = (props: ConsoleInputProps) => {
positronConsoleContext.positronConsoleService.onDidChangeActivePositronConsoleInstance(
positronConsoleInstance => {
if (positronConsoleInstance === props.positronConsoleInstance) {
codeEditorWidget.focus();
// https://github.com/posit-dev/positron/issues/2802
// Only take focus if there is no focused editor to avoid stealing
// focus when the user could be actively working in an editor

const ctxt = positronConsoleContext.contextKeyService.getContext(getActiveElement());

// Sensitive to all editor contexts, simple (e.g. git commit textbox) or not (e.g. code editor)
const inTextInput = ctxt.getValue(EditorContextKeys.textInputFocus.key);
// Sensitive to all quick pick contexts, e.g. the commande palette or the file picker const inQuickPick = positronConsoleContext.contextKeyService.getContextKeyValue(InQuickPickContextKey.key);
const inQuickPick = ctxt.getValue(InQuickPickContextKey.key);
// Sensitive to terminal focus
const inTerminal = ctxt.getValue(TerminalContextKeys.focus.key);

if (!inTextInput && !inQuickPick && !inTerminal) {
codeEditorWidget.focus();
}
}
}
)
);

// Add the onFocusInput event handler.
disposableStore.add(props.positronConsoleInstance.onFocusInput(() => {
// Focus the input editor when the Console takes focus, i.e. when the
// user clicks somewhere on the console output
codeEditorWidget.focus();
}));

Expand Down Expand Up @@ -777,9 +798,6 @@ export const ConsoleInput = (props: ConsoleInputProps) => {
})
);

// Focus the console.
codeEditorWidget.focus();

// Return the cleanup function that will dispose of the disposables.
return () => disposableStore.dispose();
}, []);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { IExecutionHistoryService } from 'vs/workbench/contrib/executionHistory/
import { IPositronConsoleInstance, IPositronConsoleService } from 'vs/workbench/services/positronConsole/browser/interfaces/positronConsoleService';
import { IRuntimeSessionService } from 'vs/workbench/services/runtimeSession/common/runtimeSessionService';
import { IRuntimeStartupService } from 'vs/workbench/services/runtimeStartup/common/runtimeStartupService';
import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';

/**
* PositronConsoleServices interface. Defines the set of services that are required by the Positron
Expand All @@ -41,6 +42,7 @@ export interface PositronConsoleServices extends PositronActionBarServices {
readonly positronPlotsService: IPositronPlotsService;
readonly viewsService: IViewsService;
readonly workbenchLayoutService: IWorkbenchLayoutService;
readonly contextKeyService: IContextKeyService;
}

/**
Expand Down

0 comments on commit 1abacb3

Please sign in to comment.