Skip to content

Commit

Permalink
Merge pull request #131180 from microsoft/joh/hoverElements
Browse files Browse the repository at this point in the history
support any HTMLElement has hover content, rename from text to conten…
  • Loading branch information
jrieken authored Aug 19, 2021
2 parents 6fea665 + 5888928 commit 9ff4122
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/vs/base/browser/ui/iconLabel/iconHoverDelegate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface IHoverDelegateTarget extends IDisposable {
}

export interface IHoverDelegateOptions {
text: IMarkdownString | string;
content: IMarkdownString | string | HTMLElement;
target: IHoverDelegateTarget | HTMLElement;
hoverPosition?: HoverPosition;
showPointer?: boolean;
Expand Down
4 changes: 2 additions & 2 deletions src/vs/base/browser/ui/iconLabel/iconLabelHover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export function setupCustomHover(hoverDelegate: IHoverDelegate, htmlElement: HTM
if (hoverPreparation) {

const hoverOptions = {
text: localize('iconLabel.loading', "Loading..."),
content: localize('iconLabel.loading', "Loading..."),
target,
hoverPosition: HoverPosition.BELOW
};
Expand All @@ -87,7 +87,7 @@ export function setupCustomHover(hoverDelegate: IHoverDelegate, htmlElement: HTM
// awaiting the tooltip could take a while. Make sure we're still preparing to hover.
if (resolvedTooltip && hoverPreparation) {
const hoverOptions = {
text: resolvedTooltip,
content: resolvedTooltip,
target,
showPointer: hoverDelegate.placement === 'element',
hoverPosition: HoverPosition.BELOW
Expand Down
2 changes: 1 addition & 1 deletion src/vs/workbench/browser/parts/compositeBarActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ export class ActivityActionViewItem extends BaseActionViewItem {
this.hover.value = this.hoverService.showHover({
target: this.container,
hoverPosition,
text: this.computeTitle(),
content: this.computeTitle(),
showPointer: true,
compact: true,
skipFadeInAnimation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class EnvironmentVariableInfoWidget extends Widget implements ITerminalWi
const actions = this._info.getActions ? this._info.getActions() : undefined;
this._hoverOptions = {
target: this._domNode,
text: new MarkdownString(this._info.getInfo()),
content: new MarkdownString(this._info.getInfo()),
actions
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class TerminalHover extends Disposable implements ITerminalWidget {
const target = new CellHoverTarget(container, this._targetOptions);
const hover = this._hoverService.showHover({
target,
text: this._text,
content: this._text,
linkHandler: this._linkHandler,
// .xterm-hover lets xterm know that the hover is part of a link
additionalClasses: ['xterm-hover']
Expand Down
4 changes: 2 additions & 2 deletions src/vs/workbench/services/hover/browser/hover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ export interface IHoverService {

export interface IHoverOptions {
/**
* The text to display in the primary section of the hover. The type of text determines the
* The content to display in the primary section of the hover. The type of text determines the
* default `hideOnHover` behavior.
*/
text: IMarkdownString | string;
content: IMarkdownString | string | HTMLElement;

/**
* The target for the hover. This determines the position of the hover and it will only be
Expand Down
16 changes: 10 additions & 6 deletions src/vs/workbench/services/hover/browser/hoverWidget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { AnchorPosition } from 'vs/base/browser/ui/contextview/contextview';
import { IOpenerService } from 'vs/platform/opener/common/opener';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { MarkdownRenderer } from 'vs/editor/browser/core/markdownRenderer';
import { isString } from 'vs/base/common/types';
import { isMarkdownString } from 'vs/base/common/htmlContent';

const $ = dom.$;
type TargetRect = {
Expand Down Expand Up @@ -73,7 +73,7 @@ export class HoverWidget extends Widget {
) {
super();

this._linkHandler = options.linkHandler || (url => this._openerService.open(url, { allowCommands: (!isString(options.text) && options.text.isTrusted) }));
this._linkHandler = options.linkHandler || (url => this._openerService.open(url, { allowCommands: (isMarkdownString(options.content) && options.content.isTrusted) }));

this._target = 'targetElements' in options.target ? options.target : new ElementHoverTarget(options.target);

Expand Down Expand Up @@ -108,11 +108,15 @@ export class HoverWidget extends Widget {

const rowElement = $('div.hover-row.markdown-hover');
const contentsElement = $('div.hover-contents');
if (typeof options.text === 'string') {
contentsElement.textContent = options.text;
if (typeof options.content === 'string') {
contentsElement.textContent = options.content;
contentsElement.style.whiteSpace = 'pre-wrap';

} else if (options.content instanceof HTMLElement) {
contentsElement.appendChild(options.content);

} else {
const markdown = options.text;
const markdown = options.content;
const mdRenderer = this._instantiationService.createInstance(
MarkdownRenderer,
{ codeBlockFontFamily: this._configurationService.getValue<IEditorOptions>('editor').fontFamily || EDITOR_FONT_DEFAULTS.fontFamily }
Expand Down Expand Up @@ -168,7 +172,7 @@ export class HoverWidget extends Widget {
} else {
if (options.hideOnHover === undefined) {
// Defaults to true when string, false when markdown as it may contain links
hideOnHover = typeof options.text === 'string';
hideOnHover = typeof options.content === 'string';
} else {
// It's set explicitly
hideOnHover = options.hideOnHover;
Expand Down

0 comments on commit 9ff4122

Please sign in to comment.