Skip to content

Commit 62686a3

Browse files
idootopaiday-mar
andauthored
feat: introducing new hover focus options for editor.action.showHover (#196891)
* fix: revert and optimize showHover action behavior * feat: update focus option for controlling hover focus behavior * polishing the code, extracting an enum, doing a check on the focus argument * refactoring the code * Refactor default value to use ShowOrFocusHoverArgument enum * misc: renaming the enums to more accurately reflect their behavior --------- Co-authored-by: Aiday Marlen Kyzy <[email protected]>
1 parent 01485dd commit 62686a3

File tree

1 file changed

+37
-10
lines changed
  • src/vs/editor/contrib/hover/browser

1 file changed

+37
-10
lines changed

src/vs/editor/contrib/hover/browser/hover.ts

+37-10
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,12 @@ export class ModesHoverController extends Disposable implements IEditorContribut
371371
}
372372
}
373373

374+
enum HoverFocusBehavior {
375+
NoAutoFocus = 'noAutoFocus',
376+
FocusIfVisible = 'focusIfVisible',
377+
AutoFocusImmediately = 'autoFocusImmediately'
378+
}
379+
374380
class ShowOrFocusHoverAction extends EditorAction {
375381

376382
constructor() {
@@ -381,8 +387,7 @@ class ShowOrFocusHoverAction extends EditorAction {
381387
comment: [
382388
'Label for action that will trigger the showing/focusing of a hover in the editor.',
383389
'If the hover is not visible, it will show the hover.',
384-
'This allows for users to show the hover without using the mouse.',
385-
'If the hover is already visible, it will take focus.'
390+
'This allows for users to show the hover without using the mouse.'
386391
]
387392
}, "Show or Focus Hover"),
388393
metadata: {
@@ -393,9 +398,14 @@ class ShowOrFocusHoverAction extends EditorAction {
393398
type: 'object',
394399
properties: {
395400
'focus': {
396-
description: 'Controls if when triggered with the keyboard, the hover should take focus immediately.',
397-
type: 'boolean',
398-
default: false
401+
description: 'Controls if and when the hover should take focus upon being triggered by this action.',
402+
enum: [HoverFocusBehavior.NoAutoFocus, HoverFocusBehavior.FocusIfVisible, HoverFocusBehavior.AutoFocusImmediately],
403+
enumDescriptions: [
404+
nls.localize('showOrFocusHover.focus.noAutoFocus', 'The hover will not automatically take focus.'),
405+
nls.localize('showOrFocusHover.focus.focusIfVisible', 'The hover will take focus only if it is already visible.'),
406+
nls.localize('showOrFocusHover.focus.autoFocusImmediately', 'The hover will automatically take focus when it appears.'),
407+
],
408+
default: HoverFocusBehavior.FocusIfVisible,
399409
}
400410
},
401411
}
@@ -419,14 +429,31 @@ class ShowOrFocusHoverAction extends EditorAction {
419429
if (!controller) {
420430
return;
421431
}
422-
const position = editor.getPosition();
423-
const range = new Range(position.lineNumber, position.column, position.lineNumber, position.column);
424-
const focus = editor.getOption(EditorOption.accessibilitySupport) === AccessibilitySupport.Enabled || !!args?.focus;
432+
433+
const focusArgument = args?.focus;
434+
let focusOption = HoverFocusBehavior.FocusIfVisible;
435+
if (focusArgument in HoverFocusBehavior) {
436+
focusOption = focusArgument;
437+
} else if (typeof focusArgument === 'boolean' && focusArgument) {
438+
focusOption = HoverFocusBehavior.AutoFocusImmediately;
439+
}
440+
441+
const showContentHover = (focus: boolean) => {
442+
const position = editor.getPosition();
443+
const range = new Range(position.lineNumber, position.column, position.lineNumber, position.column);
444+
controller.showContentHover(range, HoverStartMode.Immediate, HoverStartSource.Keyboard, focus);
445+
};
446+
447+
const accessibilitySupportEnabled = editor.getOption(EditorOption.accessibilitySupport) === AccessibilitySupport.Enabled;
425448

426449
if (controller.isHoverVisible) {
427-
controller.focus();
450+
if (focusOption !== HoverFocusBehavior.NoAutoFocus) {
451+
controller.focus();
452+
} else {
453+
showContentHover(accessibilitySupportEnabled);
454+
}
428455
} else {
429-
controller.showContentHover(range, HoverStartMode.Immediate, HoverStartSource.Keyboard, focus);
456+
showContentHover(accessibilitySupportEnabled || focusOption === HoverFocusBehavior.AutoFocusImmediately);
430457
}
431458
}
432459
}

0 commit comments

Comments
 (0)