UNPKG

lazy-widgets

Version:

Typescript retained mode GUI for the HTML canvas API

107 lines 3.66 kB
import { LayeredContainer } from '../widgets/LayeredContainer.js'; /** * Controls the visibility of a tooltip container (such as * {@link TooltipContainer}). Used for implementing tooltips, such as in * {@link Tooltip}, or to implement tooltip-like functionality, such as * dropdowns or context menus. */ export class BaseTooltipController { constructor(tooltipWrapper, tooltipContainer) { this.tooltipWrapper = tooltipWrapper; this.tooltipContainer = tooltipContainer; /** The top-most container in the current UI tree */ this.topLayeredContainer = null; /** The currently created layer for the {@link Tooltip#tooltipWidget} */ this.layer = null; } /** * Find the top-most layered container that is an ascendant of the * {@link BaseTooltipController#tooltipWrapper}. You must call this when the * wrapper widget is attached to a parent. * * Removes the current layer if there is any, and if the top-most layered * container changed. */ findTopLayeredContainer() { // find top-most layered container and use it do add/remove the tooltip // widget let newTop = null; let focus = this.tooltipWrapper.parent; while (focus !== null) { if (focus instanceof LayeredContainer) { newTop = focus; } focus = focus.parent; } if (newTop === null) { // TODO show only once? console.warn('Tooltip has no LayeredContainer ascendant. Tooltip will not be shown'); } if (newTop !== this.topLayeredContainer) { this.removeLayer(); this.topLayeredContainer = newTop; } } /** * Clear the top-most layered container. You must call this when the wrapper * widget is detached from a parent. * * Removes the current layer if there is any. */ clearTopLayeredContainer() { this.removeLayer(); this.topLayeredContainer = null; } /** * Add a layer for the {@link TooltipController#tooltipContainer}. If there * is a layer and adding another one would succeed, remove the old one. * * @returns True if the layer was addedd successfully, or false if there was no top layered container. */ addLayer(_options) { if (this.topLayeredContainer === null) { return false; } this.removeLayer(); // add layer with tooltip widget this.layer = { child: this.tooltipContainer, canExpand: false }; this.topLayeredContainer.pushLayer(this.layer); return true; } /** * Remove the layer of the {@link TooltipController#tooltipContainer} if * there is any. */ removeLayer() { if (!this.layer) { return; } const layer = this.layer; const container = this.topLayeredContainer; const layerIndex = container.getLayerIndex(layer); if (layerIndex) { container.removeLayer(layerIndex); } else { console.warn('Could not find tooltip layer in LayeredContainer. Maybe it was removed externally?'); } this.layer = null; } get hasLayer() { return this.layer !== null; } /** * Update the {@link TooltipContainer#tooltipRect} of the * {@link TooltipController#tooltipContainer}. */ updateTooltipRect() { if (this.layer === null) { return; } this.doTooltipRectUpdate(); } } //# sourceMappingURL=BaseTooltipController.js.map