Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Significantly reduce scene tree remounts, display improvements #328

Merged
merged 4 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/viser/client/src/ControlPanel/SceneTreeTable.css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,36 @@ export const tableRow = style({
padding: "0 0.25em",
lineHeight: "2em",
fontSize: "0.875em",
":hover": {
[vars.lightSelector]: {
backgroundColor: vars.colors.gray[1],
},
[vars.darkSelector]: {
backgroundColor: vars.colors.dark[6],
},
},
});

export const tableHierarchyLine = style({
[vars.lightSelector]: {
borderColor: vars.colors.gray[2],
},
[vars.darkSelector]: {
borderColor: vars.colors.dark[5],
},
borderLeft: "0.3em solid",
width: "0.2em",
marginLeft: "0.375em",
height: "2em",
});

globalStyle(`${tableRow}:hover ${tableHierarchyLine}`, {
[vars.lightSelector]: {
borderColor: vars.colors.gray[3],
},
[vars.darkSelector]: {
borderColor: vars.colors.dark[4],
},
});

globalStyle(`${tableRow}:hover ${editIconWrapper}`, {
Expand Down
64 changes: 26 additions & 38 deletions src/viser/client/src/ControlPanel/SceneTreeTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import React from "react";
import {
editIconWrapper,
propsWrapper,
tableHierarchyLine,
tableRow,
tableWrapper,
} from "./SceneTreeTable.css";
Expand All @@ -24,8 +25,6 @@ import {
TextInput,
Tooltip,
ColorInput,
useMantineColorScheme,
useMantineTheme,
} from "@mantine/core";

function EditNodeProps({
Expand Down Expand Up @@ -297,43 +296,42 @@ const SceneTreeTableRow = React.memo(function SceneTreeTableRow(props: {

const [expanded, { toggle: toggleExpanded }] = useDisclosure(false);

function setOverrideVisibility(name: string, visible: boolean | undefined) {
const attr = viewer.nodeAttributesFromName.current;
attr[name]!.overrideVisibility = visible;
rerenderTable();
}
const setLabelVisibility = viewer.useSceneTree(
(state) => state.setLabelVisibility,
);

// For performance, scene node visibility is stored in a ref instead of the
// zustand state. This means that re-renders for the table need to be
// triggered manually when visibilities are updated.
const [, setTime] = React.useState(Date.now());
function rerenderTable() {
setTime(Date.now());
}
const pollIsVisible = React.useCallback(() => {
const attrs = viewer.nodeAttributesFromName.current[props.nodeName];
return (
(attrs?.overrideVisibility === undefined
? attrs?.visibility
: attrs.overrideVisibility) ?? true
);
}, [props.nodeName]);

const [isVisible, setIsVisible] = React.useState(pollIsVisible());
React.useEffect(() => {
const interval = setInterval(rerenderTable, 200);
// We put the visibility in a ref, so it needs to be polled. This was for
// performance reasons, but we should probably move it into the zustand
// store and just be careful to avoid subscribing to it from the r3f
// components.
const interval = setInterval(() => {
const visible = pollIsVisible();
if (visible !== isVisible) {
setIsVisible(visible);
}
}, 200);
return () => {
clearInterval(interval);
};
}, []);
}, [isVisible]);

const attrs = viewer.nodeAttributesFromName.current[props.nodeName];
const isVisible =
(attrs?.overrideVisibility === undefined
? attrs?.visibility
: attrs.overrideVisibility) ?? true;
const isVisibleEffective = isVisible && props.isParentVisible;
const VisibleIcon = isVisible ? IconEye : IconEyeOff;

const [propsPanelOpened, { open: openPropsPanel, close: closePropsPanel }] =
useDisclosure(false);

const colorScheme = useMantineColorScheme();
const theme = useMantineTheme();

return (
<>
<Box
Expand All @@ -346,19 +344,7 @@ const SceneTreeTableRow = React.memo(function SceneTreeTableRow(props: {
onMouseOut={() => setLabelVisibility(props.nodeName, false)}
>
{new Array(props.indentCount).fill(null).map((_, i) => (
<Box
key={i}
style={{
borderLeft: "0.3em solid",
borderColor:
colorScheme.colorScheme == "dark"
? theme.colors.gray[7]
: theme.colors.gray[2],
width: "0.2em",
marginLeft: "0.375em",
height: "2em",
}}
/>
<Box className={tableHierarchyLine} key={i} />
))}
<Box
style={{
Expand Down Expand Up @@ -395,7 +381,9 @@ const SceneTreeTableRow = React.memo(function SceneTreeTableRow(props: {
}}
onClick={(evt) => {
evt.stopPropagation();
setOverrideVisibility(props.nodeName, !isVisible);
const attr = viewer.nodeAttributesFromName.current;
attr[props.nodeName]!.overrideVisibility = !isVisible;
setIsVisible(!isVisible);
}}
/>
</Tooltip>
Expand Down
Loading