Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import * as React from "react";
import { RelativePosition } from "@bentley/ui-abstract";
import { ToolAssistanceInstruction } from "@bentley/imodeljs-frontend";
import { Timer, BodyText, Point, PointProps, Icon } from "@bentley/ui-core";
import { CursorInformation, CursorPopupManager, CursorUpdatedEventArgs } from "../../../ui-framework";
import "./CursorPrompt.scss";
/** @alpha */
export class CursorPrompt {
private _timeOut: number;
private _fadeOut: boolean;
private _timer: Timer;
private _relativePosition = RelativePosition.BottomRight;
private _offset: Point = new Point(20, 20);
private _popupId = "cursor-prompt";
constructor(timeOut: number, fadeOut: boolean) {
this._timeOut = timeOut;
this._fadeOut = fadeOut;
this._timer = new Timer(timeOut);
}
public display(toolIconSpec: string, instruction: ToolAssistanceInstruction, offset: PointProps = { x: 20, y: 20 }, relativePosition: RelativePosition = RelativePosition.BottomRight) {
if (!instruction.text) {
// istanbul ignore else
if (this._timer.isRunning)
this.close(false);
return;
}
private renderPositions(): React.ReactNode[] {
const positions = new Array();
let renderedPosition: React.ReactNode;
const begin = RelativePosition.Left;
const end = RelativePosition.BottomRight;
for (let position = begin; position <= end; position++) {
renderedPosition = this.renderRelativePosition(position);
if (renderedPosition)
positions.push(renderedPosition);
}
return positions;
}
}
}
if (rect.top < 0) {
flipped = true;
switch (inPos) {
case RelativePosition.Left:
case RelativePosition.TopLeft:
outPos = RelativePosition.BottomLeft;
break;
case RelativePosition.Top:
outPos = RelativePosition.Bottom;
break;
case RelativePosition.Right:
case RelativePosition.TopRight:
outPos = RelativePosition.BottomRight;
break;
}
}
return { outPos, flipped };
}
public static getRelativePositionFromCursorDirection(cursorDirection: CursorDirection): RelativePosition {
let relativePosition: RelativePosition = RelativePosition.BottomRight;
switch (cursorDirection) {
case CursorDirection.Top:
relativePosition = RelativePosition.Top;
break;
case CursorDirection.Left:
relativePosition = RelativePosition.Left;
break;
case CursorDirection.Right:
relativePosition = RelativePosition.Right;
break;
case CursorDirection.Bottom:
relativePosition = RelativePosition.Bottom;
break;
case CursorDirection.TopLeft:
relativePosition = RelativePosition.TopLeft;
private renderRelativePosition(relativePosition: RelativePosition): React.ReactNode {
const filteredInfo = CursorPopupManager.popups.filter((popupInfo: CursorPopupInfo) => popupInfo.renderRelativePosition === relativePosition);
if (filteredInfo.length > 0) {
let totalDimension = 0;
const ascending =
relativePosition === RelativePosition.Right ||
relativePosition === RelativePosition.BottomRight ||
relativePosition === RelativePosition.Bottom ||
relativePosition === RelativePosition.BottomLeft;
const positionPopups = filteredInfo
.sort((a: CursorPopupInfo, b: CursorPopupInfo): number => ascending ? a.priority - b.priority : b.priority - a.priority)
.map((popupInfo: CursorPopupInfo, index: number) => {
const title = (popupInfo.options !== undefined) ? popupInfo.options.title : "";
const shadow = (popupInfo.options !== undefined) ? popupInfo.options.shadow : false;
let offset = popupInfo.offset;
if (index > 0)
offset = this.adjustOffset(offset, popupInfo.renderRelativePosition, totalDimension);
if (popupInfo.popupSize)
totalDimension += this.getDimension(popupInfo.popupSize, relativePosition);
relativePosition = RelativePosition.Right;
break;
case CursorDirection.Bottom:
relativePosition = RelativePosition.Bottom;
break;
case CursorDirection.TopLeft:
relativePosition = RelativePosition.TopLeft;
break;
case CursorDirection.TopRight:
relativePosition = RelativePosition.TopRight;
break;
case CursorDirection.BottomLeft:
relativePosition = RelativePosition.BottomLeft;
break;
case CursorDirection.BottomRight:
relativePosition = RelativePosition.BottomRight;
break;
}
return relativePosition;
}
break;
}
}
if (rect.bottom > windowHeight) {
flipped = true;
switch (inPos) {
case RelativePosition.Left:
case RelativePosition.BottomLeft:
outPos = RelativePosition.TopLeft;
break;
case RelativePosition.Bottom:
outPos = RelativePosition.Top;
break;
case RelativePosition.Right:
case RelativePosition.BottomRight:
outPos = RelativePosition.TopRight;
break;
}
}
if (rect.top < 0) {
flipped = true;
switch (inPos) {
case RelativePosition.Left:
case RelativePosition.TopLeft:
outPos = RelativePosition.BottomLeft;
break;
case RelativePosition.Top:
outPos = RelativePosition.Bottom;
break;
case RelativePosition.Right:
popupRect.top = popupRect.bottom - popupSize.height;
popupRect.left = popupRect.right - popupSize.width;
break;
case RelativePosition.TopRight:
popupRect.bottom = pt.y - offset.y;
popupRect.left = pt.x + offset.x;
popupRect.top = popupRect.bottom - popupSize.height;
popupRect.right = popupRect.left + popupSize.width;
break;
case RelativePosition.BottomLeft:
popupRect.top = pt.y + offset.y;
popupRect.right = pt.x - offset.x;
popupRect.bottom = popupRect.top + popupSize.height;
popupRect.left = popupRect.right - popupSize.width;
break;
case RelativePosition.BottomRight:
popupRect.top = pt.y + offset.y;
popupRect.left = pt.x + offset.x;
popupRect.bottom = popupRect.top + popupSize.height;
popupRect.right = popupRect.left + popupSize.width;
break;
}
return popupRect;
}