refactor(ui): 合入 G1 Git 页可拖动分区堆叠

This commit is contained in:
Menghuan1918 committed 2026-11-26 11:42:44 +08:00
commit c301c38e94
10 files changed
+118 -57

No files matched your search

+29 -6
View File
@@ -27,7 +27,7 @@ import type { SidebarDiffRef } from '../state.ts'
import { buildRepositoryLabels } from './repo-labels.ts'
import { RepoSwitcher } from './RepoSwitcher.tsx'
import {
ActionButton, ConfirmDialog, IconButton, Menu, Notice, StatusBadge, TextArea, WorkbenchSection, invalidateGitStatus, statusOfXY,
ActionButton, ConfirmDialog, IconButton, Menu, Notice, StatusBadge, TextArea, WorkbenchSection, WorkbenchStack, invalidateGitStatus, statusOfXY,
useGitStatus, type GitFileStatus, type GitTone, type StatusTone,
} from '../ui/index.ts'
import { buildChangeTree, sumLineCounts, type ChangeDir, type ChangeFile, type ChangeNode } from './change-tree.ts'
@@ -432,6 +432,8 @@ export function GitLens(props: GitLensProps) {
const [graphRefresh, setGraphRefresh] = useState(0)
const [changesOpen, setChangesOpen] = useState(true)
const [graphOpen, setGraphOpen] = useState(true)
/** Section weights of the changes/graph stack; kept in memory, the Git page has no settings key yet. */
const [sectionWeights, setSectionWeights] = useState<Record<string, number>>({})
/**
* The open More Actions menu: the trigger's viewport corner (null = closed).
* Its repo/worktree rows carry nested submenus, which the primitives anchor
@@ -1152,8 +1154,18 @@ export function GitLens(props: GitLensProps) {
)}
{isRepo && (
<WorkbenchStack
initialWeights={sectionWeights}
onResize={setSectionWeights}
sections={[
{
id: 'changes',
title: t('gitChanges'),
open: changesOpen,
weight: 2,
content: (
<section className={css.changesSection} data-scm-section="changes">
<WorkbenchSection title={t('gitChanges')} open={changesOpen} onOpenChange={setChangesOpen}
<WorkbenchSection title={t('gitChanges')} open={changesOpen} onOpenChange={setChangesOpen} bodyClassName={css.changesBody}
actions={<div className={css.scmActions}>
<IconButton size="sm" icon={<IconCheckOutlineRegular size={14} />} label={t('commit')}
disabled={!canCommit} onClick={commit} />
@@ -1221,9 +1233,15 @@ export function GitLens(props: GitLensProps) {
</div>}
</WorkbenchSection>
</section>
)}
{isRepo && <section className={graphOpen ? css.graphSection : undefined} data-scm-section="graph">
),
},
{
id: 'graph',
title: t('gitGraph'),
open: graphOpen,
weight: 1,
content: (
<section className={graphOpen ? css.graphSection : undefined} data-scm-section="graph">
<WorkbenchSection title={t('gitGraph')} open={graphOpen} onOpenChange={setGraphOpen}
className={css.graphWorkbench} bodyClassName={css.graphArea}>
{graphOpen && <div data-scm-graph className={css.graphArea}>
@@ -1240,7 +1258,12 @@ export function GitLens(props: GitLensProps) {
/>
</div>}
</WorkbenchSection>
</section>}
</section>
),
},
]}
/>
)}
</div>
{/* A sibling of the scroller: usable even with both sections folded. */}
+6 -4
View File
@@ -79,10 +79,12 @@
flex-direction: column;
}
.changesSection { flex: none; }
/* Bound the section's minimum, not its maximum: 28px header + 220px viewport.
An automatic minimum would let the entire history stretch the outer scroller. */
.graphSection { flex: 1; min-height: 248px; display: flex; flex-direction: column; }
.changesSection { display: flex; flex: 1; flex-direction: column; min-height: 0; min-width: 0; overflow: hidden; }
/* The stack hands each open pane a flex height; the section's own body scrolls,
so both sections keep their header while the list moves under it. */
.changesBody { display: flex; flex: 1; flex-direction: column; min-height: 0; overflow: hidden; }
.changesSection [data-scm-changes] { flex: 1; min-height: 0; overflow-y: auto; }
.graphSection { display: flex; flex: 1; flex-direction: column; min-height: 248px; overflow: hidden; }
.graphArea { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.scmHeader { display: flex; align-items: center; min-height: 28px; padding: 0 6px; }
+2 -3
View File
@@ -31,7 +31,7 @@ import { t } from '../../locales.ts'
import { CluePacksSection } from './packs.tsx'
import { ClueBrowser } from './browser.tsx'
import { ClueHelpOverlay, clueHelpSeen, markClueHelpSeen } from './help.tsx'
import { ActionButton, EmptyState, IconButton, SidebarToolbar, WorkbenchSection } from '../../ui/index.ts'
import { ActionButton, EmptyState, IconButton, SidebarToolbar, WorkbenchSection, WorkbenchStack } from '../../ui/index.ts'
import { openClueDetailWindow, type ClueDetailTarget } from '../windows/detail.tsx'
import { openClueCollectionForTarget } from '../windows/collect.tsx'
import { openPackManagementWindow } from '../windows/pack.tsx'
@@ -41,7 +41,6 @@ import { LoadingIndicator } from '../../ui/LoadingIndicator.tsx'
import { SelectMenu } from '../../ui/SelectMenu.tsx'
import design from '../../ui/design.module.css'
import css from './clues-tab.module.css'
import { SectionStack } from './SectionStack.tsx'
/** The plugin-settings key of the clue tab (one sparse blob). */
const CLUE_TAB_SETTINGS_KEY = 'clues'
@@ -378,7 +377,7 @@ export function CluesTab({ ctx, store, scope, visible }: CluesTabProps): ReactNo
)}
{ready && browseWorkspaceId !== undefined && (
<SectionStack key={browseWorkspaceId} initialWeights={sectionLayoutOf(settings).weights}
<WorkbenchStack key={browseWorkspaceId} initialWeights={sectionLayoutOf(settings).weights}
onResize={persistWeights} sections={[
{ id: 'browse', title: t('clueViewBrowse'), open: sections.browse, weight: 3, content: (
<WorkbenchSection
-28
View File
@@ -42,34 +42,6 @@
font-size: 12px;
line-height: 16px;
}
.stack {
display: flex;
flex: 1;
flex-direction: column;
min-height: 0;
min-width: 0;
overflow-y: auto;
}
.stackPane { display: flex; flex: none; min-width: 0; }
.stackPane[data-expanded='true'] { flex: 1 0 112px; min-height: 112px; }
.sash {
position: relative;
flex: 0 0 7px;
cursor: row-resize;
touch-action: none;
outline: none;
box-sizing: border-box;
padding-block: 3px;
background: var(--dsw-alias-border-l2) content-box;
}
.sash + .stackPane > div > div:first-child { border-top: 0; }
.sash:hover, .sash:focus-visible, .sash:active {
background-color: var(--ds-sidebar-accent);
}
@media (pointer: coarse) {
.stackPane[data-expanded='true'] { flex-basis: 156px; min-height: 156px; }
.sash { flex-basis: 14px; padding-block: 6px 7px; }
}
/* The very first workbench section at the top of the tab needs no double top rule. */
.browseSection > div:first-child {
border-top: 0;
@@ -1,9 +1,10 @@
/** A vertical workbench stack. A sash redistributes only the two nearest open sections. */
import { Fragment, useEffect, useId, useRef, useState, type ReactNode } from 'react'
import { createFrameBatcher } from '../../frame-batcher.ts'
import css from './clues-tab.module.css'
import { createFrameBatcher } from '../frame-batcher.ts'
import css from './workbench-stack.module.css'
interface Section {
export interface WorkbenchStackSection {
/** Stable id; also the persistence key of this section's weight. */
id: string
title: string
open: boolean
@@ -82,8 +83,8 @@ function Sash({ top, bottom, ratio, onPreview, onCommit }: {
}} />
}
export function SectionStack({ sections, initialWeights, onResize }: {
sections: readonly Section[]
export function WorkbenchStack({ sections, initialWeights, onResize }: {
sections: readonly WorkbenchStackSection[]
initialWeights?: Record<string, unknown>
onResize(weights: Record<string, number>): void
}): ReactNode {
@@ -94,7 +95,7 @@ export function SectionStack({ sections, initialWeights, onResize }: {
})))
const opened = sections.filter(section => section.open)
const openWeight = opened.reduce((sum, section) => sum + (weights[section.id] ?? section.weight), 0)
let previous: Section | undefined
let previous: WorkbenchStackSection | undefined
return <div className={css.stack}>
{sections.map(section => {
const top = section.open ? previous : undefined
+1
View File
@@ -9,6 +9,7 @@ export {
} from './kit.tsx'
export { LoadingIndicator, type LoadingIndicatorProps } from './LoadingIndicator.tsx'
export { WorkbenchSection, type WorkbenchSectionProps } from './WorkbenchSection.tsx'
export { WorkbenchStack, type WorkbenchStackSection } from './WorkbenchStack.tsx'
export { SelectMenu, type SelectMenuProps, type SelectOption } from './SelectMenu.tsx'
export { TextInput, TextArea, NumberInput, Checkbox, ToggleSwitch, Tabs, SettingRow, type TabOption } from './controls.tsx'
export {
+29
View File
@@ -0,0 +1,29 @@
/** A vertical workbench stack: adjacent open sections share the height through a draggable sash. */
.stack {
display: flex;
flex: 1;
flex-direction: column;
min-height: 0;
min-width: 0;
overflow-y: auto;
}
.stackPane { display: flex; flex: none; min-width: 0; }
.stackPane[data-expanded='true'] { flex: 1 0 112px; min-height: 112px; }
.sash {
position: relative;
flex: 0 0 7px;
cursor: row-resize;
touch-action: none;
outline: none;
box-sizing: border-box;
padding-block: 3px;
background: var(--dsw-alias-border-l2) content-box;
}
.sash + .stackPane > div > div:first-child { border-top: 0; }
.sash:hover, .sash:focus-visible, .sash:active {
background-color: var(--ds-sidebar-accent);
}
@media (pointer: coarse) {
.stackPane[data-expanded='true'] { flex-basis: 156px; min-height: 156px; }
.sash { flex-basis: 14px; padding-block: 6px 7px; }
}
+14 -6
View File
@@ -155,9 +155,15 @@ function mockGraphEmpty(): void {
})
}
/** The bottom pane's own resize handle. The Git lens stacks Changes and Graph
* behind a section sash, so a bare `[role="separator"]` query would now hit
* that sash first: the section sash carries `aria-controls`, the pane handle
* does not, which is what tells them apart. */
const PREVIEW_HANDLE = '[role="separator"]:not([aria-controls])'
/** The open bottom pane's frame: the drag handle's parent (the pane root). */
function paneOf(container: HTMLElement): HTMLElement {
const handle = container.querySelector<HTMLElement>('[role="separator"]')
const handle = container.querySelector<HTMLElement>(PREVIEW_HANDLE)
expect(handle, 'the bottom pane frame is open').not.toBeNull()
return handle!.parentElement as HTMLElement
}
@@ -829,7 +835,7 @@ describe('ChangesTab', () => {
expect(row).not.toBeNull()
await act(async () => { row!.click() })
await flushEffects()
return container.querySelector<HTMLElement>('[role="separator"]')?.parentElement?.style.height
return container.querySelector<HTMLElement>(PREVIEW_HANDLE)?.parentElement?.style.height
} finally {
act(() => { root.unmount() })
container.remove()
@@ -914,7 +920,9 @@ describe('ChangesTab', () => {
expect(rule('.gitScroll')).toContain('overflow-x: hidden')
expect(rule('.gitScroll')).toContain('flex: 1')
expect(rule('.gitScroll')).toContain('flex-direction: column')
expect(rule('.changesSection')).toContain('flex: none')
// The two sections now share one stack: each fills the height the sash
// hands it, while .gitScroll stays the panel's single scroller for notices.
expect(rule('.changesSection')).toContain('flex: 1')
expect(rule('.graphSection')).toContain('flex: 1')
expect(rule('.graphArea')).toContain('flex: 1')
expect(rule('.graphArea')).not.toContain('height: min(')
@@ -986,7 +994,7 @@ describe('ChangesTab bottom pane tabs', () => {
// ONE region: a single drag handle, and the strip row is its sibling
// INSIDE that same frame (the header's lens control is outside it). The
// row carries the tablist and the pane's close button.
expect(container.querySelectorAll('[role="separator"]')).toHaveLength(1)
expect(container.querySelectorAll(PREVIEW_HANDLE)).toHaveLength(1)
const pane = paneOf(container)
expect(pane.children[0]!.getAttribute('role')).toBe('separator')
expect(pane.children[1]!.querySelector('[role="tablist"]')).not.toBeNull()
@@ -1084,7 +1092,7 @@ describe('ChangesTab bottom pane tabs', () => {
await act(async () => { row.click() })
await flushEffects()
expect(container.querySelectorAll('[role="separator"]')).toHaveLength(1)
expect(container.querySelectorAll(PREVIEW_HANDLE)).toHaveLength(1)
const pane = paneOf(container)
// Same strip as everywhere else — the lens above does not change it.
expect(paneTabs(container).map(tab => tab.textContent)).toEqual([t('graphDetails'), t('changesContentTab'), t('changesDiffTab')])
@@ -1292,7 +1300,7 @@ describe('ChangesTab bottom pane tabs', () => {
expect(paneOf(container).querySelector(`section[aria-label="${t('graphDetails')}"]`)).not.toBeNull()
await act(async () => { closeButtons()[0]!.click() })
await settle()
expect(container.querySelector('[role="separator"]')).toBeNull()
expect(container.querySelector(PREVIEW_HANDLE)).toBeNull()
} finally {
act(() => { root.unmount() })
container.remove()
+4 -4
View File
@@ -3,7 +3,7 @@
import { createElement } from 'react'
import { act } from 'react-dom/test-utils'
import { expect, it, vi } from 'vitest'
import { SectionStack } from '../src/client/clues/tab/SectionStack.tsx'
import { WorkbenchStack } from '../src/client/ui/WorkbenchStack.tsx'
import { renderRoot, setupReactAct } from './test-utils.ts'
setupReactAct()
@@ -12,7 +12,7 @@ it('resizes a pair without moving the third or unmounting it when collapsed', ()
const commit = vi.fn()
const sections = ['first', 'second', 'third'].map(id => ({ id, title: id, open: true, weight: 1,
content: createElement('input', { 'aria-label': id, defaultValue: id }) }))
const view = renderRoot(createElement(SectionStack, { sections, onResize: commit }))
const view = renderRoot(createElement(WorkbenchStack, { sections, onResize: commit }))
try {
const separators = () => Array.from(view.container.querySelectorAll<HTMLElement>('[role="separator"]'))
const press = (node: HTMLElement, key: string) => {
@@ -26,7 +26,7 @@ it('resizes a pair without moving the third or unmounting it when collapsed', ()
expect(firstResize.third).toBe(1)
const input = view.container.querySelector<HTMLInputElement>('[aria-label="second"]')!
input.value = 'retained draft'
view.rerender(createElement(SectionStack, { sections: sections.map(section => section.id === 'second'
view.rerender(createElement(WorkbenchStack, { sections: sections.map(section => section.id === 'second'
? { ...section, open: false } : section), onResize: commit }))
expect(separators()).toHaveLength(1)
expect(separators()[0]!.getAttribute('aria-label')).toBe('first / third')
@@ -35,7 +35,7 @@ it('resizes a pair without moving the third or unmounting it when collapsed', ()
expect(secondResize.first).toBeCloseTo(0)
expect(secondResize.second).toBe(firstResize.second)
expect(secondResize.third).toBeCloseTo(3)
view.rerender(createElement(SectionStack, { sections, onResize: commit }))
view.rerender(createElement(WorkbenchStack, { sections, onResize: commit }))
expect(view.container.querySelector<HTMLInputElement>('[aria-label="second"]')).toBe(input)
expect(input.value).toBe('retained draft')
expect(separators()).toHaveLength(2)
+26
View File
@@ -303,3 +303,29 @@ it('checks out a plain branch and refreshes placeholder, resources, graph and fo
expect(vi.mocked(api.gitGraph).mock.calls.length).toBeGreaterThan(graphCalls)
} finally { view.unmount() }
})
it('stacks Changes and Graph behind one draggable sash and drops it when a section folds', async () => {
const view = await mount(() => tracked())
try {
const { container } = view
const separators = (): HTMLElement[] => [...container.querySelectorAll<HTMLElement>('[role="separator"]')]
expect(separators()).toHaveLength(1)
const sash = separators()[0]!
expect(sash.getAttribute('aria-orientation')).toBe('horizontal')
const [topId, bottomId] = sash.getAttribute('aria-controls')!.split(' ')
const top = document.getElementById(topId!)!
const bottom = document.getElementById(bottomId!)!
expect(top.contains(container.querySelector('[data-scm-section="changes"]'))).toBe(true)
expect(bottom.contains(container.querySelector('[data-scm-section="graph"]'))).toBe(true)
const grow = (el: HTMLElement): number => Number.parseFloat(el.style.flexGrow)
const before = grow(top)
act(() => { sash.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowDown', bubbles: true })) })
// The sash hands the next slice of the pair to the pane below it.
expect(grow(top)).toBeGreaterThan(before)
expect(grow(bottom)).toBeLessThan(grow(top))
// One open section leaves nothing to divide: the sash goes away with it.
const graph = container.querySelector<HTMLButtonElement>('[data-scm-section="graph"] button[aria-expanded]')!
await click(graph)
expect(separators()).toHaveLength(0)
} finally { view.unmount() }
})