Merge the file-icon API from #531 (feat/file-icons-api) into the rc.2 line
Brings in the plugin-side file-icon feature (issue #611): the public `registerFileIcon` API plus the resolver chain the tree, the editor tab and the external-plugin surface all read. Conflicts (three, all comment/list unions — resolved by union, verified green): - src/client/service.ts: main's "v0.19.0 REMOVED 'floatWindows'" note stays (the capability is gone and must not come back); the feature list gains 'fileIcons'. - src/client/Sidebar.tsx: one comment above `tabIconOf`; both sides' wording is kept (the editor-with-path special case and the descriptor fallback). - docs/external-plugin-guide.md: the `features` list comment keeps main's "additive only, the one exception being floatWindows" wording plus the new 'fileIcons' entry. Notes on what this merge does NOT bring in: #594 (feat/file-icon-theme) is stacked on this branch and lands next, but its colored-theme implementation (563-entry react-icons dataset in a 255 kB lazy chunk plus a `fileIconTheme` switch) is being replaced — DSH 0.1.5-rc.2 exports the official `FileTypeIcon`/`CodeFileIcon` artwork from ui-primitives, so the plugin does not need to ship its own color dataset. Gates on the merge result: pnpm typecheck clean, pnpm test 127 files / 1331 passed / 9 skipped.
This commit is contained in:
commit
1d05e39be2
10 files changed
+644
-12
No files matched your search
@@ -401,6 +401,7 @@ export function EditorHost(props: {
|
||||
onReferenceFile={onReferenceFile}
|
||||
onPathRenamed={onPathRenamed}
|
||||
onPathDeleted={onPathDeleted}
|
||||
service={ctx.get('betterSidebar')}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
@@ -525,6 +526,7 @@ export function EditorHost(props: {
|
||||
onReferenceFile={onReferenceFile}
|
||||
onPathRenamed={onPathRenamed}
|
||||
onPathDeleted={onPathDeleted}
|
||||
service={ctx.get('betterSidebar')}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
|
||||
+41
-7
@@ -28,15 +28,17 @@ import {
|
||||
IconEditOutline16, IconLinkOutline16, IconTrashOutline16, Menu, Modal, type MenuEntry, type MenuItem, writeClipboard,
|
||||
} from '@deepseek-ai/dsh-client-ui-primitives'
|
||||
import { SiCursor, SiZedindustries } from 'react-icons/si'
|
||||
import { VscFile, VscFolder, VscFolderOpened, VscLinkExternal, VscPin, VscPinned } from 'react-icons/vsc'
|
||||
import { VscFolderOpened, VscLinkExternal, VscPin, VscPinned } from 'react-icons/vsc'
|
||||
import { api, downloadUrl, isOutsideWorkspaceMessage, type FsEntry } from './api.ts'
|
||||
import { FenceErrorNotice } from './FenceErrorNotice.tsx'
|
||||
import { builtinFileIcon, builtinFolderIcon } from './file-icons.tsx'
|
||||
import { IconUploadOutline16, IconVscode16 } from './icons.tsx'
|
||||
import { isImeComposition } from './ime-guard.ts'
|
||||
import { useSubmenuFlip } from './menu-flip.ts'
|
||||
import type { OpenWithTarget } from './open-with.ts'
|
||||
import { relativeTo } from './paths.ts'
|
||||
import { t } from './locales.ts'
|
||||
import type { BetterSidebarService } from './service.ts'
|
||||
import type { SidebarStore } from './state.ts'
|
||||
import { uploadItemsFromDrop, uploadItemsFromFiles, type UploadItem } from './upload.ts'
|
||||
import css from './sidebar.module.css'
|
||||
@@ -145,9 +147,41 @@ export function FileTree(props: {
|
||||
onUploadRequest: (dir: string, items: UploadItem[]) => void
|
||||
/** True while an upload is in flight (drops are ignored). */
|
||||
busy: boolean
|
||||
/**
|
||||
* The sidebar registry service: when present, externally registered file
|
||||
* icons (`registerFileIcon`) outrank the built-in glyph map on file rows.
|
||||
* Absent → the built-ins alone (the host always passes it today).
|
||||
*/
|
||||
service?: BetterSidebarService
|
||||
}) {
|
||||
const { sessionId, cwd, store, expanded, revealed, onToggle, onOpenFile, onOpenFileNewTab, onOpenFileSide, openWithTargets, openWithPinned, openWithSsh, onOpenWith, onToggleOpenWithPin, onReferenceFile, onPathRenamed, onPathDeleted, refreshTick, onUploadRequest, busy } = props
|
||||
const { sessionId, cwd, store, expanded, revealed, onToggle, onOpenFile, onOpenFileNewTab, onOpenFileSide, openWithTargets, openWithPinned, openWithSsh, onOpenWith, onToggleOpenWithPin, onReferenceFile, onPathRenamed, onPathDeleted, refreshTick, onUploadRequest, busy, service } = props
|
||||
const [data, setData] = useState<Record<string, LevelData>>({})
|
||||
/**
|
||||
* Registry revision for the file-icon feature: bumps on ANY registry
|
||||
* change (register/dispose of tabs, viewers, or icons — one listener
|
||||
* set) so rows re-resolve their icons. The value itself is unread; the
|
||||
* state bump IS the re-render trigger.
|
||||
*/
|
||||
const [, setIconsVersion] = useState(0)
|
||||
useEffect(
|
||||
() => service?.subscribe(() => { setIconsVersion(version => version + 1) }),
|
||||
[service],
|
||||
)
|
||||
/**
|
||||
* One file row's leading glyph. The service resolver owns the whole
|
||||
* chain (registered specific ext → built-in glyph map → registered
|
||||
* global default → stock VscFile, with per-factory crash isolation);
|
||||
* without a service the built-in map alone applies.
|
||||
*/
|
||||
const fileRowIcon = (path: string): ReactNode =>
|
||||
service !== undefined ? service.fileIcon(path, 14) : builtinFileIcon(path, 14)
|
||||
|
||||
/**
|
||||
* One directory row's leading glyph: the registered `'folder'` /
|
||||
* `'folder-open'` icon when present, else the built-in VSCodicons glyphs.
|
||||
*/
|
||||
const dirRowIcon = (path: string, open: boolean): ReactNode =>
|
||||
service !== undefined ? service.folderIcon(path, open, 14) : builtinFolderIcon(open, 14)
|
||||
const dataRef = useRef(data)
|
||||
/** The row whose path was just copied ("copied" label replaces its button). */
|
||||
const [copiedPath, setCopiedPath] = useState<string | null>(null)
|
||||
@@ -546,8 +580,8 @@ export function FileTree(props: {
|
||||
style={{ paddingLeft: depth * 22 + 6 }}
|
||||
>
|
||||
{entry.isDir
|
||||
? (expandedSet.has(entry.path) ? <VscFolderOpened size={14} /> : <VscFolder size={14} />)
|
||||
: <VscFile size={14} />}
|
||||
? dirRowIcon(entry.path, expandedSet.has(entry.path))
|
||||
: fileRowIcon(entry.path)}
|
||||
<input
|
||||
ref={renameInputRef}
|
||||
className={css.explorerRenameInput}
|
||||
@@ -625,7 +659,7 @@ export function FileTree(props: {
|
||||
onDrop={(event) => { handleDirDrop(event, entry.path) }}
|
||||
onContextMenu={(event) => { openRowMenu(event, entry.path, true) }}
|
||||
>
|
||||
{isOpen ? <VscFolderOpened size={14} /> : <VscFolder size={14} />}
|
||||
{dirRowIcon(entry.path, isOpen)}
|
||||
<span className={css.explorerName}>{entry.name}</span>
|
||||
{entry.isSymlink && <IconLinkOutline16 size={12} className={css.explorerSymlink} />}
|
||||
{rowActions(entry)}
|
||||
@@ -658,7 +692,7 @@ export function FileTree(props: {
|
||||
onDrop={(event) => { handleFileDrop(event, entry.path) }}
|
||||
onContextMenu={(event) => { openRowMenu(event, entry.path, false) }}
|
||||
>
|
||||
<VscFile size={14} />
|
||||
{fileRowIcon(entry.path)}
|
||||
<span className={css.explorerName}>{entry.name}</span>
|
||||
{entry.isSymlink && <IconLinkOutline16 size={12} className={css.explorerSymlink} />}
|
||||
{rowActions(entry)}
|
||||
@@ -703,7 +737,7 @@ export function FileTree(props: {
|
||||
onDrop={(event) => { handleDirDrop(event, root) }}
|
||||
onContextMenu={(event) => { openRowMenu(event, root, true) }}
|
||||
>
|
||||
<VscFolderOpened size={14} />
|
||||
{dirRowIcon(root, true)}
|
||||
<span className={css.explorerName}>{baseName(root)}</span>
|
||||
{copiedPath === root
|
||||
? <span className={css.explorerCopied}>{t('copied')}</span>
|
||||
|
||||
+10
-1
@@ -628,8 +628,17 @@ export function Sidebar(props: { ctx: Context; store: SidebarStore }) {
|
||||
service.openTab({ type: optionId, title, target: 'bottom' }, { sessionId, cwd })
|
||||
}
|
||||
|
||||
/** The tab icon from the tab-type registry. */
|
||||
/**
|
||||
* The tab icon from the tab-type registry. An editor tab WITH a file path
|
||||
* (the per-path windows of split mode — `meta.dir` marks folder windows,
|
||||
* which keep the folder glyph) shows the same file icon the tree row shows
|
||||
* (`fileIcon`, feature `fileIcons`); every other tab uses its tab-type
|
||||
* descriptor icon.
|
||||
*/
|
||||
const tabIconOf = (tab: SidebarTab): ReactNode => {
|
||||
if (tab.type === 'editor' && tab.path !== undefined && (tab.meta as { dir?: boolean } | undefined)?.dir !== true) {
|
||||
return ctx.get('betterSidebar')?.fileIcon(tab.path, 14) ?? null
|
||||
}
|
||||
const descriptor = ctx.get('betterSidebar')?.getTab(tab.type)
|
||||
if (descriptor === undefined) return null
|
||||
return typeof descriptor.icon === 'function' ? descriptor.icon(14) : descriptor.icon
|
||||
|
||||
@@ -19,6 +19,7 @@ import { useEffect, useRef, useState, type InputHTMLAttributes } from 'react'
|
||||
import clsx from 'clsx'
|
||||
import { IconFolderOpen16, IconRefreshOutline16 } from '@deepseek-ai/dsh-client-ui-primitives'
|
||||
import { api } from './api.ts'
|
||||
import type { BetterSidebarService } from './service.ts'
|
||||
import type { SidebarStore } from './state.ts'
|
||||
import { FileTree } from './FileTree.tsx'
|
||||
import { IconUploadOutline16 } from './icons.tsx'
|
||||
@@ -70,8 +71,10 @@ export function TreePanel(props: {
|
||||
/** Full-window presentation: the panel fills its host instead of docking
|
||||
* at a fixed width. */
|
||||
full?: boolean
|
||||
/** The sidebar registry service (file-icon registrations; passed through to FileTree). */
|
||||
service?: BetterSidebarService
|
||||
}) {
|
||||
const { sessionId, cwd, store, expanded, revealed, onToggle, onOpenFile, onOpenFileNewTab, onOpenFileSide, openWithTargets, openWithPinned, openWithSsh, onOpenWith, onToggleOpenWithPin, onReferenceFile, onPathRenamed, onPathDeleted, full } = props
|
||||
const { sessionId, cwd, store, expanded, revealed, onToggle, onOpenFile, onOpenFileNewTab, onOpenFileSide, openWithTargets, openWithPinned, openWithSsh, onOpenWith, onToggleOpenWithPin, onReferenceFile, onPathRenamed, onPathDeleted, full, service } = props
|
||||
const [query, setQuery] = useState('')
|
||||
const [results, setResults] = useState<{ matches: string[]; truncated: boolean } | null>(null)
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
@@ -261,6 +264,7 @@ export function TreePanel(props: {
|
||||
refreshTick={refreshTick}
|
||||
onUploadRequest={startUpload}
|
||||
busy={busy}
|
||||
service={service}
|
||||
/>
|
||||
) : (
|
||||
<div className={css.explorerBody}>
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
/**
|
||||
* Built-in per-extension file glyphs for the file tree (feature: file icons).
|
||||
*
|
||||
* All glyphs are VSCodicons consumed as `currentColor` monochrome — the same
|
||||
* skin contract as every other icon this plugin draws (visual values ride the
|
||||
* `--dsw-alias-*` tokens, never hardcoded colors). External plugins that want
|
||||
* colorful icons register their own `FileIconDescriptor`s through
|
||||
* `ctx.betterSidebar.registerFileIcon` (they own their colors); this map is
|
||||
* only the fallback nobody claimed.
|
||||
*/
|
||||
import type { ReactNode } from 'react'
|
||||
import {
|
||||
VscDatabase, VscFile, VscFileCode, VscFileMedia, VscFilePdf, VscFileZip,
|
||||
VscFolder, VscFolderOpened, VscJson, VscLock, VscMarkdown, VscSettings,
|
||||
} from 'react-icons/vsc'
|
||||
import { extOf } from './paths.ts'
|
||||
|
||||
/** One glyph shared by a group of extensions. */
|
||||
type GlyphFactory = (size: number) => ReactNode
|
||||
|
||||
/** The generic file glyph — the fallback for every unclaimed extension. */
|
||||
export function fallbackFileIcon(size: number): ReactNode {
|
||||
return <VscFile size={size} />
|
||||
}
|
||||
|
||||
/** The built-in directory glyphs (what a folder row shows unregistered). */
|
||||
export function builtinFolderIcon(open: boolean, size: number): ReactNode {
|
||||
return open ? <VscFolderOpened size={size} /> : <VscFolder size={size} />
|
||||
}
|
||||
|
||||
/** One glyph shared by a group of extensions. */
|
||||
type GlyphGroup = [readonly string[], GlyphFactory]
|
||||
|
||||
/**
|
||||
* Extension → glyph groups. Extensions are lowercase without the leading dot,
|
||||
* matched case-insensitively by the caller's normalization. Group order is
|
||||
* documentation only: the flattened lookup below is a Map, so every extension
|
||||
* key must stay unique (a duplicate would be last-write-wins). The reserved
|
||||
* folder values (`'folder'`/`'folder-open'`) never appear here — directories
|
||||
* resolve through `builtinFolderIcon`.
|
||||
*/
|
||||
const GROUPS: readonly GlyphGroup[] = [
|
||||
[['md', 'markdown', 'mdx'], (size) => <VscMarkdown size={size} />],
|
||||
[['png', 'jpg', 'jpeg', 'gif', 'webp', 'svg', 'bmp', 'ico', 'avif', 'tiff'], (size) => <VscFileMedia size={size} />],
|
||||
[['pdf'], (size) => <VscFilePdf size={size} />],
|
||||
[['json', 'jsonc', 'json5'], (size) => <VscJson size={size} />],
|
||||
[['html', 'htm', 'xhtml', 'xml', 'jsx', 'tsx'], (size) => <VscFileCode size={size} />],
|
||||
[['css', 'scss', 'sass', 'less', 'styl'], (size) => <VscFileCode size={size} />],
|
||||
[['js', 'mjs', 'cjs', 'ts', 'mts', 'cts', 'py', 'rs', 'go', 'java', 'kt', 'kts', 'swift', 'rb', 'php', 'c', 'h', 'cpp', 'hpp', 'cs', 'm', 'mm', 'scala', 'sh', 'bash', 'zsh', 'fish', 'lua', 'pl', 'r', 'jl', 'dart', 'ex', 'exs', 'erl', 'hs', 'clj', 'cljs', 'vb', 'fs'], (size) => <VscFileCode size={size} />],
|
||||
[['yml', 'yaml', 'toml', 'ini', 'cfg', 'conf', 'properties', 'editorconfig'], (size) => <VscSettings size={size} />],
|
||||
[['sql', 'db', 'sqlite', 'sqlite3'], (size) => <VscDatabase size={size} />],
|
||||
[['lock'], (size) => <VscLock size={size} />],
|
||||
[['zip', 'tar', 'gz', 'tgz', 'bz2', 'xz', '7z', 'rar'], (size) => <VscFileZip size={size} />],
|
||||
]
|
||||
|
||||
const LOOKUP: ReadonlyMap<string, GlyphFactory> = new Map(
|
||||
GROUPS.flatMap(([exts, glyph]) => exts.map((ext) => [ext, glyph] as const)),
|
||||
)
|
||||
|
||||
/**
|
||||
* The built-in glyph FACTORY claiming a path's extension, or undefined when
|
||||
* the extension is unclaimed (the caller decides the fallback — the generic
|
||||
* `VscFile` or a registered catch-all). Exposed as a separate function so
|
||||
* the service's resolver can distinguish "builtin claims it" from "builtin
|
||||
* is just its own fallback" without comparing ReactNodes by reference.
|
||||
*/
|
||||
export function builtinFileIconOf(path: string): GlyphFactory | undefined {
|
||||
return LOOKUP.get(extOf(path))
|
||||
}
|
||||
|
||||
/**
|
||||
* The built-in glyph for a path: its extension's group glyph, or the generic
|
||||
* `VscFile` when nothing claims it. Pure and synchronous — callers run this
|
||||
* on every render, so it stays a Map lookup. The no-service fallback path
|
||||
* for surfaces without the registry (and the last-but-one link of the
|
||||
* service's `fileIcon` chain).
|
||||
*/
|
||||
export function builtinFileIcon(path: string, size: number): ReactNode {
|
||||
const glyph = LOOKUP.get(extOf(path))
|
||||
return glyph === undefined ? fallbackFileIcon(size) : glyph(size)
|
||||
}
|
||||
@@ -27,6 +27,7 @@ import {
|
||||
type SidebarSnapshot, type SidebarState, type SidebarStore, type SidebarTab, type TabType,
|
||||
} from './state.ts'
|
||||
import { extOf } from './paths.ts'
|
||||
import { builtinFileIconOf, builtinFolderIcon, fallbackFileIcon } from './file-icons.tsx'
|
||||
import type { SessionScope } from './api.ts'
|
||||
import type { SidebarPrefs } from '../prefs-shared.ts'
|
||||
|
||||
@@ -334,6 +335,41 @@ export interface FileViewerDescriptor {
|
||||
component: (props: FileViewerProps) => ReactNode
|
||||
}
|
||||
|
||||
/**
|
||||
* Describes one external file-icon registration (feature `fileIcons`).
|
||||
* Registrations override the built-in per-extension glyph map for their
|
||||
* extensions; unlike the built-ins (monochrome `currentColor` per the skin
|
||||
* contract), a registration's icon may be ANY ReactNode — colored included —
|
||||
* and the registering plugin owns how its colors behave across skins.
|
||||
*/
|
||||
export interface FileIconDescriptor {
|
||||
/** Unique id (`'my-plugin:icons'`). */
|
||||
id: string
|
||||
/**
|
||||
* Lowercase extensions without leading dot (`['csv','tsv']`). `[]` = the
|
||||
* global default (catch-all): it only claims files the built-in glyph map
|
||||
* does not cover — registered specifics and built-in glyphs always outrank
|
||||
* it. Two values are RESERVED for directory rows (never matched against
|
||||
* real file extensions): `'folder'` (a closed directory) and
|
||||
* `'folder-open'` (an expanded directory) — see `FOLDER_EXT`.
|
||||
*/
|
||||
exts: readonly string[]
|
||||
/** Higher wins; default 0. Registered icons always outrank the built-in map. */
|
||||
priority?: number
|
||||
/** Size-aware icon factory (the tree and file tabs render at 14 today). */
|
||||
icon: (path: string, size: number) => ReactNode
|
||||
}
|
||||
|
||||
/**
|
||||
* Reserved `exts` values that claim DIRECTORY rows instead of file
|
||||
* extensions: `'folder'` matches a closed directory, `'folder-open'` an
|
||||
* expanded one (`folderIcon(path, open)` resolves them). They are filtered out of
|
||||
* real-extension matching, so a file literally named `x.folder` is NOT
|
||||
* claimed by a folder registration.
|
||||
*/
|
||||
export const FOLDER_EXT = 'folder' as const
|
||||
export const FOLDER_OPEN_EXT = 'folder-open' as const
|
||||
|
||||
/** One `openTab` request. */
|
||||
export interface OpenTabSeed {
|
||||
type: string
|
||||
@@ -408,8 +444,43 @@ export interface SidebarSurface {
|
||||
export interface BetterSidebarService {
|
||||
registerTab(descriptor: TabDescriptor): () => void
|
||||
registerFileViewer(descriptor: FileViewerDescriptor): () => void
|
||||
registerFileIcon(descriptor: FileIconDescriptor): () => void
|
||||
getTabs(): readonly TabDescriptor[]
|
||||
getFileViewers(): readonly FileViewerDescriptor[]
|
||||
getFileIcons(): readonly FileIconDescriptor[]
|
||||
/**
|
||||
* Find a SPECIFIC registered file icon for a path (priority desc, then
|
||||
* registration order). Catch-alls (`exts: []`) and folder registrations
|
||||
* (`'folder'`/`'folder-open'`) are not consulted — this answers "did a
|
||||
* registration claim this exact extension". Consumers should prefer
|
||||
* `fileIcon`/`folderIcon`, which run the whole fallback chain.
|
||||
*/
|
||||
matchFileIcon(path: string): FileIconDescriptor | undefined
|
||||
/**
|
||||
* Find the registered icon for DIRECTORY rows (priority desc, then
|
||||
* registration order): `open` picks between the `'folder'` and
|
||||
* `'folder-open'` reserved exts. Undefined = fall back to the built-in
|
||||
* VSCodicons folder glyphs.
|
||||
*/
|
||||
matchFolderIcon(open: boolean): FileIconDescriptor | undefined
|
||||
/**
|
||||
* The authoritative FILE icon for a path (feature `fileIcons`), running
|
||||
* the whole chain with per-factory crash isolation:
|
||||
* 1. a specific registered extension (priority desc, registration order),
|
||||
* 2. the built-in monochrome glyph map (md/media/pdf/json/code/...),
|
||||
* 3. the best registered global default (`exts: []`, priority desc),
|
||||
* 4. the generic `VscFile`.
|
||||
* A throwing factory is logged (console.error) and skipped — the caller
|
||||
* always gets a valid ReactNode.
|
||||
*/
|
||||
fileIcon(path: string, size: number): ReactNode
|
||||
/**
|
||||
* The authoritative DIRECTORY icon for a tree row: the registered
|
||||
* `'folder'`/`'folder-open'` icon (priority desc), else the built-in
|
||||
* `VscFolder`/`VscFolderOpened`. `path` is the directory's own path (a
|
||||
* theme may vary icons per directory). Same crash isolation as `fileIcon`.
|
||||
*/
|
||||
folderIcon(path: string, open: boolean, size: number): ReactNode
|
||||
/** Find a tab descriptor by id (undefined if not registered). */
|
||||
getTab(id: string): TabDescriptor | undefined
|
||||
/**
|
||||
@@ -544,6 +615,8 @@ export const SIDEBAR_SERVICE_VERSION = '0.19.1'
|
||||
* - 'pluginSettings': SidebarSettingsDeclaration.pluginToggles/render
|
||||
* - 'urlTarget' (v0.13.0): TabDescriptor.urlTarget (external-link claims)
|
||||
* - 'settingSelect': SidebarSettingToggle type 'select' (options/multi)
|
||||
* - 'fileIcons' (v0.19.0): registerFileIcon/getFileIcons/matchFileIcon —
|
||||
* external per-extension file-tree icons overriding the built-in glyphs.
|
||||
*
|
||||
* v0.19.0 REMOVED 'floatWindows': the free-window feature is gone (DSH 0.1.5
|
||||
* owns the right column, so the plugin keeps only its bottom workbench).
|
||||
@@ -560,6 +633,7 @@ export const SIDEBAR_FEATURES = [
|
||||
'pluginSettings',
|
||||
'urlTarget',
|
||||
'settingSelect',
|
||||
'fileIcons',
|
||||
] as const
|
||||
|
||||
/** Run one plugin callback; a throw is logged and never breaks the caller. */
|
||||
@@ -579,6 +653,7 @@ function safeCall(fn: () => void): void {
|
||||
export function createBetterSidebarService(store: SidebarStore): BetterSidebarService {
|
||||
const tabs = new Map<string, TabDescriptor>()
|
||||
const viewers = new Map<string, FileViewerDescriptor>()
|
||||
const fileIcons = new Map<string, FileIconDescriptor>()
|
||||
const listeners = new Set<() => void>()
|
||||
/** The native right-Sidebar write face, installed by the client half. */
|
||||
let surface: SidebarSurface | undefined
|
||||
@@ -622,8 +697,98 @@ export function createBetterSidebarService(store: SidebarStore): BetterSidebarSe
|
||||
|
||||
const getTabs = (): readonly TabDescriptor[] => Array.from(tabs.values())
|
||||
const getFileViewers = (): readonly FileViewerDescriptor[] => Array.from(viewers.values())
|
||||
const getFileIcons = (): readonly FileIconDescriptor[] => Array.from(fileIcons.values())
|
||||
const getTab = (id: string): TabDescriptor | undefined => tabs.get(id)
|
||||
|
||||
const registerFileIcon = (descriptor: FileIconDescriptor): (() => void) => {
|
||||
if (fileIcons.has(descriptor.id)) {
|
||||
throw new Error(`[dsh-better-sidebar] file icons "${descriptor.id}" already registered`)
|
||||
}
|
||||
fileIcons.set(descriptor.id, descriptor)
|
||||
notify()
|
||||
return () => {
|
||||
if (fileIcons.get(descriptor.id) === descriptor) {
|
||||
fileIcons.delete(descriptor.id)
|
||||
notify()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Registrations in ranking order: priority desc, stable for equal
|
||||
// priorities (insertion order) — the same ranking `matchFileViewer` uses.
|
||||
const rankedFileIcons = (): FileIconDescriptor[] =>
|
||||
Array.from(fileIcons.values()).sort((a, b) => (b.priority ?? 0) - (a.priority ?? 0))
|
||||
|
||||
// Specific registrations only: catch-alls (`exts: []`) and folder
|
||||
// registrations (`'folder'`/`'folder-open'`) are skipped, and the reserved
|
||||
// folder values never match a real file's extension. The built-in glyph
|
||||
// map is not consulted here — an undefined result IS the "fall through"
|
||||
// signal the `fileIcon` resolver acts on.
|
||||
const matchFileIcon = (path: string): FileIconDescriptor | undefined => {
|
||||
const ext = extOf(path)
|
||||
// Reserved folder values never claim a real file: `x.folder` falls
|
||||
// through to the built-in/catch-all chain like any unknown extension.
|
||||
if (ext === FOLDER_EXT || ext === FOLDER_OPEN_EXT) return undefined
|
||||
for (const d of rankedFileIcons()) {
|
||||
if (d.exts.includes(ext)) return d
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
// Directory rows have no extension: the reserved `'folder'`/`'folder-open'`
|
||||
// exts are the only claim surface (a catch-all never claims a directory).
|
||||
const matchFolderIcon = (open: boolean): FileIconDescriptor | undefined => {
|
||||
const want = open ? FOLDER_OPEN_EXT : FOLDER_EXT
|
||||
for (const d of rankedFileIcons()) {
|
||||
if (d.exts.includes(want)) return d
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
/** Run one registered factory; a throw is logged and returns undefined. */
|
||||
const safeIcon = (d: FileIconDescriptor, path: string, size: number): ReactNode => {
|
||||
try {
|
||||
return d.icon(path, size)
|
||||
} catch (error) {
|
||||
console.error(`[dsh-better-sidebar] file icon factory "${d.id}" error:`, error)
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
|
||||
// The authoritative file-icon chain (see the interface doc): specific
|
||||
// registration → built-in glyph → best catch-all → stock VscFile. The
|
||||
// catch-all ranks by priority desc then registration order (first wins);
|
||||
// an unclaimed extension reaching it is exactly "nothing specified falls
|
||||
// back to the (registered or stock) default".
|
||||
const fileIcon = (path: string, size: number): ReactNode => {
|
||||
const specific = matchFileIcon(path)
|
||||
if (specific !== undefined) {
|
||||
const icon = safeIcon(specific, path, size)
|
||||
if (icon !== undefined) return icon
|
||||
}
|
||||
const builtin = builtinFileIconOf(path)
|
||||
if (builtin !== undefined) return builtin(size)
|
||||
for (const d of rankedFileIcons()) {
|
||||
if (d.exts.length === 0) {
|
||||
const icon = safeIcon(d, path, size)
|
||||
if (icon !== undefined) return icon
|
||||
}
|
||||
}
|
||||
return fallbackFileIcon(size)
|
||||
}
|
||||
|
||||
// Directory rows: registered folder/folder-open icon, else the built-in
|
||||
// VSCodicons glyphs. The row's path feeds the factory (a theme may vary
|
||||
// icons per directory).
|
||||
const folderIcon = (path: string, open: boolean, size: number): ReactNode => {
|
||||
const registered = matchFolderIcon(open)
|
||||
if (registered !== undefined) {
|
||||
const icon = safeIcon(registered, path, size)
|
||||
if (icon !== undefined) return icon
|
||||
}
|
||||
return builtinFolderIcon(open, size)
|
||||
}
|
||||
|
||||
// The enable switches come from the user's side card prefs (the shared
|
||||
// store the service is bound to): an absent key means enabled.
|
||||
const isTabEnabled = (id: string): boolean => store.getPrefs().tabsEnabled[id] !== false
|
||||
@@ -907,8 +1072,14 @@ export function createBetterSidebarService(store: SidebarStore): BetterSidebarSe
|
||||
return {
|
||||
registerTab,
|
||||
registerFileViewer,
|
||||
registerFileIcon,
|
||||
getTabs,
|
||||
getFileViewers,
|
||||
getFileIcons,
|
||||
matchFileIcon,
|
||||
matchFolderIcon,
|
||||
fileIcon,
|
||||
folderIcon,
|
||||
getTab,
|
||||
isTabEnabled,
|
||||
isViewerEnabled,
|
||||
|
||||
Reference in new issue
Block a user