Skip to content

Commit 08ed829

Browse files
authored
Test viewer: Add selection scopes picker (#1140)
1 parent d8b0b84 commit 08ed829

File tree

1 file changed

+67
-1
lines changed

1 file changed

+67
-1
lines changed

apps/test-viewer/src/components/ViewerOptions.tsx

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
import { createContext, useContext, useState } from "react";
77
import { StatusBarSection } from "@itwin/appui-react";
88
import { SvgSelection, SvgVisibilityShow } from "@itwin/itwinui-icons-react";
9-
import { IconButton } from "@itwin/itwinui-react";
9+
import { IconButton, Select } from "@itwin/itwinui-react";
10+
import { Presentation } from "@itwin/presentation-frontend";
1011

12+
import type { SelectionScopesManager } from "@itwin/presentation-frontend";
1113
import type { PropsWithChildren } from "react";
1214
import type { UiItemsProvider } from "@itwin/appui-react";
1315

@@ -57,6 +59,12 @@ export const statusBarActionsProvider: UiItemsProvider = {
5759
itemPriority: 2,
5860
section: StatusBarSection.Left,
5961
},
62+
{
63+
id: `selectionScopeSelectorButton`,
64+
content: <SelectionScopeSelectorButton />,
65+
itemPriority: 3,
66+
section: StatusBarSection.Left,
67+
},
6068
],
6169
};
6270

@@ -77,3 +85,61 @@ function ToggleTreeNodesSelectionButton() {
7785
</IconButton>
7886
);
7987
}
88+
89+
function SelectionScopeSelectorButton() {
90+
const [activeScope, setActiveScope] = useState(getScopeId(Presentation.selection.scopes.activeScope));
91+
return (
92+
<Select
93+
options={Object.entries(scopes).map(([key, { label }]) => ({
94+
value: key,
95+
label,
96+
}))}
97+
value={activeScope}
98+
onChange={(value) => {
99+
const scopeId = value as keyof typeof scopes;
100+
setActiveScope(scopeId);
101+
// TODO: Viewer should allow specifying the active selection scope without using `Presentation.selection`
102+
Presentation.selection.scopes.activeScope = scopes[scopeId].props;
103+
}}
104+
/>
105+
);
106+
}
107+
function getScopeId(scopeArg: SelectionScopesManager["activeScope"]): keyof typeof scopes {
108+
if (!scopeArg) {
109+
return "element";
110+
}
111+
if (typeof scopeArg === "string") {
112+
return scopeArg as keyof typeof scopes;
113+
}
114+
if (scopeArg.id === "element" && "ancestorLevel" in scopeArg) {
115+
if (scopeArg.ancestorLevel === 1) {
116+
return "assembly";
117+
}
118+
if (scopeArg.ancestorLevel === 2) {
119+
return "top-assembly";
120+
}
121+
}
122+
return scopeArg.id as keyof typeof scopes;
123+
}
124+
const scopes = {
125+
element: {
126+
props: { id: "element" },
127+
label: "Element",
128+
},
129+
assembly: {
130+
props: { id: "element", ancestorLevel: 1 },
131+
label: "Assembly",
132+
},
133+
"top-assembly": {
134+
props: { id: "element", ancestorLevel: 2 },
135+
label: "Top assembly",
136+
},
137+
model: {
138+
props: { id: "model" },
139+
label: "Model",
140+
},
141+
category: {
142+
props: { id: "category" },
143+
label: "Category",
144+
},
145+
};

0 commit comments

Comments
 (0)