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