Skip to content

Commit ccd48cf

Browse files
authored
Refactoring the hover.ts code (microsoft#210217)
refactoring some more the hover.ts file
1 parent d06e212 commit ccd48cf

File tree

1 file changed

+55
-32
lines changed
  • src/vs/editor/contrib/hover/browser

1 file changed

+55
-32
lines changed

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

Lines changed: 55 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -140,23 +140,40 @@ export class HoverController extends Disposable implements IEditorContribution {
140140
private _onEditorMouseDown(mouseEvent: IEditorMouseEvent): void {
141141

142142
this._hoverState.mouseDown = true;
143-
const target = mouseEvent.target;
144143

145-
if (target.type === MouseTargetType.CONTENT_WIDGET && target.detail === ContentHoverWidget.ID) {
146-
// mouse down on top of content hover widget
144+
const shouldNotHideCurrentHoverWidget = this._shouldNotHideCurrentHoverWidget(mouseEvent);
145+
if (shouldNotHideCurrentHoverWidget) {
147146
return;
148147
}
149148

150-
if (target.type === MouseTargetType.OVERLAY_WIDGET && target.detail === MarginHoverWidget.ID) {
151-
// mouse down on top of margin hover widget
152-
return;
149+
this._hideWidgets();
150+
}
151+
152+
private _shouldNotHideCurrentHoverWidget(mouseEvent: IPartialEditorMouseEvent): boolean {
153+
if (
154+
this._isMouseOnContentHoverWidget(mouseEvent)
155+
|| this._isMouseOnMarginHoverWidget(mouseEvent)
156+
|| this._isContentWidgetResizing()
157+
) {
158+
return true;
153159
}
160+
return false;
161+
}
154162

155-
if (this._contentWidget?.widget.isResizing) {
156-
return;
163+
private _isMouseOnMarginHoverWidget(mouseEvent: IPartialEditorMouseEvent): boolean {
164+
const target = mouseEvent.target;
165+
if (!target) {
166+
return false;
157167
}
168+
return target.type === MouseTargetType.OVERLAY_WIDGET && target.detail === MarginHoverWidget.ID;
169+
}
158170

159-
this._hideWidgets();
171+
private _isMouseOnContentHoverWidget(mouseEvent: IPartialEditorMouseEvent): boolean {
172+
const target = mouseEvent.target;
173+
if (!target) {
174+
return false;
175+
}
176+
return target.type === MouseTargetType.CONTENT_WIDGET && target.detail === ContentHoverWidget.ID;
160177
}
161178

162179
private _onEditorMouseUp(): void {
@@ -166,11 +183,9 @@ export class HoverController extends Disposable implements IEditorContribution {
166183
private _onEditorMouseLeave(mouseEvent: IPartialEditorMouseEvent): void {
167184

168185
this._cancelScheduler();
169-
const targetElement = (mouseEvent.event.browserEvent.relatedTarget) as HTMLElement;
170186

171-
if (this._contentWidget?.widget.isResizing || this._contentWidget?.containsNode(targetElement)) {
172-
// When the content widget is resizing
173-
// When the mouse is inside hover widget
187+
const shouldNotHideCurrentHoverWidget = this._shouldNotHideCurrentHoverWidget(mouseEvent);
188+
if (shouldNotHideCurrentHoverWidget) {
174189
return;
175190
}
176191
if (_sticky) {
@@ -179,21 +194,22 @@ export class HoverController extends Disposable implements IEditorContribution {
179194
this._hideWidgets();
180195
}
181196

182-
private _isMouseOverHoverWidget(mouseEvent: IEditorMouseEvent): boolean {
197+
private _shouldNotRecomputeCurrentHoverWidget(mouseEvent: IEditorMouseEvent): boolean {
183198

184-
const sticky = this._hoverSettings.sticky;
199+
const isHoverSticky = this._hoverSettings.sticky;
185200

186-
const isMouseOnMarginHoverWidget = (mouseEvent: IEditorMouseEvent, sticky: boolean) => {
187-
const target = mouseEvent.target;
188-
return sticky
189-
&& target.type === MouseTargetType.OVERLAY_WIDGET
190-
&& target.detail === MarginHoverWidget.ID;
201+
const isMouseOnStickyMarginHoverWidget = (mouseEvent: IEditorMouseEvent, isHoverSticky: boolean) => {
202+
const isMouseOnMarginHoverWidget = this._isMouseOnMarginHoverWidget(mouseEvent);
203+
return isHoverSticky && isMouseOnMarginHoverWidget;
191204
}
192-
const isMouseOnContentHoverWidget = (mouseEvent: IEditorMouseEvent, sticky: boolean) => {
193-
const target = mouseEvent.target;
194-
const isMouseOnContentHoverWidget = target.type === MouseTargetType.CONTENT_WIDGET && target.detail === ContentHoverWidget.ID;
205+
const isMouseOnStickyContentHoverWidget = (mouseEvent: IEditorMouseEvent, isHoverSticky: boolean) => {
206+
const isMouseOnContentHoverWidget = this._isMouseOnContentHoverWidget(mouseEvent);
207+
return isHoverSticky && isMouseOnContentHoverWidget;
208+
}
209+
const isMouseOnColorPicker = (mouseEvent: IEditorMouseEvent) => {
210+
const isMouseOnContentHoverWidget = this._isMouseOnContentHoverWidget(mouseEvent);
195211
const isColorPickerVisible = this._contentWidget?.isColorPickerVisible;
196-
return isMouseOnContentHoverWidget && (sticky || isColorPickerVisible);
212+
return isMouseOnContentHoverWidget && isColorPickerVisible;
197213
}
198214
// TODO@aiday-mar verify if the following is necessary code
199215
const isTextSelectedWithinContentHoverWidget = (mouseEvent: IEditorMouseEvent, sticky: boolean) => {
@@ -203,9 +219,10 @@ export class HoverController extends Disposable implements IEditorContribution {
203219
}
204220

205221
if (
206-
isMouseOnMarginHoverWidget(mouseEvent, sticky)
207-
|| isMouseOnContentHoverWidget(mouseEvent, sticky)
208-
|| isTextSelectedWithinContentHoverWidget(mouseEvent, sticky)
222+
isMouseOnStickyMarginHoverWidget(mouseEvent, isHoverSticky)
223+
|| isMouseOnStickyContentHoverWidget(mouseEvent, isHoverSticky)
224+
|| isMouseOnColorPicker(mouseEvent)
225+
|| isTextSelectedWithinContentHoverWidget(mouseEvent, isHoverSticky)
209226
) {
210227
return true;
211228
}
@@ -225,17 +242,19 @@ export class HoverController extends Disposable implements IEditorContribution {
225242
return;
226243
}
227244

228-
const mouseIsOverWidget = this._isMouseOverHoverWidget(mouseEvent);
229-
// If the mouse is over the widget and the hiding timeout is defined, then cancel it
230-
if (mouseIsOverWidget) {
245+
const shouldNotRecomputeCurrentHoverWidget = this._shouldNotRecomputeCurrentHoverWidget(mouseEvent);
246+
if (shouldNotRecomputeCurrentHoverWidget) {
231247
this._reactToEditorMouseMoveRunner.cancel();
232248
return;
233249
}
234250

251+
const hidingDelay = this._hoverSettings.hidingDelay;
252+
const isContentHoverWidgetVisible = this._contentWidget?.isVisible;
235253
// If the mouse is not over the widget, and if sticky is on,
236254
// then give it a grace period before reacting to the mouse event
237-
const hidingDelay = this._hoverSettings.hidingDelay;
238-
if (this._contentWidget?.isVisible && sticky && hidingDelay > 0) {
255+
const shouldRescheduleHoverComputation = isContentHoverWidgetVisible && sticky && hidingDelay > 0;
256+
257+
if (shouldRescheduleHoverComputation) {
239258
if (!this._reactToEditorMouseMoveRunner.isScheduled()) {
240259
this._reactToEditorMouseMoveRunner.schedule(hidingDelay);
241260
}
@@ -386,6 +405,10 @@ export class HoverController extends Disposable implements IEditorContribution {
386405
this._getOrCreateContentWidget().startShowingAtRange(range, mode, source, focus);
387406
}
388407

408+
private _isContentWidgetResizing(): boolean {
409+
return this._contentWidget?.widget.isResizing || false;
410+
}
411+
389412
public focus(): void {
390413
this._contentWidget?.focus();
391414
}

0 commit comments

Comments
 (0)