Skip to content

Commit da81dcd

Browse files
LemelsonLemelson
authored andcommitted
fix(mobile): polish Finder browser for compact Android phones and fold/unfold transitions
- Keep pane wrapper stable across split <-> compact transitions so inputs keep focus on fold/unfold - Pin Places toggle chip left of fading chip rail, preserve active chip highlight for Recent subfolders, and auto-scroll active chip into view - Render expandable Places as a compact 2-column grid on phones, dismiss keyboard on open, and wire Android useHardwareBack to collapse Places / Edit path before leaving - Preserve vertical touch targets inside horizontal ScrollViews, fix pending crumb & search border contrast, ellipsize search paths from head, and scroll back to top on folder navigation
1 parent da20a13 commit da81dcd

6 files changed

Lines changed: 377 additions & 88 deletions

File tree

‎apps/mobile/src/features/projects/AddProjectScreen.test.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ const state = vi.hoisted(() => ({
5050
dispatch: vi.fn(),
5151
alert: vi.fn(),
5252
dismissKeyboard: vi.fn(),
53+
/** What Android's Back does while the browser has something to fold away. */
54+
hardwareBack: null as (() => boolean) | null,
5355
navigationState: undefined as NavigationState | undefined,
5456
environments: [{ environmentId: "environment", environmentLabel: "Environment" }],
5557
/** Capabilities every mocked host advertises. */
@@ -159,6 +161,11 @@ vi.mock("@react-navigation/native", () => ({
159161
push: (name: string, params: unknown) => ({ type: "PUSH", payload: { name, params } }),
160162
},
161163
}));
164+
vi.mock("../../lib/backHandler", () => ({
165+
useHardwareBack: (active: boolean, onBack: () => boolean) => {
166+
state.hardwareBack = active ? onBack : null;
167+
},
168+
}));
162169
vi.mock("react-native-safe-area-context", () => ({
163170
useSafeAreaInsets: () => ({ bottom: 0 }),
164171
}));
@@ -1840,6 +1847,42 @@ describe("Finder folder browser", () => {
18401847
expect(screen.text()).not.toContain("Favorites");
18411848
});
18421849

1850+
it("keeps a place chip lit inside a recent folder under it", async () => {
1851+
const screen = await finder();
1852+
// Its own folder: visited folders outlive a test, and Clients is the sidebar test's.
1853+
state.browse["/home/tester/Documents/"] = ["Clients", "Drafts"];
1854+
state.browse["/home/tester/Documents/Drafts/"] = [];
1855+
await screen.pressControl("Documents");
1856+
await screen.pressRow("Drafts");
1857+
1858+
expect(screen.button("Add “Drafts”")).toBeDefined();
1859+
expect(screen.control("Documents")?.accessibilityState?.selected).toBe(true);
1860+
});
1861+
1862+
it("opens Places with the keyboard down, and folds it, then the path field, on Back", async () => {
1863+
const screen = await finder();
1864+
expect(state.hardwareBack).toBeNull();
1865+
state.dismissKeyboard.mockReset();
1866+
1867+
await screen.pressControl("Places");
1868+
expect(state.dismissKeyboard).toHaveBeenCalledTimes(1);
1869+
await screen.pressControl("Edit path");
1870+
expect(screen.text()).toContain("Favorites");
1871+
expect(screen.control("Edit path")?.accessibilityState?.selected).toBe(true);
1872+
1873+
await act(async () => {
1874+
expect(state.hardwareBack?.()).toBe(true);
1875+
});
1876+
expect(screen.text()).not.toContain("Favorites");
1877+
expect(screen.control("Edit path")?.accessibilityState?.selected).toBe(true);
1878+
1879+
await act(async () => {
1880+
expect(state.hardwareBack?.()).toBe(true);
1881+
});
1882+
expect(screen.control("Edit path")?.accessibilityState?.selected).toBe(false);
1883+
expect(state.hardwareBack).toBeNull();
1884+
});
1885+
18431886
it("splits into a sidebar when the browser is wide, and folds it away", async () => {
18441887
const screen = await finder({ baseDirectory: "/home/tester/Documents/Clients" });
18451888
expect(screen.control("Places")).toBeDefined();

‎apps/mobile/src/features/projects/AddProjectScreen.tsx‎

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ import {
5858
} from "@react-navigation/native";
5959
import { SymbolView } from "../../components/ui/AppSymbol";
6060
import { msg, useGT, useMessages } from "gt-react-native";
61-
import { useEffect, useRef, useState, type ReactNode } from "react";
62-
import { Keyboard, Platform, Pressable, View } from "react-native";
61+
import { useEffect, useRef, useState, type ReactNode, type RefObject } from "react";
62+
import { Keyboard, Platform, Pressable, View, type ScrollViewInstance } from "react-native";
6363
import { useSafeAreaInsets } from "react-native-safe-area-context";
6464
import * as Arr from "effect/Array";
6565
import * as Cause from "effect/Cause";
@@ -171,13 +171,22 @@ function SectionTitle(props: {
171171
);
172172
}
173173

174-
function AddProjectShell(props: { readonly children: ReactNode; readonly title: string }) {
174+
function AddProjectShell({
175+
children,
176+
title,
177+
scrollRef,
178+
}: {
179+
readonly children: ReactNode;
180+
readonly title: string;
181+
readonly scrollRef?: RefObject<ScrollViewInstance | null>;
182+
}) {
175183
const insets = useSafeAreaInsets();
176184

177185
return (
178186
// A collapsible wrapper lets formSheet resizing mistake the full-height view for a header and collapse the ScrollView.
179-
<SettingsScreen title={props.title}>
187+
<SettingsScreen title={title}>
180188
<ScrollView
189+
ref={scrollRef}
181190
keyboardShouldPersistTaps="handled"
182191
// Dragging the list drops the keyboard, and the keyboard never covers search results.
183192
keyboardDismissMode={Platform.OS === "ios" ? "interactive" : "on-drag"}
@@ -190,12 +199,22 @@ function AddProjectShell(props: { readonly children: ReactNode; readonly title:
190199
gap: Platform.OS === "android" ? 16 : 10,
191200
}}
192201
>
193-
{props.children}
202+
{children}
194203
</ScrollView>
195204
</SettingsScreen>
196205
);
197206
}
198207

208+
/**
209+
* The screen's scroll view, and a way back to its top: a folder opened from deep in a long list
210+
* starts at its breadcrumbs, with the screen's button in view.
211+
*/
212+
function useShellScroll() {
213+
const scrollRef = useRef<ScrollViewInstance>(null);
214+
const scrollToTop = () => scrollRef.current?.scrollTo({ y: 0, animated: true });
215+
return { scrollRef, scrollToTop };
216+
}
217+
199218
function ListSection(props: { readonly children: ReactNode }) {
200219
return (
201220
<View
@@ -969,6 +988,7 @@ export function AddProjectRepositoryScreen(props: {
969988
}
970989

971990
export function AddProjectNewScreen(props: { readonly environmentId?: string | string[] }) {
991+
const shellScroll = useShellScroll();
972992
const openNewTaskDraft = useOpenNewTaskDraft();
973993
const navigation = useNavigation();
974994
const environmentOptions = useEnvironmentOptions().filter(
@@ -1192,7 +1212,7 @@ export function AddProjectNewScreen(props: { readonly environmentId?: string | s
11921212
};
11931213

11941214
return (
1195-
<AddProjectShell title={gt("New project")}>
1215+
<AddProjectShell title={gt("New project")} scrollRef={shellScroll.scrollRef}>
11961216
{error ? <ErrorBanner message={error} /> : null}
11971217
{environment ? (
11981218
<>
@@ -1366,6 +1386,7 @@ export function AddProjectNewScreen(props: { readonly environmentId?: string | s
13661386
readBrowseInteractionGeneration={readBrowseInteractionGeneration}
13671387
isBrowseNavigating={isBrowseNavigating}
13681388
onCreatingFolderChange={setIsCreatingFolder}
1389+
onFolderNavigated={shellScroll.scrollToTop}
13691390
missingFolderHint={
13701391
customLocationMissing
13711392
? gt("This folder does not exist yet. Create it with New folder first.")
@@ -1407,6 +1428,7 @@ export function AddProjectNewScreen(props: { readonly environmentId?: string | s
14071428
}
14081429

14091430
export function AddProjectLocalFolderScreen(props: { readonly environmentId?: string | string[] }) {
1431+
const shellScroll = useShellScroll();
14101432
const environment = useEnvironmentFromParam(props.environmentId);
14111433
const canCreateProject = useEnvironmentScope(
14121434
environment?.environmentId ?? null,
@@ -1457,7 +1479,7 @@ export function AddProjectLocalFolderScreen(props: { readonly environmentId?: st
14571479
};
14581480

14591481
return (
1460-
<AddProjectShell title={gt("Local folder")}>
1482+
<AddProjectShell title={gt("Local folder")} scrollRef={shellScroll.scrollRef}>
14611483
{error ? <ErrorBanner message={error} /> : null}
14621484
{environment ? (
14631485
<>
@@ -1486,6 +1508,7 @@ export function AddProjectLocalFolderScreen(props: { readonly environmentId?: st
14861508
readBrowseInteractionGeneration={readBrowseInteractionGeneration}
14871509
isBrowseNavigating={isBrowseNavigating}
14881510
onCreatingFolderChange={setIsCreatingFolder}
1511+
onFolderNavigated={shellScroll.scrollToTop}
14891512
missingFolderHint={
14901513
browse.willCreateFolder
14911514
? gt("This folder does not exist yet. Adding the project creates it.")
@@ -1507,6 +1530,7 @@ export function AddProjectDestinationScreen(props: {
15071530
readonly repositoryName?: string | string[];
15081531
readonly prompt?: string | string[];
15091532
}) {
1533+
const shellScroll = useShellScroll();
15101534
const openNewTaskDraft = useOpenNewTaskDraft();
15111535
const startProjectClone = useAtomCommand(sourceControlEnvironment.startProjectClone, {
15121536
reportFailure: false,
@@ -1643,7 +1667,7 @@ export function AddProjectDestinationScreen(props: {
16431667
};
16441668

16451669
return (
1646-
<AddProjectShell title={gt("Clone destination")}>
1670+
<AddProjectShell title={gt("Clone destination")} scrollRef={shellScroll.scrollRef}>
16471671
{error ? <ErrorBanner message={error} /> : null}
16481672
{repositoryTitle ? (
16491673
<View className="rounded-[24px] bg-grouped-card px-4 py-3">
@@ -1690,6 +1714,7 @@ export function AddProjectDestinationScreen(props: {
16901714
readBrowseInteractionGeneration={readBrowseInteractionGeneration}
16911715
isBrowseNavigating={isBrowseNavigating}
16921716
onCreatingFolderChange={setIsCreatingFolder}
1717+
onFolderNavigated={shellScroll.scrollToTop}
16931718
missingFolderHint={
16941719
remoteUrl && isMissingBrowsePathFailure(browse.browseState.failure)
16951720
? gt("This folder does not exist yet. Cloning creates it.")

‎apps/mobile/src/features/projects/FolderBrowser.logic.test.ts‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
projectRootsByRecency,
1212
rememberVisitedFolder,
1313
resolveActivePlaceId,
14+
revealScrollOffset,
1415
} from "./FolderBrowser.logic";
1516

1617
const macPlaces = [
@@ -122,6 +123,59 @@ describe("resolveActivePlaceId", () => {
122123
id,
123124
);
124125
});
126+
127+
it("keeps a rail place lit inside a recent folder, which the rail does not show", () => {
128+
const railPlaces = macPlaces;
129+
const places = [
130+
...railPlaces,
131+
{ id: "recent:/Users/me/Documents/app/", path: "/Users/me/Documents/app/" },
132+
];
133+
const directoryPath = "/Users/me/Documents/app/";
134+
expect(resolveActivePlaceId({ directoryPath, homePath: "/Users/me", places })).toBe(
135+
"recent:/Users/me/Documents/app/",
136+
);
137+
expect(resolveActivePlaceId({ directoryPath, homePath: "/Users/me", places: railPlaces })).toBe(
138+
"documents",
139+
);
140+
});
141+
});
142+
143+
describe("revealScrollOffset", () => {
144+
const rail = { viewportWidth: 300, contentWidth: 900, margin: 24 };
145+
it.each([
146+
{ name: "leaves a visible chip alone", itemX: 100, itemWidth: 80, scrollX: 0, offset: null },
147+
{
148+
name: "brings a chip past the right edge in",
149+
itemX: 400,
150+
itemWidth: 80,
151+
scrollX: 0,
152+
offset: 204,
153+
},
154+
{
155+
name: "brings a chip before the left edge in",
156+
itemX: 100,
157+
itemWidth: 80,
158+
scrollX: 200,
159+
offset: 76,
160+
},
161+
{ name: "stops at the rail's start", itemX: 10, itemWidth: 80, scrollX: 100, offset: 0 },
162+
{ name: "stops at the rail's end", itemX: 850, itemWidth: 50, scrollX: 0, offset: 600 },
163+
{
164+
name: "starts a chip wider than the rail",
165+
itemX: 400,
166+
itemWidth: 400,
167+
scrollX: 0,
168+
offset: 376,
169+
},
170+
])("$name", ({ itemX, itemWidth, scrollX, offset }) => {
171+
expect(revealScrollOffset({ ...rail, itemX, itemWidth, scrollX })).toBe(offset);
172+
});
173+
174+
it("waits for the rail to be measured", () => {
175+
expect(
176+
revealScrollOffset({ ...rail, viewportWidth: 0, itemX: 400, itemWidth: 80, scrollX: 0 }),
177+
).toBeNull();
178+
});
125179
});
126180

127181
describe("deriveRecentFolders", () => {

‎apps/mobile/src/features/projects/FolderBrowser.logic.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,3 +356,31 @@ export function describeFolderSearch(input: {
356356
error: isCurrent && !input.isPending ? (input.error ?? null) : null,
357357
};
358358
}
359+
360+
/**
361+
* Where a horizontal rail should scroll so an item is in view with `margin` to spare, or null
362+
* when it already is; an item wider than the rail lines up at its start.
363+
*/
364+
export function revealScrollOffset(input: {
365+
readonly itemX: number;
366+
readonly itemWidth: number;
367+
readonly scrollX: number;
368+
readonly viewportWidth: number;
369+
readonly contentWidth: number;
370+
readonly margin: number;
371+
}): number | null {
372+
const { itemX, itemWidth, scrollX, viewportWidth, contentWidth, margin } = input;
373+
if (viewportWidth <= 0) return null;
374+
const maxScroll = Math.max(0, contentWidth - viewportWidth);
375+
const start = itemX - margin;
376+
const end = itemX + itemWidth + margin;
377+
const target =
378+
start < scrollX || end - start > viewportWidth
379+
? start
380+
: end > scrollX + viewportWidth
381+
? end - viewportWidth
382+
: null;
383+
if (target === null) return null;
384+
const clamped = Math.min(maxScroll, Math.max(0, target));
385+
return Math.abs(clamped - scrollX) < 1 ? null : clamped;
386+
}

0 commit comments

Comments
 (0)