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

manchette space time zoom #764

Merged
merged 3 commits into from
Jan 14, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ type State = {
xZoom: number;
yZoom: number;
xOffset: number;
/** the current y-scroll of the view. always updates */
yOffset: number;
/** only update after a zoom. used to update back the view scroll value */
scrollTo: number | null;
panning: { initialOffset: { x: number; y: number } } | null;
scrollPosition: number;
isProportional: boolean;
waypointsChart: Waypoint[];
scales: SpaceScale[];
Expand All @@ -51,14 +53,14 @@ const useManchettesWithSpaceTimeChart = (
yZoom: 1,
xOffset: 0,
yOffset: 0,
scrollTo: null,
panning: null,
scrollPosition: 0,
isProportional: true,
waypointsChart: [],
scales: [],
});

const { xZoom, yZoom, xOffset, yOffset, panning, scrollPosition, isProportional } = state;
const { xZoom, yZoom, xOffset, yOffset, scrollTo, panning, isProportional } = state;

const paths = usePaths(projectPathTrainResult, selectedTrain);

Expand All @@ -78,15 +80,39 @@ const useManchettesWithSpaceTimeChart = (

const zoomYIn = useCallback(() => {
if (yZoom < MAX_ZOOM_Y) {
setState((prev) => ({ ...prev, yZoom: yZoom + ZOOM_Y_DELTA }));
const newYZoom = yZoom + ZOOM_Y_DELTA;
const newYOffset = yOffset * (newYZoom / yZoom);

setState((prev) => ({
...prev,
yZoom: newYZoom,
yOffset: newYOffset,
scrollTo: newYOffset,
}));
}
}, [yZoom]);
}, [yZoom, yOffset]);

const zoomYOut = useCallback(() => {
if (yZoom > MIN_ZOOM_Y) {
setState((prev) => ({ ...prev, yZoom: yZoom - ZOOM_Y_DELTA }));
const newYZoom = yZoom - ZOOM_Y_DELTA;
const newYOffset = yOffset * (newYZoom / yZoom);
setState((prev) => ({
...prev,
yZoom: newYZoom,
yOffset: newYOffset,
scrollTo: newYOffset,
}));
}
}, [yZoom, yOffset]);
Yohh marked this conversation as resolved.
Show resolved Hide resolved

useEffect(() => {
if (scrollTo !== null && manchetteWithSpaceTimeChartContainer.current) {
manchetteWithSpaceTimeChartContainer.current.scrollTo({
top: scrollTo,
behavior: 'instant',
});
}
}, [yZoom]);
}, [scrollTo, manchetteWithSpaceTimeChartContainer]);

const resetZoom = useCallback(() => {
setState((prev) => ({ ...prev, yZoom: 1 }));
Expand All @@ -96,7 +122,7 @@ const useManchettesWithSpaceTimeChart = (
if (!isShiftPressed && manchetteWithSpaceTimeChartContainer.current) {
const { scrollTop } = manchetteWithSpaceTimeChartContainer.current;
if (scrollTop || scrollTop === 0) {
setState((prev) => ({ ...prev, scrollPosition: scrollTop, yOffset: scrollTop }));
setState((prev) => ({ ...prev, yOffset: scrollTop }));
}
}
}, [isShiftPressed, manchetteWithSpaceTimeChartContainer]);
Expand Down Expand Up @@ -141,8 +167,9 @@ const useManchettesWithSpaceTimeChart = (
toggleMode,
yZoom,
isProportional,
yOffset,
}),
[waypointWithHeight, zoomYIn, zoomYOut, resetZoom, toggleMode, yZoom, isProportional]
[waypointWithHeight, zoomYIn, zoomYOut, resetZoom, toggleMode, yZoom, isProportional, yOffset]
);

const handleXZoom = useCallback(
Expand All @@ -162,7 +189,7 @@ const useManchettesWithSpaceTimeChart = (
timeScale: zoomValueToTimeScale(xZoom),
paths,
xOffset,
yOffset: -scrollPosition + 14,
yOffset: -yOffset + 14,
onZoom: ({ delta, position }: Parameters<NonNullable<SpaceTimeChartProps['onZoom']>>[0]) => {
if (isShiftPressed) {
handleXZoom(xZoom + delta, position.x);
Expand Down Expand Up @@ -192,7 +219,6 @@ const useManchettesWithSpaceTimeChart = (
manchetteWithSpaceTimeChartContainer.current.scrollHeight
) {
newState.yOffset = newYPos;
newState.scrollPosition = newYPos;
manchetteWithSpaceTimeChartContainer.current.scrollTop = newYPos;
}
}
Expand All @@ -205,7 +231,6 @@ const useManchettesWithSpaceTimeChart = (
xZoom,
paths,
xOffset,
scrollPosition,
isShiftPressed,
state,
panning,
Expand All @@ -221,9 +246,8 @@ const useManchettesWithSpaceTimeChart = (
spaceTimeChartProps,
handleScroll,
handleXZoom,
xZoom,
}),
[manchetteProps, spaceTimeChartProps, handleScroll, handleXZoom, xZoom]
[manchetteProps, spaceTimeChartProps, handleScroll, handleXZoom]
);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
.space-time-chart-container {
height: 561px;
width: 100%;
padding-top: 10px;
bottom: 0;
}
}
Loading