-
Notifications
You must be signed in to change notification settings - Fork 2
/
ToolSettings.tsx
204 lines (187 loc) · 6.78 KB
/
ToolSettings.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module ToolSettings
*/
import { IModelApp } from "@itwin/core-frontend";
import * as React from "react";
import { UiFramework } from "../UiFramework";
import { InternalFrontstageManager } from "../frontstage/InternalFrontstageManager";
import { useLayout } from "../layout/base/LayoutStore";
import { DockedToolSettings } from "../layout/tool-settings/Docked";
import { DockedToolSetting } from "../layout/tool-settings/Setting";
import { ScrollableWidgetContent } from "../layout/widget/Content";
import "./ToolSettings.scss";
import { useActiveToolId } from "../hooks/useActiveToolId";
import { useTranslation } from "../hooks/useTranslation";
import { Text } from "@itwin/itwinui-react";
import { DockedBar } from "./DockedBar";
/** Defines a ToolSettings property entry.
* @public
*/
export interface ToolSettingsEntry {
// label node which potentially can contain a lock node as well.
labelNode: React.ReactNode;
// editor entry used to display and edit the property value
editorNode: React.ReactNode;
}
function EmptyToolSettingsLabel({ toolId }: { toolId: string }) {
const { translate } = useTranslation();
const tool = IModelApp.tools.find(toolId);
const toolName = tool?.flyover;
return (
<Text as="i" isMuted={true} className="uifw-toolSettings-label-empty">
{translate("tools.noToolSettingsStart")}
{toolName || translate("tools.noToolSettingsPlaceholderName")}
{translate("tools.noToolSettingsEnd")}
</Text>
);
}
/**
* Hook that returns true if the tool settings should be rendered docked.
* @internal */
export function useShouldRenderDockedToolSettings() {
return useLayout((state) => {
const toolSettings = state.toolSettings;
if (!toolSettings || toolSettings.type === "widget") return false;
return !toolSettings.hidden;
});
}
/** @internal */
export function WidgetPanelsToolSettings() {
if (!useShouldRenderDockedToolSettings()) return null;
return <ToolSettingsDockedContent />;
}
/** @internal */
export function ToolSettingsDockedContent() {
const activeToolId = useActiveToolId();
const toolSettingEntries = useHorizontalToolSettingEntries();
const forceRefreshKey = useRefreshKey(toolSettingEntries);
const emptySettings = React.useMemo<ToolSettingsEntry[]>(
() => [
{
editorNode: null,
labelNode: <EmptyToolSettingsLabel toolId={activeToolId} />,
},
],
[activeToolId]
);
const entries =
!toolSettingEntries || toolSettingEntries.length === 0
? emptySettings
: toolSettingEntries;
// for the overflow to work properly each setting in the DockedToolSettings should be wrapped by a DockedToolSetting component
return (
<DockedBar placement="top">
<DockedToolSettings
itemId={
InternalFrontstageManager.activeToolSettingsProvider?.uniqueId ??
"none"
}
key={forceRefreshKey}
>
{entries.map((entry, index) => (
<DockedToolSetting key={index}>
{entry.labelNode}
{entry.editorNode}
</DockedToolSetting>
))}
</DockedToolSettings>
</DockedBar>
);
}
/** @internal */
export function useHorizontalToolSettingEntries() {
React.useEffect(() => {
UiFramework.frontstages.activeToolInformation?.toolUiProvider?.reloadPropertiesFromTool();
}, []);
const [settings, setSettings] = React.useState(
InternalFrontstageManager.activeToolSettingsProvider
?.horizontalToolSettingNodes
);
React.useEffect(() => {
return UiFramework.frontstages.onToolActivatedEvent.addListener(() => {
const nodes =
InternalFrontstageManager.activeToolSettingsProvider
?.horizontalToolSettingNodes;
setSettings(nodes);
});
}, []);
React.useEffect(() => {
return UiFramework.frontstages.onToolSettingsReloadEvent.addListener(() => {
const nodes =
InternalFrontstageManager.activeToolSettingsProvider
?.horizontalToolSettingNodes;
setSettings(nodes);
});
}, []);
return settings;
}
/** @internal */
export function useToolSettingsNode() {
React.useEffect(() => {
UiFramework.frontstages.activeToolInformation?.toolUiProvider?.reloadPropertiesFromTool();
}, []);
const [settings, setSettings] = React.useState(
InternalFrontstageManager.activeToolSettingsProvider?.toolSettingsNode
);
React.useEffect(() => {
return UiFramework.frontstages.onToolActivatedEvent.addListener(() => {
const nodes =
InternalFrontstageManager.activeToolSettingsProvider?.toolSettingsNode;
setSettings(nodes);
});
}, [setSettings]);
React.useEffect(() => {
return UiFramework.frontstages.onToolSettingsReloadEvent.addListener(() => {
const nodes =
InternalFrontstageManager.activeToolSettingsProvider?.toolSettingsNode;
setSettings(nodes);
});
}, [setSettings]);
return settings;
}
/** @internal */
export function ToolSettingsContent() {
const toolSettingsType = useLayout((state) => state.toolSettings?.type);
// This is needed to remount underlying components tree when going into widget state.
if (!toolSettingsType || toolSettingsType === "docked") return null;
return <ToolSettingsWidgetContent />;
}
/** @internal */
export function ToolSettingsWidgetContent() {
const floatingToolSettingsContainerRef = React.useRef<HTMLDivElement>(null);
const node = useToolSettingsNode();
const activeToolId = useActiveToolId();
const forceRefreshKey = useRefreshKey(node);
const providerId =
InternalFrontstageManager.activeToolSettingsProvider?.uniqueId ?? "none";
return (
<div
data-toolsettings-provider={providerId}
className="uifw-floating-toolSettings-container"
ref={floatingToolSettingsContainerRef}
key={forceRefreshKey}
>
<ScrollableWidgetContent>
{node ?? <EmptyToolSettingsLabel toolId={activeToolId} />}
</ScrollableWidgetContent>
</div>
);
}
/**
* Hook that returns a key that can be used to force refresh the tool settings.
* @param toolSettingNodes Nodes that are used to determine if the tool settings should be refreshed.
* @returns a new key if the dependency changes.
*/
function useRefreshKey(toolSettingNodes: any) {
const [forceRefreshKey, setForceRefreshKey] = React.useState(Date.now());
React.useEffect(() => {
// We cant work with the content of the settings, but we can force refresh when
// the array is different.
setForceRefreshKey(Date.now());
}, [toolSettingNodes]);
return forceRefreshKey;
}