/
ViewportContentControl.ts
204 lines (181 loc) · 6.14 KB
/
ViewportContentControl.ts
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module ContentView
*/
import type { Id64String } from "@itwin/core-bentley";
import type {
IModelConnection,
ScreenViewport,
ViewState,
} from "@itwin/core-frontend";
import {
DrawingViewState,
IModelApp,
OrthographicViewState,
SheetViewState,
SpatialViewState,
} from "@itwin/core-frontend";
import {
ConfigurableCreateInfo,
ConfigurableUiControlType,
} from "../configurableui/ConfigurableUiControl";
import { CubeNavigationAidControl } from "../navigationaids/CubeNavigationAidControl";
import { DrawingNavigationAidControl } from "../navigationaids/DrawingNavigationAidControl";
import { SheetNavigationAidControl } from "../navigationaids/SheetNavigationAid";
import { UiFramework } from "../UiFramework";
import { ViewUtilities } from "../utils/ViewUtilities";
import type { SupportsViewSelectorChange } from "./ContentControl";
import { ContentControl } from "./ContentControl";
/**
* The base class for frontstage Viewport content controls that connects to a `ScreenViewport`
* that is managed by the `ViewManager`.
* @public
*/
export class ViewportContentControl
extends ContentControl
implements SupportsViewSelectorChange
{
private _viewport: ScreenViewport | undefined;
private _isReady: Promise<void>;
private _viewportReadyCallback?: () => void;
/** Creates an instance of ViewportContentControl.
* @param info An object that the subclass must pass to this base class.
* @param options Options provided via the applicationData in a [[ContentProps]].
*/
constructor(info: ConfigurableCreateInfo, options: any) {
super(info, options);
this._isReady = new Promise<void>(
(onSuccess: () => void, _onFailure: () => void): void => {
this._viewportReadyCallback = onSuccess;
}
);
}
/** Gets the type of ConfigurableUiControl, which is 'Viewport' in this case */
public override getType(): ConfigurableUiControlType {
return ConfigurableUiControlType.Viewport;
}
/** Returns true if this control is a Viewport control. */
public override get isViewport(): boolean {
return true;
}
/** The underlying ScreenViewport */
public override get viewport(): ScreenViewport | undefined {
return this._viewport;
}
public override set viewport(v: ScreenViewport | undefined) {
this._viewport = v;
this.setIsReady();
}
/** Returns a promise that resolves when the control is ready for usage.
*/
public setIsReady(): void {
// istanbul ignore else
if (this._viewportReadyCallback) {
this._viewportReadyCallback();
}
}
/** Returns a promise that resolves when the control is ready for usage.
*/
public override get isReady(): Promise<void> {
return this._isReady;
}
/** Called when this ContentControl is activated */
public override onActivated(): void {
super.onActivated();
}
/** Get the NavigationAidControl associated with this ContentControl */
public override get navigationAidControl(): string {
let navigationAidId = "";
// istanbul ignore else
if (this.viewport) {
navigationAidId = this._getNavigationAid(
this.viewport.view.classFullName
);
}
return navigationAidId;
}
/**
* Fetches appropriate NavigationAid based on the class of the current viewport.
* @param classFullName The full name of the current viewport class.
* @returns The ID of the navigation aid to be displayed.
*/
private _getNavigationAid = (classFullName: string) => {
const className = ViewUtilities.getBisBaseClass(classFullName);
let navigationAidId = "";
switch (className) {
case SheetViewState.className:
navigationAidId = SheetNavigationAidControl.navigationAidId;
break;
case DrawingViewState.className:
navigationAidId = DrawingNavigationAidControl.navigationAidId;
break;
case SpatialViewState.className:
case OrthographicViewState.className:
navigationAidId = CubeNavigationAidControl.navigationAidId;
break;
}
return navigationAidId;
};
/** Returns true if this control supports processing ViewSelector changes. */
public get supportsViewSelectorChange(): boolean {
return true;
}
/** Process a ViewSelector change. */
// istanbul ignore next
public async processViewSelectorChange(
iModel: IModelConnection,
viewDefinitionId: Id64String,
viewState: ViewState,
name: string
): Promise<void> {
if (this.viewport) {
if (
IModelApp.viewManager &&
this.viewport === IModelApp.viewManager.selectedView
)
this.viewport.changeView(viewState);
} else {
this.reactNode = this.getReactElementForViewSelectorChange(
iModel,
viewDefinitionId,
viewState,
name
);
}
UiFramework.content.refreshActive(this.reactNode);
}
/** Get the React.Element for a ViewSelector change. */
// istanbul ignore next
public getReactElementForViewSelectorChange(
_iModel: IModelConnection,
_viewDefinitionId: Id64String,
_viewState: ViewState,
_name: string
): React.ReactNode {
return null;
}
}
/**
* @beta
*/
export class FloatingViewportContentControl extends ViewportContentControl {
constructor(uniqueId: string, name: string, node: React.ReactNode) {
super(new ConfigurableCreateInfo(name, uniqueId, name), undefined);
this._reactNode = node;
this.setIsReady();
}
/** The React node associated with this control. */
public override get reactNode(): React.ReactNode {
return this._reactNode;
}
public override set reactNode(r: React.ReactNode) {
this._reactNode = r;
const activeFrontstageDef = UiFramework.frontstages.activeFrontstageDef;
// istanbul ignore else
if (this.viewport && activeFrontstageDef)
activeFrontstageDef.setActiveViewFromViewport(this.viewport);
}
}