Open History to see the last commit
1import type { WorkspaceTab } from "./workspace/workspace-tabs-state";
2import { createContext, use } from "react";
3import type { InspectorContent } from "./inspector-panes";
4import type { WorkspaceDetail, WorkspaceDetailSheet } from "./workspace/workspace-detail-selection";
5import {
6 deriveFileInspectorPaneLayout,
7 deriveLayout,
8 deriveWorkspacePaneLayout,
9 type FileInspectorPaneLayout,
10 type Layout,
11 type WorkspacePaneLayout,
12} from "../../lib/layout";
13
14interface AdaptiveWorkspaceContextValue {
15 readonly layout: Layout;
16 readonly panes: WorkspacePaneLayout;
17 readonly fileInspector: FileInspectorPaneLayout;
18 readonly primarySidebarSearchQuery: string;
19 readonly startNewTask: () => void;
20 /** Use `useSelectWorkspaceDetail`, which also works outside the layout. */
21 readonly selectDetail?: (detail: WorkspaceDetail, sheet?: WorkspaceDetailSheet) => void;
22 readonly registerWorkspaceInspector: (
23 content: InspectorContent,
24 routeKey: string | undefined,
25 revealKey?: string,
26 ) => () => void;
27 readonly setPrimarySidebarSearchQuery: (query: string) => void;
28 /** Opens the inspector: its column where the window has one, otherwise a sheet. */
29 readonly showInspector: () => void;
30 readonly toggleAuxiliaryPane: () => void;
31 /**
32 * Whether the content pane offers a sidebar button: false without columns, on a list that keeps
33 * its sidebar, and on a workspace page (Android Settings, Usage) that covers it.
34 */
35 readonly sidebarToggleable: boolean;
36 readonly togglePrimarySidebar: () => void;
37 /** Shows the sidebar, setting aside the keyboard or an inspector that took its room. */
38 readonly revealPrimarySidebar: () => void;
39 readonly setPrimarySidebarVisible: (visible: boolean) => void;
40 /** Opens a workspace tab with its list and, with columns, the page it last showed. */
41 readonly openWorkspaceTab: (
42 tab: WorkspaceTab | "settings",
43 options?: { readonly columns?: boolean },
44 ) => void;
45 readonly setAuxiliaryPaneWidth: (width: number) => void;
46 readonly nativeInspectorWidth: number;
47 readonly setNativeInspectorWidth: (width: number) => void;
48 /**
49 * Android split view while the thread composer has the soft keyboard: the
50 * sidebar and rail step aside and the thread header drops to one line.
51 */
52 readonly composerKeyboardActive: boolean;
53 /**
54 * Whether the sidebar shows in each orientation, where it floats over the content in portrait
55 * (the unfolded Duo). The native split switches between them as a rotation starts.
56 */
57 readonly sidebarByOrientation: { readonly landscape: boolean; readonly portrait: boolean } | null;
58 /** Android split: the composer's keyboard slides the sidebar, from focus until it lets go. */
59 readonly keyboardHoldsSidebar?: boolean;
60 /** Android split: the width a workspace page (Usage) opens at; null without columns. */
61 readonly workspacePageWidth?: number | null;
62 /** The thread composer reports its focus so the layout can make room for typing. */
63 readonly setComposerEditing: (editing: boolean) => void;
64}
65
66const compactLayout = deriveLayout({ width: 0, height: 0 });
67const compactPanes = deriveWorkspacePaneLayout({
68 layout: compactLayout,
69 viewportWidth: 0,
70 primarySidebarPreferredVisible: true,
71 auxiliaryPanePreferredVisible: true,
72});
73const compactFileInspector = deriveFileInspectorPaneLayout({
74 layout: compactLayout,
75 viewportWidth: 0,
76});
77export const AdaptiveWorkspaceContext = createContext<AdaptiveWorkspaceContextValue>({
78 layout: compactLayout,
79 panes: compactPanes,
80 fileInspector: compactFileInspector,
81 primarySidebarSearchQuery: "",
82 startNewTask: () => undefined,
83 registerWorkspaceInspector: () => () => undefined,
84 setPrimarySidebarSearchQuery: () => undefined,
85 showInspector: () => undefined,
86 toggleAuxiliaryPane: () => undefined,
87 sidebarToggleable: false,
88 togglePrimarySidebar: () => undefined,
89 revealPrimarySidebar: () => undefined,
90 setPrimarySidebarVisible: () => undefined,
91 openWorkspaceTab: () => undefined,
92 setAuxiliaryPaneWidth: () => undefined,
93 nativeInspectorWidth: 0,
94 setNativeInspectorWidth: () => undefined,
95 composerKeyboardActive: false,
96 sidebarByOrientation: null,
97 setComposerEditing: () => undefined,
98});
99
100export function useAdaptiveWorkspaceLayout(): AdaptiveWorkspaceContextValue {
101 return use(AdaptiveWorkspaceContext);
102}