-
Notifications
You must be signed in to change notification settings - Fork 2
/
CursorPopup.tsx
286 lines (260 loc) · 8.55 KB
/
CursorPopup.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Cursor
*/
import { RelativePosition } from "@itwin/appui-abstract";
import type { XAndY } from "@itwin/core-geometry";
import type {
CommonDivProps,
CommonProps,
RectangleProps,
SizeProps,
} from "@itwin/core-react";
import { Div, Size } from "@itwin/core-react";
import classnames from "classnames";
import * as React from "react";
import { StatusBarDialog } from "../../statusbar/dialog/Dialog";
import "./CursorPopup.scss";
import type { CursorPopupFadeOutEventArgs } from "./CursorPopupManager";
import { CursorPopupManager } from "./CursorPopupManager";
import { type Placement } from "../../utils/Placement";
import type { RequireAtLeastOne } from "@itwin/core-bentley";
/** Properties for the [[CursorPopup]] React component
* @public
*/
export type CursorPopupProps = {
id: string;
content: React.ReactNode;
pt: XAndY;
offset: XAndY;
title?: string;
shadow?: boolean;
/** Function called when size is known. */
onSizeKnown?: (size: SizeProps) => void;
} & CommonProps &
RequireAtLeastOne<{
relativePosition: RelativePosition /** @deprecated in 4.11.x. Use `placement` instead. */;
placement: Placement;
}>;
/** Enum for showing CursorPopup
* @internal - unit testing
*/
export enum CursorPopupShow {
Open,
FadeOut,
}
/** State for the [[CursorPopup]] React component
* @internal
*/
interface CursorPopupState {
showPopup: CursorPopupShow;
size: Size;
}
/** CursorPopup React component
* @public
*/
export class CursorPopup extends React.Component<
CursorPopupProps,
CursorPopupState
> {
private _isMounted: boolean = false;
/** @internal */
public static fadeOutTime = 500;
/** @internal */
constructor(props: CursorPopupProps) {
super(props);
this.state = {
showPopup: CursorPopupShow.Open,
size: new Size(-1, -1),
};
}
public override componentDidMount() {
this._isMounted = true;
CursorPopupManager.onCursorPopupFadeOutEvent.addListener(
this._handleCursorPopupFadeOutEvent
);
}
public override componentWillUnmount() {
this._isMounted = false;
CursorPopupManager.onCursorPopupFadeOutEvent.removeListener(
this._handleCursorPopupFadeOutEvent
);
}
private _handleCursorPopupFadeOutEvent = (
args: CursorPopupFadeOutEventArgs
) => {
if (this.props.id === args.id) {
// istanbul ignore else
if (this._isMounted)
this.setState({ showPopup: CursorPopupShow.FadeOut });
}
};
/** @internal */
public static getPopupRect(
pt: XAndY,
offset: XAndY,
popupSize: SizeProps | undefined,
relativePosition: RelativePosition | Placement
): RectangleProps {
const popupRect = { top: 0, left: 0, right: 0, bottom: 0 };
if (popupSize === undefined) return popupRect;
if (typeof relativePosition === "string") {
const placement = relativePosition.split("-");
const [mainPlacement, subPlacement] = placement;
switch (mainPlacement) {
case "top":
popupRect.left = pt.x - popupSize.width / 2 - offset.x;
popupRect.top = pt.y - offset.y + popupSize.height;
break;
case "left":
popupRect.left = pt.x - popupSize.width - offset.x;
popupRect.top = pt.y + popupSize.height / 2 - offset.y;
break;
case "right":
popupRect.left = pt.x + offset.x;
popupRect.top = pt.y + popupSize.height / 2 - offset.y;
break;
case "bottom":
popupRect.left = pt.x - popupSize.width / 2 - offset.x;
popupRect.top = pt.y + offset.y;
break;
}
if (subPlacement === "start") {
switch (mainPlacement) {
case "top":
case "bottom":
popupRect.left = pt.x - offset.x;
break;
case "left":
case "right":
popupRect.top = pt.y - offset.y;
break;
}
} else if (subPlacement === "end") {
switch (mainPlacement) {
case "top":
case "bottom":
popupRect.left = pt.x - popupSize.width - offset.x;
break;
case "left":
case "right":
popupRect.top = popupSize.height + pt.y - offset.y;
break;
}
}
popupRect.right = popupRect.left + popupSize.width;
popupRect.bottom = popupRect.top + popupSize.height;
} else {
switch (relativePosition) {
case RelativePosition.Top:
popupRect.bottom = pt.y - offset.y;
popupRect.left = pt.x - popupSize.width / 2;
popupRect.top = popupRect.bottom - popupSize.height;
popupRect.right = popupRect.left + popupSize.width;
break;
case RelativePosition.Left:
popupRect.right = pt.x - offset.x;
popupRect.top = pt.y - popupSize.height / 2;
popupRect.left = popupRect.right - popupSize.width;
popupRect.bottom = popupRect.top + popupSize.height;
break;
case RelativePosition.Right:
popupRect.left = pt.x + offset.x;
popupRect.top = pt.y - popupSize.height / 2;
popupRect.right = popupRect.left + popupSize.width;
popupRect.bottom = popupRect.top + popupSize.height;
break;
case RelativePosition.Bottom:
popupRect.top = pt.y + offset.y;
popupRect.left = pt.x - popupSize.width / 2;
popupRect.bottom = popupRect.top + popupSize.height;
popupRect.right = popupRect.left + popupSize.width;
break;
case RelativePosition.TopLeft:
popupRect.bottom = pt.y - offset.y;
popupRect.right = pt.x - offset.x;
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;
}
private setDivRef(div: HTMLDivElement | null) {
// istanbul ignore else
if (div) {
const rect = div.getBoundingClientRect();
const newSize = new Size(rect.width, rect.height);
// istanbul ignore else
if (!this.state.size.equals(newSize)) {
// istanbul ignore else
if (this.props.onSizeKnown) this.props.onSizeKnown(newSize);
// istanbul ignore else
if (this._isMounted) this.setState({ size: newSize });
}
}
}
/** @internal */
public override render() {
const popupRect = CursorPopup.getPopupRect(
this.props.pt,
this.props.offset,
this.state.size,
// eslint-disable-next-line deprecation/deprecation
this.props.placement || this.props.relativePosition
);
const positioningStyle: React.CSSProperties = {
left: popupRect.left,
top: popupRect.top,
};
const classNames = classnames(
"uifw-cursorpopup",
this.props.shadow && "core-popup-shadow",
this.state.showPopup === CursorPopupShow.FadeOut &&
"uifw-cursorpopup-fadeOut"
);
return (
<div
className={classNames}
ref={(e) => this.setDivRef(e)}
style={positioningStyle}
>
{this.props.title && (
<StatusBarDialog.TitleBar
title={this.props.title}
className="uifw-cursorpopup-title"
/>
)}
{this.props.content}
</div>
);
}
}
/** CursorPopup content with padding
* @public
*/
export function CursorPopupContent(props: CommonDivProps) {
// eslint-disable-next-line deprecation/deprecation
return <Div {...props} mainClassName="uifw-cursorpopup-content" />;
}