Skip to content

Commit 3da552c

Browse files
committed
fix: restore scroll and focus when returning from folder references
1 parent 0ddcc64 commit 3da552c

7 files changed

Lines changed: 74 additions & 25 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Get-FileHash -LiteralPath '.\Remova_1.5.2_x64-setup.exe' -Algorithm SHA256
2121
### 新增
2222

2323
- 软件页可直接打开磁盘雷达。磁盘目录可反查安装登记、桌面与开始菜单快捷方式及服务路径,并查看已识别的软件详情。拖入未匹配的文件夹也能开始反查。
24-
- 文件夹反查取消后会列出已查完的来源。从关联的软件详情返回时,键盘焦点回到刚才打开的证据。
24+
- 文件夹反查取消后会列出已查完的来源。从软件详情返回反查,或从反查返回磁盘雷达时,会恢复之前的焦点和滚动位置。
2525
- 文件夹反查会向读屏软件播报未发现引用、部分来源未查完和扫描取消的状态。
2626
- 软件详情分开显示登记大小和已测得的安装目录大小,并列提供关联扫描和卸载。清理预览入口移到已选摘要旁,勾选变化时会提示重新检查。
2727
- 软件详情可展开查看磁盘测量范围,并把应优先核对的关联项和判断依据放在扫描结果上方。

‎src/__tests__/directoryWorkspace.test.tsx‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ afterEach(() => {
4444
vi.clearAllMocks();
4545
});
4646

