@@ -23,6 +23,8 @@ export class HoverService implements IHoverService {
23
23
private _currentHoverOptions : IHoverOptions | undefined ;
24
24
private _currentHover : HoverWidget | undefined ;
25
25
26
+ private _lastFocusedElementBeforeOpen : HTMLElement | undefined ;
27
+
26
28
constructor (
27
29
@IInstantiationService private readonly _instantiationService : IInstantiationService ,
28
30
@IContextViewService private readonly _contextViewService : IContextViewService ,
@@ -37,10 +39,16 @@ export class HoverService implements IHoverService {
37
39
return undefined ;
38
40
}
39
41
this . _currentHoverOptions = options ;
42
+ if ( document . activeElement ) {
43
+ this . _lastFocusedElementBeforeOpen = document . activeElement as HTMLElement ;
44
+ }
40
45
41
46
const hoverDisposables = new DisposableStore ( ) ;
42
47
const hover = this . _instantiationService . createInstance ( HoverWidget , options ) ;
43
48
hover . onDispose ( ( ) => {
49
+ // Required to handle cases such as closing the hover with the escape key
50
+ this . _lastFocusedElementBeforeOpen ?. focus ( ) ;
51
+
44
52
// Only clear the current options if it's the current hover, the current options help
45
53
// reduce flickering when the same hover is shown multiple times
46
54
if ( this . _currentHoverOptions === options ) {
@@ -64,11 +72,11 @@ export class HoverService implements IHoverService {
64
72
hoverDisposables . add ( addDisposableListener ( document , EventType . KEY_DOWN , e => this . _keyDown ( e , hover ) ) ) ;
65
73
hoverDisposables . add ( addDisposableListener ( focusedElement , EventType . KEY_UP , e => this . _keyUp ( e , hover ) ) ) ;
66
74
hoverDisposables . add ( addDisposableListener ( document , EventType . KEY_UP , e => this . _keyUp ( e , hover ) ) ) ;
67
- }
68
- if ( options . hideOnKeyDown ) {
69
- const focusedElement = document . activeElement ;
70
- if ( focusedElement ) {
71
- hoverDisposables . add ( addDisposableListener ( focusedElement , EventType . KEY_DOWN , ( ) => this . hideHover ( ) ) ) ;
75
+ if ( options . hideOnKeyDown ) {
76
+ hoverDisposables . add ( addDisposableListener ( focusedElement , EventType . KEY_DOWN , ( ) => {
77
+ this . hideHover ( ) ;
78
+ this . _lastFocusedElementBeforeOpen ?. focus ( ) ;
79
+ } ) ) ;
72
80
}
73
81
}
74
82
@@ -110,7 +118,10 @@ export class HoverService implements IHoverService {
110
118
if ( keybinding . getSingleModifierDispatchParts ( ) . some ( value => ! ! value ) || this . _keybindingService . softDispatch ( event , event . target ) ) {
111
119
return ;
112
120
}
113
- this . hideHover ( ) ;
121
+ if ( e . key !== 'Tab' ) {
122
+ this . hideHover ( ) ;
123
+ this . _lastFocusedElementBeforeOpen ?. focus ( ) ;
124
+ }
114
125
}
115
126
116
127
private _keyUp ( e : KeyboardEvent , hover : HoverWidget ) {
@@ -119,6 +130,7 @@ export class HoverService implements IHoverService {
119
130
// Hide if alt is released while the mouse os not over hover/target
120
131
if ( ! hover . isMouseIn ) {
121
132
this . hideHover ( ) ;
133
+ this . _lastFocusedElementBeforeOpen ?. focus ( ) ;
122
134
}
123
135
}
124
136
}
0 commit comments