-
Notifications
You must be signed in to change notification settings - Fork 2
/
UiFramework.ts
1323 lines (1186 loc) · 45.8 KB
/
UiFramework.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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Utilities
*/
// cSpell:ignore configurableui clientservices
import type { Store } from "redux";
import { Logger, ProcessDetector } from "@itwin/core-bentley";
import type { TranslationOptions } from "@itwin/core-common";
import type { IModelConnection, ViewState } from "@itwin/core-frontend";
import { IModelApp, SnapMode } from "@itwin/core-frontend";
import type {
DialogLayoutDataProvider,
DialogProps,
Primitives,
PropertyDescription,
PropertyRecord,
} from "@itwin/appui-abstract";
import { UiAdmin, UiError, UiEvent } from "@itwin/appui-abstract";
import type { UiStateStorage } from "@itwin/core-react";
import { LocalStateStorage, SettingsManager } from "@itwin/core-react";
import { getObjectClassName } from "@itwin/core-react";
import { UiIModelComponents } from "@itwin/imodel-components-react";
import { BackstageManager } from "./backstage/BackstageManager";
import { InternalChildWindowManager } from "./childwindow/InternalChildWindowManager";
import { InternalConfigurableUiManager } from "./configurableui/InternalConfigurableUiManager";
import { ConfigurableUiActionId } from "./configurableui/state";
import type { FrameworkState } from "./redux/FrameworkState";
import type {
CursorMenuData,
CursorMenuPayload,
PresentationSelectionScope,
} from "./redux/SessionState";
import { SessionStateActionId } from "./redux/SessionState";
import { StateManager } from "./redux/StateManager";
import type { HideIsolateEmphasizeActionHandler } from "./selection/HideIsolateEmphasizeManager";
import { HideIsolateEmphasizeManager } from "./selection/HideIsolateEmphasizeManager";
import type { ThemeId } from "./theme/ThemeManager";
import {
SYSTEM_PREFERRED_COLOR_THEME,
TOOLBAR_OPACITY_DEFAULT,
WIDGET_OPACITY_DEFAULT,
} from "./theme/ThemeManager";
import * as keyinPaletteTools from "./tools/KeyinPaletteTools";
import * as openSettingTools from "./tools/OpenSettingsTool";
import * as restoreLayoutTools from "./tools/RestoreLayoutTool";
import * as toolSettingTools from "./tools/ToolSettingsTools";
import {
InternalUiShowHideManager,
UiShowHideSettingsProvider,
} from "./utils/InternalUiShowHideManager";
import { WidgetManager } from "./widgets/WidgetManager";
import { InternalFrontstageManager } from "./frontstage/InternalFrontstageManager";
import { InternalContentViewManager } from "./content/InternalContentViewManager";
import { InternalModalDialogManager } from "./dialog/InternalModalDialogManager";
import { InternalModelessDialogManager } from "./dialog/InternalModelessDialogManager";
import { InternalKeyboardShortcutManager } from "./keyboardshortcut/InternalKeyboardShortcut";
import { InternalToolSettingsManager } from "./toolsettings/InternalToolSettingsManager";
import type { FrameworkBackstage } from "./framework/FrameworkBackstage";
import type { FrameworkChildWindows } from "./framework/FrameworkChildWindows";
import type { FrameworkControls } from "./framework/FrameworkControls";
import type { FrameworkFrontstages } from "./framework/FrameworkFrontstages";
import type { FrameworkToolSettings } from "./framework/FrameworkToolSettings";
import type { FrameworkContent } from "./framework/FrameworkContent";
import type { FrameworkDialogs } from "./framework/FrameworkDialogs";
import type { FrameworkKeyboardShortcuts } from "./framework/FrameworkKeyboardShortcuts";
import type { FrameworkVisibility } from "./framework/FrameworkVisibility";
import {
SyncUiEventDispatcher,
SyncUiEventId,
} from "./syncui/SyncUiEventDispatcher";
import type { XAndY } from "@itwin/core-geometry";
import { PopupManager } from "./popup/PopupManager";
import { AccuDrawPopupManager } from "./accudraw/AccuDrawPopupManager";
import { UiDataProvidedDialog } from "./dialog/UiDataProvidedDialog";
import type { RefObject } from "react";
import { createElement } from "react";
import type { DialogInfo } from "./dialog/DialogManagerBase";
import type { KeyinEntry } from "./keyins/Keyins";
import { mapToRelativePosition, type Placement } from "./utils/Placement";
import type { ToolbarProps } from "./toolbar/Toolbar";
import type { CursorMenuItemProps } from "./shared/MenuItem";
interface ShowInputEditorOptions {
location: XAndY;
anchorElement?: HTMLElement;
initialValue: Primitives.Value; // need strategy for replacing (appui-abstract)
propertyDescription: PropertyDescription;
onCommit: (value: Primitives.Value) => void;
onCancel: () => void;
}
// cSpell:ignore Mobi
/** Interface to be implemented but any classes that wants to load their user settings when the UiStateEntry storage class is set.
* @public
*/
export interface UserSettingsProvider {
/** Unique provider Id */
providerId: string;
/** Function to load settings from settings storage */
loadUserSettings(storage: UiStateStorage): Promise<void>;
}
/** UiVisibility Event Args interface.
* @public
* @deprecated in 4.13.x. Event args are inferred from a listener. If explicit type is needed use a type helper.
*/
export interface UiVisibilityEventArgs {
visible: boolean;
}
/** UiVisibility Event class.
* @public
* @deprecated in 4.13.x. This class should not be used by applications to instantiate objects.
*/
// eslint-disable-next-line deprecation/deprecation
export class UiVisibilityChangedEvent extends UiEvent<UiVisibilityEventArgs> {}
/** TrackingTime time argument used by our feature tracking manager as an option argument to the TelemetryClient
* @internal
*/
export interface TrackingTime {
startTime: Date;
endTime: Date;
}
type fn = typeof PopupManager.showComponent;
type ShowComponentParams = Parameters<fn>;
type OptionalShowComponentParams = [
ShowComponentParams[0],
Partial<
Exclude<ShowComponentParams[1], "anchor"> & {
anchorRef?: RefObject<HTMLElement>;
}
>?
];
/** Main entry point to configure and interact with the features provided by the AppUi-react package.
* @public
*/
export class UiFramework {
/**
* Operation on the backstage component.
* @public
*/
public static get backstage(): FrameworkBackstage {
// istanbul ignore next
if (!UiFramework._backstageManager)
// eslint-disable-next-line deprecation/deprecation
throw new UiError(
UiFramework.loggerCategory(this),
UiFramework._complaint
);
return UiFramework._backstageManager;
}
/**
* Manage access to the child windows.
* @public
*/
public static get childWindows(): FrameworkChildWindows {
return this._childWindowManager;
}
/**
* Manage registered controls
* @public
*/
public static get controls(): FrameworkControls {
return InternalConfigurableUiManager;
}
/**
* Manage access to frontstages and related helper methods.
* @public
*/
public static get frontstages(): FrameworkFrontstages {
return InternalFrontstageManager;
}
/**
* Manage access and behavior of the tool settings.
* @public
*/
public static get toolSettings(): FrameworkToolSettings {
return InternalToolSettingsManager;
}
/**
* Manage content presented by the frontstages.
* @public
*/
public static get content(): FrameworkContent {
return InternalContentViewManager;
}
/**
* Manage displayed dialogs.
* @public
*/
public static get dialogs(): FrameworkDialogs {
return {
modal: InternalModalDialogManager,
modeless: InternalModelessDialogManager,
};
}
/**
* Manages global keyboard shortcuts
*
* Note: This only manages the list of available shortcuts registered with it. It does not listens to the actual
* keyboard events. In order for these shortcuts to be called upon a keyboard event, the application can
* override the `IModelApp.toolAdmin` and assign it [[FrameworkToolAdmin]] or create an event listener
* and call `UiFramework.keyboardShortcuts.processKey`.
* @public
*/
public static get keyboardShortcuts(): FrameworkKeyboardShortcuts {
return InternalKeyboardShortcutManager;
}
/**
* Manages UI visibility (Show/Hide)
* @public
*/
public static get visibility(): FrameworkVisibility {
return InternalUiShowHideManager;
}
private static _initialized = false;
private static _store?: Store<any>;
private static _complaint = "UiFramework not initialized";
private static _frameworkStateKeyInStore: string = "frameworkState"; // default name
private static _backstageManager?: BackstageManager;
private static _widgetManager?: WidgetManager;
private static _hideIsolateEmphasizeActionHandler?: HideIsolateEmphasizeActionHandler;
/** this provides a default state storage handler */
private static _uiStateStorage: UiStateStorage = new LocalStateStorage();
private static _settingsManager?: SettingsManager;
private static _uiSettingsProviderRegistry: Map<
string,
UserSettingsProvider
> = new Map<string, UserSettingsProvider>();
private static _childWindowManager = new InternalChildWindowManager();
public static useDefaultPopoutUrl = false;
private static readonly CONTEXT_MENU_OFFSET = -8;
/** Registers class that will be informed when the UserSettingsStorage location has been set or changed. This allows
* classes to load any previously saved settings from the new storage location. Common storage locations are the browser's
* local storage, or the iTwin Product Settings cloud storage available via the SettingsAdmin see `IModelApp.settingsAdmin`.
* @public
*/
public static registerUserSettingsProvider(entry: UserSettingsProvider) {
if (this._uiSettingsProviderRegistry.has(entry.providerId)) return false;
this._uiSettingsProviderRegistry.set(entry.providerId, entry);
return true;
}
/** Get Show Ui event.
* @public
*/
// eslint-disable-next-line deprecation/deprecation
public static readonly onUiVisibilityChanged = new UiVisibilityChangedEvent();
/**
* Called by the application to initialize the UiFramework. Also initializes UIIModelComponents, UiComponents, UiCore.
* @param store The single Redux store created by the host application. If this is `undefined` then it is assumed that the [[StateManager]] is being used to provide the Redux store.
* @param frameworkStateKey The name of the key used by the app when adding the UiFramework state into the Redux store. If not defined "frameworkState" is assumed. This value is ignored if [[StateManager]] is being used. The StateManager use "frameworkState".
*/
public static async initialize(
store: Store<any> | undefined,
frameworkStateKey?: string
): Promise<void> {
if (UiFramework._initialized) {
Logger.logInfo(
UiFramework.loggerCategory(UiFramework),
`UiFramework.initialize already called`
);
return;
}
/* if store is undefined then the StateManager class should have been initialized by parent app and the apps default set of reducers registered with it.
If the app has no reducers to add and does not initialize a StateManager then just initialize the StateManager with the default framework reducer now */
if (undefined === store && !StateManager.isInitialized(true))
new StateManager();
UiFramework._store = store;
// ignore setting _frameworkStateKeyInStore if not using store
if (frameworkStateKey && store)
UiFramework._frameworkStateKeyInStore = frameworkStateKey;
// set up namespace and register all tools from package
const frameworkNamespace = IModelApp.localization?.registerNamespace(
UiFramework.localizationNamespace
);
[
restoreLayoutTools,
keyinPaletteTools,
openSettingTools,
toolSettingTools,
].forEach((tool) =>
IModelApp.tools.registerModule(tool, this.localizationNamespace)
);
UiFramework._backstageManager = new BackstageManager();
UiFramework._hideIsolateEmphasizeActionHandler =
new HideIsolateEmphasizeManager(); // this allows user to override the default HideIsolateEmphasizeManager implementation.
UiFramework._widgetManager = new WidgetManager();
// Initialize ui-imodel-components, ui-components, ui-core & ui-abstract
await UiIModelComponents.initialize();
UiFramework.settingsManager.onSettingsProvidersChanged.addListener(() => {
SyncUiEventDispatcher.dispatchSyncUiEvent(
SyncUiEventId.SettingsProvidersChanged
);
});
// Initialize the MessagePresenter interface in UiAdmin for Editor notifications
UiAdmin.messagePresenter = IModelApp.notifications;
UiFramework._initialized = true;
// initialize any standalone settings providers that don't need to have defaults set by iModelApp
UiShowHideSettingsProvider.initialize();
InternalConfigurableUiManager.initialize();
return frameworkNamespace;
}
/** Un-registers the UiFramework internationalization service namespace */
public static terminate() {
UiFramework._store = undefined;
UiFramework._frameworkStateKeyInStore = "frameworkState";
if (StateManager.isInitialized(true)) StateManager.clearStore();
// istanbul ignore next
IModelApp.localization?.unregisterNamespace(
UiFramework.localizationNamespace
);
UiFramework._backstageManager = undefined;
UiFramework._widgetManager = undefined;
UiFramework._hideIsolateEmphasizeActionHandler = undefined;
UiFramework._settingsManager = undefined;
UiIModelComponents.terminate();
InternalUiShowHideManager.terminate();
UiFramework._initialized = false;
}
/** Determines if UiFramework has been initialized */
public static get initialized(): boolean {
return UiFramework._initialized;
}
/** Property that returns the SettingManager used by AppUI-based applications.
* @public
*/
public static get settingsManager() {
if (undefined === UiFramework._settingsManager)
UiFramework._settingsManager = new SettingsManager();
return UiFramework._settingsManager;
}
/** @public */
public static get frameworkStateKey(): string {
return UiFramework._frameworkStateKeyInStore;
}
/** The UiFramework state maintained by Redux
* @public
*/
public static get frameworkState(): FrameworkState | undefined {
try {
return UiFramework.store.getState()[UiFramework.frameworkStateKey];
} catch (_e) {
return undefined;
}
}
/** The Redux store */
public static get store(): Store<any> {
if (UiFramework._store) return UiFramework._store;
// istanbul ignore else
if (!StateManager.isInitialized(true))
// eslint-disable-next-line deprecation/deprecation
throw new UiError(
UiFramework.loggerCategory(this),
`Error trying to access redux store before either store or StateManager has been initialized.`
);
// istanbul ignore next
return StateManager.store;
}
/** The internationalization service namespace. */
public static get localizationNamespace(): string {
return "UiFramework";
}
/** @public */
public static get hideIsolateEmphasizeActionHandler(): HideIsolateEmphasizeActionHandler {
// istanbul ignore next
if (!UiFramework._hideIsolateEmphasizeActionHandler)
// eslint-disable-next-line deprecation/deprecation
throw new UiError(
UiFramework.loggerCategory(this),
UiFramework._complaint
);
return UiFramework._hideIsolateEmphasizeActionHandler;
}
/** @public */
public static setHideIsolateEmphasizeActionHandler(
handler: HideIsolateEmphasizeActionHandler | undefined
) {
// istanbul ignore else
if (handler) UiFramework._hideIsolateEmphasizeActionHandler = handler;
else
UiFramework._hideIsolateEmphasizeActionHandler =
new HideIsolateEmphasizeManager();
}
/** @alpha */
public static get widgetManager(): WidgetManager {
// istanbul ignore next
if (!UiFramework._widgetManager)
// eslint-disable-next-line deprecation/deprecation
throw new UiError(
UiFramework.loggerCategory(this),
UiFramework._complaint
);
return UiFramework._widgetManager;
}
/** Calls localization.getLocalizedString with the "UiFramework" namespace. Do NOT include the namespace in the key.
* @internal
*/
public static translate(
key: string | string[],
options?: TranslationOptions
): string {
return IModelApp.localization.getLocalizedString(
`${UiFramework.localizationNamespace}:${key}`,
options
);
}
/** @internal */
public static get packageName(): string {
return "appui-react";
}
/** @internal */
public static loggerCategory(obj: any): string {
const className = getObjectClassName(obj);
const category =
UiFramework.packageName + (className ? `.${className}` : "");
return category;
}
public static dispatchActionToStore(
type: string,
payload: any,
immediateSync = false
) {
UiFramework.store.dispatch({ type, payload });
if (immediateSync) SyncUiEventDispatcher.dispatchImmediateSyncUiEvent(type);
else SyncUiEventDispatcher.dispatchSyncUiEvent(type);
}
public static setAccudrawSnapMode(snapMode: SnapMode) {
UiFramework.dispatchActionToStore(
ConfigurableUiActionId.SetSnapMode,
snapMode,
true
);
}
public static getAccudrawSnapMode(): SnapMode {
return UiFramework.frameworkState
? UiFramework.frameworkState.configurableUiState.snapMode
: /* istanbul ignore next */ SnapMode.NearestKeypoint;
}
/**
* Returns the stored active selection scope id.
*/
public static getActiveSelectionScope(): string {
return UiFramework.frameworkState
? UiFramework.frameworkState.sessionState.activeSelectionScope
: /* istanbul ignore next */ "element";
}
/**
* This method stores the active selection scope to the supplied scope id, and triggers
* a `SessionStateActionId.SetSelectionScope` event in the `SyncUiEventDispatcher`.
* Note: As of 4.0, this method *does not change* the active selection scope in the `Presentation.selection.scopes.activeScope` property.
* This event should be listened to and the change should typically be applied to
* `Presentation.selection.scopes.activeScope` property from the `@itwin/presentation-frontend` package.
*/
public static setActiveSelectionScope(selectionScopeId: string): void {
// istanbul ignore else
if (UiFramework.frameworkState) {
const foundIndex =
UiFramework.frameworkState.sessionState.availableSelectionScopes.findIndex(
(selectionScope: PresentationSelectionScope) =>
selectionScope.id === selectionScopeId
);
if (-1 !== foundIndex) {
const scope =
UiFramework.frameworkState.sessionState.availableSelectionScopes[
foundIndex
];
UiFramework.dispatchActionToStore(
SessionStateActionId.SetSelectionScope,
scope.id
);
}
}
}
/** Show a context menu at a particular location.
* @param items Properties of the menu items to display.
* @param location Location of the context menu, relative to the origin of anchorElement or the overall window.
* @param anchorElement The HTMLElement that anchors the context menu. If undefined, the location is relative to the overall window.
* @return true if the menu was displayed, false if the menu could not be displayed.
*/
public static openContextMenu(
items: CursorMenuItemProps[],
location: XAndY,
anchorElement?: HTMLElement
): boolean {
let position = location;
let childWindowId: string | undefined;
if (anchorElement) {
const anchorOffset = anchorElement.getBoundingClientRect();
position = {
x: anchorOffset.left + location.x,
y: anchorOffset.top + location.y,
};
childWindowId = UiFramework.childWindows.findId(
anchorElement.ownerDocument.defaultView
);
}
position = {
x: position.x + UiFramework.CONTEXT_MENU_OFFSET,
y: position.y + UiFramework.CONTEXT_MENU_OFFSET,
};
const cursorMenuData: CursorMenuPayload = {
position,
items,
childWindowId,
};
UiFramework.openCursorMenu(cursorMenuData);
return true;
}
/** @public */
public static openCursorMenu(
// eslint-disable-next-line deprecation/deprecation
menuData: CursorMenuData | CursorMenuPayload | undefined
): void {
UiFramework.dispatchActionToStore(
SessionStateActionId.UpdateCursorMenu,
menuData
);
}
/** @public */
public static closeCursorMenu(): void {
UiFramework.dispatchActionToStore(
SessionStateActionId.UpdateCursorMenu,
undefined
);
}
/** @public */
public static getCursorMenuData(): // eslint-disable-next-line deprecation/deprecation
CursorMenuData | CursorMenuPayload | undefined {
return UiFramework.frameworkState
? UiFramework.frameworkState.sessionState.cursorMenuPayload ??
UiFramework.frameworkState.sessionState.cursorMenuData
: /* istanbul ignore next */ undefined;
}
public static getActiveIModelId(): string {
return UiFramework.frameworkState
? UiFramework.frameworkState.sessionState.iModelId
: /* istanbul ignore next */ "";
}
public static setActiveIModelId(iModelId: string): void {
UiFramework.dispatchActionToStore(
SessionStateActionId.SetActiveIModelId,
iModelId
);
}
public static setIModelConnection(
iModelConnection: IModelConnection | undefined,
immediateSync = false
) {
const oldConnection = UiFramework.getIModelConnection();
if (oldConnection === iModelConnection) return;
InternalFrontstageManager.clearFrontstageDefsForIModelId(
oldConnection?.iModelId
);
oldConnection && SyncUiEventDispatcher.clearConnectionEvents(oldConnection);
iModelConnection &&
SyncUiEventDispatcher.initializeConnectionEvents(iModelConnection);
UiFramework.dispatchActionToStore(
SessionStateActionId.SetIModelConnection,
iModelConnection,
immediateSync
);
const itemsSelected = iModelConnection
? iModelConnection.selectionSet.elements.size
: 0;
UiFramework.dispatchActionToStore(
SessionStateActionId.SetNumItemsSelected,
itemsSelected
);
UiFramework.setActiveIModelId(iModelConnection?.iModelId ?? "");
}
public static getIModelConnection(): IModelConnection | undefined {
return UiFramework.frameworkState
? UiFramework.frameworkState.sessionState.iModelConnection
: /* istanbul ignore next */ undefined;
}
/** Called by iModelApp to initialize saved UI state from registered UseSettingsProviders
* @public
*/
public static async initializeStateFromUserSettingsProviders(
immediateSync = false
) {
// let any registered providers to load values from the new storage location
const providerKeys = [...this._uiSettingsProviderRegistry.keys()];
for await (const key of providerKeys) {
await this._uiSettingsProviderRegistry
.get(key)!
.loadUserSettings(UiFramework._uiStateStorage);
}
// istanbul ignore next
if (immediateSync)
SyncUiEventDispatcher.dispatchImmediateSyncUiEvent(
SyncUiEventId.UiStateStorageChanged
);
else
SyncUiEventDispatcher.dispatchSyncUiEvent(
SyncUiEventId.UiStateStorageChanged
);
}
/** @public */
public static async setUiStateStorage(
storage: UiStateStorage,
immediateSync = false
) {
if (UiFramework._uiStateStorage === storage) return;
UiFramework._uiStateStorage = storage;
await this.initializeStateFromUserSettingsProviders(immediateSync);
}
/** The UI Settings Storage is a convenient wrapper around Local Storage to assist in caching state information across user sessions.
* It was previously used to conflate both the state information across session and the information driven directly from user explicit action,
* which are now handled with user preferences.
* @public
*/
public static getUiStateStorage(): UiStateStorage {
return UiFramework._uiStateStorage;
}
public static setDefaultIModelViewportControlId(
iModelViewportControlId: string,
immediateSync = false
) {
UiFramework.dispatchActionToStore(
SessionStateActionId.SetDefaultIModelViewportControlId,
iModelViewportControlId,
immediateSync
);
}
public static getDefaultIModelViewportControlId(): string | undefined {
return UiFramework.frameworkState
? UiFramework.frameworkState.sessionState.defaultIModelViewportControlId
: /* istanbul ignore next */ undefined;
}
public static setDefaultViewId(viewId: string, immediateSync = false) {
UiFramework.dispatchActionToStore(
SessionStateActionId.SetDefaultViewId,
viewId,
immediateSync
);
}
public static getDefaultViewId(): string | undefined {
return UiFramework.frameworkState
? UiFramework.frameworkState.sessionState.defaultViewId
: /* istanbul ignore next */ undefined;
}
public static setDefaultViewState(
viewState: ViewState,
immediateSync = false
) {
UiFramework.dispatchActionToStore(
SessionStateActionId.SetDefaultViewState,
viewState,
immediateSync
);
}
public static getDefaultViewState(): ViewState | undefined {
return UiFramework.frameworkState
? UiFramework.frameworkState.sessionState.defaultViewState
: /* istanbul ignore next */ undefined;
}
/**
* Returns the stored list of available selection scopes. This list should be set by the application
* by dispatching the `setAvailableSelectionScopes` action.
* The value for this action typically come from `Presentation.selection.scopes.getSelectionScopes()`
* method found in the `@itwin/presentation-frontend` package.
* @public */
public static getAvailableSelectionScopes(): PresentationSelectionScope[] {
return UiFramework.frameworkState
? UiFramework.frameworkState.sessionState.availableSelectionScopes
: /* istanbul ignore next */
[{ id: "element", label: "Element" } as PresentationSelectionScope];
}
public static getIsUiVisible() {
return UiFramework.visibility.isUiVisible;
}
public static setIsUiVisible(visible: boolean) {
if (UiFramework.visibility.isUiVisible !== visible) {
UiFramework.visibility.isUiVisible = visible;
UiFramework.onUiVisibilityChanged.emit({ visible });
}
}
/**
* Set the theme value used by the [[ThemeManager]] component.
*/
public static setColorTheme(theme: ThemeId) {
if (UiFramework.getColorTheme() === theme) return;
UiFramework.dispatchActionToStore(
ConfigurableUiActionId.SetTheme,
theme,
true
);
}
public static getColorTheme(): ThemeId {
return UiFramework.frameworkState
? UiFramework.frameworkState.configurableUiState.theme
: /* istanbul ignore next */ SYSTEM_PREFERRED_COLOR_THEME;
}
/** UiFramework.setToolbarOpacity() sets the non-hovered opacity to the value specified.
* @param opacity a value between 0 and 1. The default value is 0.5. IT IS NOT ADVISED TO USE A VALUE BELOW 0.2
* @public
*/
public static setToolbarOpacity(opacity: number) {
if (UiFramework.getToolbarOpacity() === opacity) return;
UiFramework.dispatchActionToStore(
ConfigurableUiActionId.SetToolbarOpacity,
opacity,
true
);
}
/** UiFramework.getToolbarOpacity() returns a number between 0 and 1 that is the non-hovered opacity for toolbars.
* @public
*/
public static getToolbarOpacity(): number {
return UiFramework.frameworkState
? UiFramework.frameworkState.configurableUiState.toolbarOpacity
: /* istanbul ignore next */ TOOLBAR_OPACITY_DEFAULT;
}
public static setWidgetOpacity(opacity: number) {
if (UiFramework.getWidgetOpacity() === opacity) return;
UiFramework.dispatchActionToStore(
ConfigurableUiActionId.SetWidgetOpacity,
opacity,
true
);
}
public static getWidgetOpacity(): number {
return UiFramework.frameworkState
? UiFramework.frameworkState.configurableUiState.widgetOpacity
: /* istanbul ignore next */ WIDGET_OPACITY_DEFAULT;
}
/** @deprecated in 4.13.x. Use {@link @itwin/core-bentley#ProcessDetector.isMobileBrowser} instead. */
// eslint-disable-next-line @itwin/prefer-get
public static isMobile() {
return ProcessDetector.isMobileBrowser;
}
public static get showWidgetIcon(): boolean {
return UiFramework.frameworkState
? UiFramework.frameworkState.configurableUiState.showWidgetIcon
: /* istanbul ignore next */ false;
}
public static setShowWidgetIcon(value: boolean) {
if (UiFramework.showWidgetIcon === value) return;
UiFramework.dispatchActionToStore(
ConfigurableUiActionId.SetShowWidgetIcon,
value,
true
);
}
/** Animate Tool Settings on appear */
public static get animateToolSettings(): boolean {
return UiFramework.frameworkState
? UiFramework.frameworkState.configurableUiState.animateToolSettings
: /* istanbul ignore next */ false;
}
public static setAnimateToolSettings(value: boolean) {
if (UiFramework.animateToolSettings === value) return;
UiFramework.dispatchActionToStore(
ConfigurableUiActionId.AnimateToolSettings,
value,
true
);
}
/** Use Tool Name As Tool Settings Widget Tab Label */
public static get useToolAsToolSettingsLabel(): boolean {
return UiFramework.frameworkState
? UiFramework.frameworkState.configurableUiState
.useToolAsToolSettingsLabel
: /* istanbul ignore next */ false;
}
public static setUseToolAsToolSettingsLabel(value: boolean) {
if (UiFramework.useToolAsToolSettingsLabel === value) return;
UiFramework.dispatchActionToStore(
ConfigurableUiActionId.UseToolAsToolSettingsLabel,
value,
true
);
}
/** When `true`, panels will close as soon as the mouse leave the panel.
* When `false` (default), panels will close on next click outside the panel.
* @public */
public static get autoCollapseUnpinnedPanels(): boolean {
return UiFramework.frameworkState
? UiFramework.frameworkState.configurableUiState
.autoCollapseUnpinnedPanels
: /* istanbul ignore next */ false;
}
/** Method used to enable the automatic closing of an unpinned widget panel as soon as the
* mouse leaves the widget panel. The default behavior is to require a mouse click outside
* the panel before it is closed.
* @public */
public static setAutoCollapseUnpinnedPanels(value: boolean) {
if (UiFramework.autoCollapseUnpinnedPanels === value) return;
UiFramework.dispatchActionToStore(
ConfigurableUiActionId.AutoCollapseUnpinnedPanels,
value,
true
);
}
public static get useDragInteraction(): boolean {
return UiFramework.frameworkState
? UiFramework.frameworkState.configurableUiState.useDragInteraction
: false;
}
public static setUseDragInteraction(useDragInteraction: boolean) {
UiFramework.dispatchActionToStore(
ConfigurableUiActionId.SetDragInteraction,
useDragInteraction,
true
);
}
/** Returns the variable controlling whether the overlay is displayed in a Viewport
* @public
*/
public static get viewOverlayDisplay() {
return UiFramework.frameworkState
? UiFramework.frameworkState.configurableUiState.viewOverlayDisplay
: /* istanbul ignore next */ true;
}
/** Set the variable that controls display of the view overlay. Applies to all viewports in the app
* @public
*/
public static setViewOverlayDisplay(display: boolean) {
if (UiFramework.viewOverlayDisplay === display) return;
UiFramework.dispatchActionToStore(
ConfigurableUiActionId.SetViewOverlayDisplay,
display
);
}
/** Determines whether a ContextMenu is open
* @alpha
* */
public static get isContextMenuOpen(): boolean {
const contextMenu = document.querySelector("div.core-context-menu-opened");
return contextMenu !== null && contextMenu !== undefined;
}
/** Show a Card containing content, a title and a toolbar at a particular location.
* @param content The React component of the content to display
* @param title Title to display at the top of the card.
* @param toolbarProps Properties of the Toolbar to display.
* @param location Location of the Card, relative to the origin of anchorElement or the window.
* @param offset Offset of the Card from the location.
* @param onItemExecuted Function invoked after a Toolbar item is executed
* @param onCancel Function invoked when the Escape key is pressed or a click occurs outside the Card
* @param placement {@link Placement} relative to the given location. Defaults to top-end.
* @param anchorElement The HTMLElement that anchors the Card. If undefined, the location is relative to the overall window.
* @return true if the Card was displayed, false if the Card could not be displayed.
* @internal
*/
public static showCard(
content: React.ReactNode,
title: string | PropertyRecord | undefined,
toolbarProps: ToolbarProps,
location: XAndY,
offset: XAndY,
onItemExecuted: (item: any) => void,
onCancel: () => void,
placement: Placement = "top-end",
anchorElement?: HTMLElement
): boolean {
const anchor = this.resolveHtmlElement(anchorElement);
return PopupManager.displayCard(content, {
title,
toolbarProps,
location,
offset,
onItemExecuted,
onCancel,
placement,
anchor,
});
}
/**
* Hides a Card displayed with {@link UiFramework.showCard}
* @internal
*/
public static hideCard() {
return PopupManager.hideCard();
}
/** Opens a Tool Settings Ui popup at a particular location.
* @param dataProvider The DialogLayoutDataProvider for the tool settings popup dialog.
* @param location Location of the tool settings, relative to the origin of anchorElement or the window
* @param offset Offset of the tool settings from the location
* @param onCancel Function invoked when the Escape key is pressed or a click occurs outside the tool settings
* @param placement {@link Placement} relative to the given location. Defaults to top-end.
* @param anchorElement The HTMLElement that anchors the tool settings. If undefined, the location is relative to the overall window.
* @return true if the tool settings were displayed, false if the tool settings could not be displayed.
* @internal
*/
public static openToolSettingsPopup(
dataProvider: DialogLayoutDataProvider,
location: XAndY,
offset: XAndY,
onCancel: () => void,
placement: Placement = "top-end",