mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-20 17:11:17 -08:00
perf(mobile): smoother room-open swipe reveal, drawer glitch-proofing, coalesced system-bar sampling
This commit is contained in:
2 files changed
+113
-24
No files matched your search
@@ -45,6 +45,7 @@ function useBarColor(
|
||||
if (!enabled) return undefined;
|
||||
|
||||
let frame = 0;
|
||||
let timer = 0;
|
||||
const sample = () => {
|
||||
frame = 0;
|
||||
const rect = probeRef.current?.getBoundingClientRect();
|
||||
@@ -65,13 +66,16 @@ function useBarColor(
|
||||
}
|
||||
}
|
||||
};
|
||||
// Trailing edge: sample once mutations settle, so navigation recolors land.
|
||||
const schedule = () => {
|
||||
const runSample = () => {
|
||||
if (frame) cancelAnimationFrame(frame);
|
||||
frame = requestAnimationFrame(sample);
|
||||
};
|
||||
const schedule = () => {
|
||||
window.clearTimeout(timer);
|
||||
timer = window.setTimeout(runSample, 120);
|
||||
};
|
||||
|
||||
schedule();
|
||||
runSample();
|
||||
const observer = new MutationObserver(schedule);
|
||||
// childList = navigation; class/style = theme swaps and per-view recolors.
|
||||
observer.observe(document.body, {
|
||||
@@ -84,6 +88,7 @@ function useBarColor(
|
||||
window.addEventListener('resize', schedule);
|
||||
|
||||
return () => {
|
||||
window.clearTimeout(timer);
|
||||
if (frame) cancelAnimationFrame(frame);
|
||||
observer.disconnect();
|
||||
window.removeEventListener('resize', schedule);
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import type { ReactNode } from 'react';
|
||||
import { useCallback, useLayoutEffect, useRef, useState } from 'react';
|
||||
import { startTransition, useCallback, useEffect, useLayoutEffect, useRef, useState } from 'react';
|
||||
import { motion, useMotionValue, useReducedMotion } from 'framer-motion';
|
||||
import { useDrag } from '@use-gesture/react';
|
||||
import { useAtomValue, useSetAtom } from 'jotai';
|
||||
import { matchPath, useLocation, useNavigate } from 'react-router-dom';
|
||||
import { useMatrixClient } from '$hooks/useMatrixClient';
|
||||
import { useSetting } from '$state/hooks/settings';
|
||||
import { settingsAtom } from '$state/settings';
|
||||
import { lastVisitedRoomAtom } from '$state/room/lastRoom';
|
||||
@@ -40,6 +41,7 @@ const clamp = (value: number, min: number, max: number): number =>
|
||||
export function MobileNavDrawer({ nav, rail, bottomNav, children }: MobileNavDrawerProps) {
|
||||
const [mobileGestures] = useSetting(settingsAtom, 'mobileGestures');
|
||||
const reduceMotion = useReducedMotion();
|
||||
const matrixClient = useMatrixClient();
|
||||
const location = useLocation();
|
||||
const navigate = useNavigate();
|
||||
const setLastRoom = useSetAtom(lastVisitedRoomAtom);
|
||||
@@ -66,15 +68,15 @@ export function MobileNavDrawer({ nav, rail, bottomNav, children }: MobileNavDra
|
||||
const [width, setWidth] = useState(0);
|
||||
const x = useMotionValue(0);
|
||||
const draggingRef = useRef(false);
|
||||
const openingRef = useRef(false);
|
||||
const prevContentOpenRef = useRef(contentOpen);
|
||||
const openRafRef = useRef(0);
|
||||
const [deferContent, setDeferContent] = useState(false);
|
||||
const [prevOpen, setPrevOpen] = useState(contentOpen);
|
||||
|
||||
// Detect the open transition during render so the room isn't rendered in the slide's commit.
|
||||
if (contentOpen !== prevOpen) {
|
||||
setPrevOpen(contentOpen);
|
||||
setDeferContent(contentOpen && !reduceMotion);
|
||||
setDeferContent(contentOpen && !reduceMotion && !draggingRef.current);
|
||||
}
|
||||
|
||||
// Compositor transition for the settle animation; off = finger-1:1 during drag.
|
||||
@@ -136,9 +138,9 @@ export function MobileNavDrawer({ nav, rail, bottomNav, children }: MobileNavDra
|
||||
}
|
||||
}, [contentOpen, width, x, settle, applyTransition, reduceMotion]);
|
||||
|
||||
const goToList = useCallback(() => {
|
||||
const goToList = useCallback((): boolean => {
|
||||
const section = resolveSection(location.pathname);
|
||||
if (!section) return;
|
||||
if (!section) return false;
|
||||
|
||||
const id = roomMatch?.params.roomIdOrAlias;
|
||||
if (section.getRoomPath && id) {
|
||||
@@ -146,17 +148,59 @@ export function MobileNavDrawer({ nav, rail, bottomNav, children }: MobileNavDra
|
||||
}
|
||||
|
||||
navigate(section.listPath);
|
||||
return true;
|
||||
}, [roomMatch, location.pathname, navigate, setLastRoom]);
|
||||
|
||||
const goToRoom = useCallback(() => {
|
||||
const goToRoom = useCallback((): boolean => {
|
||||
const section = resolveSection(location.pathname);
|
||||
if (!section?.getRoomPath) return;
|
||||
if (!section?.getRoomPath) return false;
|
||||
// Scope the remembered room to its section so a DM never opens under /home/, etc.
|
||||
if (!lastRoom || lastRoom.section !== section.key) return;
|
||||
if (!lastRoom || lastRoom.section !== section.key) return false;
|
||||
|
||||
navigate(section.getRoomPath(lastRoom.roomId));
|
||||
startTransition(() => navigate(section.getRoomPath!(lastRoom.roomId)));
|
||||
return true;
|
||||
}, [lastRoom, location.pathname, navigate]);
|
||||
|
||||
const openableSection = resolveSection(location.pathname);
|
||||
const canOpenRoom =
|
||||
!!openableSection?.getRoomPath && !!lastRoom && lastRoom.section === openableSection.key;
|
||||
|
||||
useEffect(() => {
|
||||
if (contentOpen || !canOpenRoom || !lastRoom) return undefined;
|
||||
const room = matrixClient.getRoom(lastRoom.roomId);
|
||||
if (!room) return undefined;
|
||||
const ric =
|
||||
window.requestIdleCallback ??
|
||||
((cb: IdleRequestCallback) =>
|
||||
window.setTimeout(
|
||||
() => cb({ didTimeout: false, timeRemaining: () => 30 } as IdleDeadline),
|
||||
200
|
||||
));
|
||||
const cic = window.cancelIdleCallback ?? window.clearTimeout;
|
||||
const handle = ric(() => {
|
||||
room
|
||||
.getLiveTimeline()
|
||||
.getEvents()
|
||||
.slice(-20)
|
||||
.forEach((event) => {
|
||||
if (event.isEncrypted()) matrixClient.decryptEventIfNeeded(event).catch(() => undefined);
|
||||
});
|
||||
});
|
||||
return () => cic(handle);
|
||||
}, [contentOpen, canOpenRoom, lastRoom, matrixClient]);
|
||||
|
||||
const readX = useCallback((): number => {
|
||||
const el = sliderRef.current;
|
||||
if (!el) return x.get();
|
||||
const t = getComputedStyle(el).transform;
|
||||
if (!t || t === 'none') return 0;
|
||||
try {
|
||||
return new DOMMatrix(t).m41;
|
||||
} catch {
|
||||
return x.get();
|
||||
}
|
||||
}, [x]);
|
||||
|
||||
const bind = useDrag(
|
||||
({
|
||||
first,
|
||||
@@ -169,7 +213,15 @@ export function MobileNavDrawer({ nav, rail, bottomNav, children }: MobileNavDra
|
||||
event,
|
||||
cancel,
|
||||
}) => {
|
||||
if (canceled || !mobileGestures || width === 0) return;
|
||||
if (canceled) {
|
||||
if (draggingRef.current) {
|
||||
draggingRef.current = false;
|
||||
openingRef.current = false;
|
||||
settle(contentOpen ? -width : 0);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (!mobileGestures || width === 0) return;
|
||||
|
||||
const target = event?.target;
|
||||
if (target instanceof HTMLElement && target.closest('[data-gestures="ignore"]')) {
|
||||
@@ -178,8 +230,7 @@ export function MobileNavDrawer({ nav, rail, bottomNav, children }: MobileNavDra
|
||||
}
|
||||
|
||||
if (contentOpen) {
|
||||
// Drawer owns the rightward reveal; yield leftward drags to in-room actions.
|
||||
if (mx < -DIRECTION_DEADZONE) {
|
||||
if (mx < -DIRECTION_DEADZONE && !openingRef.current) {
|
||||
if (draggingRef.current) {
|
||||
draggingRef.current = false;
|
||||
settle(-width);
|
||||
@@ -198,14 +249,31 @@ export function MobileNavDrawer({ nav, rail, bottomNav, children }: MobileNavDra
|
||||
return;
|
||||
}
|
||||
draggingRef.current = false;
|
||||
if (openingRef.current) {
|
||||
openingRef.current = false;
|
||||
const keepRoom = -ox > width * OPEN_FRACTION || (vx > VELOCITY_THRESHOLD && dx < 0);
|
||||
if (keepRoom) {
|
||||
settle(-width);
|
||||
} else {
|
||||
goToList();
|
||||
settle(0);
|
||||
}
|
||||
return;
|
||||
}
|
||||
const opened = width + ox > width * OPEN_FRACTION || (vx > VELOCITY_THRESHOLD && dx > 0);
|
||||
settle(opened ? 0 : -width);
|
||||
if (opened) goToList();
|
||||
if (opened && goToList()) {
|
||||
settle(0);
|
||||
} else {
|
||||
settle(-width);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// On the list: dragging left follows the finger to reveal the room; release commits.
|
||||
if (mx > DIRECTION_DEADZONE) {
|
||||
if (mx > DIRECTION_DEADZONE || !canOpenRoom) {
|
||||
if (draggingRef.current) {
|
||||
draggingRef.current = false;
|
||||
settle(0);
|
||||
}
|
||||
cancel();
|
||||
return;
|
||||
}
|
||||
@@ -216,12 +284,21 @@ export function MobileNavDrawer({ nav, rail, bottomNav, children }: MobileNavDra
|
||||
}
|
||||
draggingRef.current = true;
|
||||
x.set(clamp(ox, -width, 0));
|
||||
if (!openingRef.current && -ox > width * 0.12 && goToRoom()) {
|
||||
openingRef.current = true;
|
||||
}
|
||||
return;
|
||||
}
|
||||
draggingRef.current = false;
|
||||
const wantRoom = -ox > width * OPEN_FRACTION || (vx > VELOCITY_THRESHOLD && dx < 0);
|
||||
if (wantRoom) {
|
||||
goToRoom();
|
||||
if (openingRef.current) {
|
||||
openingRef.current = false;
|
||||
const keep = -ox > width * OPEN_FRACTION || (vx > VELOCITY_THRESHOLD && dx < 0);
|
||||
if (keep) {
|
||||
settle(-width);
|
||||
} else {
|
||||
goToList();
|
||||
settle(0);
|
||||
}
|
||||
} else {
|
||||
settle(0);
|
||||
}
|
||||
@@ -231,7 +308,7 @@ export function MobileNavDrawer({ nav, rail, bottomNav, children }: MobileNavDra
|
||||
axis: 'x',
|
||||
filterTaps: true,
|
||||
pointer: { capture: false },
|
||||
from: () => [x.get(), 0],
|
||||
from: () => [readX(), 0],
|
||||
}
|
||||
);
|
||||
|
||||
@@ -276,7 +353,14 @@ export function MobileNavDrawer({ nav, rail, bottomNav, children }: MobileNavDra
|
||||
{rail && (
|
||||
<div style={{ flexShrink: 0, display: 'flex', overflow: 'hidden' }}>{rail}</div>
|
||||
)}
|
||||
<div style={{ flexGrow: 1, minWidth: 0, display: 'flex', overflow: 'hidden' }}>
|
||||
<div
|
||||
style={{
|
||||
flexGrow: 1,
|
||||
minWidth: 0,
|
||||
display: 'flex',
|
||||
overflow: 'hidden',
|
||||
}}
|
||||
>
|
||||
{nav}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in new issue
Block a user