feat(git): make the repository switcher responsive (#1130)

Keep the contributor label work but place the control above Changes per the product decision: chips at natural width while every root fits, one native select once it does not. A hidden measurement row makes the decision content-driven; the More -> Repository submenu stays as the folded fallback.
This commit is contained in:
Menghuan1918 committed 2026-11-16 00:37:02 +08:00
1 parent da860d9aef
commit 83a25c27bc
9 files changed
+350 -113

No files matched your search

+1 -31
View File
@@ -35,18 +35,6 @@ export interface GraphDetailsSelection {
readonly slot: number
}
/** The lens-owned multi-repository picker state. `labels` pairs each
* discovered root with its display-only disambiguation label; `value` is the
* RESOLVED status root; `standInLabel` is set when the server fell back from
* the requested root — rendered as a disabled option, never a candidate. */
export interface RepoPickerState {
readonly labels: ReadonlyArray<{ readonly root: string; readonly label: string }>
readonly value: string
readonly standInLabel?: string
readonly disabled: boolean
readonly onChooseRepo: (root: string) => void
}
export interface GitGraphSectionProps {
scope: SessionScope
worktree?: string
@@ -64,12 +52,6 @@ export interface GitGraphSectionProps {
/** Row context menu: the Git lens's advanced commit actions (view / copy /
* revert / cherry-pick). Absent, the rows simply carry no menu. */
onContextMenu?: (event: MouseEvent<HTMLButtonElement>, entry: GraphEntry) => void
/** The lens-owned multi-repository picker, rendered at the head of the
* toolbar so the row reads as one scope line: pick the root, then filter
* its history. Present only when the workspace holds several roots; the
* lens keeps the ⋯ → Repository submenu reachable so a FOLDED graph never
* orphans the switcher (the toolbar unmounts with the section). */
repoPicker?: RepoPickerState
}
/** One-line rows: the GitLens-style picture is rail + ref pills + subject on
@@ -154,7 +136,7 @@ export function RefPill({ pill, slot }: { pill: RefPillModel; slot?: number }) {
</span>
}
export function GitGraphSection({ scope, worktree, visible, refreshTick = 0, onPreview, selectedRef, onClearPreview, onDetailsChange, onContextMenu, repoPicker }: GitGraphSectionProps) {
export function GitGraphSection({ scope, worktree, visible, refreshTick = 0, onPreview, selectedRef, onClearPreview, onDetailsChange, onContextMenu }: GitGraphSectionProps) {
const [branch, setBranch] = useState('')
const [page, setPage] = useState<GraphPage | null>(null)
const [branches, setBranches] = useState<GraphPage['branches']>([])
@@ -368,18 +350,6 @@ export function GitGraphSection({ scope, worktree, visible, refreshTick = 0, onP
return <section className={css.root} aria-label={t('gitGraph')} data-git-graph
data-graph-count={page?.entries.length ?? 0}>
<div className={css.toolbar}>
{repoPicker && (
<label className={css.repoPick} data-scm-repos>
<span className={css.repoPickLabel}>{t('gitRepository')}</span>
<select className={css.select} aria-label={t('gitRepository')}
value={repoPicker.value} disabled={repoPicker.disabled}
onChange={event => { repoPicker.onChooseRepo(event.target.value) }}>
{repoPicker.standInLabel !== undefined &&
<option value={repoPicker.value} disabled>{repoPicker.standInLabel}</option>}
{repoPicker.labels.map(item => <option key={item.root} value={item.root}>{item.label}</option>)}
</select>
</label>
)}
{linearFallback && <div role="status">{t('graphLinearFallback')}</div>}
{!linearFallback && <select className={css.select} aria-label={t('graphBranchFilter')}
value={branch} onChange={event => { chooseBranch(event.target.value) }}>
+20 -11
View File
@@ -25,6 +25,7 @@ import { isZh, t } from '../locales.ts'
import { GitGraphSection, type GraphDetailsSelection } from './GitGraphSection.tsx'
import type { SidebarDiffRef } from '../state.ts'
import { buildRepositoryLabels } from './repo-labels.ts'
import { RepoSwitcher } from './RepoSwitcher.tsx'
import {
ConfirmDialog, IconButton, Notice, StatusBadge, invalidateGitStatus, statusOfXY,
useGitStatus, type GitFileStatus, type GitTone, type StatusTone,
@@ -1133,6 +1134,25 @@ export function GitLens(props: GitLensProps) {
{snapshot !== null && !isRepo && <Notice kind="empty" tone="page">{t('notRepo')}</Notice>}
{snapshot?.truncated === true && <Notice kind="warn">{t('statusTruncated')}</Notice>}
{/* A container workspace can hold several independent repositories, and
every view below is pinned to ONE of them — without this row the
panel silently shows the first root, so nothing on screen says the
others exist. The switcher is not a second source of truth:
repoChoices comes from the status snapshot, chooseRepo re-pins the
scope (reads, writes, graph) to the picked root. It renders chips
while they fit and one select otherwise; the list is shared with
the More -> Repository submenu. */}
{isRepo && repoChoices.length > 1 && (
<RepoSwitcher
labels={repositoryLabels}
value={resolvedRepository}
standInLabel={resolvedRepositoryLabel}
disabled={busy || suggesting}
visible={visible && active}
onChoose={chooseRepo}
/>
)}
{isRepo && (
<section className={css.changesSection} data-scm-section="changes">
<div className={css.scmHeader}>
@@ -1214,10 +1234,6 @@ export function GitLens(props: GitLensProps) {
</section>
)}
{/* The graph toolbar hosts the multi-repo picker at its head (repository
first, then the branch filter — one scope row). The picker rides the
section, so a folded graph hides it; the ⋯ → Repository submenu in
the Changes header stays reachable as the fallback surface. */}
{isRepo && <section className={graphOpen ? css.graphSection : undefined} data-scm-section="graph">
<div className={css.scmHeader}>
<button type="button" className={css.scmDisclosure} aria-expanded={graphOpen}
@@ -1239,13 +1255,6 @@ export function GitLens(props: GitLensProps) {
onClearPreview={() => { props.onClearPreview?.() }}
onDetailsChange={props.onDetailsChange}
onContextMenu={openHistoryMenu}
repoPicker={repoChoices.length > 1 ? {
labels: repositoryLabels,
value: resolvedRepository ?? '',
standInLabel: resolvedRepositoryLabel,
disabled: busy || suggesting,
onChooseRepo: chooseRepo,
} : undefined}
/>
</div>}
</section>}
+126
View File
@@ -0,0 +1,126 @@
/**
* Multi-repository switcher for a workspace CONTAINER (a project directory
* that is not itself a repository but holds several).
*
* The row is responsive by content, not by a fixed viewport breakpoint: the
* chips render at their natural width whenever every discovered root fits on
* one line; once they do not (many roots, long location labels, or a narrow
* side card) the same list becomes one native select. A hidden measurement row
* keeps the decision stable even while the select is mounted — there is no
* flip-flop between the two forms.
*
* Both forms are display-only over the same label list; selecting a root calls
* the lens's existing chooseRepo, so reads, writes and the commit graph stay
* pinned to one root.
*/
import { useCallback, useEffect, useLayoutEffect, useRef, useState } from 'react'
import { t } from '../locales.ts'
import type { RepositoryLabel } from './repo-labels.ts'
import css from './changes.module.css'
/** The switcher's current presentation. */
type RepoSwitcherMode = 'chips' | 'select'
export interface RepoSwitcherProps {
/** Discovered roots in discovery order, with display-only labels. */
readonly labels: readonly RepositoryLabel[]
/** The RESOLVED root (never the stale request), when known. */
readonly value: string | undefined
/** Label for a resolved root that is no longer in the discovered list. */
readonly standInLabel: string | undefined
readonly disabled: boolean
/** Whether the hosting tab is on screen (gates the resize observer). */
readonly visible: boolean
readonly onChoose: (root: string) => void
}
/** One root's display row, including the server-fallback stand-in. */
interface SwitcherOption extends RepositoryLabel {
readonly standIn?: boolean
}
/**
* Render the responsive repository switcher.
* @param props - labels, resolved root, and the existing chooseRepo callback.
* @returns the switcher, or nothing when only one root exists.
*/
export function RepoSwitcher({ labels, value, standInLabel, disabled, visible, onChoose }: RepoSwitcherProps) {
const [mode, setMode] = useState<RepoSwitcherMode>('chips')
const hostRef = useRef<HTMLDivElement>(null)
const measureRef = useRef<HTMLDivElement>(null)
const options: readonly SwitcherOption[] = value !== undefined && standInLabel !== undefined
? [...labels, { root: value, label: standInLabel, standIn: true }]
: labels
/** Compare the chips' natural width with the row's content box. */
const measure = useCallback((): void => {
const host = hostRef.current
const row = measureRef.current
if (host === null || row === null) return
// A hidden tab (and jsdom) reports width 0: keep the current form rather
// than deciding on a meaningless measurement. The observer re-runs once
// the tab is on screen.
if (host.clientWidth <= 0) {
setMode('chips')
return
}
// The row's horizontal padding (6px each side) is not available content.
const available = host.clientWidth - 12
setMode(row.scrollWidth > available ? 'select' : 'chips')
}, [])
useLayoutEffect(() => { measure() }, [measure, labels, value, standInLabel, visible])
useEffect(() => {
if (!visible) return
const host = hostRef.current
const row = measureRef.current
if (host === null || row === null || typeof ResizeObserver === 'undefined') return
const observer = new ResizeObserver(() => { measure() })
observer.observe(host)
observer.observe(row)
measure()
return () => { observer.disconnect() }
}, [visible, measure, labels, value, standInLabel])
if (options.length <= 1) return null
return (
<div ref={hostRef} className={css.repoSwitcher} data-scm-repos role="group"
aria-label={t('gitRepository')} data-repo-mode={mode}>
{mode === 'chips'
? (
<div className={css.repoChips} role="presentation">
{options.map(item => (
<button key={item.root} type="button" className={css.repoChip} data-scm-repo={item.root}
data-active={item.standIn === true || item.root === value ? 'true' : undefined}
data-standin={item.standIn === true ? 'true' : undefined}
aria-pressed={item.root === value} title={item.root}
disabled={disabled || item.standIn === true}
onClick={() => { onChoose(item.root) }}>
{item.label}
</button>
))}
</div>
)
: (
<label className={css.repoPick}>
<span className={css.repoPickLabel}>{t('gitRepository')}</span>
<select className={css.repoPickSelect} aria-label={t('gitRepository')}
value={value ?? ''} disabled={disabled}
onChange={event => { onChoose(event.target.value) }}>
{value === undefined && <option value="" disabled>{t('gitRepository')}</option>}
{value !== undefined && standInLabel !== undefined &&
<option value={value} disabled>{standInLabel}</option>}
{labels.map(item => <option key={item.root} value={item.root}>{item.label}</option>)}
</select>
</label>
)}
{/* Natural-width measurement copy. Hidden from layout, AT and tests. */}
<div ref={measureRef} className={css.repoMeasure} data-repo-measure aria-hidden="true">
{options.map(item => <span key={item.root} className={css.repoChip}>{item.label}</span>)}
</div>
</div>
)
}
+86
View File
@@ -58,6 +58,92 @@
.scmHeader:hover .scmActions, .scmHeader:focus-within .scmActions { opacity: 1; }
@media (hover: none) { .scmActions { opacity: 1; } }
/* Child repositories of a workspace container: every view in this panel is
pinned to ONE root, so the switcher is the only surface that says the others
exist. The SAME list renders as natural-width chips while it fits on one
line and as one native select once it does not; a hidden measurement row
makes that decision from content width, not a viewport breakpoint. */
.repoSwitcher {
position: relative;
flex: none;
display: flex;
align-items: center;
min-width: 0;
padding: 0 6px 2px;
}
.repoChips {
flex: 1;
display: flex;
align-items: center;
gap: 4px;
min-width: 0;
overflow-x: auto;
overflow-y: hidden;
scrollbar-width: none;
}
.repoChips::-webkit-scrollbar { display: none; }
.repoChip {
flex: none;
min-height: 22px;
padding: 2px 8px;
border: 1px solid var(--dsw-alias-border-l1);
border-radius: 3px;
background: transparent;
color: var(--dsw-alias-label-secondary);
font: var(--dsw-font-xxxs-11);
text-align: left;
white-space: nowrap;
cursor: pointer;
}
.repoChip:hover:not(:disabled) { background: var(--dsw-alias-interactive-bg-hover); }
.repoChip[data-active='true'] {
border-color: var(--dsw-alias-border-l2);
background: var(--dsw-alias-interactive-bg-hover-accent);
color: var(--dsw-alias-label-primary);
}
.repoChip:disabled { opacity: 0.45; cursor: default; }
.repoChip:focus-visible { outline: 2px solid var(--dsw-alias-interactive-bg-hover-accent); outline-offset: -2px; }
/* The narrow form: the same caption/box rhythm as the graph's branch select. */
.repoPick {
flex: 1;
display: flex;
align-items: center;
gap: 6px;
min-width: 0;
font: var(--dsw-font-xxs-strong-12);
color: var(--dsw-alias-label-primary);
}
.repoPickLabel { flex: none; white-space: nowrap; }
.repoPickSelect {
flex: 1;
min-width: 0;
max-width: 100%;
height: 28px;
border: 1px solid var(--dsw-alias-border-l2);
border-radius: 3px;
background: var(--dsw-alias-bg-base);
color: var(--dsw-alias-label-primary);
font: inherit;
}
.repoPickSelect:disabled { opacity: 0.45; cursor: default; }
.repoPickSelect:focus-visible { outline: 2px solid var(--dsw-alias-interactive-bg-hover-accent); outline-offset: -2px; }
/* Natural-width copy used only for the fit decision. Keep it in the layout
tree (visibility, not display) so scrollWidth is measurable. */
.repoMeasure {
position: absolute;
left: 0;
top: 0;
display: flex;
gap: 4px;
height: 0;
overflow: hidden;
visibility: hidden;
pointer-events: none;
white-space: nowrap;
}
/* Docked by the flex column above (a sibling of `.gitScroll`), plus sticky so
it also survives the case where the host tab body itself scrolls the whole
tab — the branch control must never leave the bottom of the pane.
-5
View File
@@ -1,11 +1,6 @@
.root { display: flex; flex: 1; flex-direction: column; min-height: 0; min-width: 0; font: var(--dsw-font-xxs-12); color: var(--dsw-alias-label-primary); }
.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; padding: 6px 12px; }
.select { flex: 1; min-width: 0; max-width: 100%; height: 28px; border: 1px solid var(--dsw-alias-border-l2); border-radius: 3px; background: var(--dsw-alias-bg-base); color: var(--dsw-alias-label-primary); font: inherit; }
/* The multi-repo picker slots in ahead of the branch filter so the toolbar
reads as one scope row — root first, then its history filter. The slot
flexes like .select, so both boxes share the row instead of stacking. */
.repoPick { flex: 1; min-width: 0; display: flex; align-items: center; gap: 6px; }
.repoPickLabel { flex: none; white-space: nowrap; font: var(--dsw-font-xxs-strong-12); color: var(--dsw-alias-label-primary); }
.hint { color: var(--dsw-alias-label-secondary); font: var(--dsw-font-xxxs-11); }
.scroll { flex: 1; min-height: 0; overflow: auto; }
/* A commit row is ONE 28px line — rail, ref pills, subject (the GitLens-style