diff --git a/studio/src/renderer/src/studio/MapStudio.tsx b/studio/src/renderer/src/studio/MapStudio.tsx index 2374a9ffe..e5429d210 100644 --- a/studio/src/renderer/src/studio/MapStudio.tsx +++ b/studio/src/renderer/src/studio/MapStudio.tsx @@ -233,7 +233,7 @@ export function MapStudio({ client, world, theme, onOpenPack, onDisconnect }: Ma return (
- setHelpOpen(true)} onSettings={() => setSettingsOpen(true)} onDisconnect={onDisconnect} /> + setHelpOpen(true)} onSettings={() => setSettingsOpen(true)} />
{openingPack !== null && ( diff --git a/studio/src/renderer/src/studio/PackSwitcher.tsx b/studio/src/renderer/src/studio/PackSwitcher.tsx index 2457541d8..1340ca1d4 100644 --- a/studio/src/renderer/src/studio/PackSwitcher.tsx +++ b/studio/src/renderer/src/studio/PackSwitcher.tsx @@ -4,6 +4,7 @@ import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigge import { cn } from '@/lib/utils' import type { StudioPack, WorldInfo } from '../bridge/types' import { Glyph } from './Glyph' +import { badgeColour, badgeInitials } from './packBadges' interface PackSwitcherProps { world: WorldInfo @@ -11,12 +12,13 @@ interface PackSwitcherProps { opening: StudioPack | null loadPacks(): Promise onOpenPack(pack: StudioPack): void - onDisconnect(): void } -/** The open pack in the window's bar, which lists the server's packs to switch the studio to, as IntelliJ's project widget does. */ -export function PackSwitcher({ world, opening, loadPacks, onOpenPack, onDisconnect }: PackSwitcherProps): React.JSX.Element { - const [packs, setPacks] = useState(null) +/** The open pack in the window's bar, which lists the server's other packs to switch the studio to, as IntelliJ's project widget does. */ +export function PackSwitcher({ world, opening, loadPacks, onOpenPack }: PackSwitcherProps): React.JSX.Element { + const [packs, setPacks] = useState([]) + const openPack = opening ?? { key: world.dimension, name: world.dimensionName, version: world.packVersion } + const otherPacks = packs.filter((pack) => pack.key !== world.dimension) const refreshPacks = (open: boolean): void => { if (!open) { return @@ -25,42 +27,65 @@ export function PackSwitcher({ world, opening, loadPacks, onOpenPack, onDisconne .then(setPacks) .catch(() => setPacks([])) } - const shownName = opening?.name ?? world.dimensionName - const shownVersion = opening?.version ?? world.packVersion return ( - - {packs === null &&

Listing packs…

} - {packs?.length === 0 &&

No packs to switch to

} - {packs?.map((pack) => { - const current = pack.key === world.dimension - return ( - !current && onOpenPack(pack)} className={cn(current && 'bg-selection')}> - - {pack.name} - v{pack.version} - - ) - })} -
- Disconnect + + Open Pack + + + + {otherPacks.length > 0 && ( + <> +
+ Packs + {otherPacks.map((pack) => ( + onOpenPack(pack)}> + + + ))} + + )} ) } -function PackBadge({ name }: { name: string }): React.JSX.Element { +/** A pack as the version may be unknown for: a server that does not send it. */ +type ShownPack = Omit & { version?: number } + +function PackRow({ pack }: { pack: ShownPack }): React.JSX.Element { return ( - - {name.charAt(0)} + + + + {pack.name} + + {pack.version === undefined ? pack.key : `${pack.key} · v${pack.version}`} + + + + ) +} + +function SectionLabel({ children }: { children: React.ReactNode }): React.JSX.Element { + return

{children}

+} + +function PackBadge({ pack }: { pack: ShownPack }): React.JSX.Element { + return ( + + {badgeInitials(pack.name)} ) } diff --git a/studio/src/renderer/src/studio/StudioToolbar.tsx b/studio/src/renderer/src/studio/StudioToolbar.tsx index e9b9e7a6e..ab73dcc08 100644 --- a/studio/src/renderer/src/studio/StudioToolbar.tsx +++ b/studio/src/renderer/src/studio/StudioToolbar.tsx @@ -21,15 +21,14 @@ interface StudioToolbarProps { onOpenPack(pack: StudioPack): void onHelp(): void onSettings(): void - onDisconnect(): void } /** The window's bar: the pack the studio is on, which switches to another, and the studio's own buttons. */ -export function StudioToolbar({ world, openingPack, loadPacks, onOpenPack, onHelp, onSettings, onDisconnect }: StudioToolbarProps): React.JSX.Element { +export function StudioToolbar({ world, openingPack, loadPacks, onOpenPack, onHelp, onSettings }: StudioToolbarProps): React.JSX.Element { return (
- +
diff --git a/studio/src/renderer/src/studio/packBadges.test.ts b/studio/src/renderer/src/studio/packBadges.test.ts new file mode 100644 index 000000000..9cc906a32 --- /dev/null +++ b/studio/src/renderer/src/studio/packBadges.test.ts @@ -0,0 +1,30 @@ +import { describe, expect, it } from 'vitest' +import { badgeColour, badgeInitials } from './packBadges' + +describe('badgeInitials', () => { + it('takes the first letter of a one word name', () => { + expect(badgeInitials('Etopia')).toBe('E') + }) + + it('takes the first letters of the first two words, split at separators, capitals and digits', () => { + expect(badgeInitials('voxy-modular')).toBe('VM') + expect(badgeInitials('IrisV5')).toBe('IV') + expect(badgeInitials('CodexProvider')).toBe('CP') + expect(badgeInitials('the overworld pack')).toBe('TO') + }) + + it('marks a name without letters or digits', () => { + expect(badgeInitials('--')).toBe('?') + }) +}) + +describe('badgeColour', () => { + it('gives a pack the same colour every time', () => { + expect(badgeColour('natureplanet')).toBe(badgeColour('natureplanet')) + }) + + it('spreads packs over the colours', () => { + const colours = new Set(['natureplanet', 'overworld', 'cavetest', 'riverworld', 'underworld', 'etopia'].map(badgeColour)) + expect(colours.size).toBeGreaterThan(2) + }) +}) diff --git a/studio/src/renderer/src/studio/packBadges.ts b/studio/src/renderer/src/studio/packBadges.ts new file mode 100644 index 000000000..423937bd5 --- /dev/null +++ b/studio/src/renderer/src/studio/packBadges.ts @@ -0,0 +1,20 @@ +/** Badge colours as IntelliJ gives its projects: each pack keeps one, picked by its folder. */ +const BADGE_COLOURS = ['#3f9b5e', '#e07a50', '#1fa0b4', '#c0479c', '#9a55d8', '#5f6ce0', '#4a8fdc', '#7ea64a', '#c89a22'] as const +const MOST_INITIALS = 2 +const WORD_BREAKS = /[^\p{L}\p{N}]+|(?<=\p{Ll})(?=\p{Lu})|(?<=\p{L})(?=\p{N})/u + +/** The first letters of a pack name's words, at most two: "IrisV5" is "IV", "voxy-modular" "VM", "Etopia" "E". */ +export function badgeInitials(name: string): string { + const words = name.split(WORD_BREAKS).filter((word) => word !== '') + const initials = words.slice(0, MOST_INITIALS).map((word) => word.charAt(0).toUpperCase()).join('') + return initials === '' ? '?' : initials +} + +/** The colour of a pack's badge, the same every time for the same folder. */ +export function badgeColour(packKey: string): string { + let hash = 0 + for (const character of packKey) { + hash = (hash * 31 + character.charCodeAt(0)) | 0 + } + return BADGE_COLOURS[Math.abs(hash) % BADGE_COLOURS.length] +}