Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support triggering tooltip callback using customizable delay. #1251

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/Linkifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import { IMouseZoneManager } from './input/Types';
import { ILinkHoverEvent, ILinkMatcher, LinkMatcherHandler, LinkMatcherValidationCallback, LineData, LinkHoverEventTypes, ILinkMatcherOptions, ITerminal, IBufferAccessor, ILinkifier, IElementAccessor } from './Types';
import { MouseZone } from './input/MouseZoneManager';
import { MouseZone, HOVER_DURATION } from './input/MouseZoneManager';
import { EventEmitter } from './EventEmitter';

const protocolClause = '(https?:\\/\\/)';
Expand Down Expand Up @@ -142,6 +142,10 @@ export class Linkifier extends EventEmitter implements ILinkifier {
if (this._nextLinkMatcherId !== HYPERTEXT_LINK_MATCHER_ID && !handler) {
throw new Error('handler must be defined');
}
let tooltipDelay = options.tooltipDelay;
if (tooltipDelay === undefined) {
tooltipDelay = HOVER_DURATION;
}
const matcher: ILinkMatcher = {
id: this._nextLinkMatcherId++,
regex,
Expand All @@ -150,7 +154,8 @@ export class Linkifier extends EventEmitter implements ILinkifier {
validationCallback: options.validationCallback,
hoverTooltipCallback: options.tooltipCallback,
hoverLeaveCallback: options.leaveCallback,
priority: options.priority || 0
priority: options.priority || 0,
tooltipDelay: tooltipDelay
};
this._addLinkMatcherToList(matcher);
return matcher.id;
Expand Down Expand Up @@ -290,7 +295,8 @@ export class Linkifier extends EventEmitter implements ILinkifier {
if (matcher.hoverLeaveCallback) {
matcher.hoverLeaveCallback();
}
}
},
matcher.tooltipDelay
));
}
}
6 changes: 6 additions & 0 deletions src/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ export interface ILinkMatcher {
matchIndex?: number;
validationCallback?: LinkMatcherValidationCallback;
priority?: number;
tooltipDelay?: number;
}

export interface ICharset {
Expand Down Expand Up @@ -321,6 +322,11 @@ export interface ILinkMatcherOptions {
* default value is 0.
*/
priority?: number;
/**
* The amount of time to delay in milliseconds before triggering the tooltip
* callback.
*/
tooltipDelay?: number;
}

export interface IBrowser {
Expand Down
13 changes: 10 additions & 3 deletions src/input/MouseZoneManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { ITerminal } from '../Types';
import { IMouseZoneManager, IMouseZone } from './Types';

const HOVER_DURATION = 500;
export const HOVER_DURATION = 500;

/**
* The MouseZoneManager allows components to register zones within the terminal
Expand Down Expand Up @@ -130,7 +130,13 @@ export class MouseZoneManager implements IMouseZoneManager {
}

// Restart the tooltip timeout
this._tooltipTimeout = <number><any>setTimeout(() => this._onTooltip(e), HOVER_DURATION);
if (zone.tooltipDelay <= 0) {
// Trigger the tooltip immediately, don't queue it up into the event
// loop.
this._onTooltip(e);
} else {
this._tooltipTimeout = <number><any>setTimeout(() => this._onTooltip(e), zone.tooltipDelay);
}
}

private _onTooltip(e: MouseEvent): void {
Expand Down Expand Up @@ -191,7 +197,8 @@ export class MouseZone implements IMouseZone {
public clickCallback: (e: MouseEvent) => any,
public hoverCallback?: (e: MouseEvent) => any,
public tooltipCallback?: (e: MouseEvent) => any,
public leaveCallback?: () => void
public leaveCallback?: () => void,
public tooltipDelay: number = HOVER_DURATION,
) {
}
}
1 change: 1 addition & 0 deletions src/input/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ export interface IMouseZone {
hoverCallback?: (e: MouseEvent) => any;
tooltipCallback?: (e: MouseEvent) => any;
leaveCallback?: () => any;
tooltipDelay?: number;
}
6 changes: 6 additions & 0 deletions typings/xterm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ declare module 'xterm' {
* default value is 0.
*/
priority?: number;

/**
* The amount of time to delay in milliseconds before triggering the tooltip
* callback.
*/
tooltipDelay?: number;
}

export interface IEventEmitter {
Expand Down