47-
it("keeps the inspected references and disk radar when returning from app details", async () => {
47+
it("restores the reference trigger and the original radar focus and scroll position", async () => {
4848
const L = t();
4949
vi.mocked(api.listLocalDrives).mockResolvedValue([{ letter: "D", is_system: true, total_gb: 10, free_gb: 2 }]);
5050
vi.mocked(api.listTopDirSizes).mockResolvedValue([{
@@ -59,22 +59,36 @@ it("keeps the inspected references and disk radar when returning from app detail
5959
omitted: 0,
6060
});
6161
render(<Fixture />);
62-
fireEvent.click(await screen.findByRole("button", { name: L.folderInspectAction }));
62+
const radarAction = await screen.findByRole("button", { name: L.folderInspectAction });
63+
const radarDialog = screen.getByRole("dialog", { name: L.diskRadarShortcut });
64+
radarDialog.scrollTo = (options?: ScrollToOptions | number, y?: number) => {
65+
radarDialog.scrollTop = (typeof options === "object" ? options.top : y) ?? 0;
66+
};
67+
radarAction.focus();
68+
radarDialog.scrollTop = 240;
69+
fireEvent.click(radarAction);
6370
const inspected = await screen.findByRole("dialog", { name: L.folderInspectTitle });
71+
inspected.scrollTop = 0;
6472
expect(await within(inspected).findByText(L.folderInspectPartial(L.folderInspectShortcut))).toBeTruthy();
6573
expect(within(inspected).getByText("D:\\Apps\\Editor\\updater.exe")).toBeTruthy();
6674
expect(api.inspectFolderReferences).toHaveBeenCalledWith(app.install_location, 4);
6775
const openDetail = within(inspected).getByRole("button", { name: L.folderInspectShowApp });
76+
openDetail.focus();
77+
inspected.scrollTop = 135;
6878
fireEvent.click(openDetail);
6979
const detail = screen.getByRole("dialog", { name: L.folderInspectShowApp });
80+
expect(detail.scrollTop).toBe(0);
7081
expect(within(detail).getByRole("button", { name: L.detailInspect })).toBeTruthy();
7182
expect(within(detail).getByRole("button", { name: L.detailUninstall })).toBeTruthy();
7283
fireEvent.click(within(detail).getByRole("button", { name: L.folderInspectBack }));
7384
expect(document.activeElement).toBe(openDetail);
85+
expect(inspected.scrollTop).toBe(135);
7486
expect(screen.getByText("D:\\Apps\\Editor\\updater.exe")).toBeTruthy();
7587
expect(api.inspectFolderReferences).toHaveBeenCalledTimes(1);
7688
fireEvent.click(screen.getByRole("button", { name: L.folderInspectRadar }));
77-
expect(screen.getByRole("button", { name: L.folderInspectAction })).toBeTruthy();
89+
expect(screen.getByRole("button", { name: L.folderInspectAction })).toBe(radarAction);
90+
expect(document.activeElement).toBe(radarAction);
91+
expect(radarDialog.scrollTop).toBe(240);
7892
expect(api.listTopDirSizes).toHaveBeenCalledTimes(1);
7993
});
8094

‎src/__tests__/diskRadarComparison.test.tsx‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ it("can save, reopen, inspect a growing directory and clear the saved measuremen
3636
fireEvent.click(screen.getByText(L.diskRadarCompareDetails(2)));
3737
expect(screen.getByText("+80 KB")).toBeTruthy();
3838
expect(screen.getByText("−25 KB")).toBeTruthy();
39-
fireEvent.click(within(screen.getByRole("region", { name: L.diskRadarCompareTitle })).getByRole("button", { name: L.folderInspectAction }));
40-
expect(inspect).toHaveBeenCalledWith(growing);
39+
const inspectGrowth = within(screen.getByRole("region", { name: L.diskRadarCompareTitle })).getByRole("button", { name: L.folderInspectAction });
40+
fireEvent.click(inspectGrowth);
41+
expect(inspect).toHaveBeenCalledWith(growing, inspectGrowth);
4142
fireEvent.click(screen.getByRole("button", { name: L.diskRadarClearSnapshot }));
4243
expect(screen.getByRole("button", { name: L.diskRadarSaveSnapshot })).toBeTruthy();
4344
});

‎src/components/DirectoryWorkspace.css‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
padding: 20px;
88
}
99
.directory-dialog {
10+
--directory-padding: 16px;
11+
position: relative;
1012
box-sizing: border-box;
1113
width: min(850px, 100%);
1214
max-height: 100%;
@@ -15,7 +17,17 @@
1517
border: 1px solid var(--border-strong);
1618
border-radius: 12px;
1719
box-shadow: var(--shadow-float);
18-
padding: 16px;
20+
padding: var(--directory-padding);
21+
}
22+
/* Preserve the virtual list's viewport measurements while the radar is covered. */
23+
.directory-radar-view[hidden] {
24+
display: block;
25+
position: absolute;
26+
top: 0;
27+
inset-inline: var(--directory-padding);
28+
height: 0;
29+
overflow: clip;
30+
visibility: hidden;
1931
}
2032
.directory-heading {
2133
display: flex;

‎src/components/DirectoryWorkspace.tsx‎

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useCallback, useEffect, useId, useRef, useState } from "react";
1+
import { useCallback, useEffect, useId, useLayoutEffect, useRef, useState } from "react";
22
import { createPortal } from "react-dom";
33
import { formatSize, t } from "../i18n";
44
import { api } from "../lib/api";
@@ -15,6 +15,18 @@ import { BackButton } from "./ui/BackButton";
1515
import "./DirectoryWorkspace.css";
1616

1717
export type FolderInspection = { path: string; measurement?: DirSizeRow; returnToRadar?: boolean };
18+
type ReturnPosition = { opener: HTMLButtonElement; scroll: { element: HTMLElement; top: number }[] };
19+
20+
function capturePosition(opener: HTMLButtonElement, dialog: HTMLDivElement | null): ReturnPosition {
21+
const scroll: ReturnPosition["scroll"] = [];
22+
for (let element = opener.parentElement; element; element = element.parentElement) {
23+
if (element === dialog || /^(auto|scroll)$/.test(getComputedStyle(element).overflowY)) {
24+
scroll.push({ element, top: element.scrollTop });
25+
}
26+
if (element === dialog) break;
27+
}
28+
return { opener, scroll };
29+
}
1830

1931
export function DirectoryWorkspace({
2032
radar, inspection, apps, onInspect, onBack, onClose, onOpenSoftware,
@@ -31,27 +43,34 @@ export function DirectoryWorkspace({
3143
const title = useId();
3244
const dialog = useRef<HTMLDivElement>(null);
3345
const backButton = useRef<HTMLButtonElement>(null);
34-
const restoreEvidenceFocus = useRef<HTMLButtonElement | null>(null);
46+
const radarPosition = useRef<ReturnPosition | null>(null);
47+
const referencePosition = useRef<ReturnPosition | null>(null);
3548
const [error, setError] = useState<string | null>(null);
36-
const [opened, setOpened] = useState<{ inspection: FolderInspection; key: string; opener: HTMLButtonElement } | null>(null);
49+
const [opened, setOpened] = useState<({ inspection: FolderInspection; key: string } & ReturnPosition) | null>(null);
3750
const app = opened && opened.inspection === inspection
3851
? apps.find(item => appKey(item) === opened.key)
3952
: undefined;
4053
const back = () => {
4154
if (app && opened) {
42-
restoreEvidenceFocus.current = opened.opener;
55+
referencePosition.current = opened;
4356
setOpened(null);
4457
}
4558
else if (inspection && radar) onBack();
4659
else onClose();
4760
};
4861
useDialogFocus(true, dialog, dialogLayer.overview, back);
49-
useEffect(() => {
50-
const opener = restoreEvidenceFocus.current;
51-
restoreEvidenceFocus.current = null;
52-
if (opener?.isConnected) opener.focus({ preventScroll: true });
53-
else backButton.current?.focus();
54-
}, [inspection, app]);
62+
useLayoutEffect(() => {
63+
const position = referencePosition.current ?? (!inspection && radar ? radarPosition.current : null);
64+
referencePosition.current = null;
65+
if (!inspection) radarPosition.current = null;
66+
if (position?.opener.isConnected) {
67+
for (const { element, top } of position.scroll) element.scrollTo({ top, behavior: "instant" });
68+
position.opener.focus({ preventScroll: true });
69+
} else {
70+
if (dialog.current) dialog.current.scrollTop = 0;
71+
backButton.current?.focus();
72+
}
73+
}, [inspection, app, radar]);
5574

5675
const openPath = (path: string) => {
5776
setError(null);
@@ -67,10 +86,13 @@ export function DirectoryWorkspace({
6786
</header>
6887
{error && <p role="alert" className="folder-reference-incomplete">{error}</p>}
6988
{radar && (
70-
<div hidden={Boolean(inspection)}>
89+
<div className="directory-radar-view" hidden={Boolean(inspection)}>
7190
<DiskRadarPanel
7291
onError={setError}
73-
onInspect={row => onInspect({ path: row?.path ?? "", measurement: row })}
92+
onInspect={(row, opener) => {
93+
radarPosition.current = capturePosition(opener, dialog.current);
94+
onInspect({ path: row?.path ?? "", measurement: row });
95+
}}
7496
/>
7597
</div>
7698
)}
@@ -79,7 +101,7 @@ export function DirectoryWorkspace({
79101
<FolderReferencePanel key={inspection.path} inspection={inspection} apps={apps}
80102
onInspect={path => onInspect({ path, returnToRadar: inspection.returnToRadar })}
81103
onOpenPath={openPath}
82-
onApp={(item, opener) => setOpened({ inspection, key: appKey(item), opener })}
104+
onApp={(item, opener) => setOpened({ inspection, key: appKey(item), ...capturePosition(opener, dialog.current) })}
83105
/>
84106
</div>
85107
)}

‎src/components/DiskRadarComparison.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export function DiskRadarComparison({ scope, rows, busy, onInspect }: {
1212
scope: RadarScope | null;
1313
rows: DirSizeRow[] | null;
1414
busy: boolean;
15-
onInspect?: (row: DirSizeRow) => void;
15+
onInspect?: (row: DirSizeRow, opener: HTMLButtonElement) => void;
1616
}) {
1717
const L = t();
1818
const [saved, setSaved] = useState(() => {
@@ -66,7 +66,7 @@ export function DiskRadarComparison({ scope, rows, busy, onInspect }: {
6666
? change.before === undefined ? L.diskRadarNewRow : change.after === undefined ? L.diskRadarMissingRow : L.diskRadarCappedRow
6767
: change.delta === 0 ? L.diskRadarUnchanged : `${change.delta > 0 ? "+" : "−"}${formatSize(Math.abs(change.delta))}`}</span>
6868
{change.delta !== undefined && change.delta > 0 && change.row && onInspect && (
69-
<button type="button" style={css.btnGhost} onClick={() => onInspect(change.row!)}>{L.folderInspectAction}</button>
69+
<button type="button" style={css.btnGhost} onClick={event => onInspect(change.row!, event.currentTarget)}>{L.folderInspectAction}</button>
7070
)}
7171
</li>)}
7272
</ul>

‎src/components/DiskRadarPanel.tsx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export function DiskRadarPanel({
3636
}: {
3737
onClose?: () => void;
3838
onError: (msg: string) => void;
39-
onInspect?: (row?: DirSizeRow) => void;
39+
onInspect?: (row: DirSizeRow | undefined, opener: HTMLButtonElement) => void;
4040
}) {
4141
const L = t();
4242
const [rows, setRows] = useState<DirSizeRow[] | null>(null);
@@ -150,7 +150,7 @@ export function DiskRadarPanel({
150150
<h2>{L.diskRadarTitle}</h2>
151151
<div style={{ marginLeft: "auto", display: "flex", gap: 8, alignItems: "center", flexWrap: "wrap" }}>
152152
{onInspect && (
153-
<button type="button" style={css.btnGhost} onClick={() => onInspect()}>
153+
<button type="button" style={css.btnGhost} onClick={event => onInspect(undefined, event.currentTarget)}>
154154
{L.folderInspectTitle}
155155
</button>
156156
)}
@@ -306,7 +306,7 @@ export function DiskRadarPanel({
306306
{L.detailShow}
307307
</button>
308308
{onInspect && (
309-
<button type="button" style={css.btnGhost} onClick={() => onInspect(r)}>
309+
<button type="button" style={css.btnGhost} onClick={event => onInspect(r, event.currentTarget)}>
310310
{L.folderInspectAction}
311311
</button>
312312
)}

0 commit comments

Comments
 (0)